Last active
April 1, 2020 13:00
-
-
Save manuelep/4b64492ceeaa07f095302f94956ea554 to your computer and use it in GitHub Desktop.
authenticate woocommerce webhook
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 compute(body): | |
| secret = '<here is my secret key>' | |
| dig = hmac.new(secret.encode(), | |
| msg = body.encode(), | |
| digestmod = hashlib.sha256 | |
| ).digest() | |
| computed = base64.b64encode(dig).decode() | |
| return computed | |
| def hookCheck(func): | |
| def wrapper(*args, **kw): | |
| signature = request.env.http_x_wc_webhook_signature | |
| body = request.body.read() # ?? | |
| computed = compute(body) | |
| if signature==computed: | |
| return func(*args, **kw) | |
| raise HTTP(403) | |
| return wrapper | |
| @service.json | |
| def listenToHooks(): | |
| @hookCheck | |
| def _main_(): | |
| # do stuff | |
| return {} | |
| return _main_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you man for putting it on GH. It truly help me a lot today =]