create-ailk 0.38.0 → 0.39.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.
Files changed (135) hide show
  1. package/dist/add-route.js +1 -1
  2. package/dist/derive-architecture.js +3 -1
  3. package/dist/remove-module.js +18 -14
  4. package/dist/routes.d.ts +29 -2
  5. package/dist/routes.js +269 -23
  6. package/dist/site-manifest.js +6 -4
  7. package/dist/site-roster.d.ts +11 -3
  8. package/dist/site-roster.js +42 -22
  9. package/dist/surfaces.d.ts +11 -3
  10. package/dist/surfaces.js +69 -22
  11. package/dist/sync-routes.js +22 -5
  12. package/dist/template-record.js +3 -1
  13. package/dist/topology.d.ts +11 -0
  14. package/dist/topology.js +253 -3
  15. package/package.json +3 -3
  16. package/templates/apps/web/CLAUDE.md +26 -4
  17. package/templates/apps/web/__tests__/__mocks__/next-intl-server.ts +5 -1
  18. package/templates/apps/web/__tests__/__mocks__/next-intl.ts +5 -1
  19. package/templates/apps/web/__tests__/auth/seam.test.ts +34 -0
  20. package/templates/apps/web/account.config.tsx +2 -3
  21. package/templates/apps/web/app/[locale]/(authed)/account/connect/__tests__/page.test.tsx +1 -1
  22. package/templates/apps/web/app/[locale]/(authed)/account/connect/page.tsx +1 -1
  23. package/templates/apps/web/app/[locale]/(authed)/signed-out-gate.generated.tsx +1 -1
  24. package/templates/apps/web/app/[locale]/layout.tsx +1 -1
  25. package/templates/apps/web/auth.config.tsx +107 -0
  26. package/templates/apps/web/auth.ts +2 -1
  27. package/templates/apps/web/i18n/request.ts +9 -2
  28. package/templates/apps/web/jest.config.cjs +4 -0
  29. package/templates/apps/web/lib/__tests__/fixtures/site-topology.ts +3 -0
  30. package/templates/apps/web/lib/__tests__/server-api.test.ts +1 -1
  31. package/templates/apps/web/lib/server-api.ts +1 -1
  32. package/templates/apps/web/lib/topology.generated.ts +5 -0
  33. package/templates/apps/web/messages/en.json +0 -152
  34. package/templates/apps/web/middleware.ts +12 -25
  35. package/templates/apps/web/next.config.mjs +8 -0
  36. package/templates/apps/web/package.json +1 -0
  37. package/templates/apps/web/types/messages.d.ts +6 -1
  38. package/versions.json +49 -48
  39. package/templates/apps/web/app/[locale]/(auth)/__tests__/auth-pages.test.tsx +0 -322
  40. package/templates/apps/web/app/[locale]/(auth)/__tests__/password-reset.test.tsx +0 -554
  41. package/templates/apps/web/app/[locale]/(auth)/consent/__tests__/consent-actions.test.ts +0 -56
  42. package/templates/apps/web/app/[locale]/(auth)/consent/__tests__/page.test.tsx +0 -164
  43. package/templates/apps/web/app/[locale]/(auth)/consent/consent-form.tsx +0 -132
  44. package/templates/apps/web/app/[locale]/(auth)/consent/consent.actions.ts +0 -51
  45. package/templates/apps/web/app/[locale]/(auth)/consent/page.tsx +0 -144
  46. package/templates/apps/web/app/[locale]/(auth)/forgot-password/forgot-password-card.tsx +0 -101
  47. package/templates/apps/web/app/[locale]/(auth)/forgot-password/page.tsx +0 -111
  48. package/templates/apps/web/app/[locale]/(auth)/layout.tsx +0 -81
  49. package/templates/apps/web/app/[locale]/(auth)/legal-footnote.tsx +0 -35
  50. package/templates/apps/web/app/[locale]/(auth)/reset-password/__tests__/return.test.tsx +0 -179
  51. package/templates/apps/web/app/[locale]/(auth)/reset-password/page.tsx +0 -157
  52. package/templates/apps/web/app/[locale]/(auth)/reset-password/reset-password-card.tsx +0 -126
  53. package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/next-path.test.ts +0 -54
  54. package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/page.test.tsx +0 -628
  55. package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/remembered-method.test.ts +0 -43
  56. package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/sign-in-card.test.tsx +0 -266
  57. package/templates/apps/web/app/[locale]/(auth)/sign-in/next-path.ts +0 -20
  58. package/templates/apps/web/app/[locale]/(auth)/sign-in/page.tsx +0 -234
  59. package/templates/apps/web/app/[locale]/(auth)/sign-in/remembered-method.ts +0 -37
  60. package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-card.tsx +0 -323
  61. package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-text.ts +0 -93
  62. package/templates/apps/web/app/[locale]/(auth)/sign-up/__tests__/page.test.tsx +0 -301
  63. package/templates/apps/web/app/[locale]/(auth)/sign-up/__tests__/sign-up-card.test.tsx +0 -309
  64. package/templates/apps/web/app/[locale]/(auth)/sign-up/page.tsx +0 -201
  65. package/templates/apps/web/app/[locale]/(auth)/sign-up/sign-up-card.tsx +0 -366
  66. package/templates/apps/web/app/[locale]/(auth)/use-auth-card-flow.ts +0 -116
  67. package/templates/apps/web/app/[locale]/(auth)/verify-email/__tests__/actions.test.ts +0 -94
  68. package/templates/apps/web/app/[locale]/(auth)/verify-email/actions.ts +0 -77
  69. package/templates/apps/web/app/[locale]/(auth)/verify-email/page.tsx +0 -154
  70. package/templates/apps/web/app/[locale]/(authed)/account/appointments/[slug]/page.tsx +0 -3
  71. package/templates/apps/web/app/[locale]/(authed)/account/appointments/page.tsx +0 -3
  72. package/templates/apps/web/app/[locale]/(authed)/account/artifacts/[pieceId]/page.tsx +0 -3
  73. package/templates/apps/web/app/[locale]/(authed)/account/artifacts/[pieceId]/versions/[version]/file/route.ts +0 -2
  74. package/templates/apps/web/app/[locale]/(authed)/account/artifacts/page.tsx +0 -3
  75. package/templates/apps/web/app/[locale]/(authed)/account/billing/page.tsx +0 -12
  76. package/templates/apps/web/app/[locale]/(authed)/account/general/page.tsx +0 -12
  77. package/templates/apps/web/app/[locale]/(authed)/account/inbox/[kind]/[id]/page.tsx +0 -12
  78. package/templates/apps/web/app/[locale]/(authed)/account/inbox/message/[id]/file/route.ts +0 -2
  79. package/templates/apps/web/app/[locale]/(authed)/account/inbox/thread/page.tsx +0 -12
  80. package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/page.tsx +0 -3
  81. package/templates/apps/web/app/[locale]/(authed)/account/layout.tsx +0 -10
  82. package/templates/apps/web/app/[locale]/(authed)/account/notifications/page.tsx +0 -3
  83. package/templates/apps/web/app/[locale]/(authed)/account/onboarding/page.tsx +0 -3
  84. package/templates/apps/web/app/[locale]/(authed)/account/page.tsx +0 -12
  85. package/templates/apps/web/app/[locale]/(authed)/account/privacy/export/route.ts +0 -2
  86. package/templates/apps/web/app/[locale]/(authed)/account/privacy/page.tsx +0 -3
  87. package/templates/apps/web/app/[locale]/(authed)/account/security/page.tsx +0 -3
  88. package/templates/apps/web/app/[locale]/(authed)/account/settings/page.tsx +0 -2
  89. package/templates/apps/web/app/[locale]/(authed)/account/support/page.tsx +0 -3
  90. package/templates/apps/web/app/api/auth/[...all]/route.ts +0 -16
  91. package/templates/apps/web/app/api/auth/hub/__tests__/continuation.test.ts +0 -143
  92. package/templates/apps/web/app/api/auth/hub/__tests__/entry-routes.test.ts +0 -749
  93. package/templates/apps/web/app/api/auth/hub/authorize/__tests__/route.test.ts +0 -964
  94. package/templates/apps/web/app/api/auth/hub/authorize/route.ts +0 -390
  95. package/templates/apps/web/app/api/auth/hub/continuation.ts +0 -127
  96. package/templates/apps/web/app/api/auth/hub/continue/[ref]/__tests__/route.test.ts +0 -188
  97. package/templates/apps/web/app/api/auth/hub/continue/[ref]/route.ts +0 -53
  98. package/templates/apps/web/app/api/auth/hub/email/route.ts +0 -111
  99. package/templates/apps/web/app/api/auth/hub/entry-shared.ts +0 -163
  100. package/templates/apps/web/app/api/auth/hub/password/__tests__/return.test.ts +0 -86
  101. package/templates/apps/web/app/api/auth/hub/password/__tests__/route.test.ts +0 -368
  102. package/templates/apps/web/app/api/auth/hub/password/return.ts +0 -86
  103. package/templates/apps/web/app/api/auth/hub/password/route.ts +0 -171
  104. package/templates/apps/web/app/api/auth/hub/purchase/route.ts +0 -230
  105. package/templates/apps/web/app/api/auth/hub/purchase-link/route.ts +0 -174
  106. package/templates/apps/web/app/api/auth/hub/sign-up/route.ts +0 -60
  107. package/templates/apps/web/app/api/auth/hub/sign-up-evidence.ts +0 -97
  108. package/templates/apps/web/app/api/auth/mcp/__tests__/continuation.test.ts +0 -92
  109. package/templates/apps/web/app/api/auth/mcp/continuation.ts +0 -89
  110. package/templates/apps/web/app/api/auth/mcp/continue/[ref]/__tests__/route.test.ts +0 -59
  111. package/templates/apps/web/app/api/auth/mcp/continue/[ref]/route.ts +0 -41
  112. package/templates/apps/web/app/api/hub-session/__tests__/routes.test.ts +0 -1166
  113. package/templates/apps/web/app/api/hub-session/callback/route.ts +0 -134
  114. package/templates/apps/web/app/api/hub-session/email/route.ts +0 -125
  115. package/templates/apps/web/app/api/hub-session/one-tap/nonce/route.ts +0 -49
  116. package/templates/apps/web/app/api/hub-session/one-tap/route.ts +0 -106
  117. package/templates/apps/web/app/api/hub-session/open/route.ts +0 -111
  118. package/templates/apps/web/app/api/hub-session/password/return/route.ts +0 -69
  119. package/templates/apps/web/app/api/hub-session/password/route.ts +0 -106
  120. package/templates/apps/web/app/api/hub-session/sign-in/route.ts +0 -91
  121. package/templates/apps/web/app/api/hub-session/sign-out/route.ts +0 -54
  122. package/templates/apps/web/components/sign-out-trigger.tsx +0 -62
  123. package/templates/apps/web/lib/__tests__/auth-routes.test.ts +0 -140
  124. package/templates/apps/web/lib/auth-routes.ts +0 -125
  125. package/templates/apps/web/lib/hub-session/__tests__/hub-session.test.ts +0 -433
  126. package/templates/apps/web/lib/hub-session/__tests__/spoke-sign-in.test.tsx +0 -332
  127. package/templates/apps/web/lib/hub-session/config.ts +0 -110
  128. package/templates/apps/web/lib/hub-session/entry.ts +0 -238
  129. package/templates/apps/web/lib/hub-session/index.ts +0 -21
  130. package/templates/apps/web/lib/hub-session/one-tap.tsx +0 -148
  131. package/templates/apps/web/lib/hub-session/reseal.ts +0 -90
  132. package/templates/apps/web/lib/hub-session/seal.ts +0 -86
  133. package/templates/apps/web/lib/hub-session/session.ts +0 -298
  134. package/templates/apps/web/lib/hub-session/spoke-sign-in.tsx +0 -277
  135. package/templates/apps/web/lib/next-path.ts +0 -35
