Skip to content

Instantly share code, notes, and snippets.

@categulario
Created June 23, 2014 15:15
Show Gist options
  • Select an option

  • Save categulario/deeb41c402c800d1f6e6 to your computer and use it in GitHub Desktop.

Select an option

Save categulario/deeb41c402c800d1f6e6 to your computer and use it in GitHub Desktop.
Validate GitHub signature in python
# validate github signature
import hashlib
import hmac
import json
signature = hmac.new(GITHUB_TOKEN, payload, hashlib.sha1).hexdigest()
# assuming that the 'payload' variable keeps the content sent by github as plain text
# and 'headers' variable keeps the headers sent by GitHub
if hmac.compare_digest(signature, headers['X-Hub-Signature'].split('=')[1]):
payload_object = json.loads(payload)
# do something here
return json.dumps({'msg': 'Done!'})
else:
return json.dumps({'msg': 'not a chance'})
@ddabberu
Copy link

ddabberu commented Aug 14, 2020

If using flask service, just get the raw body as bytes (request.data), no need to do json.dumps or anything and encode the payload, or minify, it'll work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment