Skip to main content

Webhook Verification

Every webhook Cas sends to your system is signed with the secret key of that webhook. You should verify the signature before processing the data, to make sure the request really comes from Cas and its content was not modified in transit.

Each webhook has its own secret key, which you can find on the webhook configuration page in Cas Console. Keep the secret key on your server and never ship it in client-side code.

Headers​

Each webhook request carries the following 2 headers:

HeaderDescription
X-Cas-TimestampThe time Cas sent the request, as a Unix timestamp in milliseconds. Example: 1732352400000
X-Cas-SignatureThe HMAC-SHA512 signature of the request, as a lowercase hex string (128 characters)

Verification steps​

  1. Read X-Cas-Timestamp and X-Cas-Signature from the headers.
  2. Read the raw body of the request, i.e. the original JSON string you received.
  3. Build the message to sign in this format:
    {X-Cas-Timestamp}.{raw body}
  4. Compute the HMAC-SHA512 of that message using the secret key, hex-encoded.
  5. Compare the result with X-Cas-Signature. If they match, the request is valid.
  6. Recommended: reject requests whose X-Cas-Timestamp differs from your system clock by more than 5 minutes to prevent replay attacks.
Use the raw body

Always compute the signature over the raw body. Do not parse the JSON and serialize it back to a string, because key order, whitespace or character escaping may change and the signature will no longer match.

Notes
  • Each time a webhook is retried, Cas generates a new X-Cas-Timestamp and X-Cas-Signature.
  • Use a constant-time comparison function instead of == when comparing signatures.
  • Keep your server clock synchronized (NTP) so the timestamp check works correctly.

Example​

You can use the data below to test your signature function (skip the timestamp check):

Value
Secret keyb3c1f9a2-7d4e-4c8a-9f21-5e6d7c8b9a01
X-Cas-Timestamp1732352400000
Raw body{"environment":"dev","webhookType":"GRANT","webhookCode":"DEFAULT_UPDATE","grantId":"4c657924-13f3-11ee-a4bb-42010a40001b","error":null}

Message to sign:

1732352400000.{"environment":"dev","webhookType":"GRANT","webhookCode":"DEFAULT_UPDATE","grantId":"4c657924-13f3-11ee-a4bb-42010a40001b","error":null}

Expected signature (X-Cas-Signature):

9031281e6559f0d801c242d2241f65747dfccf04faaef471162f61955e213118276f0916b882adae065e9be33c7b0660034437de60d272df9c7d4990338c558b

Sample code​

Example with Express. Use express.raw() to get the raw body, do not use express.json() for this route.

const crypto = require('crypto');
const express = require('express');

const app = express();
const WEBHOOK_SECRET = process.env.CAS_WEBHOOK_SECRET; // Webhook secret key
const TOLERANCE_MS = 5 * 60 * 1000; // 5 minutes

function verifyCasSignature(rawBody, timestamp, signature, secret) {
if (!timestamp || !signature) return false;

// Replay protection: reject requests whose timestamp is off by more than 5 minutes
const ts = Number(timestamp);
if (!Number.isFinite(ts) || Math.abs(Date.now() - ts) > TOLERANCE_MS) return false;

const expected = crypto
.createHmac('sha512', secret)
.update(`${timestamp}.${rawBody}`)
.digest('hex');

const a = Buffer.from(expected);
const b = Buffer.from(signature);
return a.length === b.length && crypto.timingSafeEqual(a, b);
}

app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
const rawBody = req.body.toString('utf8');

const isValid = verifyCasSignature(
rawBody,
req.get('X-Cas-Timestamp'),
req.get('X-Cas-Signature'),
WEBHOOK_SECRET,
);
if (!isValid) return res.status(401).send('Invalid signature');

const event = JSON.parse(rawBody);
// TODO: handle the event
return res.sendStatus(200);
});

app.listen(3000);