Last active
January 22, 2024 07:52
-
-
Save ruanbekker/6a4b0ee3941c3788cc60235486636ec8 to your computer and use it in GitHub Desktop.
Validate Github Secret with Github Webhooks and API Gateway / Lambda
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
# GITHUB_SECRET | |
# 'fhe3UdxahLO0Txs......' | |
# >>> event['body'] | |
# 'eyJ6ZW4iOi.......' | |
# >>> event['headers']['X-Hub-Signature'] | |
# 'sha1=2c887c74da271045b170d224................' | |
import hashlib | |
import hmac | |
github_signature = event['headers']['X-Hub-Signature'].split('=')[1] | |
def validate_signature(github_secret, request_body, github_signature): | |
encoded_secret = github_secret.encode() | |
decoded_rbody = base64.b64decode(request_body) | |
calculated_signature = hmac.new(encoded_secret, decoded_rbody, hashlib.sha1).hexdigest() | |
response = hmac.compare_digest(calculated_signature, github_signature) | |
return response | |
if validate_signature(GITHUB_SECRET, event['body'], github_signature): | |
print('validated') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment