Verify webhook signatures
Every delivery carries a Mountaya-Signature header. Verify it on the raw body before you trust the event.
- Read
t, the time Mountaya signed the delivery, in Unix seconds. - Join
t, a period and the raw request body:<t>.<body>. - Compute its HMAC-SHA256, keyed by your whole secret as UTF-8,
whsk_included. - Compare the lowercase hexadecimal result with each
v1of the header, in constant time. - Refuse a
tmore than 300 seconds away from your clock.
The secret is the one the endpoint was created with. Keep it in MOUNTAYA_WEBHOOK_SECRET on your server.
constructWebhookEvent checks the signature and the timestamp, then parses the event. It throws MountayaWebhookSignatureError when either check fails.
The standard library is enough: hmac computes and compares, hashlib supplies SHA-256.
Call it with the body exactly as it arrived. In Flask, that is request.get_data():
Rotating a secret in the Mountaya app keeps the previous one signing for up to 7 days. Meanwhile, the header carries one v1 per secret:
Accept a delivery when any v1 matches your secret. Deploy the new secret before the overlap ends. If a secret leaked, rotate with no overlap: every earlier secret stops at once.
Check your code against the published test vectors before real deliveries reach it. Each case gives a secret, a clock, a header, a body and the expected verdict.
Contract v0.8.0 publishes them: download the test vectors.