Check a call is from us

Anyone could post to your address, so check the signature.

Compute the signature yourself with your secret and compare. Use the body exactly as it arrived, and ignore calls whose timestamp is more than a few minutes old.

Node.js
import crypto from 'node:crypto'

// rawBody: the request body exactly as received (a string), not parsed and re-written.
function isFromVisionSage(req, rawBody, secret) {
  const timestamp = req.headers['x-visionsage-timestamp']
  const given = req.headers['x-visionsage-signature'] // "sha256=…"
  const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(timestamp + '.' + rawBody).digest('hex')
  const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) < 300 // ignore old calls replayed
  return fresh && given.length === expected.length && crypto.timingSafeEqual(Buffer.from(given), Buffer.from(expected))
}
Python
import hashlib, hmac, time

def is_from_visionsage(headers, raw_body: bytes, secret: str) -> bool:
    timestamp = headers["X-VisionSage-Timestamp"]
    given = headers["X-VisionSage-Signature"]  # "sha256=…"
    signed = timestamp.encode() + b"." + raw_body
    expected = "sha256=" + hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    fresh = abs(time.time() - float(timestamp)) < 300  # ignore old calls replayed
    return fresh and hmac.compare_digest(given, expected)

Something missing or unclear? Tell us and we will fix this page.