Skip to content
Verify the signature of a webhook request

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

  1. Read the Signature-Input header. Check that the signature labelled irm has the tag parameter irm-messaging-event and covers all of these components: @method, @path, @query, content-type, content-digest, x-irm-topic and x-irm-eventtype. The signature must also contain the created parameter.
  2. Find your secret. The keyid parameter is the id of the webhook system. Each webhook system can have its own API key, so use the keyid to find the secret that belongs to it.
  3. Check that the created parameter is recent and reject older signatures. Authway signs the request again for every retry, so created is always fresh. Five minutes is a reasonable limit.
  4. 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 irm value in the Signature header (a base64 value between colons, as defined in RFC 9421). Use a constant-time comparison.
  5. Check the body. The signature covers the Content-Digest header, 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 the sha-256 value in Content-Digest (RFC 9530). Do not serialise the body again before you calculate the digest.
  6. If any step fails, do not process the event. Respond with 401 (or another 4xx status code). Authway retries on 408 and 5XX responses, 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