Files
crosscode-platform/packages/contracts/tests/hub-signing.test.ts
T
joshuaandClaude Opus 5.5 ea1575f716 Contracts: event, event feed, church, identity, sign-in return, Hub protocol v2
- crosscode.event/1 and crosscode.event-feed/1 (feed, event.changed
  webhook), with session and registration rules; parseEventFeed skips
  and reports bad events instead of failing the whole feed.
- crosscode.church/1: branding, campuses, service times, contact, links.
- crosscode.identity/1: sign-in claims and the user.updated /
  user.deleted / user.export back-channel calls and replies.
- Sign-in return helpers (isSafeReturnPath, encodeState/decodeState,
  isRegisteredRedirectUri) so every product returns people to the page
  they came from in the same safe way.
- Hub protocol v2: product on register/heartbeat, stats per product,
  install public key, linked products, customerId/product in licences.
- @crosscode/contracts/signing (Node only): the Hub's HMAC scheme.
- Samples for every contract in samples/, checked by the tests (120).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 09:47:40 +02:00

79 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createHmac } from "node:crypto";
import { hub } from "../src";
import { HUB_HEADERS, LINK_HEADERS, sign, signedHeaders, verify } from "../src/signing";
import { sample } from "./helpers";
describe("Hub protocol v2", () => {
it("picks the stats by product", () => {
const events = sample<Record<string, unknown>>("hub-heartbeat-events.json");
const website = sample<Record<string, unknown>>("hub-heartbeat-website.json");
expect(hub.heartbeatRequestSchema.safeParse({ ...events, stats: website.stats }).success).toBe(false);
});
it("refuses protocol v1 bodies (the Hub routes those to its v1 handler)", () => {
expect(hub.heartbeatRequestSchema.safeParse({ ...sample("hub-heartbeat-events.json"), protocolVersion: 1 }).success).toBe(false);
});
it("accepts a licence token payload from a Hub before 2.0 (no customerId or product)", () => {
const payload = sample<Record<string, unknown>>("hub-licence-payload.json");
delete payload.customerId;
delete payload.product;
expect(hub.licencePayloadSchema.safeParse(payload).success).toBe(true);
});
it("only sends income as a band", () => {
const hb = sample<{ stats: { income: Record<string, unknown> } }>("hub-heartbeat-events.json");
hb.stats.income.last30d = 12345;
expect(hub.heartbeatRequestSchema.safeParse(hb).success).toBe(false);
});
it.each([
[0, "0–100"],
[100, "0–100"],
[101, "101–300"],
[750, "301–750"],
[751, "751–2 000"],
[5000, "2 001–5 000"],
[5001, "over 5 000"],
] as const)("puts %d active members in the %s band", (n, band) => {
expect(hub.activeMemberBand(n)).toBe(band);
});
});
describe("signing", () => {
const secret = "s3cret-for-tests";
const body = JSON.stringify({ hello: "world" });
const now = new Date("2026-11-03T10:00:00.000Z");
it("signs exactly like the Hub protocol v1 heartbeat", () => {
const timestamp = now.toISOString();
const expected = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest("hex");
expect(sign(secret, timestamp, body)).toBe(expected);
expect(sign(secret, timestamp, new TextEncoder().encode(body))).toBe(expected);
});
it("builds the headers and verifies them", () => {
const h = signedHeaders(LINK_HEADERS, { senderId: "inst-1", secret, rawBody: body, now });
expect(h[LINK_HEADERS.sender]).toBe("inst-1");
expect(verify({ secret, timestamp: h[LINK_HEADERS.timestamp], signature: h[LINK_HEADERS.signature], rawBody: body, now })).toEqual({ ok: true });
expect(Object.keys(signedHeaders(HUB_HEADERS, { senderId: "i", secret, rawBody: body, now }))).toEqual(["x-hub-install", "x-hub-timestamp", "x-hub-signature"]);
});
it("refuses a changed body, a wrong secret or a bad signature", () => {
const timestamp = now.toISOString();
const signature = sign(secret, timestamp, body);
expect(verify({ secret, timestamp, signature, rawBody: body + " ", now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
expect(verify({ secret: "other", timestamp, signature, rawBody: body, now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
expect(verify({ secret, timestamp, signature: "zz", rawBody: body, now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
expect(verify({ secret, timestamp, signature: undefined, rawBody: body, now })).toEqual({ ok: false, code: "BAD_SIGNATURE" });
});
it("refuses requests more than five minutes off the clock", () => {
const old = new Date(now.getTime() - 5 * 60 * 1000 - 1).toISOString();
expect(verify({ secret, timestamp: old, signature: sign(secret, old, body), rawBody: body, now })).toEqual({ ok: false, code: "STALE_REQUEST" });
const edge = new Date(now.getTime() - 5 * 60 * 1000).toISOString();
expect(verify({ secret, timestamp: edge, signature: sign(secret, edge, body), rawBody: body, now })).toEqual({ ok: true });
expect(verify({ secret, timestamp: "yesterday", signature: sign(secret, "yesterday", body), rawBody: body, now })).toEqual({ ok: false, code: "STALE_REQUEST" });
});
});