@t3b/hub-guard 0.12.0 → 0.13.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 +19 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/sale.d.ts +27 -0
- package/dist/sale.js +63 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -318,6 +318,23 @@ const r2 = await guard.memoAct(session, "reply", id, { text, lang: "ar" });
|
|
|
318
318
|
Hub's pass - person, app and action named, bound to the exact bytes - and `memosRefusal(reason)` answers a refusal;
|
|
319
319
|
name the door in the manifest as `memos` and in `healthResponse(..., { memos })`.
|
|
320
320
|
|
|
321
|
+
## A sale from another app: `verifySaleCall` (0.13.0, WORKFLOWS §25)
|
|
322
|
+
|
|
323
|
+
For the till (Beez POS) only. Another app (Beez AI) sells through the Hub; the Hub asks the till's `sales` door (named in
|
|
324
|
+
its manifest) with its own pass. Read the body as raw bytes first:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
const bytes = req.method === "POST" ? new Uint8Array(await req.arrayBuffer()) : null;
|
|
328
|
+
const c = await verifySaleCall(req, bytes, { moduleId: "beez-pos", keys: hubKeys(hubUrl), environment });
|
|
329
|
+
if (!c.ok) return saleRefusal(c.reason);
|
|
330
|
+
// c.from = the selling app ("beez-ai"), c.saleId = its sale id, c.jti = a one-use number
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
- Keep `c.jti` once; a second time is a replay.
|
|
334
|
+
- On a POST, the body's `id` must equal `c.saleId`; on a GET, the id in your path must. Never make two sales for one id:
|
|
335
|
+
the same id again answers with the first sale (the Hub tells the app to retry that way after a silence).
|
|
336
|
+
- A new sale's pass (bound to a body) used for a read, or another kind of Hub token, is `wrong_use`.
|
|
337
|
+
|
|
321
338
|
## What it enforces
|
|
322
339
|
|
|
323
340
|
- `/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.
|
|
@@ -329,8 +346,8 @@ const r2 = await guard.memoAct(session, "reply", id, { text, lang: "ar" });
|
|
|
329
346
|
## Changes
|
|
330
347
|
|
|
331
348
|
- **0.12.0** (WORKFLOWS §24, D-092; owner 2026-10-25: *"each one is built differently"*, then *"Yes to section 24 as
|
|
332
|
-
drawn: ship it inside hub-guard 0.12.0 together with HR's features (one release)"*) - **
|
|
333
|
-
|
|
349
|
+
drawn: ship it inside hub-guard 0.12.0 together with HR's features (one release)"*) - **published 2026-10-25 on his
|
|
350
|
+
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,
|
|
334
351
|
setCookie)`, `BAR_PATH`, `SIGNOUT_PATH`, the `BarData` type. A page the person opens (`isPageLoad`) and `/hub/bar`
|
|
335
352
|
ask the Hub at once (`PAGE_RECHECK_MS`, 5 seconds); anything else every minute (`RECHECK_MS` 60 s, was 15 minutes).
|
|
336
353
|
`signOut()` goes to the Hub's `/sign-out` (was the Hub's home - the Hub stayed signed in, B-058). `appMenu` lines
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export { sessionCheck, sessionCheckPages, spendTicket, memosCall, tokenBroker, a
|
|
|
9
9
|
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
|
+
export { verifySaleCall, saleRefusal, SALE_ID_RE, type SaleCheck } from "./sale.js";
|
|
12
13
|
export { ownVercelToken, ownEnvironment, hubKeys, APP_ID_HEADER, OIDC_HEADER, type TokenProvider } from "./identity.js";
|
|
13
14
|
/**
|
|
14
15
|
* Reads the guard's settings from a module's environment. Nothing secret: the Hub's address
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,8 @@ export { sessionCheck, sessionCheckPages, spendTicket, memosCall, tokenBroker, a
|
|
|
9
9
|
export { checkCloudflareAccess } from "./cf-access.js";
|
|
10
10
|
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
|
+
// §25 (0.13.0): a sale from another app (Beez AI), passed to the till by the Hub
|
|
13
|
+
export { verifySaleCall, saleRefusal, SALE_ID_RE } from "./sale.js";
|
|
12
14
|
export { ownVercelToken, ownEnvironment, hubKeys, APP_ID_HEADER, OIDC_HEADER } from "./identity.js";
|
|
13
15
|
/**
|
|
14
16
|
* Reads the guard's settings from a module's environment. Nothing secret: the Hub's address
|
package/dist/sale.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type JWTVerifyGetKey } from "jose";
|
|
2
|
+
/** The calling app's own sale id: the same for every retry of one sale. */
|
|
3
|
+
export declare const SALE_ID_RE: RegExp;
|
|
4
|
+
export type SaleCheck = {
|
|
5
|
+
ok: true;
|
|
6
|
+
from: string;
|
|
7
|
+
saleId: string;
|
|
8
|
+
jti: string;
|
|
9
|
+
} | {
|
|
10
|
+
ok: false;
|
|
11
|
+
reason: "missing" | "bad_signature" | "expired" | "wrong_module" | "wrong_use" | "wrong_environment" | "body_changed";
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Is this the Hub passing a sale from another app? `body` is the request's raw bytes for a POST (read it with
|
|
15
|
+
* `await req.arrayBuffer()` before parsing - the pass covers the exact bytes) and null for a GET. On a GET the till also
|
|
16
|
+
* checks that the sale id in its own path equals `saleId`; on a POST, that the body's `id` does. Keep `jti` once.
|
|
17
|
+
*/
|
|
18
|
+
export declare function verifySaleCall(req: Request, body: Uint8Array | string | null, opts: {
|
|
19
|
+
moduleId: string;
|
|
20
|
+
keys: JWTVerifyGetKey;
|
|
21
|
+
now?: Date;
|
|
22
|
+
environment?: string;
|
|
23
|
+
}): Promise<SaleCheck>;
|
|
24
|
+
/** The refusal the till's sales door answers with: 401 and only the reason word (the Hub passes it back by name). */
|
|
25
|
+
export declare function saleRefusal(reason: Extract<SaleCheck, {
|
|
26
|
+
ok: false;
|
|
27
|
+
}>["reason"]): Response;
|
package/dist/sale.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { jwtVerify } from "jose";
|
|
2
|
+
import { APP_ID_RE } from "./apps.js";
|
|
3
|
+
import { HUB_ISSUER, TYP } from "./identity.js";
|
|
4
|
+
import { b64urlEncode, constantTimeEqual, sha256 } from "./webcrypto.js";
|
|
5
|
+
// WORKFLOWS §25 (D-095): a sale from another app (Beez AI) reaches the till only through the Hub. The Hub has checked the
|
|
6
|
+
// calling app's Vercel ID, that the owner accepted its "sell through Beez POS" door and that his switch is on; then it asks
|
|
7
|
+
// the till's sales door with its own 60-second pass for "sale", naming the calling app, the sale id and a one-use number
|
|
8
|
+
// (`jti`). On a new sale (POST) the pass also carries the SHA-256 of the exact body, so it cannot be lifted onto another
|
|
9
|
+
// sale. The till keeps each one-use number once, and never makes two sales for one sale id.
|
|
10
|
+
/** The calling app's own sale id: the same for every retry of one sale. */
|
|
11
|
+
export const SALE_ID_RE = /^[A-Za-z0-9_-]{8,64}$/;
|
|
12
|
+
/**
|
|
13
|
+
* Is this the Hub passing a sale from another app? `body` is the request's raw bytes for a POST (read it with
|
|
14
|
+
* `await req.arrayBuffer()` before parsing - the pass covers the exact bytes) and null for a GET. On a GET the till also
|
|
15
|
+
* checks that the sale id in its own path equals `saleId`; on a POST, that the body's `id` does. Keep `jti` once.
|
|
16
|
+
*/
|
|
17
|
+
export async function verifySaleCall(req, body, opts) {
|
|
18
|
+
const auth = req.headers.get("authorization") ?? "";
|
|
19
|
+
const token = auth.startsWith("Bearer ") ? auth.slice(7).trim() : "";
|
|
20
|
+
if (!token || token.length > 4096)
|
|
21
|
+
return { ok: false, reason: "missing" };
|
|
22
|
+
let p;
|
|
23
|
+
try {
|
|
24
|
+
({ 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 }));
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
const code = e.code ?? "";
|
|
28
|
+
if (code === "ERR_JWT_EXPIRED")
|
|
29
|
+
return { ok: false, reason: "expired" };
|
|
30
|
+
if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED" && /"aud"/.test(e.message))
|
|
31
|
+
return { ok: false, reason: "wrong_module" };
|
|
32
|
+
// another kind of Hub token (a ticket, a relayed event) is named as such, not as a clock problem
|
|
33
|
+
if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED" && /"typ"/.test(e.message))
|
|
34
|
+
return { ok: false, reason: "wrong_use" };
|
|
35
|
+
if (code === "ERR_JWT_CLAIM_VALIDATION_FAILED")
|
|
36
|
+
return { ok: false, reason: "expired" };
|
|
37
|
+
return { ok: false, reason: "bad_signature" };
|
|
38
|
+
}
|
|
39
|
+
if (p.use !== "sale")
|
|
40
|
+
return { ok: false, reason: "wrong_use" };
|
|
41
|
+
if (opts.environment && p.env !== opts.environment)
|
|
42
|
+
return { ok: false, reason: "wrong_environment" };
|
|
43
|
+
const from = typeof p.from === "string" ? p.from : "";
|
|
44
|
+
const saleId = typeof p.sale_id === "string" ? p.sale_id : "";
|
|
45
|
+
const jti = typeof p.jti === "string" ? p.jti : "";
|
|
46
|
+
if (!APP_ID_RE.test(from) || !SALE_ID_RE.test(saleId) || !/^[A-Za-z0-9_-]{16,64}$/.test(jti))
|
|
47
|
+
return { ok: false, reason: "bad_signature" };
|
|
48
|
+
if (body !== null) {
|
|
49
|
+
const want = typeof p.body_sha256 === "string" ? p.body_sha256 : "";
|
|
50
|
+
const bytes = typeof body === "string" ? new TextEncoder().encode(body) : body;
|
|
51
|
+
if (!want || !constantTimeEqual(b64urlEncode(await sha256(bytes)), want))
|
|
52
|
+
return { ok: false, reason: "body_changed" };
|
|
53
|
+
}
|
|
54
|
+
else if (p.body_sha256 !== undefined) {
|
|
55
|
+
// a new sale's pass (bound to a body) is never good for a read
|
|
56
|
+
return { ok: false, reason: "wrong_use" };
|
|
57
|
+
}
|
|
58
|
+
return { ok: true, from, saleId, jti };
|
|
59
|
+
}
|
|
60
|
+
/** The refusal the till's sales door answers with: 401 and only the reason word (the Hub passes it back by name). */
|
|
61
|
+
export function saleRefusal(reason) {
|
|
62
|
+
return new Response(JSON.stringify({ error: "hub_only", reason }), { status: 401, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } });
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t3b/hub-guard",
|
|
3
|
-
"version": "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). Web Crypto only: runs in Node 20+ and in edge runtimes.",
|
|
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.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|