Files
loki/scripts/test/orangecat-webhooks.ts
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

74 lines
2.8 KiB
TypeScript

import assert from "node:assert/strict";
import { createHmac } from "node:crypto";
import { verifyOrangeCatWebhookSignature } from "../../src/lib/integrations/orangecat-webhook";
/**
* Cross-repo contract test for the OrangeCat → Loki webhook HMAC.
*
* Both receivers (/api/orangecat/entitlement, /api/orangecat/events) trust
* verifyOrangeCatWebhookSignature. OrangeCat's emitters
* (src/services/loki/entitlement-notify.ts in the orangecat repo) sign
* exactly like `ocSign` below: `sha256=` + hex HMAC-SHA256 of the raw JSON body
* with the shared ORANGECAT_WEBHOOK_SECRET. If either side's scheme drifts,
* this fails before a silently-rejected settlement ships.
*/
const secret = "shared-orangecat-webhook-secret-at-least-32ch";
/** The exact header OrangeCat produces. */
function ocSign(rawBody: string): string {
return "sha256=" + createHmac("sha256", secret).update(rawBody).digest("hex");
}
// The exact bodies OrangeCat POSTs (mirrors the emitters' JSON.stringify shape).
const entitlementBody = JSON.stringify({
actorId: "11111111-1111-1111-1111-111111111111",
plan: "pro",
externalId: "pi_abc123",
periodDays: 30,
amountBtc: "0.0005",
});
const eventBody = JSON.stringify({
type: "payment.settled",
entityType: "project",
entityId: "22222222-2222-2222-2222-222222222222",
title: "Neighbourhood fund",
amountBtc: "0.01",
externalId: "pi_def456",
});
// 1. A genuine OrangeCat-signed body verifies for both rails.
assert.equal(
verifyOrangeCatWebhookSignature(entitlementBody, ocSign(entitlementBody), secret),
true,
);
assert.equal(verifyOrangeCatWebhookSignature(eventBody, ocSign(eventBody), secret), true);
// 2. A bare hex (no "sha256=" prefix) is accepted for forward-compat.
const bareHex = createHmac("sha256", secret).update(entitlementBody).digest("hex");
assert.equal(verifyOrangeCatWebhookSignature(entitlementBody, bareHex, secret), true);
// 3. Tampered body → reject (signature no longer matches the raw body).
assert.equal(
verifyOrangeCatWebhookSignature(entitlementBody + " ", ocSign(entitlementBody), secret),
false,
);
// 4. Wrong secret → reject.
assert.equal(
verifyOrangeCatWebhookSignature(entitlementBody, ocSign(entitlementBody), "not-the-secret"),
false,
);
// 5. Missing header → reject (fail-closed).
assert.equal(verifyOrangeCatWebhookSignature(entitlementBody, null, secret), false);
// 6. Garbage / non-hex header → reject, never throw.
assert.equal(verifyOrangeCatWebhookSignature(entitlementBody, "sha256=not-hex!!", secret), false);
assert.equal(verifyOrangeCatWebhookSignature(entitlementBody, "", secret), false);
// 7. Right length but wrong bytes → reject (timing-safe path still says no).
const wrongHex = "0".repeat(64);
assert.equal(verifyOrangeCatWebhookSignature(entitlementBody, `sha256=${wrongHex}`, secret), false);
console.log("OrangeCat webhook HMAC contract checks passed.");