Created
April 27, 2021 12:06
-
-
Save Clivern/5d1d7ba295a91a036301d6c99a0b275b to your computer and use it in GitHub Desktop.
Validate github webhook signature
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
| import hashlib | |
| import hmac | |
| class Signature(object): | |
| """Signature Validates Github Webhook Payload""" | |
| def sign_request(self, webhook_secret, data): | |
| """Generate Payload Signature""" | |
| message = bytes(data, 'utf-8') | |
| secret = bytes(webhook_secret, 'utf-8') | |
| hash = hmac.new(secret, message, hashlib.sha1) | |
| return hash.hexdigest() | |
| def validate_request(self, webhook_secret, data, signature): | |
| """Validate Payload Signature""" | |
| sha_name, signature = signature.split('=') | |
| if sha_name != 'sha1': | |
| return False | |
| return hmac.compare_digest(self.sign_request(webhook_secret, data), signature) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment