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,134 +0,0 @@
1
- /**
2
- * Finish a sign-in through the hub (#7894 D2, D7) — `GET /api/hub-session/callback?code=…&state=…`.
3
- *
4
- * The URI an operator registers on the hub (`spokes sign-in-client
5
- * --redirect-uri <site>/api/hub-session/callback`). It reads and clears the
6
- * flow cookie, checks the returned `state` against it in constant time, and
7
- * exchanges the code, the PKCE verifier and the client secret at the hub API
8
- * (the api-client adds this site's spoke key; the hub requires both). On
9
- * success the credential and its person are sealed into the session cookie,
10
- * whose `Max-Age` is the credential's own lifetime, and the visitor lands on
11
- * the flow cookie's `next` — never on a value from this request's query.
12
- *
13
- * Every other outcome — no flow cookie, an expired one, a state mismatch, an
14
- * `error` from the hub, a failed exchange — lands on the site's home
15
- * (#11095 D4; `/account` would redirect straight back into the sign-in, a
16
- * loop) after BURNING any code it received
17
- * (`hubSignIn.revoke({ code })`, best-effort): a code this route cannot
18
- * complete must not stay live for someone else to complete (SEC-7894-4).
19
- * The log line names the reason only — never the query string, a code, a
20
- * state or a credential.
21
- */
22
- import { timingSafeEqual } from "node:crypto";
23
-
24
- import { hubSignIn } from "@working-theory/api-client";
25
- import { hubAuthorizationCodeSchema } from "@working-theory/validation";
26
-
27
- import {
28
- type HubSessionConfig,
29
- hubSessionConfig,
30
- } from "../../../../lib/hub-session/config";
31
- import { unseal } from "../../../../lib/hub-session/seal";
32
- import {
33
- HUB_SESSION_HOME_PATH,
34
- cookieHeader,
35
- flowCookieName,
36
- hubNextPath,
37
- readCookie,
38
- seeOther,
39
- sessionCookieFor,
40
- } from "../../../../lib/hub-session/session";
41
- import { SIGN_IN_PROVIDER } from "../../../../lib/topology.generated";
42
-
43
- /** What the sign-in route sealed into the flow cookie. */
44
- interface FlowPayload {
45
- v: 1;
46
- state: string;
47
- verifier: string;
48
- next: string;
49
- expiresAt: number;
50
- }
51
-
52
- function isFlow(value: unknown): value is FlowPayload {
53
- if (typeof value !== "object" || value === null) return false;
54
- const v = value as Record<string, unknown>;
55
- return (
56
- v["v"] === 1 &&
57
- typeof v["state"] === "string" &&
58
- typeof v["verifier"] === "string" &&
59
- typeof v["next"] === "string" &&
60
- typeof v["expiresAt"] === "number"
61
- );
62
- }
63
-
64
- function sameState(a: string, b: string): boolean {
65
- const x = Buffer.from(a, "utf8");
66
- const y = Buffer.from(b, "utf8");
67
- return x.length === y.length && timingSafeEqual(x, y);
68
- }
69
-
70
- /** Burn a code this route cannot complete. Best-effort: a failure is logged, never thrown. */
71
- async function burn(config: HubSessionConfig, code: string | null): Promise<void> {
72
- if (code === null || !hubAuthorizationCodeSchema.safeParse(code).success) {
73
- return;
74
- }
75
- try {
76
- await hubSignIn.revoke(
77
- { clientId: config.clientId, clientSecret: config.clientSecret },
78
- { code },
79
- );
80
- } catch (err) {
81
- console.error(
82
- `[hub-session] code burn failed errName=${err instanceof Error ? err.name : "unknown"}`,
83
- );
84
- }
85
- }
86
-
87
- // platform-decisions-waiver: api-versioning — sign-in redirect endpoint, not a versioned API contract (spec 7894 §7)
88
- export async function GET(request: Request): Promise<Response> {
89
- if (SIGN_IN_PROVIDER !== "hub") return new Response(null, { status: 404 });
90
- const config = hubSessionConfig();
91
- if (config === null) return seeOther(HUB_SESSION_HOME_PATH);
92
-
93
- const params = new URL(request.url).searchParams;
94
- const code = params.get("code");
95
- const flowName = flowCookieName(config);
96
- const clearFlow = cookieHeader(flowName, "", 0, config);
97
-
98
- const refuse = async (reason: string): Promise<Response> => {
99
- await burn(config, code);
100
- console.warn(`[hub-session] sign-in not completed reason=${reason}`);
101
- return seeOther(HUB_SESSION_HOME_PATH, [clearFlow]);
102
- };
103
-
104
- const sealed = readCookie(request.headers.get("cookie"), flowName);
105
- const flow =
106
- sealed === undefined
107
- ? null
108
- : unseal(sealed, config.sessionSecret, "flow", flowName);
109
- if (!isFlow(flow)) return refuse("no_flow");
110
- if (flow.expiresAt <= Date.now()) return refuse("flow_expired");
111
- const state = params.get("state");
112
- if (state === null || !sameState(state, flow.state)) {
113
- return refuse("state_mismatch");
114
- }
115
- if (params.has("error")) return refuse("hub_error");
116
- if (code === null) return refuse("no_code");
117
-
118
- let exchanged: Awaited<ReturnType<typeof hubSignIn.exchange>>;
119
- try {
120
- exchanged = await hubSignIn.exchange(
121
- { clientId: config.clientId, clientSecret: config.clientSecret },
122
- { code, redirectUri: config.redirectUri, codeVerifier: flow.verifier },
123
- );
124
- } catch {
125
- return refuse("exchange_failed");
126
- }
127
- if (!exchanged.ok) return refuse(`exchange_status_${exchanged.status}`);
128
-
129
- // The cookie's lifetime is the credential's: a malformed answer from the
130
- // hub must never yield a session that does not expire (#11096 D6).
131
- const session = sessionCookieFor(config, exchanged.body);
132
- if (session === null) return refuse("token_response_invalid");
133
- return seeOther(hubNextPath(flow.next), [clearFlow, session]);
134
- }
@@ -1,125 +0,0 @@
1
- /**
2
- * The spoke's own sign-in form, posted (#11096 D2) — `POST /api/hub-session/email`.
3
- *
4
- * The signed-out `/account` of a spoke that signs in through its hub is the
5
- * spoke's own form (`lib/hub-session/spoke-sign-in.tsx`): one email field, the
6
- * hub's sign-up agreements, Continue with Google. Its email lands here as a
7
- * native form POST; this route starts a flow and asks the hub web, server to
8
- * server, to mail the hub's magic link for it. Using the link signs the person
9
- * in on the hub and comes back through the callback, with no hub page shown.
10
- *
11
- * 1. 404 unless `SIGN_IN_PROVIDER` is `"hub"`; not configured → 303
12
- * `/account?error=unavailable`.
13
- * 2. Same-origin only (`Sec-Fetch-Site`, `Origin`), else 403.
14
- * 3. 10 posts per visitor per 10 minutes, then the bot check; a refusal is
15
- * 303 `/account?error=unavailable`.
16
- * 4. The form is `spokeEmailFormSchema`: one `email`, an optional `next`,
17
- * zero or more ticked `agreements`. Any other field — a `password` among
18
- * them (D5) — or a repeated `email` or `next` is 400.
19
- * 5. A flow starts as `GET /api/hub-session/sign-in` starts one.
20
- * 6. The hub web's `POST /api/auth/hub/email` gets the authorize keys, the
21
- * email, the locale and — only when one was ticked — the agreements.
22
- * 7. 202 → 303 `/account?sent=1` with the flow cookie; `agreements_required`
23
- * → `/account?error=agreements`; anything else → `?error=unavailable`.
24
- * Only the 202 sets a cookie.
25
- *
26
- * The answer never says whether the address has an account. Nothing here logs
27
- * the email, the agreements or the flow.
28
- */
29
- import { spokeEmailFormSchema } from "@working-theory/validation";
30
-
31
- import { runBotCheck } from "../../../../lib/bot-check";
32
- import { hubSessionConfig } from "../../../../lib/hub-session/config";
33
- import {
34
- accountPath,
35
- hubError,
36
- localeOfPath,
37
- postToHub,
38
- readFormFields,
39
- } from "../../../../lib/hub-session/entry";
40
- import {
41
- hubNextPath,
42
- isSameOrigin,
43
- seeOther,
44
- startHubFlow,
45
- } from "../../../../lib/hub-session/session";
46
- import { SIGN_IN_PROVIDER } from "../../../../lib/topology.generated";
47
- import { takeVisitorLimit } from "../../../../src/lib/rate-limit";
48
-
49
- /** The hub web's email route (D2). */
50
- const HUB_EMAIL_PATH = "/api/auth/hub/email";
51
-
52
- /** The per-visitor budget for this route. */
53
- const VISITOR_LIMIT = 10;
54
- const VISITOR_WINDOW_MS = 10 * 60_000;
55
-
56
- /** The fields the form may carry, and which of them may repeat. */
57
- const SINGLE_FIELDS = new Set(["email", "next"]);
58
- const AGREEMENTS_FIELD = "agreements";
59
-
60
- /** The form as `spokeEmailFormSchema` reads it, or `null` for a refused shape. */
61
- function formBody(fields: Map<string, string[]>): Record<string, unknown> | null {
62
- const body: Record<string, unknown> = {};
63
- for (const [name, values] of fields) {
64
- if (name === AGREEMENTS_FIELD) {
65
- body[name] = values;
66
- } else if (SINGLE_FIELDS.has(name)) {
67
- if (values.length !== 1) return null;
68
- body[name] = values[0];
69
- } else {
70
- // An unknown field (a password among them) is refused, never dropped.
71
- return null;
72
- }
73
- }
74
- return body;
75
- }
76
-
77
- function badRequest(): Response {
78
- return new Response(null, { status: 400, headers: { "Cache-Control": "no-store" } });
79
- }
80
-
81
- // platform-decisions-waiver: api-versioning — sign-in form endpoint, not a versioned API contract (spec 11096 §6)
82
- export async function POST(request: Request): Promise<Response> {
83
- if (SIGN_IN_PROVIDER !== "hub") return new Response(null, { status: 404 });
84
- const config = hubSessionConfig();
85
- if (config === null) return seeOther(accountPath("en", "error=unavailable"));
86
- if (!isSameOrigin(request, config.siteOrigin)) {
87
- return new Response(null, { status: 403 });
88
- }
89
-
90
- if (!takeVisitorLimit("hub-session-email", request.headers, VISITOR_LIMIT, VISITOR_WINDOW_MS).allowed) {
91
- return seeOther(accountPath("en", "error=unavailable"));
92
- }
93
- const bot = await runBotCheck({ source: "hub-session", target: "email", scope: "write" });
94
- if (bot !== "pass") return seeOther(accountPath("en", "error=unavailable"));
95
-
96
- const fields = await readFormFields(request);
97
- const raw = fields === null ? null : formBody(fields);
98
- const parsed = raw === null ? null : spokeEmailFormSchema.safeParse(raw);
99
- if (parsed === null || !parsed.success) return badRequest();
100
- const next = hubNextPath(parsed.data.next);
101
- const locale = localeOfPath(next);
102
-
103
- const flow = startHubFlow(config, next);
104
- const answer = await postToHub(config, HUB_EMAIL_PATH, {
105
- client_id: config.clientId,
106
- redirect_uri: config.redirectUri,
107
- state: flow.state,
108
- code_challenge: flow.challenge,
109
- code_challenge_method: "S256",
110
- email: parsed.data.email,
111
- locale,
112
- ...(parsed.data.agreements ? { agreements: parsed.data.agreements } : {}),
113
- });
114
-
115
- if (answer?.status === 202) {
116
- return seeOther(accountPath(locale, "sent=1"), [flow.cookie]);
117
- }
118
- if (answer?.status === 400 && hubError(answer) === "agreements_required") {
119
- return seeOther(accountPath(locale, "error=agreements"));
120
- }
121
- console.warn(
122
- `[hub-session] email entry not sent status=${answer === null ? "unreachable" : answer.status}`,
123
- );
124
- return seeOther(accountPath(locale, "error=unavailable"));
125
- }
@@ -1,49 +0,0 @@
1
- /**
2
- * One Tap's nonce (#11096 D10) — `POST /api/hub-session/one-tap/nonce`.
3
- *
4
- * Before the spoke's sign-in page asks Google for a One Tap credential, it
5
- * asks here for a nonce: 32 random bytes, answered to the page and sealed
6
- * into a 300 s `HttpOnly` cookie. Google signs the nonce into the ID token,
7
- * and `POST /api/hub-session/one-tap` hands the hub the cookie's copy, so a
8
- * token minted for another page or another visitor does not verify.
9
- *
10
- * 404 unless this is a hub-sign-in spoke with `AILK_HUB_ONE_TAP=on`; 403
11
- * unless same-origin. Nothing here logs the nonce.
12
- */
13
- import { randomBytes } from "node:crypto";
14
-
15
- import { hubSessionConfig } from "../../../../../lib/hub-session/config";
16
- import { seal } from "../../../../../lib/hub-session/seal";
17
- import {
18
- ONE_TAP_NONCE_TTL_SECONDS,
19
- cookieHeader,
20
- isSameOrigin,
21
- oneTapCookieName,
22
- } from "../../../../../lib/hub-session/session";
23
- import { SIGN_IN_PROVIDER } from "../../../../../lib/topology.generated";
24
-
25
- // platform-decisions-waiver: api-versioning — sign-in step, not a versioned API contract (spec 11096 §6)
26
- export function POST(request: Request): Response {
27
- const config = SIGN_IN_PROVIDER === "hub" ? hubSessionConfig() : null;
28
- if (config === null || !config.oneTap) return new Response(null, { status: 404 });
29
- if (!isSameOrigin(request, config.siteOrigin)) {
30
- return new Response(null, { status: 403 });
31
- }
32
- const nonce = randomBytes(32).toString("base64url");
33
- const name = oneTapCookieName(config);
34
- const sealed = seal(
35
- { v: 1, nonce, expiresAt: Date.now() + ONE_TAP_NONCE_TTL_SECONDS * 1000 },
36
- config.sessionSecret,
37
- "flow",
38
- name,
39
- );
40
- return Response.json(
41
- { nonce },
42
- {
43
- headers: {
44
- "Cache-Control": "no-store",
45
- "Set-Cookie": cookieHeader(name, sealed, ONE_TAP_NONCE_TTL_SECONDS, config),
46
- },
47
- },
48
- );
49
- }
@@ -1,106 +0,0 @@
1
- /**
2
- * Google One Tap for a returning person (#11096 D10) — `POST /api/hub-session/one-tap`.
3
- *
4
- * The spoke's `OneTap` island posts Google's ID token here as
5
- * `{ credential }`. This route reads and clears the nonce cookie the nonce
6
- * route set, and forwards the token, that nonce and this site's own origin to
7
- * the hub API's `POST /v1/hub-sign-in/google`, authenticated as this spoke's
8
- * client. The hub verifies the token against Google's keys and mints the
9
- * credential the code exchange mints; this route seals it into the session
10
- * cookie (`sessionCookieFor`) and answers `{ ok: true }`. The page then
11
- * navigates to `next` on this site. No hub page and no Google page is shown.
12
- *
13
- * - 404 unless this is a hub-sign-in spoke with `AILK_HUB_ONE_TAP=on`;
14
- * - 403 unless same-origin;
15
- * - 400 for a body other than `spokeOneTapBodySchema` (a `password` field
16
- * among them, D5) or with no live nonce cookie — no hub call;
17
- * - 401 `{ ok: false }` for every hub refusal, so the page leaves the form
18
- * as it is.
19
- *
20
- * Nothing here logs the token, the nonce or the credential.
21
- */
22
- import { hubSignIn } from "@working-theory/api-client";
23
- import { spokeOneTapBodySchema } from "@working-theory/validation";
24
-
25
- import { hubSessionConfig } from "../../../../lib/hub-session/config";
26
- import { readBodyText } from "../../../../lib/hub-session/entry";
27
- import { unseal } from "../../../../lib/hub-session/seal";
28
- import {
29
- cookieHeader,
30
- isSameOrigin,
31
- oneTapCookieName,
32
- readCookie,
33
- sessionCookieFor,
34
- } from "../../../../lib/hub-session/session";
35
- import { SIGN_IN_PROVIDER } from "../../../../lib/topology.generated";
36
- import { takeVisitorLimit } from "../../../../src/lib/rate-limit";
37
-
38
- const VISITOR_LIMIT = 10;
39
- const VISITOR_WINDOW_MS = 10 * 60_000;
40
-
41
- function answer(status: number, ok: boolean, setCookies: readonly string[]): Response {
42
- const headers = new Headers({ "Cache-Control": "no-store" });
43
- for (const value of setCookies) headers.append("Set-Cookie", value);
44
- return Response.json({ ok }, { status, headers });
45
- }
46
-
47
- /** `text` as JSON, or `null` when there is none or it does not parse. */
48
- function parseJson(text: string | null): unknown {
49
- if (text === null) return null;
50
- try {
51
- return JSON.parse(text) as unknown;
52
- } catch {
53
- return null;
54
- }
55
- }
56
-
57
- /** The nonce the cookie seals, or `null` — absent, unreadable or expired. */
58
- function sealedNonce(value: unknown): string | null {
59
- if (typeof value !== "object" || value === null) return null;
60
- const v = value as Record<string, unknown>;
61
- if (v["v"] !== 1 || typeof v["nonce"] !== "string") return null;
62
- if (typeof v["expiresAt"] !== "number" || v["expiresAt"] <= Date.now()) return null;
63
- return v["nonce"];
64
- }
65
-
66
- // platform-decisions-waiver: api-versioning — sign-in step, not a versioned API contract (spec 11096 §6)
67
- export async function POST(request: Request): Promise<Response> {
68
- const config = SIGN_IN_PROVIDER === "hub" ? hubSessionConfig() : null;
69
- if (config === null || !config.oneTap) return new Response(null, { status: 404 });
70
- if (!isSameOrigin(request, config.siteOrigin)) {
71
- return new Response(null, { status: 403 });
72
- }
73
- if (!takeVisitorLimit("hub-session-one-tap", request.headers, VISITOR_LIMIT, VISITOR_WINDOW_MS).allowed) {
74
- return answer(429, false, []);
75
- }
76
-
77
- // The nonce is single use: cleared on every answer from here on.
78
- const name = oneTapCookieName(config);
79
- const clearNonce = cookieHeader(name, "", 0, config);
80
- const sealed = readCookie(request.headers.get("cookie"), name);
81
- const nonce =
82
- sealed === undefined ? null : sealedNonce(unseal(sealed, config.sessionSecret, "flow", name));
83
-
84
- const body = spokeOneTapBodySchema.safeParse(parseJson(await readBodyText(request)));
85
- if (!body.success || nonce === null) return answer(400, false, [clearNonce]);
86
-
87
- let res: Awaited<ReturnType<typeof hubSignIn.google>>;
88
- try {
89
- res = await hubSignIn.google(
90
- { clientId: config.clientId, clientSecret: config.clientSecret },
91
- { idToken: body.data.credential, nonce, origin: config.siteOrigin },
92
- );
93
- } catch (err) {
94
- console.error(
95
- `[hub-session] one tap failed errName=${err instanceof Error ? err.name : "unknown"}`,
96
- );
97
- return answer(401, false, [clearNonce]);
98
- }
99
- if (!res.ok) {
100
- console.warn(`[hub-session] one tap refused status=${res.status}`);
101
- return answer(401, false, [clearNonce]);
102
- }
103
- const session = sessionCookieFor(config, res.body);
104
- if (session === null) return answer(401, false, [clearNonce]);
105
- return answer(200, true, [clearNonce, session]);
106
- }
@@ -1,111 +0,0 @@
1
- /**
2
- * "Open my account" from a purchase link (#11096 D9) — `POST /api/hub-session/open`.
3
- *
4
- * After a paid Checkout Session the hub mails the buyer one link to this
5
- * spoke's `/account?claim=<claim>`. That page's GET changes nothing (a mail
6
- * scanner that follows the link creates no account, SEC-11096-3): it shows
7
- * the agreements and one "Open my account" button, whose form posts the
8
- * claim here. This route starts a flow and spends the claim at the hub web's
9
- * `POST /api/auth/hub/purchase`, server to server, as this spoke's client.
10
- *
11
- * - 200 `{ verifyUrl }` on the hub's own origin → 303 there, with the flow
12
- * cookie: the hub's verify creates the account, and the redirects land on
13
- * `/account` signed in;
14
- * - 409 `account_exists` → 303 `/account?error=exists` (the form asks for
15
- * the email instead: a claim never signs an existing account in);
16
- * - anything else → 303 `/account?error=claim`.
17
- *
18
- * The checks in front are the email route's (D2 steps 1–3); the body is
19
- * `spokeOpenFormSchema`, the one `claim` field. Nothing here logs the claim.
20
- */
21
- import { hubPurchaseResponseSchema, spokeOpenFormSchema } from "@working-theory/validation";
22
-
23
- import { runBotCheck } from "../../../../lib/bot-check";
24
- import { hubSessionConfig } from "../../../../lib/hub-session/config";
25
- import {
26
- accountPath,
27
- hubError,
28
- postToHub,
29
- readFormFields,
30
- } from "../../../../lib/hub-session/entry";
31
- import {
32
- HUB_SESSION_FALLBACK_PATH,
33
- isSameOrigin,
34
- seeOther,
35
- startHubFlow,
36
- } from "../../../../lib/hub-session/session";
37
- import { SIGN_IN_PROVIDER } from "../../../../lib/topology.generated";
38
- import { takeVisitorLimit } from "../../../../src/lib/rate-limit";
39
-
40
- /** The hub web's claim-spending route (D9). */
41
- const HUB_PURCHASE_PATH = "/api/auth/hub/purchase";
42
-
43
- const VISITOR_LIMIT = 10;
44
- const VISITOR_WINDOW_MS = 10 * 60_000;
45
-
46
- /** The one `claim` field, or `null` for any other shape. */
47
- function formBody(fields: Map<string, string[]>): Record<string, unknown> | null {
48
- const body: Record<string, unknown> = {};
49
- for (const [name, values] of fields) {
50
- if (values.length !== 1) return null;
51
- body[name] = values[0];
52
- }
53
- return body;
54
- }
55
-
56
- /** `raw` when it is a URL on `hubOrigin`, else `null`. */
57
- function hubUrl(raw: string, hubOrigin: string): string | null {
58
- try {
59
- const url = new URL(raw);
60
- return url.origin === hubOrigin ? url.toString() : null;
61
- } catch {
62
- return null;
63
- }
64
- }
65
-
66
- // platform-decisions-waiver: api-versioning — sign-in form endpoint, not a versioned API contract (spec 11096 §6)
67
- export async function POST(request: Request): Promise<Response> {
68
- if (SIGN_IN_PROVIDER !== "hub") return new Response(null, { status: 404 });
69
- const config = hubSessionConfig();
70
- if (config === null) return seeOther(accountPath("en", "error=unavailable"));
71
- if (!isSameOrigin(request, config.siteOrigin)) {
72
- return new Response(null, { status: 403 });
73
- }
74
- const refused = seeOther(accountPath("en", "error=claim"));
75
- if (!takeVisitorLimit("hub-session-open", request.headers, VISITOR_LIMIT, VISITOR_WINDOW_MS).allowed) {
76
- return refused;
77
- }
78
- if ((await runBotCheck({ source: "hub-session", target: "open", scope: "write" })) !== "pass") {
79
- return refused;
80
- }
81
-
82
- const fields = await readFormFields(request);
83
- const raw = fields === null ? null : formBody(fields);
84
- const parsed = raw === null ? null : spokeOpenFormSchema.safeParse(raw);
85
- if (parsed === null || !parsed.success) {
86
- return new Response(null, { status: 400, headers: { "Cache-Control": "no-store" } });
87
- }
88
-
89
- const flow = startHubFlow(config, HUB_SESSION_FALLBACK_PATH);
90
- const answer = await postToHub(config, HUB_PURCHASE_PATH, {
91
- claim: parsed.data.claim,
92
- client_id: config.clientId,
93
- redirect_uri: config.redirectUri,
94
- state: flow.state,
95
- code_challenge: flow.challenge,
96
- code_challenge_method: "S256",
97
- });
98
-
99
- if (answer?.status === 200) {
100
- const body = hubPurchaseResponseSchema.safeParse(answer.body);
101
- const verifyUrl = body.success ? hubUrl(body.data.verifyUrl, config.hubOrigin) : null;
102
- if (verifyUrl !== null) return seeOther(verifyUrl, [flow.cookie]);
103
- }
104
- if (answer?.status === 409 && hubError(answer) === "account_exists") {
105
- return seeOther(accountPath("en", "error=exists"));
106
- }
107
- console.warn(
108
- `[hub-session] purchase claim not opened status=${answer === null ? "unreachable" : answer.status} reason=${hubError(answer) ?? "none"}`,
109
- );
110
- return refused;
111
- }
@@ -1,69 +0,0 @@
1
- /**
2
- * Where the hub's set-password page returns a spoke's person (#11162) —
3
- * `GET /api/hub-session/password/return?next=<path>`.
4
- *
5
- * Saving a password on the hub ends every session and every spoke
6
- * credential the person held (`onPasswordReset`, SEC-7894-5) — the Security
7
- * tab's own copy says so. The sealed cookie this spoke keeps therefore
8
- * names a credential the hub no longer honours, and a page that read it as
9
- * signed in would fail closed on the hub's refusal instead of showing the
10
- * tab. So the hub's page lands here first:
11
- *
12
- * 1. 404 unless `SIGN_IN_PROVIDER` is `"hub"`; not configured → 303 home.
13
- * 2. `next` is the sanitized same-origin page path (`hubNextPath`), else
14
- * `/account`.
15
- * 3. With a session cookie, the hub is asked once whether the credential
16
- * still stands (`GET /v1/hub-sign-in/me`). A 401 — the one status the
17
- * hub answers a credential it no longer honours — clears the cookie, so
18
- * the gate on `next` shows this spoke's own sign-in form and the person
19
- * comes back to the tab with a fresh credential; a credential the hub
20
- * still honours, no cookie, or a hub that cannot answer (a limit, an
21
- * outage, a thrown read) leaves the cookie as it is.
22
- * 4. 303 to `next`, always.
23
- *
24
- * A GET, reachable cross-site: it changes nothing an attacker could not
25
- * already cause — the one write is to drop a cookie the hub has ALREADY
26
- * refused, and no credential is revoked here. Nothing is logged but a
27
- * status; the credential never is.
28
- */
29
- import { hubSignIn } from "@working-theory/api-client";
30
-
31
- import { hubSessionConfig } from "../../../../../lib/hub-session/config";
32
- import {
33
- cookieHeader,
34
- HUB_SESSION_HOME_PATH,
35
- hubNextPath,
36
- readHubSessionPayload,
37
- seeOther,
38
- sessionCookieName,
39
- } from "../../../../../lib/hub-session/session";
40
- import { SIGN_IN_PROVIDER } from "../../../../../lib/topology.generated";
41
-
42
- /**
43
- * Whether the hub still honours the credential: `false` only on its 401,
44
- * `null` when it could not answer — any other status, or a thrown read.
45
- */
46
- async function credentialStands(accessToken: string): Promise<boolean | null> {
47
- try {
48
- const res = await hubSignIn.me({ headers: { authorization: `Bearer ${accessToken}` } });
49
- if (res.ok) return true;
50
- return res.status === 401 ? false : null;
51
- } catch {
52
- return null;
53
- }
54
- }
55
-
56
- // platform-decisions-waiver: api-versioning — a sign-in step of the spoke's own form, not a versioned API contract (spec 11096 §6)
57
- export async function GET(request: Request): Promise<Response> {
58
- if (SIGN_IN_PROVIDER !== "hub") return new Response(null, { status: 404 });
59
- const config = hubSessionConfig();
60
- if (config === null) return seeOther(HUB_SESSION_HOME_PATH);
61
-
62
- const next = hubNextPath(new URL(request.url).searchParams.get("next"));
63
- const session = readHubSessionPayload(request.headers);
64
- if (session === null) return seeOther(next);
65
-
66
- const stands = await credentialStands(session.accessToken);
67
- if (stands !== false) return seeOther(next);
68
- return seeOther(next, [cookieHeader(sessionCookieName(config), "", 0, config)]);
69
- }