@@ -1,433 +0,0 @@
1
- /**
2
- * @jest-environment node
3
- */
4
-
5
- /**
6
- * The spoke's hub-session module (#7894 D7, D12, D13; spec
7
- * docs/specs/7894-the-hub-signs-in-its-spec.md AC-5, AC-7, AC-8).
8
- *
9
- * Covers the sealed cookie (a value sealed under a key derived from the
10
- * CLIENT secret never opens — AC-5), the configuration, the session read, the
11
- * one request-init re-target `forwardedCookieInit()` makes on a hub-mode spoke
12
- * (AC-7), and the buyer read that never trusts the cookie for a customer id
13
- * (AC-8). The api-client's `hubSignIn` is the one external boundary mocked.
14
- */
15
-
16
- import { createCipheriv, hkdfSync, randomBytes } from "node:crypto";
17
-
18
- const mockMe = jest.fn();
19
- jest.mock("@working-theory/api-client", () => ({
20
- __esModule: true,
21
- hubSignIn: { me: (...args: unknown[]) => mockMe(...args) },
22
- }));
23
-
24
- const mockHeaders = jest.fn();
25
- jest.mock("next/headers", () => ({
26
- __esModule: true,
27
- headers: (...args: unknown[]) => mockHeaders(...args),
28
- cookies: jest.fn(),
29
- }));
30
-
31
- // eslint-disable-next-line no-restricted-syntax -- #507: the sign-in provider is a generated constant (#7894 D8), not a runtime input; the seam IS the boundary between a hub and a hub-mode spoke build, and no env or HTTP surface selects it.
32
- jest.mock("../../topology.generated", () => ({
33
- __esModule: true,
34
- SIGN_IN_PROVIDER: "hub",
35
- }));
36
-
37
- import { cookies } from "next/headers";
38
-
39
- import { forwardedCookieInit } from "../../server-api";
40
- import { hubSessionConfig } from "../config";
41
- import { resealHubSession } from "../reseal";
42
- import { seal, unseal } from "../seal";
43
- import {
44
- type HubSessionPayload,
45
- hubNextPath,
46
- hubSessionBuyer,
47
- hubSessionInit,
48
- readHubSession,
49
- sessionCookieName,
50
- } from "../session";
51
-
52
- const ENV = process.env;
53
- const SESSION_SECRET = "s".repeat(43);
54
- const CLIENT_SECRET = `ailk_hcs_${"c".repeat(43)}`;
55
- const CREDENTIAL = `ailk_hst_${"t".repeat(43)}`;
56
- const COOKIE = "ailk-hub-session";
57
-
58
- const USER = {
59
- name: "Ada Lovelace",
60
- email: "ada@example.com",
61
- emailVerified: true,
62
- createdAt: "2026-09-01T00:00:00.000Z",
63
- };
64
-
65
- function configure(overrides: Record<string, string | undefined> = {}): void {
66
- process.env = {
67
- ...ENV,
68
- AILK_HUB_SIGN_IN_URL: "https://hub.example.com",
69
- AILK_HUB_CLIENT_ID: "spoke_0001",
70
- AILK_HUB_CLIENT_SECRET: CLIENT_SECRET,
71
- AILK_HUB_SESSION_SECRET: SESSION_SECRET,
72
- NEXT_PUBLIC_BASE_URL: "http://127.0.0.1:3100",
73
- ...overrides,
74
- };
75
- for (const [key, value] of Object.entries(overrides)) {
76
- if (value === undefined) delete process.env[key];
77
- }
78
- }
79
-
80
- function payload(overrides: Partial<HubSessionPayload> = {}): HubSessionPayload {
81
- return {
82
- v: 1,
83
- accessToken: CREDENTIAL,
84
- expiresAt: Date.now() + 60_000,
85
- user: USER,
86
- ...overrides,
87
- };
88
- }
89
-
90
- function headersWith(value: string, name = COOKIE): Headers {
91
- return new Headers({ cookie: `other=1; ${name}=${value}` });
92
- }
93
-
94
- beforeEach(() => {
95
- configure();
96
- mockMe.mockReset();
97
- mockHeaders.mockReset();
98
- });
99
-
100
- afterAll(() => {
101
- process.env = ENV;
102
- });
103
-
104
- describe("seal", () => {
105
- it("round-trips a value for the same secret, purpose and cookie name", () => {
106
- const sealed = seal({ a: 1 }, SESSION_SECRET, "session", COOKIE);
107
- expect(unseal(sealed, SESSION_SECRET, "session", COOKIE)).toEqual({ a: 1 });
108
- });
109
-
110
- it("opens nothing under another secret, purpose or cookie name, or altered", () => {
111
- const sealed = seal({ a: 1 }, SESSION_SECRET, "session", COOKIE);
112
- expect(unseal(sealed, "x".repeat(43), "session", COOKIE)).toBeNull();
113
- expect(unseal(sealed, SESSION_SECRET, "flow", COOKIE)).toBeNull();
114
- expect(unseal(sealed, SESSION_SECRET, "session", "ailk-hub-flow")).toBeNull();
115
- const flipped = Buffer.from(sealed, "base64url");
116
- flipped[flipped.length - 1] = (flipped[flipped.length - 1] ?? 0) ^ 1;
117
- expect(unseal(flipped.toString("base64url"), SESSION_SECRET, "session", COOKIE)).toBeNull();
118
- expect(unseal("", SESSION_SECRET, "session", COOKIE)).toBeNull();
119
- });
120
-
121
- // #8146 AC-4: the `continue` purpose was added to seal.ts. These two
122
- // ciphertexts were sealed on `main` BEFORE that edit and committed as
123
- // literals, so any change to INFO.session, INFO.flow or deriveKey fails
124
- // here — the round trips above cannot see a key change (a changed key still
125
- // round-trips with itself, and would silently log out every live spoke).
126
- it("still opens a `session` value sealed before the `continue` purpose existed", () => {
127
- const sealed =
128
- "T0OSblxM1tRcJ4f5OP4VDvQA5TvWQdW3K_GvZlFzETSgVCw1tzEEaDWfAhHz0HZgY1sg8vA2fy0au73t";
129
- expect(unseal(sealed, SESSION_SECRET, "session", COOKIE)).toEqual({
130
- v: 1,
131
- fixed: "session-vector",
132
- });
133
- });
134
-
135
- it("still opens a `flow` value sealed before the `continue` purpose existed", () => {
136
- const sealed =
137
- "TI60LmvzNlRIp6a4BexFT9zmNoOWn8ZYBhW2VtOklk6lwUtrEWwtJ15XSJeAfgeNpSc322z-PWlZ";
138
- expect(unseal(sealed, SESSION_SECRET, "flow", "ailk-hub-flow")).toEqual({
139
- v: 1,
140
- fixed: "flow-vector",
141
- });
142
- });
143
-
144
- it("uses a fresh IV per seal", () => {
145
- expect(seal({ a: 1 }, SESSION_SECRET, "session", COOKIE)).not.toBe(
146
- seal({ a: 1 }, SESSION_SECRET, "session", COOKIE),
147
- );
148
- });
149
- });
150
-
151
- describe("hubSessionConfig", () => {
152
- it("reads the four variables and the site origin; the redirect URI is the site's, never a header's", () => {
153
- expect(hubSessionConfig()).toEqual({
154
- hubOrigin: "https://hub.example.com",
155
- clientId: "spoke_0001",
156
- clientSecret: CLIENT_SECRET,
157
- sessionSecret: SESSION_SECRET,
158
- siteOrigin: "http://127.0.0.1:3100",
159
- redirectUri: "http://127.0.0.1:3100/api/hub-session/callback",
160
- secure: false,
161
- oneTap: false,
162
- });
163
- });
164
-
165
- // #11096 D10 — One Tap is on only for exactly `on`.
166
- it.each([
167
- ["on", true],
168
- ["ON", false],
169
- ["1", false],
170
- ["", false],
171
- ])("AILK_HUB_ONE_TAP=%p reads oneTap %p", (value, expected) => {
172
- configure({ AILK_HUB_ONE_TAP: value });
173
- expect(hubSessionConfig()?.oneTap).toBe(expected);
174
- });
175
-
176
- it("is secure on an https site", () => {
177
- configure({ NEXT_PUBLIC_BASE_URL: "https://spoke.example.com/" });
178
- expect(hubSessionConfig()).toMatchObject({
179
- siteOrigin: "https://spoke.example.com",
180
- secure: true,
181
- });
182
- expect(sessionCookieName({ secure: true })).toBe("__Host-ailk-hub-session");
183
- });
184
-
185
- it.each([
186
- ["no hub origin", { AILK_HUB_SIGN_IN_URL: undefined }],
187
- ["an http hub off loopback", { AILK_HUB_SIGN_IN_URL: "http://hub.example.com" }],
188
- ["a hub URL with a path", { AILK_HUB_SIGN_IN_URL: "https://hub.example.com/x" }],
189
- ["no client id", { AILK_HUB_CLIENT_ID: undefined }],
190
- ["no client secret", { AILK_HUB_CLIENT_SECRET: undefined }],
191
- ["a short session secret", { AILK_HUB_SESSION_SECRET: "short" }],
192
- ["an http site off loopback", { NEXT_PUBLIC_BASE_URL: "http://spoke.example.com" }],
193
- ])("is null with %s", (_label, overrides) => {
194
- configure(overrides);
195
- expect(hubSessionConfig()).toBeNull();
196
- });
197
- });
198
-
199
- describe("readHubSession", () => {
200
- it("reads the person from a sealed, unexpired cookie", async () => {
201
- const sealed = seal(payload(), SESSION_SECRET, "session", COOKIE);
202
- await expect(readHubSession(headersWith(sealed))).resolves.toEqual({ user: USER });
203
- });
204
-
205
- it("reads null with no cookie, an expired one, or with no configuration", async () => {
206
- await expect(readHubSession(new Headers())).resolves.toBeNull();
207
- const expired = seal(payload({ expiresAt: Date.now() - 1 }), SESSION_SECRET, "session", COOKIE);
208
- await expect(readHubSession(headersWith(expired))).resolves.toBeNull();
209
- const sealed = seal(payload(), SESSION_SECRET, "session", COOKIE);
210
- configure({ AILK_HUB_SESSION_SECRET: undefined });
211
- await expect(readHubSession(headersWith(sealed))).resolves.toBeNull();
212
- });
213
-
214
- it("AC-5: a cookie sealed with a key derived from AILK_HUB_CLIENT_SECRET reads as null", async () => {
215
- // Every way the client secret could have keyed the cookie: the module's own
216
- // seal with the client secret as the session secret, and a raw HKDF of it.
217
- const viaSeal = seal(payload(), CLIENT_SECRET, "session", COOKIE);
218
- await expect(readHubSession(headersWith(viaSeal))).resolves.toBeNull();
219
-
220
- const key = Buffer.from(hkdfSync("sha256", CLIENT_SECRET, Buffer.alloc(0), "ailk-hub-session/v1", 32));
221
- const iv = randomBytes(12);
222
- const cipher = createCipheriv("aes-256-gcm", key, iv);
223
- cipher.setAAD(Buffer.from(COOKIE));
224
- const body = Buffer.concat([cipher.update(JSON.stringify(payload())), cipher.final()]);
225
- const forged = Buffer.concat([iv, cipher.getAuthTag(), body]).toString("base64url");
226
- await expect(readHubSession(headersWith(forged))).resolves.toBeNull();
227
- });
228
-
229
- it("reads null for a well-sealed value of the wrong shape", async () => {
230
- const sealed = seal({ v: 2, accessToken: CREDENTIAL }, SESSION_SECRET, "session", COOKIE);
231
- await expect(readHubSession(headersWith(sealed))).resolves.toBeNull();
232
- });
233
- });
234
-
235
- describe("AC-7 — forwardedCookieInit on a hub-mode spoke", () => {
236
- it("is exactly the Bearer credential signed in", async () => {
237
- const sealed = seal(payload(), SESSION_SECRET, "session", COOKIE);
238
- mockHeaders.mockResolvedValue(headersWith(sealed));
239
- await expect(forwardedCookieInit()).resolves.toEqual({
240
- headers: { authorization: `Bearer ${CREDENTIAL}` },
241
- });
242
- await expect(hubSessionInit()).resolves.toEqual({
243
- headers: { authorization: `Bearer ${CREDENTIAL}` },
244
- });
245
- });
246
-
247
- it("is exactly no header signed out — never the visitor's cookie", async () => {
248
- mockHeaders.mockResolvedValue(new Headers({ cookie: "better-auth.session_token=abc" }));
249
- await expect(forwardedCookieInit()).resolves.toEqual({ headers: {} });
250
- });
251
- });
252
-
253
- describe("AC-8 — hubSessionBuyer reads the customer live, never from the cookie", () => {
254
- function request(cookie?: string): Request {
255
- return new Request("http://127.0.0.1:3100/api/buy", {
256
- headers: cookie === undefined ? {} : { cookie: `${COOKIE}=${cookie}` },
257
- });
258
- }
259
- const signedIn = (): Request => request(seal(payload(), SESSION_SECRET, "session", COOKIE));
260
-
261
- it("returns the customer /me vouches for, sending the credential", async () => {
262
- mockMe.mockResolvedValue({ ok: true, body: { user: USER, billingCustomerId: "cus_known" } });
263
- await expect(hubSessionBuyer(signedIn())).resolves.toEqual({ customerId: "cus_known" });
264
- expect(mockMe).toHaveBeenCalledWith({ headers: { authorization: `Bearer ${CREDENTIAL}` } });
265
- });
266
-
267
- it("is null with no session, without calling the hub", async () => {
268
- await expect(hubSessionBuyer(request())).resolves.toBeNull();
269
- expect(mockMe).not.toHaveBeenCalled();
270
- });
271
-
272
- it("is null when /me refuses the credential though the cookie is intact (a revoked credential)", async () => {
273
- mockMe.mockResolvedValue({ ok: false, status: 401, body: null });
274
- await expect(hubSessionBuyer(signedIn())).resolves.toBeNull();
275
- });
276
-
277
- it("is null when /me names no customer, or the read throws", async () => {
278
- mockMe.mockResolvedValueOnce({ ok: true, body: { user: USER, billingCustomerId: null } });
279
- await expect(hubSessionBuyer(signedIn())).resolves.toBeNull();
280
- mockMe.mockRejectedValueOnce(new Error("network"));
281
- await expect(hubSessionBuyer(signedIn())).resolves.toBeNull();
282
- });
283
- });
284
-
285
- describe("hubNextPath", () => {
286
- it.each([
287
- ["/account", "/account"],
288
- ["/fr/account/inbox?tab=x", "/fr/account/inbox?tab=x"],
289
- ["https://evil.example.com/", "/account"],
290
- ["//evil.example.com", "/account"],
291
- ["/api/hub-session/sign-out", "/account"],
292
- ["/api", "/account"],
293
- ["/en/../api/hub-session/sign-in?next=/account", "/account"],
294
- ["/%2e%2e/api/hub-session/sign-in", "/account"],
295
- ["/\u65e5\u672c", "/account"],
296
- [null, "/account"],
297
- ])("%s → %s", (raw, expected) => {
298
- expect(hubNextPath(raw)).toBe(expected);
299
- });
300
- });
301
-
302
- // #8232 D11 (spec docs/specs/8232-member-settings-account-general-and-spec.md
303
- // AC-5) — after a member's profile save, the spoke re-seals its session from
304
- // the hub's /me. Only `user` changes; the credential and its expiry are kept,
305
- // so a re-seal never extends a session (SEC-8232-1), and the cookie is
306
- // written with the attributes the callback writes at sign-in (SEC-8232-2).
307
- describe("#8232 AC-5 — resealHubSession", () => {
308
- const mockSet = jest.fn();
309
- const RENAMED = { ...USER, name: "Ada King" };
310
-
311
- beforeEach(() => {
312
- mockSet.mockReset();
313
- jest.mocked(cookies).mockResolvedValue({ set: mockSet } as never);
314
- });
315
-
316
- function signedIn(overrides: Partial<HubSessionPayload> = {}, name = COOKIE): void {
317
- mockHeaders.mockResolvedValue(headersWith(seal(payload(overrides), SESSION_SECRET, "session", name), name));
318
- }
319
-
320
- it("re-seals the session with /me's person, keeping the credential and expiry, once, with the sign-in attributes", async () => {
321
- const expiresAt = Date.now() + 60_000;
322
- signedIn({ expiresAt });
323
- mockMe.mockResolvedValue({ ok: true, body: { user: RENAMED, billingCustomerId: null } });
324
-
325
- await expect(resealHubSession()).resolves.toBe("resealed");
326
-
327
- expect(mockMe).toHaveBeenCalledTimes(1);
328
- expect(mockMe).toHaveBeenCalledWith({ headers: { authorization: `Bearer ${CREDENTIAL}` } });
329
- expect(mockSet).toHaveBeenCalledTimes(1);
330
- const [name, value, options] = mockSet.mock.calls[0] as [string, string, Record<string, unknown>];
331
- expect(name).toBe(COOKIE);
332
- expect(options).toEqual({
333
- httpOnly: true,
334
- sameSite: "lax",
335
- path: "/",
336
- secure: false,
337
- maxAge: expect.any(Number),
338
- });
339
- expect(options["maxAge"]).toBeGreaterThan(0);
340
- expect(options["maxAge"]).toBeLessThanOrEqual(60);
341
- expect(unseal(value, SESSION_SECRET, "session", COOKIE)).toEqual({
342
- v: 1,
343
- accessToken: CREDENTIAL,
344
- expiresAt,
345
- user: RENAMED,
346
- });
347
- });
348
-
349
- it("writes the __Host- cookie with Secure on an https site", async () => {
350
- configure({ NEXT_PUBLIC_BASE_URL: "https://spoke.example.com" });
351
- signedIn({}, "__Host-ailk-hub-session");
352
- mockMe.mockResolvedValue({ ok: true, body: { user: RENAMED, billingCustomerId: null } });
353
-
354
- await expect(resealHubSession()).resolves.toBe("resealed");
355
- const [name, , options] = mockSet.mock.calls[0] as [string, string, Record<string, unknown>];
356
- expect(name).toBe("__Host-ailk-hub-session");
357
- expect(options).toMatchObject({ secure: true, httpOnly: true });
358
- });
359
-
360
- it("is skipped with no configuration (a single site), calling neither /me nor set", async () => {
361
- signedIn();
362
- configure({ AILK_HUB_SESSION_SECRET: undefined });
363
- await expect(resealHubSession()).resolves.toBe("skipped");
364
- expect(mockMe).not.toHaveBeenCalled();
365
- expect(mockSet).not.toHaveBeenCalled();
366
- });
367
-
368
- it("is skipped with no session cookie, calling neither /me nor set", async () => {
369
- mockHeaders.mockResolvedValue(new Headers());
370
- await expect(resealHubSession()).resolves.toBe("skipped");
371
- expect(mockMe).not.toHaveBeenCalled();
372
- expect(mockSet).not.toHaveBeenCalled();
373
- });
374
-
375
- it.each([
376
- ["a non-2xx", () => mockMe.mockResolvedValue({ ok: false, status: 401, body: null })],
377
- ["a throw", () => mockMe.mockRejectedValue(new Error("network"))],
378
- [
379
- "a user with a non-string email",
380
- () => mockMe.mockResolvedValue({ ok: true, body: { user: { ...RENAMED, email: 42 }, billingCustomerId: null } }),
381
- ],
382
- [
383
- "a user with a non-boolean emailVerified",
384
- () =>
385
- mockMe.mockResolvedValue({ ok: true, body: { user: { ...RENAMED, emailVerified: "yes" }, billingCustomerId: null } }),
386
- ],
387
- [
388
- "a user with a non-string createdAt",
389
- () => mockMe.mockResolvedValue({ ok: true, body: { user: { ...RENAMED, createdAt: 1 }, billingCustomerId: null } }),
390
- ],
391
- [
392
- "a user whose name is neither a string nor null",
393
- () => mockMe.mockResolvedValue({ ok: true, body: { user: { ...RENAMED, name: 7 }, billingCustomerId: null } }),
394
- ],
395
- ])("fails on %s from /me and leaves the cookie as it is", async (_label, arrange) => {
396
- signedIn();
397
- arrange();
398
- await expect(resealHubSession()).resolves.toBe("failed");
399
- expect(mockSet).not.toHaveBeenCalled();
400
- });
401
-
402
- it("is skipped when the session has under a second left, with no call and no write", async () => {
403
- signedIn({ expiresAt: Date.now() + 500 });
404
- await expect(resealHubSession()).resolves.toBe("skipped");
405
- expect(mockMe).not.toHaveBeenCalled();
406
- expect(mockSet).not.toHaveBeenCalled();
407
- });
408
-
409
- it("fails, never throws, when the cookie write is refused", async () => {
410
- signedIn();
411
- mockMe.mockResolvedValue({ ok: true, body: { user: RENAMED, billingCustomerId: null } });
412
- mockSet.mockImplementation(() => {
413
- throw new Error("Cookies can only be modified in a Server Action or Route Handler");
414
- });
415
- await expect(resealHubSession()).resolves.toBe("failed");
416
- });
417
- });
418
-
419
- describe("readBodyText (#11096) — the spoke's body read caps BYTES, not characters", () => {
420
- const post = (body: string): Request =>
421
- new Request("https://spoke.example/api/hub-session/email", { method: "POST", body });
422
-
423
- it("reads a body within 8 KB", async () => {
424
- const { readBodyText } = await import("../entry");
425
- await expect(readBodyText(post("a".repeat(8_192)))).resolves.toBe("a".repeat(8_192));
426
- });
427
-
428
- it("refuses a body under 8 K characters but over 8 KB of UTF-8", async () => {
429
- const { readBodyText } = await import("../entry");
430
- // 3 000 three-byte characters: 3 000 characters, 9 000 bytes.
431
- await expect(readBodyText(post("€".repeat(3_000)))).resolves.toBeNull();
432
- });
433
- });