Last active
August 4, 2022 09:33
-
-
Save reachlin/84a16c679764a500ef326a10c153fd81 to your computer and use it in GitHub Desktop.
python verify slack bot message
This file contains hidden or 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 verify_slack_msg(msg): | |
try: | |
slack_signature = msg['headers']['X-Slack-Signature'] | |
slack_signing_secret = all_secrets.get('SLACK_SIGNING_SECRET', '') | |
sig_basestring = f"v0:{msg['headers']['X-Slack-Request-Timestamp']}:{msg['body']}" | |
my_signature = 'v0=' + hmac.new( | |
bytes(slack_signing_secret, 'latin-1'), | |
msg=bytes(sig_basestring, 'latin-1'), | |
digestmod=hashlib.sha256 | |
).hexdigest() | |
print(f"verify_slack_msg: {my_signature} - {slack_signature}") | |
return my_signature.lower() == slack_signature.lower() | |
except Exception as e: | |
print(e) | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
have to verify every message from slack, except the init. challenge msg. and the slack doc. sample code is out of date, https://api.slack.com/authentication/verifying-requests-from-slack