@t3b/hub-guard 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -335,6 +335,26 @@ if (!c.ok) return saleRefusal(c.reason);
335
335
  the same id again answers with the first sale (the Hub tells the app to retry that way after a silence).
336
336
  - A new sale's pass (bound to a body) used for a read, or another kind of Hub token, is `wrong_use`.
337
337
 
338
+ ## The till's call into the conversations: `verifyChatCall` (0.14.0, WORKFLOWS §26)
339
+
340
+ For Customers only. The till (Beez POS) asks which conversation its signed-in employee has open, and sends an order with
341
+ its payment link into it - through the Hub, which asks Customers' two doors (`GET /api/hub/chat/open`,
342
+ `POST /api/hub/chat/orders`) with its own pass in the header `x-t3b-chat` (`CHAT_HEADER`). Read the body as raw bytes
343
+ first:
344
+
345
+ ```ts
346
+ const bytes = req.method === "POST" ? new Uint8Array(await req.arrayBuffer()) : null;
347
+ const c = await verifyChatCall(req, bytes, { moduleId: "customers", keys: hubKeys(hubUrl), environment });
348
+ if (!c.ok) return chatRefusal(c.reason);
349
+ // c.from = the till ("beez-pos"), c.person = the employee's Hub id, c.name = their name,
350
+ // c.pages / c.stores = their ticks and stores in Customers (as your session pass), c.jti = a one-use number
351
+ ```
352
+
353
+ - Keep `c.jti` once; a second time is a replay.
354
+ - The ask (no body) takes only a `chat-open` pass and the send only a `chat-order` pass bound to its exact bytes; the
355
+ other one, or another kind of Hub token, is `wrong_use`; another body is `body_changed`.
356
+ - The Hub has already checked that the person may be in both apps and has Write on your `chat` page.
357
+
338
358
  ## What it enforces
339
359
 
340
360
  - `/hub/enter?ticket=…`: an EdDSA ticket signed by the Hub's published key, for this module, from this environment's Hub, 60 seconds, single use - spent at the Hub, so once across all servers (0.9.0) → sets the module session (HttpOnly, Secure, SameSite=Strict) and answers a short "Opening …" page that moves on to the page (0.4.0: a SameSite=Strict cookie set on a redirect ending a navigation from another site is not sent on it). `appName` names the app on that page; `landing(path)` maps the pass's page (e.g. `"/"` → `"/app"`), never off the module's own address.
@@ -345,6 +365,9 @@ if (!c.ok) return saleRefusal(c.reason);
345
365
 
346
366
  ## Changes
347
367
 
