Repository navigation
Expand file tree
/
Copy pathverify_webhook.py
More file actions
51 lines (42 loc) · 2.01 KB
/
Copy pathverify_webhook.py
File metadata and controls
51 lines (42 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Verify Standard Webhooks signatures with the standard library only."""
import base64
import hashlib
import hmac
import time
class WebhookVerificationError(Exception):
pass
def verify_webhook(secret, headers, raw_body, now=None, tolerance_seconds=300):
"""Return True when the delivery is authentic, otherwise raise WebhookVerificationError.
secret: signing secret, "whsec_<base64 key>".
headers: mapping of request headers (names matched case-insensitively).
raw_body: the exact bytes (or str) received. Never a re-serialized object.
now: Unix seconds, defaults to the clock.
"""
if now is None:
now = int(time.time())
lower = {name.lower(): value for name, value in headers.items()}
msg_id = lower.get("webhook-id")
timestamp = lower.get("webhook-timestamp")
signature_header = lower.get("webhook-signature")
if not msg_id or not timestamp or not signature_header:
raise WebhookVerificationError("Missing webhook-id, webhook-timestamp or webhook-signature header")
if not timestamp.isdigit():
raise WebhookVerificationError("Invalid webhook-timestamp")
if abs(now - int(timestamp)) > tolerance_seconds:
raise WebhookVerificationError("Timestamp outside the tolerance window")
key = base64.b64decode(secret.removeprefix("whsec_"))
body = raw_body.encode("utf-8") if isinstance(raw_body, str) else raw_body
signed = f"{msg_id}.{timestamp}.".encode("utf-8") + body
expected = hmac.new(key, signed, hashlib.sha256).digest()
# The header holds space-separated "v1,<base64>" entries. Any match is enough.
for entry in signature_header.split(" "):
version, _, signature = entry.partition(",")
if version != "v1" or not signature:
continue
try:
candidate = base64.b64decode(signature, validate=True)
except ValueError:
continue
if hmac.compare_digest(candidate, expected):
return True
raise WebhookVerificationError("No matching signature")