SPB Git

Repository Public

webhook.ts 3.1 KB 63 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : src/lib/webhook.ts9 * Module           : lib10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2113 * Last Modified    : 2026-11-2114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { db, now } from "./database/db";44import { hmac, safeEqual } from "./security/crypto";4546export const MAX_SKEW_SEC = 300;4748/**49 * Release webhook authentication: X-Orix-Signature = "sha256=" + HMAC-SHA256(secret, `${timestamp}.${nonce}.${body}`).50 * The timestamp bounds the validity window and each nonce is accepted once, so a captured request cannot be replayed.51 */52export function verifyWebhook(input: { secret: string; timestamp: string | null; nonce: string | null; signature: string | null; body: string }, at = Date.now()):53  { ok: true } | { ok: false; reason: "disabled" | "malformed" | "stale" | "bad_signature" | "replayed" } {54  if (!input.secret) return { ok: false, reason: "disabled" };55  const ts = Number(input.timestamp);56  if (!input.timestamp || !Number.isFinite(ts) || !input.nonce || !/^[A-Za-z0-9_-]{16,64}$/.test(input.nonce) || !input.signature?.startsWith("sha256=")) return { ok: false, reason: "malformed" };57  if (Math.abs(at / 1000 - ts) > MAX_SKEW_SEC) return { ok: false, reason: "stale" };58  if (!safeEqual(input.signature.slice(7), hmac(input.secret, `${input.timestamp}.${input.nonce}.${input.body}`))) return { ok: false, reason: "bad_signature" };59  db().prepare("DELETE FROM webhook_nonces WHERE at < ?").run(new Date(at - 2 * MAX_SKEW_SEC * 1000).toISOString());60  const r = db().prepare("INSERT OR IGNORE INTO webhook_nonces (nonce, at) VALUES (?, ?)").run(input.nonce, now());61  return r.changes === 1 ? { ok: true } : { ok: false, reason: "replayed" };62}63
Files main
Menu

Theme