@openparachute/hub 0.5.7 → 0.5.9-rc.6

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.
Files changed (60) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/admin-clients.test.ts +275 -0
  3. package/src/__tests__/admin-handlers.test.ts +70 -323
  4. package/src/__tests__/admin-host-admin-token.test.ts +52 -4
  5. package/src/__tests__/api-me.test.ts +149 -0
  6. package/src/__tests__/api-mint-token.test.ts +381 -0
  7. package/src/__tests__/api-revocation-list.test.ts +198 -0
  8. package/src/__tests__/api-revoke-token.test.ts +320 -0
  9. package/src/__tests__/api-tokens.test.ts +629 -0
  10. package/src/__tests__/auth.test.ts +680 -16
  11. package/src/__tests__/expose-2fa-warning.test.ts +3 -5
  12. package/src/__tests__/expose-cloudflare.test.ts +1 -1
  13. package/src/__tests__/expose.test.ts +2 -2
  14. package/src/__tests__/hub-server.test.ts +338 -65
  15. package/src/__tests__/hub.test.ts +108 -55
  16. package/src/__tests__/install-source.test.ts +249 -0
  17. package/src/__tests__/jwt-sign.test.ts +205 -0
  18. package/src/__tests__/module-manifest.test.ts +48 -0
  19. package/src/__tests__/oauth-handlers.test.ts +266 -5
  20. package/src/__tests__/operator-token.test.ts +379 -3
  21. package/src/__tests__/origin-check.test.ts +220 -0
  22. package/src/__tests__/status.test.ts +199 -0
  23. package/src/__tests__/well-known.test.ts +69 -0
  24. package/src/admin-clients.ts +139 -0
  25. package/src/admin-handlers.ts +32 -254
  26. package/src/admin-host-admin-token.ts +25 -10
  27. package/src/admin-login-ui.ts +256 -0
  28. package/src/admin-vault-admin-token.ts +1 -1
  29. package/src/api-me.ts +124 -0
  30. package/src/api-mint-token.ts +239 -0
  31. package/src/api-revocation-list.ts +59 -0
  32. package/src/api-revoke-token.ts +153 -0
  33. package/src/api-tokens.ts +224 -0
  34. package/src/commands/auth.ts +408 -51
  35. package/src/commands/expose-2fa-warning.ts +6 -6
  36. package/src/commands/status.ts +74 -10
  37. package/src/csrf.ts +6 -3
  38. package/src/help.ts +10 -4
  39. package/src/hub-db.ts +63 -0
  40. package/src/hub-server.ts +426 -97
  41. package/src/hub.ts +272 -149
  42. package/src/install-source.ts +291 -0
  43. package/src/jwt-sign.ts +265 -5
  44. package/src/module-manifest.ts +48 -10
  45. package/src/oauth-handlers.ts +183 -54
  46. package/src/oauth-ui.ts +23 -2
  47. package/src/operator-token.ts +272 -18
  48. package/src/origin-check.ts +127 -0
  49. package/src/rate-limit.ts +5 -2
  50. package/src/scope-explanations.ts +33 -2
  51. package/src/sessions.ts +1 -1
  52. package/src/well-known.ts +54 -1
  53. package/web/ui/dist/assets/index-Bv6Bq_wx.js +60 -0
  54. package/web/ui/dist/assets/index-D54otIhv.css +1 -0
  55. package/web/ui/dist/index.html +2 -2
  56. package/src/__tests__/admin-config.test.ts +0 -281
  57. package/src/admin-config-ui.ts +0 -534
  58. package/src/admin-config.ts +0 -226
  59. package/web/ui/dist/assets/index-BKzPDdB0.js +0 -60
  60. package/web/ui/dist/assets/index-Dyk6g7vT.css +0 -1
@@ -13,10 +13,9 @@ describe("renderHub", () => {
13
13
  expect(html).toContain("<script>");
14
14
  });
15
15
 
16
- test("fetches /.well-known/parachute.json and reads services[] + vaults[]", () => {
16
+ test("fetches /.well-known/parachute.json for the Use section", () => {
17
17
  expect(html).toContain("/.well-known/parachute.json");
18
18
  expect(html).toContain("doc.services");
19
- expect(html).toContain("doc.vaults");
20
19
  });
21
20
 
22
21
  test("uses parachute.computer sage palette and serif/sans fonts", () => {
@@ -30,79 +29,133 @@ describe("renderHub", () => {
30
29
  expect(html).toContain("prefers-color-scheme: dark");
31
30
  });
32
31
 
33
- test("falls back to a generic icon for module tiles", () => {
34
- expect(html).toContain("fallbackIcon");
32
+ test("renders two sections: Services and Admin, each with its own heading + grid", () => {
33
+ expect(html).toContain('id="services-section"');
34
+ expect(html).toContain('id="admin-section"');
35
+ expect(html).toContain('id="services-grid"');
36
+ expect(html).toContain('id="admin-grid"');
37
+ expect(html).toContain("<h2>Services</h2>");
38
+ expect(html).toContain("<h2>Admin</h2>");
35
39
  });
36
40
 
37
- test("renders one tile per module type, not per service instance", () => {
38
- expect(html).toContain("aggregate(services, vaults)");
39
- expect(html).toContain("renderTile");
40
- expect(html).toContain("MODULE_ORDER");
41
+ test("Services section sub-text frames the section as service-owned surfaces", () => {
42
+ // Services-vs-Admin axis is OWNERSHIP (services own their UIs vs
43
+ // hub owns host admin), not function. The sub-text reflects that —
44
+ // earlier "Browse, transcribe, and run" framing put it on the
45
+ // function axis, which broke down once you noticed services have
46
+ // UIs that mix use / config / admin.
47
+ expect(html).toContain("Surfaces provided by services");
41
48
  });
42
49
 
43
- test("known module display order is vault scribe notes → agent", () => {
44
- expect(html).toContain("['vault', 'scribe', 'notes', 'agent']");
50
+ test("Services tiles are data-driven from each service's uiUrl + displayName", () => {
51
+ // Phase D consumer-side: SERVICE_LABELS / SERVICE_ORDER hardcoding
52
+ // retired. Each well-known services[] row carries displayName + uiUrl
53
+ // (sourced from module.json via hub-server's loadServiceUiMetadata);
54
+ // tiles render directly from those.
55
+ expect(html).toContain("svc.uiUrl");
56
+ expect(html).toContain("svc.displayName");
57
+ // No more hardcoded short→label map.
58
+ expect(html).not.toContain("'Notes', desc:");
59
+ expect(html).not.toContain("['notes', 'scribe', 'agent']");
45
60
  });
46
61
 
47
- test("vault tile counts vaults[] (per instance) and links to /vault", () => {
48
- // Vault count is the length of doc.vaultsone entry per /vault/<name>
49
- // mount, so a single ServiceEntry with paths=[a,b,c] still shows "3
50
- // registered". The manage link is the hub's vault SPA at /vault
51
- // (renamed from /hub/vaults in the realignment), never an individual
52
- // vault backend.
53
- expect(html).toContain("vaults.length");
54
- expect(html).toContain("'/vault'");
62
+ test("Services skip rule emerges from data, not name-checks (vault has no uiUrl)", () => {
63
+ // The previous `isVaultName` hardcoded skip is gonevault doesn't
64
+ // declare uiUrl, so it naturally doesn't render. Other API-only
65
+ // modules (current or future) get the same treatment for free.
66
+ expect(html).toContain("if (!svc || !svc.uiUrl) continue;");
67
+ // The function definition is gone (the comment may still mention the
68
+ // name as historical context — we only care about the active code).
69
+ expect(html).not.toContain("function isVaultName");
55
70
  });
56
71
 
57
- test("non-vault tiles take their manageUrl from the service's path", () => {
58
- // shortName('parachute-scribe') = 'scribe' tile links to svc.path,
59
- // which is whatever the module declared (e.g. /scribe, /notes, /agent).
60
- // Hardcoding the link would silently break on a custom mount.
61
- expect(html).toContain("manageUrl: svc.path");
72
+ test("Services tiles sort alphabetically by displayName", () => {
73
+ // Per the module-json-extensibility pattern doc default ordering
74
+ // until a `displayOrder` field surfaces. localeCompare keeps the
75
+ // ordering stable across locales for ASCII labels.
76
+ expect(html).toContain("a.title.localeCompare(b.title)");
62
77
  });
63
78
 
64
- test("tiles for module types with zero instances are hidden", () => {
65
- // Aggregate only inserts a group when the type has at least one entry;
66
- // tilesInOrder iterates the map. No "0 registered" surface.
67
- expect(html).not.toContain("0 registered");
68
- expect(html).toContain("count === 1 ? '1 registered'");
79
+ test("Admin section is hardcoded (always visible) with three entries", () => {
80
+ expect(html).toContain("ADMIN_ENTRIES");
81
+ expect(html).toContain("/admin/vaults");
82
+ expect(html).toContain("/admin/permissions");
83
+ expect(html).toContain("/admin/tokens");
69
84
  });
70
85
 
71
- test("module labels are humanized (Vault / Scribe / Notes / Agent)", () => {
72
- expect(html).toContain("vault: 'Vault'");
73
- expect(html).toContain("scribe: 'Scribe'");
74
- expect(html).toContain("notes: 'Notes'");
75
- expect(html).toContain("agent: 'Agent'");
86
+ test("Admin section renders synchronously (does not depend on the well-known fetch)", () => {
87
+ // Even if the fetch is slow or fails, the operator should see Admin
88
+ // surfaces — they may be the reason the operator landed on /.
89
+ expect(html).toContain("renderAdmin();");
90
+ expect(html).toContain("Admin section is static");
76
91
  });
77
92
 
78
- test("vault-name detection covers parachute-vault and parachute-vault-<name>", () => {
79
- // Phase-1 multi-vault keeps a single ServiceEntry with multiple paths
80
- // (parachute-vault), but the door is open for per-instance entries
81
- // (parachute-vault-techne). isVaultName has to accept both.
82
- expect(html).toContain("isVaultName");
83
- expect(html).toContain("'parachute-vault'");
84
- expect(html).toContain("'parachute-vault-'");
93
+ test("Services section empty state guides operators to declare uiUrl", () => {
94
+ // Empty state under Phase D means "no installed service has declared
95
+ // uiUrl yet" different shape from pre-D ("none installed at all").
96
+ // Hint points at the pattern doc since the fix is in module.json,
97
+ // not at install time.
98
+ expect(html).toContain("No services with a UI declared yet");
99
+ expect(html).toContain("module-json-extensibility");
85
100
  });
86
101
 
87
- test("empty state when no modules are registered", () => {
88
- expect(html).toContain("No modules installed yet");
89
- expect(html).toContain("parachute install vault");
102
+ test("Use section error state surfaces the underlying message", () => {
103
+ expect(html).toContain("Could not load services");
90
104
  });
91
105
 
92
- test("error state surfaces the underlying message", () => {
93
- expect(html).toContain("Could not load modules");
94
- });
95
-
96
- test("does not retain the per-service interactive-card / config-form code", () => {
97
- // The home page is now a directory of modules — per-instance config
98
- // forms and detail panels live behind the Manage links (vault SPA at
99
- // /vault, the running module's own UI elsewhere). Keeping the
100
- // dead code around is a maintenance trap.
106
+ test("does not retain the old aggregate-by-module-type code", () => {
107
+ // The Vault collapse + per-module aggregation pattern is gone — Use
108
+ // entries are direct service-path → label lookups; Admin is hardcoded.
109
+ expect(html).not.toContain("aggregate(services, vaults)");
110
+ expect(html).not.toContain("MODULE_LABELS");
101
111
  expect(html).not.toContain("renderConfigField");
102
- expect(html).not.toContain("fetchConfig");
103
112
  expect(html).not.toContain("kind-badge");
104
- expect(html).not.toContain("info.mcpUrl");
105
- expect(html).not.toContain("info.openInNotesUrl");
113
+ });
114
+
115
+ test("default render (no session) emits the 'Sign in' affordance", () => {
116
+ expect(html).toContain('class="auth-indicator"');
117
+ expect(html).toContain("Sign in");
118
+ expect(html).toContain('href="/login?next=/"');
119
+ // No POST form, no CSRF input — those only appear when signed in.
120
+ expect(html).not.toContain('action="/logout"');
121
+ expect(html).not.toContain("__csrf");
122
+ });
123
+ });
124
+
125
+ describe("renderHub — signed-in indicator (rc.13)", () => {
126
+ test("session user → 'Signed in as <name>' + inline POST form with CSRF", () => {
127
+ const html = renderHub({
128
+ session: { displayName: "aaron", csrfToken: "csrf-token-xyz" },
129
+ });
130
+ expect(html).toContain('class="auth-indicator"');
131
+ expect(html).toContain("Signed in as");
132
+ expect(html).toContain("aaron");
133
+ // Inline POST form for sign-out — CSRF token embedded as the
134
+ // existing `__csrf` field name (matches /logout's expectations).
135
+ expect(html).toContain('method="POST" action="/logout"');
136
+ expect(html).toContain('name="__csrf"');
137
+ expect(html).toContain('value="csrf-token-xyz"');
138
+ expect(html).toContain("Sign out");
139
+ // No "Sign in" affordance when signed in.
140
+ expect(html).not.toContain('href="/login?next=/"');
141
+ });
142
+
143
+ test("displayName with HTML special chars is escaped", () => {
144
+ // Username field allows alphanumerics historically, but the
145
+ // displayName field on the wire is forward-compatible with profile
146
+ // names that may contain &, <, >. Escape at render time.
147
+ const html = renderHub({
148
+ session: { displayName: "<aaron>&friends", csrfToken: "tok" },
149
+ });
150
+ expect(html).toContain("&lt;aaron&gt;&amp;friends");
151
+ expect(html).not.toContain("<aaron>&friends");
152
+ });
153
+
154
+ test("CSRF token with HTML special chars is escaped in the value attribute", () => {
155
+ const html = renderHub({
156
+ session: { displayName: "aaron", csrfToken: 'token"with"quotes' },
157
+ });
158
+ expect(html).toContain('value="token&quot;with&quot;quotes"');
106
159
  });
107
160
  });
108
161
 
@@ -0,0 +1,249 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ type DetectInstallSourceDeps,
4
+ detectHubInstallSource,
5
+ detectInstallSource,
6
+ formatInstallSourceLabel,
7
+ isStale,
8
+ } from "../install-source.ts";
9
+
10
+ /**
11
+ * Stub helpers for the detect path. Production reads the operator's bun
12
+ * globals + real package.jsons; here we wire everything from a virtual
13
+ * filesystem so each kind (npm / bun-linked / unknown / stale) has a
14
+ * deterministic shape.
15
+ */
16
+ function makeDeps(opts: {
17
+ prefixes?: readonly string[];
18
+ packageVersions?: Record<string, string>;
19
+ bunGlobalLinks?: Record<string, string>;
20
+ gitHeads?: Record<string, string>;
21
+ }): DetectInstallSourceDeps {
22
+ const prefixes = opts.prefixes ?? ["/home/test/.bun/install/global/node_modules"];
23
+ return {
24
+ bunGlobalPrefixes: () => prefixes,
25
+ resolveBunGlobal: (pkg) => opts.bunGlobalLinks?.[pkg] ?? null,
26
+ readJson: (path) => {
27
+ // Path looks like `<pkgDir>/package.json` — strip suffix.
28
+ const pkgDirRaw = path.replace(/\/package\.json$/, "");
29
+ const v = opts.packageVersions?.[pkgDirRaw];
30
+ if (v === undefined) throw new Error(`no package.json at ${pkgDirRaw}`);
31
+ return { name: "@stub/pkg", version: v };
32
+ },
33
+ readGitHead: (path) => opts.gitHeads?.[path],
34
+ };
35
+ }
36
+
37
+ describe("detectInstallSource", () => {
38
+ test("classifies a bun-linked checkout (installDir outside bun globals)", () => {
39
+ const deps = makeDeps({
40
+ packageVersions: { "/Users/me/code/parachute-notes": "0.3.15-rc.1" },
41
+ gitHeads: { "/Users/me/code/parachute-notes": "051c404" },
42
+ });
43
+ const source = detectInstallSource(
44
+ { entryName: "parachute-notes", installDir: "/Users/me/code/parachute-notes" },
45
+ deps,
46
+ );
47
+ expect(source.kind).toBe("bun-linked");
48
+ expect(source.path).toBe("/Users/me/code/parachute-notes");
49
+ expect(source.gitHead).toBe("051c404");
50
+ expect(source.livePackageVersion).toBe("0.3.15-rc.1");
51
+ });
52
+
53
+ test("classifies an npm install (installDir under bun globals)", () => {
54
+ const deps = makeDeps({
55
+ prefixes: ["/home/test/.bun/install/global/node_modules"],
56
+ packageVersions: {
57
+ "/home/test/.bun/install/global/node_modules/@openparachute/scribe": "0.4.2-rc.1",
58
+ },
59
+ });
60
+ const source = detectInstallSource(
61
+ {
62
+ entryName: "parachute-scribe",
63
+ installDir: "/home/test/.bun/install/global/node_modules/@openparachute/scribe",
64
+ },
65
+ deps,
66
+ );
67
+ expect(source.kind).toBe("npm");
68
+ expect(source.livePackageVersion).toBe("0.4.2-rc.1");
69
+ expect(source.gitHead).toBeUndefined();
70
+ });
71
+
72
+ test("falls back to bun-global symlink lookup when installDir is absent", () => {
73
+ const deps = makeDeps({
74
+ bunGlobalLinks: { "@openparachute/vault": "/Users/me/code/parachute-vault" },
75
+ packageVersions: { "/Users/me/code/parachute-vault": "0.4.4-rc.3" },
76
+ gitHeads: { "/Users/me/code/parachute-vault": "8aa167b" },
77
+ });
78
+ const source = detectInstallSource({ entryName: "parachute-vault" }, deps);
79
+ expect(source.kind).toBe("bun-linked");
80
+ expect(source.path).toBe("/Users/me/code/parachute-vault");
81
+ expect(source.gitHead).toBe("8aa167b");
82
+ });
83
+
84
+ test("returns unknown when nothing resolves (no installDir, no first-party mapping)", () => {
85
+ const deps = makeDeps({});
86
+ const source = detectInstallSource({ entryName: "agent" }, deps);
87
+ expect(source.kind).toBe("unknown");
88
+ expect(source.path).toBeUndefined();
89
+ expect(source.gitHead).toBeUndefined();
90
+ });
91
+
92
+ test("omits gitHead when the bun-linked path isn't a git repo", () => {
93
+ const deps = makeDeps({
94
+ packageVersions: { "/tmp/no-git/pkg": "1.0.0" },
95
+ // gitHeads intentionally missing → readGitHead returns undefined.
96
+ });
97
+ const source = detectInstallSource(
98
+ { entryName: "third-party", installDir: "/tmp/no-git/pkg" },
99
+ deps,
100
+ );
101
+ expect(source.kind).toBe("bun-linked");
102
+ expect(source.gitHead).toBeUndefined();
103
+ expect(source.livePackageVersion).toBe("1.0.0");
104
+ });
105
+
106
+ test("omits livePackageVersion when package.json is unreadable", () => {
107
+ const deps = makeDeps({
108
+ packageVersions: {}, // every read throws
109
+ });
110
+ const source = detectInstallSource(
111
+ { entryName: "third-party", installDir: "/tmp/no-pkg" },
112
+ deps,
113
+ );
114
+ expect(source.kind).toBe("bun-linked");
115
+ expect(source.livePackageVersion).toBeUndefined();
116
+ });
117
+
118
+ test("trailing-slash prefix doesn't false-match a sibling directory", () => {
119
+ // Subtle: `/home/test/.bun/install/global/node_modules-other` shouldn't
120
+ // be classified as "under" `/home/test/.bun/install/global/node_modules`.
121
+ // The prefix join in `isUnderBunGlobals` adds a trailing slash precisely
122
+ // to avoid this — pin the behavior.
123
+ const deps = makeDeps({
124
+ prefixes: ["/home/test/.bun/install/global/node_modules"],
125
+ packageVersions: {
126
+ "/home/test/.bun/install/global/node_modules-other/pkg": "1.0.0",
127
+ },
128
+ });
129
+ const source = detectInstallSource(
130
+ {
131
+ entryName: "third-party",
132
+ installDir: "/home/test/.bun/install/global/node_modules-other/pkg",
133
+ },
134
+ deps,
135
+ );
136
+ expect(source.kind).toBe("bun-linked");
137
+ });
138
+ });
139
+
140
+ describe("isStale", () => {
141
+ test("flags drift between cached entry version and live package.json", () => {
142
+ expect(
143
+ isStale("0.3.11-rc.1", {
144
+ kind: "bun-linked",
145
+ path: "/Users/me/code/parachute-notes",
146
+ livePackageVersion: "0.3.15-rc.1",
147
+ }),
148
+ ).toBe(true);
149
+ });
150
+
151
+ test("does not flag a matching version", () => {
152
+ expect(
153
+ isStale("0.3.15-rc.1", {
154
+ kind: "bun-linked",
155
+ path: "/Users/me/code/parachute-notes",
156
+ livePackageVersion: "0.3.15-rc.1",
157
+ }),
158
+ ).toBe(false);
159
+ });
160
+
161
+ test("does not flag npm-installed services (cached version IS the source)", () => {
162
+ expect(
163
+ isStale("0.4.2-rc.1", {
164
+ kind: "npm",
165
+ path: "/path/to/global",
166
+ livePackageVersion: "0.4.2-rc.1",
167
+ }),
168
+ ).toBe(false);
169
+ });
170
+
171
+ test("does not flag when live version is unavailable", () => {
172
+ expect(
173
+ isStale("0.3.11-rc.1", {
174
+ kind: "bun-linked",
175
+ path: "/Users/me/code/parachute-notes",
176
+ // livePackageVersion absent — can't compute drift, don't false-flag.
177
+ }),
178
+ ).toBe(false);
179
+ });
180
+
181
+ test("does not flag unknown sources", () => {
182
+ expect(isStale("1.0.0", { kind: "unknown" })).toBe(false);
183
+ });
184
+ });
185
+
186
+ describe("formatInstallSourceLabel", () => {
187
+ test("bun-linked → basename + short SHA", () => {
188
+ expect(
189
+ formatInstallSourceLabel({
190
+ kind: "bun-linked",
191
+ path: "/Users/me/code/parachute-notes",
192
+ gitHead: "051c404",
193
+ }),
194
+ ).toBe("bun-linked → parachute-notes @ 051c404");
195
+ });
196
+
197
+ test("bun-linked without gitHead drops the @ <sha> suffix", () => {
198
+ expect(
199
+ formatInstallSourceLabel({
200
+ kind: "bun-linked",
201
+ path: "/Users/me/code/parachute-notes",
202
+ }),
203
+ ).toBe("bun-linked → parachute-notes");
204
+ });
205
+
206
+ test("npm with version", () => {
207
+ expect(
208
+ formatInstallSourceLabel({
209
+ kind: "npm",
210
+ path: "/some/global/dir",
211
+ livePackageVersion: "0.4.2-rc.1",
212
+ }),
213
+ ).toBe("npm (0.4.2-rc.1)");
214
+ });
215
+
216
+ test("npm without version", () => {
217
+ expect(formatInstallSourceLabel({ kind: "npm" })).toBe("npm");
218
+ });
219
+
220
+ test("unknown sources render as 'unknown'", () => {
221
+ expect(formatInstallSourceLabel({ kind: "unknown" })).toBe("unknown");
222
+ });
223
+ });
224
+
225
+ describe("detectHubInstallSource", () => {
226
+ test("classifies the hub based on its source location", () => {
227
+ // Exercise the happy path via the real hub's `src/` dir. The result
228
+ // depends on the test environment (CI vs. bun-linked checkout), so we
229
+ // only assert the kind is one of the known classifications — not the
230
+ // exact value. `readGitHead` is stubbed so the test never forks a real
231
+ // git process; the contract under test is "climb to package.json,
232
+ // classify by location against bun globals" — git is incidental.
233
+ const source = detectHubInstallSource(import.meta.dir, {
234
+ readGitHead: () => "deadbeef",
235
+ });
236
+ expect(["bun-linked", "npm", "unknown"]).toContain(source.kind);
237
+ });
238
+
239
+ test("returns unknown when no package.json exists above srcDir", () => {
240
+ // `/private` exists on macOS but has no package.json up the chain;
241
+ // injected readJson always throws so the walk hits the climb-cap.
242
+ const source = detectHubInstallSource("/private/var/empty", {
243
+ readJson: () => {
244
+ throw new Error("no package.json");
245
+ },
246
+ });
247
+ expect(source.kind).toBe("unknown");
248
+ });
249
+ });
@@ -9,8 +9,13 @@ import {
9
9
  REFRESH_TOKEN_TTL_MS,
10
10
  RefreshTokenInsertError,
11
11
  findRefreshToken,
12
+ findTokenRowByJti,
13
+ listActiveRevocations,
14
+ recordTokenMint,
15
+ revokeTokenByJti,
12
16
  signAccessToken,
13
17
  signRefreshToken,
18
+ tokenRowIdentity,
14
19
  validateAccessToken,
15
20
  } from "../jwt-sign.ts";
16
21
  import { getActiveSigningKey, rotateSigningKey } from "../signing-keys.ts";
@@ -359,3 +364,203 @@ describe("validateAccessToken", () => {
359
364
  }
360
365
  });
361
366
  });
367
+
368
+ // closes #212 Phase 1 — unified token registry helpers (recordTokenMint,
369
+ // revokeTokenByJti, listActiveRevocations) and the v6 schema shape.
370
+ describe("token registry (hub#212 Phase 1)", () => {
371
+ test("v6 schema: tokens has user_id NULLABLE + permissions/created_via/subject", () => {
372
+ const { db, cleanup } = makeDb();
373
+ try {
374
+ // SQLite PRAGMA table_info reports column nullability + defaults; the
375
+ // bun:sqlite driver maps the row shape onto our type. The columns are
376
+ // (cid, name, type, notnull, dflt_value, pk) per SQLite docs.
377
+ type ColInfo = {
378
+ cid: number;
379
+ name: string;
380
+ type: string;
381
+ notnull: number;
382
+ dflt_value: string | null;
383
+ pk: number;
384
+ };
385
+ const cols = db.query<ColInfo, []>("PRAGMA table_info(tokens)").all();
386
+ const byName = new Map(cols.map((c) => [c.name, c]));
387
+ // Pre-v6: user_id NOT NULL. Post-v6: user_id NULLABLE.
388
+ expect(byName.get("user_id")?.notnull).toBe(0);
389
+ // New columns.
390
+ expect(byName.has("permissions")).toBe(true);
391
+ expect(byName.has("created_via")).toBe(true);
392
+ expect(byName.has("subject")).toBe(true);
393
+ // created_via has the back-compat default for pre-v6 rows.
394
+ expect(byName.get("created_via")?.dflt_value).toMatch(/oauth_refresh/);
395
+ } finally {
396
+ cleanup();
397
+ }
398
+ });
399
+
400
+ test("recordTokenMint inserts a registry row matching the inputs", () => {
401
+ const { db, cleanup } = makeDb();
402
+ try {
403
+ const expiresAt = new Date(Date.now() + 86400_000).toISOString();
404
+ recordTokenMint(db, {
405
+ jti: "jti-cli-1",
406
+ createdVia: "cli_mint",
407
+ subject: "operator",
408
+ clientId: "parachute-hub",
409
+ scopes: ["vault:read", "scribe:transcribe"],
410
+ expiresAt,
411
+ permissions: '{"vault":{"default":{"read_tags":["public"]}}}',
412
+ });
413
+ const row = findTokenRowByJti(db, "jti-cli-1");
414
+ expect(row).not.toBeNull();
415
+ expect(row?.userId).toBeNull();
416
+ expect(row?.subject).toBe("operator");
417
+ expect(row?.createdVia).toBe("cli_mint");
418
+ expect(row?.scopes).toEqual(["vault:read", "scribe:transcribe"]);
419
+ expect(row?.expiresAt).toBe(expiresAt);
420
+ expect(row?.permissions).toBe('{"vault":{"default":{"read_tags":["public"]}}}');
421
+ expect(row?.revokedAt).toBeNull();
422
+ } finally {
423
+ cleanup();
424
+ }
425
+ });
426
+
427
+ test("recordTokenMint with a duplicate jti throws RefreshTokenInsertError", () => {
428
+ const { db, cleanup } = makeDb();
429
+ try {
430
+ const expiresAt = new Date(Date.now() + 86400_000).toISOString();
431
+ recordTokenMint(db, {
432
+ jti: "jti-dup",
433
+ createdVia: "operator_mint",
434
+ subject: "operator",
435
+ clientId: "parachute-hub",
436
+ scopes: ["hub:admin"],
437
+ expiresAt,
438
+ });
439
+ expect(() =>
440
+ recordTokenMint(db, {
441
+ jti: "jti-dup",
442
+ createdVia: "cli_mint",
443
+ subject: "operator",
444
+ clientId: "parachute-hub",
445
+ scopes: ["vault:read"],
446
+ expiresAt,
447
+ }),
448
+ ).toThrow(RefreshTokenInsertError);
449
+ } finally {
450
+ cleanup();
451
+ }
452
+ });
453
+
454
+ test("revokeTokenByJti flips revoked_at; second call returns false (idempotent)", () => {
455
+ const { db, cleanup } = makeDb();
456
+ try {
457
+ const expiresAt = new Date(Date.now() + 86400_000).toISOString();
458
+ recordTokenMint(db, {
459
+ jti: "jti-rev",
460
+ createdVia: "cli_mint",
461
+ subject: "operator",
462
+ clientId: "parachute-hub",
463
+ scopes: ["vault:read"],
464
+ expiresAt,
465
+ });
466
+ const now = new Date();
467
+ expect(revokeTokenByJti(db, "jti-rev", now)).toBe(true);
468
+ expect(revokeTokenByJti(db, "jti-rev", now)).toBe(false);
469
+ const row = findTokenRowByJti(db, "jti-rev");
470
+ expect(row?.revokedAt).toBe(now.toISOString());
471
+ } finally {
472
+ cleanup();
473
+ }
474
+ });
475
+
476
+ test("listActiveRevocations filters by revoked_at AND expires_at>now", () => {
477
+ const { db, cleanup } = makeDb();
478
+ try {
479
+ const past = new Date(Date.now() - 86400_000).toISOString();
480
+ const future = new Date(Date.now() + 86400_000).toISOString();
481
+ // Two revoked rows: one expired, one active.
482
+ recordTokenMint(db, {
483
+ jti: "jti-revoked-expired",
484
+ createdVia: "cli_mint",
485
+ subject: "operator",
486
+ clientId: "parachute-hub",
487
+ scopes: ["vault:read"],
488
+ expiresAt: past,
489
+ });
490
+ recordTokenMint(db, {
491
+ jti: "jti-revoked-active",
492
+ createdVia: "cli_mint",
493
+ subject: "operator",
494
+ clientId: "parachute-hub",
495
+ scopes: ["vault:read"],
496
+ expiresAt: future,
497
+ });
498
+ // One non-revoked active row (control — must NOT appear).
499
+ recordTokenMint(db, {
500
+ jti: "jti-not-revoked",
501
+ createdVia: "cli_mint",
502
+ subject: "operator",
503
+ clientId: "parachute-hub",
504
+ scopes: ["vault:read"],
505
+ expiresAt: future,
506
+ });
507
+ const now = new Date();
508
+ revokeTokenByJti(db, "jti-revoked-expired", now);
509
+ revokeTokenByJti(db, "jti-revoked-active", now);
510
+ const list = listActiveRevocations(db, now);
511
+ expect(list).toEqual(["jti-revoked-active"]);
512
+ } finally {
513
+ cleanup();
514
+ }
515
+ });
516
+
517
+ test("tokenRowIdentity returns userId when present, else subject", async () => {
518
+ const { db, cleanup } = makeDb();
519
+ try {
520
+ rotateSigningKey(db);
521
+ const u = await createUser(db, "owner", "pw");
522
+ // OAuth refresh row: userId set, subject NULL.
523
+ const refresh = signRefreshToken(db, {
524
+ jti: "jti-oauth",
525
+ userId: u.id,
526
+ clientId: "parachute-hub",
527
+ scopes: ["vault:read"],
528
+ });
529
+ expect(refresh.familyId).toBeDefined();
530
+ const oauthRow = findTokenRowByJti(db, "jti-oauth")!;
531
+ expect(tokenRowIdentity(oauthRow)).toBe(u.id);
532
+
533
+ // CLI mint row: userId NULL, subject set.
534
+ recordTokenMint(db, {
535
+ jti: "jti-cli",
536
+ createdVia: "cli_mint",
537
+ subject: "operator",
538
+ clientId: "parachute-hub",
539
+ scopes: ["vault:read"],
540
+ expiresAt: new Date(Date.now() + 86400_000).toISOString(),
541
+ });
542
+ const cliRow = findTokenRowByJti(db, "jti-cli")!;
543
+ expect(tokenRowIdentity(cliRow)).toBe("operator");
544
+ } finally {
545
+ cleanup();
546
+ }
547
+ });
548
+
549
+ test("signRefreshToken explicitly stamps created_via='oauth_refresh'", async () => {
550
+ const { db, cleanup } = makeDb();
551
+ try {
552
+ rotateSigningKey(db);
553
+ const u = await createUser(db, "owner", "pw");
554
+ signRefreshToken(db, {
555
+ jti: "jti-oauth-stamped",
556
+ userId: u.id,
557
+ clientId: "parachute-hub",
558
+ scopes: ["vault:read"],
559
+ });
560
+ const row = findTokenRowByJti(db, "jti-oauth-stamped");
561
+ expect(row?.createdVia).toBe("oauth_refresh");
562
+ } finally {
563
+ cleanup();
564
+ }
565
+ });
566
+ });