Spectatr.ai

Content Delivery · Webhooks

Signature

Every call is signed with a secret held only by you and us. Check it before trusting the body.


The header

Header
X-Spectatr-Signature: t=1795155856,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
PartMeaning
tWhen we signed it, in Unix seconds.
v1HMAC-SHA256 of {t}.{raw body} with the shared secret, as lowercase hex.

Verifying

  1. 1Read the raw request body as bytes, before any JSON parsing. Re-serialised JSON will not match.
  2. 2Split the header into t and v1.
  3. 3Compute HMAC-SHA256 over t, a ., then the raw body, using the secret.
  4. 4Compare the result with v1 in constant time.
  5. 5Reject the call if t is more than 300 seconds from your clock. This stops replays.
ParameterValueNotes
AlgorithmHMAC-SHA256
KeyThe shared secretUsed as the UTF-8 string we give you
Message{t}.{raw body}The raw bytes of the body
OutputLowercase hex64 characters
Tolerance300 secondsBetween t and your clock

A retry is signed again, so t and v1 change. The body stays the same.

Reference implementations

Python
import hashlib, hmac, re, time

def verify(secret: str, raw_body: bytes, header: str, tolerance: int = 300) -> bool:
    match = re.fullmatch(r"t=(\d+),v1=([0-9a-f]{64})", header or "")
    if not match:
        return False
    timestamp, signature = int(match.group(1)), match.group(2)
    if abs(time.time() - timestamp) > tolerance:
        return False
    expected = hmac.new(secret.encode(), f"{timestamp}.".encode() + raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature)
Node.js
const crypto = require("crypto");

function verify(secret, rawBody, header, toleranceSeconds = 300) {
  const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(header || "");
  if (!match) return false;
  const timestamp = Number(match[1]);
  if (Math.abs(Date.now() / 1000 - timestamp) > toleranceSeconds) return false;
  const expected = crypto.createHmac("sha256", secret).update(`${timestamp}.`).update(rawBody).digest();
  return crypto.timingSafeEqual(expected, Buffer.from(match[2], "hex"));
}