Created
February 25, 2019 15:28
-
-
Save histrio/fecbd6e8d8ce91899aea4f3b246fea2f to your computer and use it in GitHub Desktop.
Validate slack request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_request_valid(request): | |
is_token_valid = request.form['token'] == os.environ['SLACK_VERIFICATION_TOKEN'] | |
is_team_id_valid = request.form['team_id'] == os.environ['SLACK_TEAM_ID'] | |
slack_signing_secret = os.environ['SLACK_SIGNING_SECRET'] | |
timestamp = request.headers['X-Slack-Request-Timestamp'].encode() | |
is_timestamp_valid = abs(time.time() - int(timestamp)) > 60 * 5 | |
sig_basestring = 'v0:' + timestamp + ':' + request.get_data() | |
my_signature = 'v0=' + hmac.new(slack_signing_secret, msg=sig_basestring, digestmod=hashlib.sha256).hexdigest() | |
slack_signature = request.headers['X-Slack-Signature'].encode() | |
is_signature_valid = hmac.compare_digest(my_signature, slack_signature) | |
return is_token_valid and is_team_id_valid and is_team_id_valid and is_signature_valid | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment