Skip to navigation

Verify signatures

Confirm that every webhook and every request Mitu sends you is genuine before you act on it.
View as Markdown

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.

x-mitu-signature: t=1788990420,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd

The t value is the time the message was signed, in Unix seconds, and v1 is an HMAC-SHA256 signature encoded as hexadecimal.

1

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.

2

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.

3

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.

MessageSigned content
Webhook deliveryThe timestamp and the raw body
Request to your APIThe timestamp, the x-mitu-request-id header, the x-mitu-amount-charged header (or an empty value when it is absent) and the raw body

For example, a webhook signed at 1788990420 has the signed content 1788990420.{"event":"payment.completed",...}.

4

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.

import crypto from 'node:crypto'
export function verifyMituSignature(header, rawBody, secret, toleranceSeconds = 300) {
const parts = Object.fromEntries(
(header ?? '').split(',').map((part) => {
const i = part.indexOf('=')
return [part.slice(0, i), part.slice(i + 1)]
}),
)
const { t, v1 } = parts
if (!t || !v1) return false
if (Math.abs(Date.now() / 1000 - Number(t)) > toleranceSeconds) return false
const expected = crypto
.createHmac('sha256', secret)
.update(`${t}.${rawBody}`)
.digest('hex')
return (
expected.length === v1.length &&
crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(v1))
)
}

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.