Skip to Content
Webhooks (próximamente)Verificar la firma (próximamente)

Verificar la firma Próximamente

Verifica siempre la firma antes de fiarte de un aviso: así sabes que lo envía Nexunia y que nadie lo ha modificado.

Cada petición lleva la cabecera Nexunia-Signature: t=<unix>,v1=<hex>. Cada v1 es el HMAC SHA-256 de <t>.<cuerpo crudo> con tu secreto whsec_….

  1. Usa el cuerpo crudo de la petición (no el JSON ya parseado).
  2. Recalcula el HMAC y compáralo en tiempo constante con alguno de los v1.
  3. Rechaza la petición si t se aleja más de 5 minutos de tu reloj.

Durante una rotación de secreto llegan dos v1 (el nuevo y el anterior): vale cualquiera de los dos.

Node.js

import { createHmac, timingSafeEqual } from 'node:crypto'; export function verify(rawBody, header, secret) { const parts = header.split(','); const t = Number(parts.find((p) => p.startsWith('t='))?.slice(2)); if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false; const expected = createHmac('sha256', secret).update(`${t}.${rawBody}`).digest(); return parts .filter((p) => p.startsWith('v1=')) .some((p) => { const got = Buffer.from(p.slice(3), 'hex'); return got.length === expected.length && timingSafeEqual(got, expected); }); }

Python

import hashlib, hmac, time def verify(raw_body: bytes, header: str, secret: str) -> bool: parts = dict(p.split("=", 1) for p in header.split(",") if "=" in p) t = int(parts.get("t", 0)) if not t or abs(time.time() - t) > 300: return False expected = hmac.new(secret.encode(), f"{t}.".encode() + raw_body, hashlib.sha256).hexdigest() candidates = [p.split("=", 1)[1] for p in header.split(",") if p.startswith("v1=")] return any(hmac.compare_digest(expected, c) for c in candidates)
Last updated on