Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Last active January 22, 2024 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruanbekker/6a4b0ee3941c3788cc60235486636ec8 to your computer and use it in GitHub Desktop.
Save ruanbekker/6a4b0ee3941c3788cc60235486636ec8 to your computer and use it in GitHub Desktop.
Validate Github Secret with Github Webhooks and API Gateway / Lambda
# 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