368
+ - **0.14.0** (WORKFLOWS §26, D-097; owner 2026-11-16: *"Yes, build step 3"*) - the till's call into Customers'
369
+ conversations, passed by the Hub: `verifyChatCall`, `chatRefusal`, `CHAT_HEADER`, the `ChatCheck` type. Nothing else
370
+ changes; an app that does not take the till's calls has nothing to do.
348
371
  - **0.12.0** (WORKFLOWS §24, D-092; owner 2026-10-25: *"each one is built differently"*, then *"Yes to section 24 as
349
372
  drawn: ship it inside hub-guard 0.12.0 together with HR's features (one release)"*) - **published 2026-10-25 on his
350
373
  later word (D-094), without HR's features; HR's leave line goes into 0.13.0.** The top bar's door: `guard.bar(session)`, `guard.barResponse(session,
package/dist/chat.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { type JWTVerifyGetKey } from "jose";
2
+ import { type Pages } from "./pages.js";
3
+ /** The header the Hub's pass travels in (Customers' Authorization stays free for its own use). */
4
+ export declare const CHAT_HEADER = "x-t3b-chat";
5
+ export type ChatCheck = {
6
+ ok: true;
7
+ from: string;
8
+ person: string;
9
+ name: string;
10
+ pages: Pages;
11
+ stores: string[];
12
+ jti: string;
13
+ } | {
14
+ ok: false;
15
+ reason: "missing" | "bad_signature" | "expired" | "wrong_module" | "wrong_use" | "wrong_environment" | "body_changed";
16
+ };
17
+ /**
18
+ * Is this the Hub passing the till's call into the conversations? `body` is the request's raw bytes for a send (POST -
19
+ * read it with `await req.arrayBuffer()` before parsing: the pass covers the exact bytes) and null for the ask (GET).
20
+ * The ask takes only a "chat-open" pass and the send only a "chat-order" pass. Keep `jti` once.
21
+ */
22
+ export declare function verifyChatCall(req: Request, body: Uint8Array | string | null, opts: {
23
+ moduleId: string;
24
+ keys: JWTVerifyGetKey;
25
+ now?: Date;
26
+ environment?: string;
27
+ }): Promise<ChatCheck>;
28
+ /** The refusal Customers' chat doors answer with: 401 and only the reason word (the Hub passes it back unchanged). */
29
+ export declare function chatRefusal(reason: Extract<ChatCheck, {
30
+ ok: false;
31
+ }>["reason"]): Response;
package/dist/chat.js ADDED
@@ -0,0 +1,75 @@
1
+ import { jwtVerify } from "jose";
2
+ import { APP_ID_RE } from "./apps.js";
3
+ import { HUB_ISSUER, TYP } from "./identity.js";
4
+ import { readPages } from "./pages.js";
5
+ import { b64urlEncode, constantTimeEqual, sha256 } from "./webcrypto.js";
6
+ // WORKFLOWS §26 (D-097, Customers WORKFLOWS 5.26 step 3): the till (Beez POS) asks Customers which conversation its
7
+ // signed-in employee has open, and sends an order with its payment link into it - only through the Hub. The Hub has
8
+ // checked the till's Vercel ID and that the person may answer conversations (Write on Customers' `chat` page); then it
9
+ // asks Customers' door with its own 60-second pass in the header `x-t3b-chat`: `use` "chat-open" on the ask (GET) or
10
+ // "chat-order" on the send (POST), naming the till (`from`), the person, their name, their page ticks and stores in
11
+ // Customers (`pages`, `stores` - as in Customers' own session pass, so the contact tick and the store limits apply as on
12
+ // its screens) and a one-use number (`jti`).
13
+ // On a send the pass also carries `bh`, the SHA-256 of the exact body, so it cannot be lifted onto another order.
14
+ // Customers keeps each one-use number once.
15
+ /** The header the Hub's pass travels in (Customers' Authorization stays free for its own use). */
16
+ export const CHAT_HEADER = "x-t3b-chat";
17
+ /** A Hub person id. */
18
+ const PERSON_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
19
+ /** A person's name as the Hub holds it: never empty, never a wall of text. */
20
+ const NAME_MAX = 120;
21
+ const STORE_RE = /^[a-z0-9-]{1,30}$/;
22
+ /**
23
+ * Is this the Hub passing the till's call into the conversations? `body` is the request's raw bytes for a send (POST -
24
+ * read it with `await req.arrayBuffer()` before parsing: the pass covers the exact bytes) and null for the ask (GET).
25
+ * The ask takes only a "chat-open" pass and the send only a "chat-order" pass. Keep `jti` once.
26
+ */
27
+ export async function verifyChatCall(req, body, opts) {
28
+ const token = (req.headers.get(CHAT_HEADER) ?? "").trim();
29
+ if (!token || token.length > 4096)
30
+ return { ok: false, reason: "missing" };
31
+ let p;
32
+ try {
33
+ ({ payload: p } = await jwtVerify(token, opts.keys, { issuer: HUB_ISSUER, audience: opts.moduleId, algorithms: ["EdDSA"], typ: TYP.call, currentDate: opts.now, clockTolerance: 5, maxTokenAge: 65 }));
34
+ }
35
+ catch (e) {
36
+ const code = e.code ?? "";
37
+ if (code === "ERR_JWT_EXPIRED")
38
+ return { ok: false, reason: "expired" };
39
+ if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED" && /"aud"/.test(e.message))
40
+ return { ok: false, reason: "wrong_module" };
41
+ // another kind of Hub token (a ticket, a relayed event) is named as such, not as a clock problem
42
+ if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED" && /"typ"/.test(e.message))
43
+ return { ok: false, reason: "wrong_use" };
44
+ if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED")
45
+ return { ok: false, reason: "expired" };
46
+ return { ok: false, reason: "bad_signature" };
47
+ }
48
+ // the ask and the send each take only their own pass: a send's pass is never good for an ask, nor the other way
49
+ if (p.use !== (body === null ? "chat-open" : "chat-order"))
50
+ return { ok: false, reason: "wrong_use" };
51
+ if (opts.environment && p.env !== opts.environment)
52
+ return { ok: false, reason: "wrong_environment" };
53
+ const from = typeof p.from === "string" ? p.from : "";
54
+ const person = typeof p.person === "string" ? p.person : "";
55
+ const name = typeof p.name === "string" ? p.name.trim() : "";
56
+ const jti = typeof p.jti === "string" ? p.jti : "";
57
+ const stores = Array.isArray(p.stores) && p.stores.length <= 50 && p.stores.every((s) => typeof s === "string" && STORE_RE.test(s)) ? p.stores : null;
58
+ const pages = p.pages && typeof p.pages === "object" && !Array.isArray(p.pages) ? readPages(p.pages) : null;
59
+ if (!stores || !pages || !APP_ID_RE.test(from) || !PERSON_RE.test(person) || !name || name.length > NAME_MAX || !/^[A-Za-z0-9_-]{16,64}$/.test(jti))
60
+ return { ok: false, reason: "bad_signature" };
61
+ if (body !== null) {
62
+ const want = typeof p.bh === "string" ? p.bh : "";
63
+ const bytes = typeof body === "string" ? new TextEncoder().encode(body) : body;
64
+ if (!want || !constantTimeEqual(b64urlEncode(await sha256(bytes)), want))
65
+ return { ok: false, reason: "body_changed" };
66
+ }
67
+ else if (p.bh !== undefined) {
68
+ return { ok: false, reason: "wrong_use" };
69
+ }
70
+ return { ok: true, from, person: person.toLowerCase(), name, pages, stores, jti };
71
+ }
72
+ /** The refusal Customers' chat doors answer with: 401 and only the reason word (the Hub passes it back unchanged). */
73
+ export function chatRefusal(reason) {
74
+ return new Response(JSON.stringify({ error: "hub_only", reason }), { status: 401, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } });
75
+ }
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export { checkCloudflareAccess } from "./cf-access.js";
10
10
  export { verifyRelay, RELAY_HEADERS, RELAY_MAX_AGE_S, type RelayCheck } from "./relay.js";
