Verify the signature of a webhook request
This guide shows you how to verify that a request to your webhook endpoint is from Authway. The webhook receiver is available on the public internet, so validate every request before you process it.
Authway signs every request with the standard HTTP message signature (RFC 9421). The older X-IRM-Signature header is deprecated.
Prerequisites
- A webhook registered in a webhook system.
- The secret (API key) that you used when you registered the webhook. If your webhook systems have different API keys, you need to know which secret belongs to which system.
- The exact body of the request, as bytes, before your framework parses it.
Verify the HTTP message signature
- Read the
Signature-Inputheader. Check that the signature labelledirmhas thetagparameterirm-messaging-eventand covers all of these components:@method,@path,@query,content-type,content-digest,x-irm-topicandx-irm-eventtype. The signature must also contain thecreatedparameter. - Find your secret. The
keyidparameter is the id of the webhook system. Each webhook system can have its own API key, so use thekeyidto find the secret that belongs to it. - Check that the
createdparameter is recent and reject older signatures. Authway signs the request again for every retry, socreatedis always fresh. Five minutes is a reasonable limit. - Build the signature base as described in RFC 9421, section 2.5. Calculate its HMAC-SHA256 with your secret as UTF-8 bytes, and compare the result with the
irmvalue in theSignatureheader (a base64 value between colons, as defined in RFC 9421). Use a constant-time comparison. - Check the body. The signature covers the
Content-Digestheader, not the body itself, so calculate the SHA-256 digest of the exact bytes of the received body, base64 encode it, and compare it with thesha-256value inContent-Digest(RFC 9530). Do not serialise the body again before you calculate the digest. - If any step fails, do not process the event. Respond with
401(or another 4xx status code). Authway retries on408and5XXresponses, so do not use those for an invalid signature.
We recommend that you use a library that implements RFC 9421 instead of writing the signature base yourself, for example the NSign package for .NET.
Validate the deprecated X-IRM-Signature header
The X-IRM-Signature header is deprecated. Authway still sends it, together with the standard signature, but you should move to the standard HTTP message signature. If you still validate X-IRM-Signature, calculate a HMAC-SHA256 signature of the received body with your secret and compare it with the header value in constant time. Use the exact body as you received it.
using System;
using System.Security.Cryptography;
using System.Text;
public static class WebhookSignature
{
public static bool IsValid(string body, string signatureHeader, string secret)
{
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
var expected = hmac.ComputeHash(Encoding.UTF8.GetBytes(body));
var received = new byte[expected.Length];
if (!Convert.TryFromBase64String(signatureHeader, received, out var written)
|| written != expected.Length)
{
return false;
}
return CryptographicOperations.FixedTimeEquals(expected, received);
}
}Verify that it works
Send a test event to your webhook, as described in Test a webhook. Your endpoint should accept the request and respond with a 2XX status code. Then change a character in a copy of the body and check that your endpoint rejects it.
Next steps
- Webhook events and payload describes every header and the signature properties.
- Webhooks explains retries, ordering and other ways to secure your endpoint.