Verify signatures
Mitu signs everything it sends to your systems. Webhook deliveries are signed so you know an event really came from Mitu, and requests forwarded to your API when an agent uses your product are signed so you know they passed through Mitu and were paid for where payment applies. Checking the signature before you act protects you from anyone who discovers your endpoint and tries to imitate Mitu.
Both signatures work the same way. Mitu combines a timestamp with the content it is sending, signs the result with a secret that only you and Mitu hold, and sends the signature in the x-mitu-signature header.
The t value is the time the message was signed, in Unix seconds, and v1 is an HMAC-SHA256 signature encoded as hexadecimal.
Find your signing secret
Each webhook endpoint has its own signing secret, which you can find in the dashboard under Webhooks. Requests forwarded to your API are signed with the secret under Integration, which is shown only when it is created, so copy it at that point. You can rotate it there at any time.
Store each secret with your other server credentials. They are only used to verify what Mitu sends you, so they never need to leave your servers.
Read the timestamp and signature
Split the x-mitu-signature header on commas, then split each part on the first equals sign. Take t as the timestamp and v1 as the signature you will check.
Rebuild the signed content
Join the values below with full stops, in this order, using the request body exactly as it arrived. Parsing the JSON and serialising it again can change spacing or field order, and the signature will no longer match.
For example, a webhook signed at 1788990420 has the signed content 1788990420.{"event":"payment.completed",...}.
Compute and compare the signature
Compute an HMAC-SHA256 of the signed content using your signing secret, encode it as hexadecimal and compare it with v1. Use a constant time comparison, which takes the same time whether or not the values match and so reveals nothing to an attacker.
Finally, check that the timestamp is recent. Rejecting anything signed more than a few minutes ago stops a captured message from being replayed later. Five minutes is a sensible tolerance for most systems.
The example below verifies a webhook delivery. To verify a request to your API, change only the signed content, as described in the table above.
Node.js
Python
Go
Only act on a message once its signature has been verified. After that, use the event_id of a webhook or the x-mitu-request-id of a request to make sure the same message is never processed twice.