11
11
  export { verifyStationCall, stationRefusal, stationJson, STATION_ID_RE, STATION_NAME_MAX, type StationCheck, type StationAnswer, type StationOrder, type StationItem, type PackAnswer, } from "./station.js";
12
12
  export { verifySaleCall, saleRefusal, SALE_ID_RE, type SaleCheck } from "./sale.js";
13
+ export { verifyChatCall, chatRefusal, CHAT_HEADER, type ChatCheck } from "./chat.js";
13
14
  export { ownVercelToken, ownEnvironment, hubKeys, APP_ID_HEADER, OIDC_HEADER, type TokenProvider } from "./identity.js";
14
15
  /**
15
16
  * Reads the guard's settings from a module's environment. Nothing secret: the Hub's address
package/dist/index.js CHANGED
@@ -11,6 +11,8 @@ export { verifyRelay, RELAY_HEADERS, RELAY_MAX_AGE_S } from "./relay.js";
11
11
  export { verifyStationCall, stationRefusal, stationJson, STATION_ID_RE, STATION_NAME_MAX, } from "./station.js";
12
12
  // §25 (0.13.0): a sale from another app (Beez AI), passed to the till by the Hub
13
13
  export { verifySaleCall, saleRefusal, SALE_ID_RE } from "./sale.js";
14
+ // §26 (0.14.0): the till's call into Customers' conversations, passed by the Hub
15
+ export { verifyChatCall, chatRefusal, CHAT_HEADER } from "./chat.js";
14
16
  export { ownVercelToken, ownEnvironment, hubKeys, APP_ID_HEADER, OIDC_HEADER } from "./identity.js";
15
17
  /**
16
18
  * Reads the guard's settings from a module's environment. Nothing secret: the Hub's address
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@t3b/hub-guard",
3
- "version": "0.13.0",
4
- "description": "The3Beez Hub guard: a module opens only by a Hub-signed ticket and refuses everything else. No key from the Hub: towards the Hub the app shows its own Vercel ID; from the Hub it accepts only what is signed by the key the Hub publishes. Entry route, refusal page, module session, idle rule, re-check with the Hub on every page and every minute, Cloudflare Access proof, broker and audit clients, manifest and health answers for the Hub only, the memo bell and window (0.10.0), the shared top bar's door and one Sign out for every app (0.12.0), the till's check of a sale another app passes through the Hub (0.13.0). Web Crypto only: runs in Node 20+ and in edge runtimes.",
3
+ "version": "0.14.0",
4
+ "description": "The3Beez Hub guard: a module opens only by a Hub-signed ticket and refuses everything else. No key from the Hub: towards the Hub the app shows its own Vercel ID; from the Hub it accepts only what is signed by the key the Hub publishes. Entry route, refusal page, module session, idle rule, re-check with the Hub on every page and every minute, Cloudflare Access proof, broker and audit clients, manifest and health answers for the Hub only, the memo bell and window (0.10.0), the shared top bar's door and one Sign out for every app (0.12.0), the till's check of a sale another app passes through the Hub (0.13.0), Customers' check of the till's call into the conversations (0.14.0). Web Crypto only: runs in Node 20+ and in edge runtimes.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
7
7
  "main": "./dist/index.js",