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.
- package/dist/add-route.js +1 -1
- package/dist/derive-architecture.js +3 -1
- package/dist/remove-module.js +18 -14
- package/dist/routes.d.ts +29 -2
- package/dist/routes.js +269 -23
- package/dist/site-manifest.js +6 -4
- package/dist/site-roster.d.ts +11 -3
- package/dist/site-roster.js +42 -22
- package/dist/surfaces.d.ts +11 -3
- package/dist/surfaces.js +69 -22
- package/dist/sync-routes.js +22 -5
- package/dist/template-record.js +3 -1
- package/dist/topology.d.ts +11 -0
- package/dist/topology.js +253 -3
- package/package.json +3 -3
- package/templates/apps/web/CLAUDE.md +26 -4
- package/templates/apps/web/__tests__/__mocks__/next-intl-server.ts +5 -1
- package/templates/apps/web/__tests__/__mocks__/next-intl.ts +5 -1
- package/templates/apps/web/__tests__/auth/seam.test.ts +34 -0
- package/templates/apps/web/account.config.tsx +2 -3
- package/templates/apps/web/app/[locale]/(authed)/account/connect/__tests__/page.test.tsx +1 -1
- package/templates/apps/web/app/[locale]/(authed)/account/connect/page.tsx +1 -1
- package/templates/apps/web/app/[locale]/(authed)/signed-out-gate.generated.tsx +1 -1
- package/templates/apps/web/app/[locale]/layout.tsx +1 -1
- package/templates/apps/web/auth.config.tsx +107 -0
- package/templates/apps/web/auth.ts +2 -1
- package/templates/apps/web/i18n/request.ts +9 -2
- package/templates/apps/web/jest.config.cjs +4 -0
- package/templates/apps/web/lib/__tests__/fixtures/site-topology.ts +3 -0
- package/templates/apps/web/lib/__tests__/server-api.test.ts +1 -1
- package/templates/apps/web/lib/server-api.ts +1 -1
- package/templates/apps/web/lib/topology.generated.ts +5 -0
- package/templates/apps/web/messages/en.json +0 -152
- package/templates/apps/web/middleware.ts +12 -25
- package/templates/apps/web/next.config.mjs +8 -0
- package/templates/apps/web/package.json +1 -0
- package/templates/apps/web/types/messages.d.ts +6 -1
- package/versions.json +49 -48
- package/templates/apps/web/app/[locale]/(auth)/__tests__/auth-pages.test.tsx +0 -322
- package/templates/apps/web/app/[locale]/(auth)/__tests__/password-reset.test.tsx +0 -554
- package/templates/apps/web/app/[locale]/(auth)/consent/__tests__/consent-actions.test.ts +0 -56
- package/templates/apps/web/app/[locale]/(auth)/consent/__tests__/page.test.tsx +0 -164
- package/templates/apps/web/app/[locale]/(auth)/consent/consent-form.tsx +0 -132
- package/templates/apps/web/app/[locale]/(auth)/consent/consent.actions.ts +0 -51
- package/templates/apps/web/app/[locale]/(auth)/consent/page.tsx +0 -144
- package/templates/apps/web/app/[locale]/(auth)/forgot-password/forgot-password-card.tsx +0 -101
- package/templates/apps/web/app/[locale]/(auth)/forgot-password/page.tsx +0 -111
- package/templates/apps/web/app/[locale]/(auth)/layout.tsx +0 -81
- package/templates/apps/web/app/[locale]/(auth)/legal-footnote.tsx +0 -35
- package/templates/apps/web/app/[locale]/(auth)/reset-password/__tests__/return.test.tsx +0 -179
- package/templates/apps/web/app/[locale]/(auth)/reset-password/page.tsx +0 -157
- package/templates/apps/web/app/[locale]/(auth)/reset-password/reset-password-card.tsx +0 -126
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/next-path.test.ts +0 -54
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/page.test.tsx +0 -628
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/remembered-method.test.ts +0 -43
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/sign-in-card.test.tsx +0 -266
- package/templates/apps/web/app/[locale]/(auth)/sign-in/next-path.ts +0 -20
- package/templates/apps/web/app/[locale]/(auth)/sign-in/page.tsx +0 -234
- package/templates/apps/web/app/[locale]/(auth)/sign-in/remembered-method.ts +0 -37
- package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-card.tsx +0 -323
- package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-text.ts +0 -93
- package/templates/apps/web/app/[locale]/(auth)/sign-up/__tests__/page.test.tsx +0 -301
- package/templates/apps/web/app/[locale]/(auth)/sign-up/__tests__/sign-up-card.test.tsx +0 -309
- package/templates/apps/web/app/[locale]/(auth)/sign-up/page.tsx +0 -201
- package/templates/apps/web/app/[locale]/(auth)/sign-up/sign-up-card.tsx +0 -366
- package/templates/apps/web/app/[locale]/(auth)/use-auth-card-flow.ts +0 -116
- package/templates/apps/web/app/[locale]/(auth)/verify-email/__tests__/actions.test.ts +0 -94
- package/templates/apps/web/app/[locale]/(auth)/verify-email/actions.ts +0 -77
- package/templates/apps/web/app/[locale]/(auth)/verify-email/page.tsx +0 -154
- package/templates/apps/web/app/[locale]/(authed)/account/appointments/[slug]/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/appointments/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/[pieceId]/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/[pieceId]/versions/[version]/file/route.ts +0 -2
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/billing/page.tsx +0 -12
- package/templates/apps/web/app/[locale]/(authed)/account/general/page.tsx +0 -12
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/[kind]/[id]/page.tsx +0 -12
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/message/[id]/file/route.ts +0 -2
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/thread/page.tsx +0 -12
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/layout.tsx +0 -10
- package/templates/apps/web/app/[locale]/(authed)/account/notifications/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/onboarding/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/page.tsx +0 -12
- package/templates/apps/web/app/[locale]/(authed)/account/privacy/export/route.ts +0 -2
- package/templates/apps/web/app/[locale]/(authed)/account/privacy/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/security/page.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/settings/page.tsx +0 -2
- package/templates/apps/web/app/[locale]/(authed)/account/support/page.tsx +0 -3
- package/templates/apps/web/app/api/auth/[...all]/route.ts +0 -16
- package/templates/apps/web/app/api/auth/hub/__tests__/continuation.test.ts +0 -143
- package/templates/apps/web/app/api/auth/hub/__tests__/entry-routes.test.ts +0 -749
- package/templates/apps/web/app/api/auth/hub/authorize/__tests__/route.test.ts +0 -964
- package/templates/apps/web/app/api/auth/hub/authorize/route.ts +0 -390
- package/templates/apps/web/app/api/auth/hub/continuation.ts +0 -127
- package/templates/apps/web/app/api/auth/hub/continue/[ref]/__tests__/route.test.ts +0 -188
- package/templates/apps/web/app/api/auth/hub/continue/[ref]/route.ts +0 -53
- package/templates/apps/web/app/api/auth/hub/email/route.ts +0 -111
- package/templates/apps/web/app/api/auth/hub/entry-shared.ts +0 -163
- package/templates/apps/web/app/api/auth/hub/password/__tests__/return.test.ts +0 -86
- package/templates/apps/web/app/api/auth/hub/password/__tests__/route.test.ts +0 -368
- package/templates/apps/web/app/api/auth/hub/password/return.ts +0 -86
- package/templates/apps/web/app/api/auth/hub/password/route.ts +0 -171
- package/templates/apps/web/app/api/auth/hub/purchase/route.ts +0 -230
- package/templates/apps/web/app/api/auth/hub/purchase-link/route.ts +0 -174
- package/templates/apps/web/app/api/auth/hub/sign-up/route.ts +0 -60
- package/templates/apps/web/app/api/auth/hub/sign-up-evidence.ts +0 -97
- package/templates/apps/web/app/api/auth/mcp/__tests__/continuation.test.ts +0 -92
- package/templates/apps/web/app/api/auth/mcp/continuation.ts +0 -89
- package/templates/apps/web/app/api/auth/mcp/continue/[ref]/__tests__/route.test.ts +0 -59
- package/templates/apps/web/app/api/auth/mcp/continue/[ref]/route.ts +0 -41
- package/templates/apps/web/app/api/hub-session/__tests__/routes.test.ts +0 -1166
- package/templates/apps/web/app/api/hub-session/callback/route.ts +0 -134
- package/templates/apps/web/app/api/hub-session/email/route.ts +0 -125
- package/templates/apps/web/app/api/hub-session/one-tap/nonce/route.ts +0 -49
- package/templates/apps/web/app/api/hub-session/one-tap/route.ts +0 -106
- package/templates/apps/web/app/api/hub-session/open/route.ts +0 -111
- package/templates/apps/web/app/api/hub-session/password/return/route.ts +0 -69
- package/templates/apps/web/app/api/hub-session/password/route.ts +0 -106
- package/templates/apps/web/app/api/hub-session/sign-in/route.ts +0 -91
- package/templates/apps/web/app/api/hub-session/sign-out/route.ts +0 -54
- package/templates/apps/web/components/sign-out-trigger.tsx +0 -62
- package/templates/apps/web/lib/__tests__/auth-routes.test.ts +0 -140
- package/templates/apps/web/lib/auth-routes.ts +0 -125
- package/templates/apps/web/lib/hub-session/__tests__/hub-session.test.ts +0 -433
- package/templates/apps/web/lib/hub-session/__tests__/spoke-sign-in.test.tsx +0 -332
- package/templates/apps/web/lib/hub-session/config.ts +0 -110
- package/templates/apps/web/lib/hub-session/entry.ts +0 -238
- package/templates/apps/web/lib/hub-session/index.ts +0 -21
- package/templates/apps/web/lib/hub-session/one-tap.tsx +0 -148
- package/templates/apps/web/lib/hub-session/reseal.ts +0 -90
- package/templates/apps/web/lib/hub-session/seal.ts +0 -86
- package/templates/apps/web/lib/hub-session/session.ts +0 -298
- package/templates/apps/web/lib/hub-session/spoke-sign-in.tsx +0 -277
- package/templates/apps/web/lib/next-path.ts +0 -35
|
@@ -1,749 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment node
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The hub web's spoke-facing entry routes (#11096 D2, D3, D9; AC-1, AC-2,
|
|
7
|
-
* AC-4, AC-6).
|
|
8
|
-
*
|
|
9
|
-
* POST /api/auth/hub/email a spoke asks for the hub's magic link
|
|
10
|
-
* GET /api/auth/hub/sign-up the agreements a spoke's form shows
|
|
11
|
-
* POST /api/auth/hub/purchase-link the hub API's signed purchase receiver
|
|
12
|
-
* POST /api/auth/hub/purchase a spoke spends a purchase claim
|
|
13
|
-
*
|
|
14
|
-
* Better Auth (`auth.api.signInMagicLink`, the internal adapter) and
|
|
15
|
-
* `@working-theory/api-client` (the hub API's entry check, client info, the
|
|
16
|
-
* sign-up evidence and the mail relay) are the external boundaries; both are
|
|
17
|
-
* doubles. The routes' own decisions are under test: what each accepts, in
|
|
18
|
-
* what order it refuses, what it hands Better Auth, and what it sends.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import { createHash, createHmac } from "node:crypto";
|
|
22
|
-
|
|
23
|
-
// --- Mocks set up BEFORE imports ---
|
|
24
|
-
|
|
25
|
-
const mockSecret = "k".repeat(43);
|
|
26
|
-
const mockSignInMagicLink = jest.fn();
|
|
27
|
-
const mockFindUserByEmail = jest.fn();
|
|
28
|
-
const mockFindVerificationValue = jest.fn();
|
|
29
|
-
const mockCreateVerificationValue = jest.fn();
|
|
30
|
-
const mockDeleteVerificationByIdentifier = jest.fn();
|
|
31
|
-
// eslint-disable-next-line no-restricted-syntax -- auth.ts is the Better Auth singleton (its adapter needs a live database); this stand-in for its server calls and internal adapter is the repo-standard double for that boundary (see authorize/__tests__/route.test.ts).
|
|
32
|
-
jest.mock("../../../../../auth", () => ({
|
|
33
|
-
__esModule: true,
|
|
34
|
-
auth: {
|
|
35
|
-
api: {
|
|
36
|
-
signInMagicLink: (...args: unknown[]) => mockSignInMagicLink(...args),
|
|
37
|
-
},
|
|
38
|
-
$context: Promise.resolve({
|
|
39
|
-
internalAdapter: {
|
|
40
|
-
findUserByEmail: (...args: unknown[]) => mockFindUserByEmail(...args),
|
|
41
|
-
findVerificationValue: (...args: unknown[]) =>
|
|
42
|
-
mockFindVerificationValue(...args),
|
|
43
|
-
createVerificationValue: (...args: unknown[]) =>
|
|
44
|
-
mockCreateVerificationValue(...args),
|
|
45
|
-
deleteVerificationByIdentifier: (...args: unknown[]) =>
|
|
46
|
-
mockDeleteVerificationByIdentifier(...args),
|
|
47
|
-
},
|
|
48
|
-
}),
|
|
49
|
-
},
|
|
50
|
-
resolveSecret: () => mockSecret,
|
|
51
|
-
}));
|
|
52
|
-
|
|
53
|
-
const mockEntry = jest.fn();
|
|
54
|
-
const mockClientInfo = jest.fn();
|
|
55
|
-
const mockSignUpEvidence = jest.fn();
|
|
56
|
-
const mockMailSend = jest.fn();
|
|
57
|
-
jest.mock("@working-theory/api-client", () => ({
|
|
58
|
-
__esModule: true,
|
|
59
|
-
hubSignIn: {
|
|
60
|
-
entry: (...args: unknown[]) => mockEntry(...args),
|
|
61
|
-
clientInfo: (...args: unknown[]) => mockClientInfo(...args),
|
|
62
|
-
},
|
|
63
|
-
consent: {
|
|
64
|
-
signUpEvidence: (...args: unknown[]) => mockSignUpEvidence(...args),
|
|
65
|
-
},
|
|
66
|
-
authEmail: { send: (...args: unknown[]) => mockMailSend(...args) },
|
|
67
|
-
}));
|
|
68
|
-
|
|
69
|
-
import { mintContinuation, openContinuation } from "../continuation";
|
|
70
|
-
import { POST as emailPOST } from "../email/route";
|
|
71
|
-
import { __resetRefusedCallers } from "../entry-shared";
|
|
72
|
-
import { POST as purchaseLinkPOST } from "../purchase-link/route";
|
|
73
|
-
import { POST as purchasePOST } from "../purchase/route";
|
|
74
|
-
import { GET as signUpGET } from "../sign-up/route";
|
|
75
|
-
import { resetHubSignUpEvidenceCache } from "../sign-up-evidence";
|
|
76
|
-
|
|
77
|
-
const HUB = "https://hub.example";
|
|
78
|
-
const SPOKE = "https://spoke.example";
|
|
79
|
-
const CALLBACK = `${SPOKE}/api/hub-session/callback`;
|
|
80
|
-
const CONTINUE = "/api/auth/hub/continue";
|
|
81
|
-
const WEBHOOK_SECRET = "purchase-webhook-secret";
|
|
82
|
-
const BASIC = "Basic c3Bva2VfYWJjOnNlY3JldA==";
|
|
83
|
-
const SPOKE_KEY = "ailk_spk_spoke";
|
|
84
|
-
const SESSION_ID = "cs_live_abc123";
|
|
85
|
-
|
|
86
|
-
const AUTHORIZE = {
|
|
87
|
-
client_id: "spoke_abc",
|
|
88
|
-
redirect_uri: CALLBACK,
|
|
89
|
-
state: "s".repeat(43),
|
|
90
|
-
code_challenge: "c".repeat(43),
|
|
91
|
-
code_challenge_method: "S256",
|
|
92
|
-
};
|
|
93
|
-
const SIX_KEYS = new URLSearchParams({
|
|
94
|
-
response_type: "code",
|
|
95
|
-
...AUTHORIZE,
|
|
96
|
-
}).toString();
|
|
97
|
-
|
|
98
|
-
const ACCOUNT_TERMS = {
|
|
99
|
-
purposeId: "account_terms",
|
|
100
|
-
policyVersion: 1,
|
|
101
|
-
locale: "en",
|
|
102
|
-
statement:
|
|
103
|
-
"I accept the [Terms](terms), [Privacy Policy](privacy), and [Working Agreement](working_agreement).",
|
|
104
|
-
statementHash: "a".repeat(64),
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
function evidence(agreements: unknown[] = [ACCOUNT_TERMS]) {
|
|
108
|
-
return { ok: true, body: { workspaceId: "ws_hub", agreements } };
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const ORIGINAL_ENV = process.env;
|
|
112
|
-
|
|
113
|
-
beforeEach(() => {
|
|
114
|
-
jest.resetAllMocks();
|
|
115
|
-
resetHubSignUpEvidenceCache();
|
|
116
|
-
__resetRefusedCallers();
|
|
117
|
-
process.env = {
|
|
118
|
-
...ORIGINAL_ENV,
|
|
119
|
-
BETTER_AUTH_URL: `${HUB}/api/auth`,
|
|
120
|
-
AUTH_EMAIL_WEBHOOK_SECRET: WEBHOOK_SECRET,
|
|
121
|
-
AILK_AUTH_SIGNUP_OPEN: "1",
|
|
122
|
-
NEXT_PUBLIC_ENABLED_LOCALES: "en",
|
|
123
|
-
};
|
|
124
|
-
delete process.env.GOOGLE_CLIENT_ID;
|
|
125
|
-
delete process.env.GOOGLE_CLIENT_SECRET;
|
|
126
|
-
mockEntry.mockResolvedValue({ ok: true, body: { clientId: "spoke_abc" } });
|
|
127
|
-
mockSignInMagicLink.mockResolvedValue({ status: true });
|
|
128
|
-
mockSignUpEvidence.mockResolvedValue(evidence());
|
|
129
|
-
mockClientInfo.mockResolvedValue({
|
|
130
|
-
ok: true,
|
|
131
|
-
body: { clientId: "spoke_abc", name: "Spoke", origin: SPOKE },
|
|
132
|
-
});
|
|
133
|
-
mockFindVerificationValue.mockResolvedValue(null);
|
|
134
|
-
mockCreateVerificationValue.mockResolvedValue({});
|
|
135
|
-
mockDeleteVerificationByIdentifier.mockResolvedValue(undefined);
|
|
136
|
-
mockFindUserByEmail.mockResolvedValue(null);
|
|
137
|
-
mockMailSend.mockResolvedValue({ ok: true, status: 202 });
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
afterAll(() => {
|
|
141
|
-
process.env = ORIGINAL_ENV;
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// ── POST /api/auth/hub/email (D2) ───────────────────────────────────────────
|
|
145
|
-
|
|
146
|
-
function emailRequest(
|
|
147
|
-
body: unknown,
|
|
148
|
-
headers: Record<string, string> = {
|
|
149
|
-
authorization: BASIC,
|
|
150
|
-
"x-ailk-spoke-key": SPOKE_KEY,
|
|
151
|
-
},
|
|
152
|
-
): Request {
|
|
153
|
-
return new Request(`${HUB}/api/auth/hub/email`, {
|
|
154
|
-
method: "POST",
|
|
155
|
-
headers: { "content-type": "application/json", ...headers },
|
|
156
|
-
body: typeof body === "string" ? body : JSON.stringify(body),
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const EMAIL_BODY = { ...AUTHORIZE, email: "pat@example.com", locale: "en" };
|
|
161
|
-
|
|
162
|
-
describe("POST /api/auth/hub/email (D2)", () => {
|
|
163
|
-
it("AC-1: asks the API's entry check with the spoke's two headers, then sends the link to the sealed continue path", async () => {
|
|
164
|
-
const res = await emailPOST(emailRequest(EMAIL_BODY));
|
|
165
|
-
|
|
166
|
-
expect(res.status).toBe(202);
|
|
167
|
-
expect(await res.json()).toEqual({ sent: true });
|
|
168
|
-
expect(mockEntry).toHaveBeenCalledWith(
|
|
169
|
-
{ authorization: BASIC, spokeKey: SPOKE_KEY },
|
|
170
|
-
{
|
|
171
|
-
clientId: "spoke_abc",
|
|
172
|
-
redirectUri: CALLBACK,
|
|
173
|
-
email: "pat@example.com",
|
|
174
|
-
},
|
|
175
|
-
);
|
|
176
|
-
expect(mockSignInMagicLink).toHaveBeenCalledTimes(1);
|
|
177
|
-
const [call] = mockSignInMagicLink.mock.calls[0] as [
|
|
178
|
-
{ body: Record<string, unknown>; headers: Headers },
|
|
179
|
-
];
|
|
180
|
-
const callbackURL = String(call.body.callbackURL);
|
|
181
|
-
expect(callbackURL.startsWith(`${CONTINUE}/`)).toBe(true);
|
|
182
|
-
expect(
|
|
183
|
-
openContinuation(callbackURL.slice(`${CONTINUE}/`.length), mockSecret),
|
|
184
|
-
).toBe(SIX_KEYS);
|
|
185
|
-
// No agreements ticked: a sign-in request, no metadata.
|
|
186
|
-
expect(call.body).toEqual({ email: "pat@example.com", callbackURL });
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it("SEC-11096-12: Better Auth gets only the pinned Origin — never the spoke's Authorization or key", async () => {
|
|
190
|
-
await emailPOST(emailRequest(EMAIL_BODY));
|
|
191
|
-
const [call] = mockSignInMagicLink.mock.calls[0] as [{ headers: Headers }];
|
|
192
|
-
expect([...call.headers.entries()]).toEqual([["origin", HUB]]);
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it("AC-2: ticked agreements reach Better Auth as metadata with the locale, unchanged", async () => {
|
|
196
|
-
await emailPOST(
|
|
197
|
-
emailRequest({ ...EMAIL_BODY, agreements: ["account_terms"] }),
|
|
198
|
-
);
|
|
199
|
-
const [call] = mockSignInMagicLink.mock.calls[0] as [
|
|
200
|
-
{ body: Record<string, unknown> },
|
|
201
|
-
];
|
|
202
|
-
expect(call.body.metadata).toEqual({
|
|
203
|
-
agreements: ["account_terms"],
|
|
204
|
-
locale: "en",
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it("AC-1: the API's 401 is passed on as 401 and nothing is sent", async () => {
|
|
209
|
-
mockEntry.mockResolvedValue({
|
|
210
|
-
ok: false,
|
|
211
|
-
status: 401,
|
|
212
|
-
body: { ok: false, error: { code: "invalid_client" } },
|
|
213
|
-
});
|
|
214
|
-
const res = await emailPOST(emailRequest(EMAIL_BODY));
|
|
215
|
-
expect(res.status).toBe(401);
|
|
216
|
-
expect(await res.json()).toEqual({ error: "invalid_client" });
|
|
217
|
-
expect(mockSignInMagicLink).not.toHaveBeenCalled();
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
it("the API's 429 is passed on with its error", async () => {
|
|
221
|
-
mockEntry.mockResolvedValue({
|
|
222
|
-
ok: false,
|
|
223
|
-
status: 429,
|
|
224
|
-
body: { ok: false, error: { code: "rate_limited" } },
|
|
225
|
-
});
|
|
226
|
-
const res = await emailPOST(emailRequest(EMAIL_BODY));
|
|
227
|
-
expect(res.status).toBe(429);
|
|
228
|
-
expect(mockSignInMagicLink).not.toHaveBeenCalled();
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it("a request without the spoke's two headers is 401 with no API call", async () => {
|
|
232
|
-
const res = await emailPOST(emailRequest(EMAIL_BODY, {}));
|
|
233
|
-
expect(res.status).toBe(401);
|
|
234
|
-
expect(mockEntry).not.toHaveBeenCalled();
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it.each([
|
|
238
|
-
["a Bearer authorization", { authorization: "Bearer x", "x-ailk-spoke-key": SPOKE_KEY }],
|
|
239
|
-
["a Basic header with no credentials", { authorization: "Basic ", "x-ailk-spoke-key": SPOKE_KEY }],
|
|
240
|
-
["a Basic header that is not base64", { authorization: "Basic a b", "x-ailk-spoke-key": SPOKE_KEY }],
|
|
241
|
-
])("%s is 401 invalid_client with no API call", async (_name, headers) => {
|
|
242
|
-
const res = await emailPOST(emailRequest(EMAIL_BODY, headers));
|
|
243
|
-
expect(res.status).toBe(401);
|
|
244
|
-
expect(await res.json()).toEqual({ error: "invalid_client" });
|
|
245
|
-
expect(mockEntry).not.toHaveBeenCalled();
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it("a caller the API refused ten times in ten minutes is 429 before any API call; another caller is not", async () => {
|
|
249
|
-
process.env.VERCEL = "1";
|
|
250
|
-
mockEntry.mockResolvedValue({
|
|
251
|
-
ok: false,
|
|
252
|
-
status: 401,
|
|
253
|
-
body: { ok: false, error: { code: "invalid_client" } },
|
|
254
|
-
});
|
|
255
|
-
const from = (ip: string): Request =>
|
|
256
|
-
emailRequest(EMAIL_BODY, {
|
|
257
|
-
authorization: BASIC,
|
|
258
|
-
"x-ailk-spoke-key": SPOKE_KEY,
|
|
259
|
-
"x-vercel-forwarded-for": ip,
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
for (let i = 0; i < 10; i += 1) {
|
|
263
|
-
expect((await emailPOST(from("203.0.113.9"))).status).toBe(401);
|
|
264
|
-
}
|
|
265
|
-
const limited = await emailPOST(from("203.0.113.9"));
|
|
266
|
-
expect(limited.status).toBe(429);
|
|
267
|
-
expect(await limited.json()).toEqual({ error: "rate_limited" });
|
|
268
|
-
expect(Number(limited.headers.get("retry-after"))).toBeGreaterThan(0);
|
|
269
|
-
expect(mockEntry).toHaveBeenCalledTimes(10);
|
|
270
|
-
|
|
271
|
-
mockEntry.mockResolvedValue({ ok: true, body: { clientId: "spoke_abc" } });
|
|
272
|
-
expect((await emailPOST(from("198.51.100.4"))).status).toBe(202);
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
it("a spoke the API accepts is never limited by its own successes", async () => {
|
|
276
|
-
process.env.VERCEL = "1";
|
|
277
|
-
for (let i = 0; i < 15; i += 1) {
|
|
278
|
-
const res = await emailPOST(
|
|
279
|
-
emailRequest(EMAIL_BODY, {
|
|
280
|
-
authorization: BASIC,
|
|
281
|
-
"x-ailk-spoke-key": SPOKE_KEY,
|
|
282
|
-
"x-vercel-forwarded-for": "203.0.113.20",
|
|
283
|
-
}),
|
|
284
|
-
);
|
|
285
|
-
expect(res.status).toBe(202);
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
it("AC-2: AGREEMENTS_REQUIRED from the plugin is 400 agreements_required", async () => {
|
|
290
|
-
mockSignInMagicLink.mockRejectedValue({
|
|
291
|
-
statusCode: 400,
|
|
292
|
-
body: { code: "AGREEMENTS_REQUIRED" },
|
|
293
|
-
});
|
|
294
|
-
const res = await emailPOST(
|
|
295
|
-
emailRequest({ ...EMAIL_BODY, agreements: ["account_terms"] }),
|
|
296
|
-
);
|
|
297
|
-
expect(res.status).toBe(400);
|
|
298
|
-
expect(await res.json()).toEqual({ error: "agreements_required" });
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
it("SIGN_UP_CLOSED is 400 sign_up_closed; any other throw is 503", async () => {
|
|
302
|
-
mockSignInMagicLink.mockRejectedValueOnce({
|
|
303
|
-
body: { code: "SIGN_UP_CLOSED" },
|
|
304
|
-
});
|
|
305
|
-
const closed = await emailPOST(
|
|
306
|
-
emailRequest({ ...EMAIL_BODY, agreements: ["account_terms"] }),
|
|
307
|
-
);
|
|
308
|
-
expect(closed.status).toBe(400);
|
|
309
|
-
expect(await closed.json()).toEqual({ error: "sign_up_closed" });
|
|
310
|
-
|
|
311
|
-
mockSignInMagicLink.mockRejectedValueOnce(new Error("boom"));
|
|
312
|
-
const failed = await emailPOST(emailRequest(EMAIL_BODY));
|
|
313
|
-
expect(failed.status).toBe(503);
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
it("AC-6: an added password field is 400 invalid_request and no entry call", async () => {
|
|
317
|
-
const res = await emailPOST(
|
|
318
|
-
emailRequest({ ...EMAIL_BODY, password: "hunter22" }),
|
|
319
|
-
);
|
|
320
|
-
expect(res.status).toBe(400);
|
|
321
|
-
expect(await res.json()).toEqual({ error: "invalid_request" });
|
|
322
|
-
expect(mockEntry).not.toHaveBeenCalled();
|
|
323
|
-
expect(mockSignInMagicLink).not.toHaveBeenCalled();
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
it.each([
|
|
327
|
-
["not JSON", "{"],
|
|
328
|
-
["over 4 KB", JSON.stringify({ ...EMAIL_BODY, pad: "x".repeat(5000) })],
|
|
329
|
-
["a response_type key", { ...EMAIL_BODY, response_type: "code" }],
|
|
330
|
-
])("%s → 400, no call", async (_name, body) => {
|
|
331
|
-
const res = await emailPOST(emailRequest(body));
|
|
332
|
-
expect(res.status).toBe(400);
|
|
333
|
-
expect(mockEntry).not.toHaveBeenCalled();
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
it("503 with no pinned origin", async () => {
|
|
337
|
-
delete process.env.BETTER_AUTH_URL;
|
|
338
|
-
const res = await emailPOST(emailRequest(EMAIL_BODY));
|
|
339
|
-
expect(res.status).toBe(503);
|
|
340
|
-
expect(mockEntry).not.toHaveBeenCalled();
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
// ── GET /api/auth/hub/sign-up (D3) ──────────────────────────────────────────
|
|
345
|
-
|
|
346
|
-
describe("GET /api/auth/hub/sign-up (D3)", () => {
|
|
347
|
-
it("answers sign-up open, Google, and each agreement with its policy links absolute on the hub", async () => {
|
|
348
|
-
process.env.GOOGLE_CLIENT_ID = "g";
|
|
349
|
-
process.env.GOOGLE_CLIENT_SECRET = "g-secret";
|
|
350
|
-
|
|
351
|
-
const res = await signUpGET(
|
|
352
|
-
new Request(`${HUB}/api/auth/hub/sign-up?locale=en`),
|
|
353
|
-
);
|
|
354
|
-
|
|
355
|
-
expect(res.status).toBe(200);
|
|
356
|
-
expect(res.headers.get("cache-control")).toBe("public, max-age=60");
|
|
357
|
-
const body = (await res.json()) as {
|
|
358
|
-
signUpOpen: boolean;
|
|
359
|
-
google: boolean;
|
|
360
|
-
agreements: Array<{
|
|
361
|
-
purposeId: string;
|
|
362
|
-
statement: string;
|
|
363
|
-
policies: Record<string, string>;
|
|
364
|
-
}>;
|
|
365
|
-
};
|
|
366
|
-
expect(body.signUpOpen).toBe(true);
|
|
367
|
-
expect(body.google).toBe(true);
|
|
368
|
-
expect(body.agreements).toHaveLength(1);
|
|
369
|
-
expect(body.agreements[0]!.purposeId).toBe("account_terms");
|
|
370
|
-
expect(body.agreements[0]!.statement).toBe(ACCOUNT_TERMS.statement);
|
|
371
|
-
expect(body.agreements[0]!.policies.terms).toMatch(new RegExp(`^${HUB}/`));
|
|
372
|
-
expect(mockSignUpEvidence).toHaveBeenCalledWith({ locale: "en" });
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
it("Google off and sign-up closed are reported as such", async () => {
|
|
376
|
-
delete process.env.AILK_AUTH_SIGNUP_OPEN;
|
|
377
|
-
const res = await signUpGET(new Request(`${HUB}/api/auth/hub/sign-up`));
|
|
378
|
-
const body = (await res.json()) as { signUpOpen: boolean; google: boolean };
|
|
379
|
-
expect(body).toMatchObject({ signUpOpen: false, google: false });
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
it("an unreadable evidence read is a 503, never an empty agreement list", async () => {
|
|
383
|
-
mockSignUpEvidence.mockResolvedValue({
|
|
384
|
-
ok: false,
|
|
385
|
-
status: 503,
|
|
386
|
-
body: null,
|
|
387
|
-
});
|
|
388
|
-
const res = await signUpGET(new Request(`${HUB}/api/auth/hub/sign-up`));
|
|
389
|
-
expect(res.status).toBe(503);
|
|
390
|
-
expect(res.headers.get("cache-control")).toBe("no-store");
|
|
391
|
-
});
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
// ── POST /api/auth/hub/purchase-link (D9) ───────────────────────────────────
|
|
395
|
-
|
|
396
|
-
const LINK_BODY = {
|
|
397
|
-
email: "buyer@example.com",
|
|
398
|
-
clientId: "spoke_abc",
|
|
399
|
-
sessionId: SESSION_ID,
|
|
400
|
-
policyVersion: 1,
|
|
401
|
-
locale: "en",
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
function signedLink(
|
|
405
|
-
body: unknown,
|
|
406
|
-
opts: { secret?: string; ageSeconds?: number; signature?: string } = {},
|
|
407
|
-
): Request {
|
|
408
|
-
const raw = typeof body === "string" ? body : JSON.stringify(body);
|
|
409
|
-
const ts = String(Math.floor(Date.now() / 1000) - (opts.ageSeconds ?? 0));
|
|
410
|
-
const signature =
|
|
411
|
-
opts.signature ??
|
|
412
|
-
`sha256=${createHmac("sha256", opts.secret ?? WEBHOOK_SECRET)
|
|
413
|
-
.update(`v0:purchase-link:${ts}:${raw}`, "utf8")
|
|
414
|
-
.digest("hex")}`;
|
|
415
|
-
return new Request(`${HUB}/api/auth/hub/purchase-link`, {
|
|
416
|
-
method: "POST",
|
|
417
|
-
headers: {
|
|
418
|
-
"content-type": "application/json",
|
|
419
|
-
"x-ailk-purchase-timestamp": ts,
|
|
420
|
-
"x-ailk-purchase-signature": signature,
|
|
421
|
-
},
|
|
422
|
-
body: raw,
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/** The account-open sends the relay received. */
|
|
427
|
-
function accountOpenSends(): Array<Record<string, unknown>> {
|
|
428
|
-
return mockMailSend.mock.calls
|
|
429
|
-
.map(([raw]) => JSON.parse(String(raw)) as Record<string, unknown>)
|
|
430
|
-
.filter((body) => body.kind === "account-open");
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
describe("POST /api/auth/hub/purchase-link (D9)", () => {
|
|
434
|
-
it("AC-4: a valid signature → 202 and one account-open send to the spoke's /account?claim=", async () => {
|
|
435
|
-
const res = await purchaseLinkPOST(signedLink(LINK_BODY));
|
|
436
|
-
|
|
437
|
-
expect(res.status).toBe(202);
|
|
438
|
-
const sends = accountOpenSends();
|
|
439
|
-
expect(sends).toHaveLength(1);
|
|
440
|
-
expect(sends[0]!.to).toBe("buyer@example.com");
|
|
441
|
-
const url = new URL(String(sends[0]!.url));
|
|
442
|
-
expect(`${url.origin}${url.pathname}`).toBe(`${SPOKE}/account`);
|
|
443
|
-
const claim = url.searchParams.get("claim") ?? "";
|
|
444
|
-
// The claim opens only under its own name, never as a continuation.
|
|
445
|
-
expect(openContinuation(claim, mockSecret)).toBeNull();
|
|
446
|
-
const query = new URLSearchParams(
|
|
447
|
-
openContinuation(claim, mockSecret, Date.now(), "hub-purchase") ?? "",
|
|
448
|
-
);
|
|
449
|
-
expect(Object.fromEntries(query)).toEqual({
|
|
450
|
-
e: "buyer@example.com",
|
|
451
|
-
c: "spoke_abc",
|
|
452
|
-
s: SESSION_ID,
|
|
453
|
-
v: "1",
|
|
454
|
-
l: "en",
|
|
455
|
-
});
|
|
456
|
-
expect(mockCreateVerificationValue).toHaveBeenCalledWith(
|
|
457
|
-
expect.objectContaining({
|
|
458
|
-
identifier: `purchase-link:${createHash("sha256").update(SESSION_ID).digest("hex")}`,
|
|
459
|
-
}),
|
|
460
|
-
);
|
|
461
|
-
});
|
|
462
|
-
|
|
463
|
-
it("SEC-11096-13: the same body again → 202 and no second send", async () => {
|
|
464
|
-
mockFindVerificationValue.mockResolvedValue({
|
|
465
|
-
value: "1",
|
|
466
|
-
expiresAt: new Date(Date.now() + 60_000),
|
|
467
|
-
});
|
|
468
|
-
const res = await purchaseLinkPOST(signedLink(LINK_BODY));
|
|
469
|
-
expect(res.status).toBe(202);
|
|
470
|
-
expect(accountOpenSends()).toHaveLength(0);
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
it.each([
|
|
474
|
-
["a bad signature", () => signedLink(LINK_BODY, { secret: "other" })],
|
|
475
|
-
[
|
|
476
|
-
"a 301-second-old timestamp",
|
|
477
|
-
() => signedLink(LINK_BODY, { ageSeconds: 301 }),
|
|
478
|
-
],
|
|
479
|
-
["no signature", () => signedLink(LINK_BODY, { signature: "" })],
|
|
480
|
-
[
|
|
481
|
-
"an invalid body behind a bad signature",
|
|
482
|
-
() => signedLink("{not json", { secret: "other" }),
|
|
483
|
-
],
|
|
484
|
-
])("%s → 401, nothing sent or written", async (_name, build) => {
|
|
485
|
-
const res = await purchaseLinkPOST(build());
|
|
486
|
-
expect(res.status).toBe(401);
|
|
487
|
-
expect(accountOpenSends()).toHaveLength(0);
|
|
488
|
-
expect(mockCreateVerificationValue).not.toHaveBeenCalled();
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
it("a validly signed invalid body → 400", async () => {
|
|
492
|
-
const res = await purchaseLinkPOST(
|
|
493
|
-
signedLink({ ...LINK_BODY, sessionId: "not-a-session" }),
|
|
494
|
-
);
|
|
495
|
-
expect(res.status).toBe(400);
|
|
496
|
-
});
|
|
497
|
-
|
|
498
|
-
it("503 with no secret, before anything else", async () => {
|
|
499
|
-
delete process.env.AUTH_EMAIL_WEBHOOK_SECRET;
|
|
500
|
-
const res = await purchaseLinkPOST(signedLink(LINK_BODY, { secret: "" }));
|
|
501
|
-
expect(res.status).toBe(503);
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
it.each([
|
|
505
|
-
[
|
|
506
|
-
"evidence asking for two agreements",
|
|
507
|
-
[ACCOUNT_TERMS, { ...ACCOUNT_TERMS, purposeId: "newsletter" }],
|
|
508
|
-
],
|
|
509
|
-
[
|
|
510
|
-
"account_terms at another version",
|
|
511
|
-
[{ ...ACCOUNT_TERMS, policyVersion: 2 }],
|
|
512
|
-
],
|
|
513
|
-
])("%s → 202, nothing sent or written", async (_name, agreements) => {
|
|
514
|
-
mockSignUpEvidence.mockResolvedValue(evidence(agreements));
|
|
515
|
-
const res = await purchaseLinkPOST(signedLink(LINK_BODY));
|
|
516
|
-
expect(res.status).toBe(202);
|
|
517
|
-
expect(accountOpenSends()).toHaveLength(0);
|
|
518
|
-
expect(mockCreateVerificationValue).not.toHaveBeenCalled();
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
it("a client the registry does not know → 202, nothing sent or written", async () => {
|
|
522
|
-
mockClientInfo.mockResolvedValue({ ok: false, status: 404, body: null });
|
|
523
|
-
const res = await purchaseLinkPOST(signedLink(LINK_BODY));
|
|
524
|
-
expect(res.status).toBe(202);
|
|
525
|
-
expect(accountOpenSends()).toHaveLength(0);
|
|
526
|
-
expect(mockCreateVerificationValue).not.toHaveBeenCalled();
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
it("an evidence read that fails → 503, nothing sent or written, so the next try can send", async () => {
|
|
530
|
-
mockSignUpEvidence.mockResolvedValue({ ok: false, status: 502, body: null });
|
|
531
|
-
const res = await purchaseLinkPOST(signedLink(LINK_BODY));
|
|
532
|
-
expect(res.status).toBe(503);
|
|
533
|
-
expect(accountOpenSends()).toHaveLength(0);
|
|
534
|
-
expect(mockCreateVerificationValue).not.toHaveBeenCalled();
|
|
535
|
-
});
|
|
536
|
-
|
|
537
|
-
it("a send the relay refuses → 503 and the once-per-session marker is removed", async () => {
|
|
538
|
-
mockMailSend.mockResolvedValue({ ok: false, status: 500 });
|
|
539
|
-
const res = await purchaseLinkPOST(signedLink(LINK_BODY));
|
|
540
|
-
expect(res.status).toBe(503);
|
|
541
|
-
const identifier = `purchase-link:${createHash("sha256").update(SESSION_ID).digest("hex")}`;
|
|
542
|
-
expect(mockCreateVerificationValue).toHaveBeenCalledWith(
|
|
543
|
-
expect.objectContaining({ identifier }),
|
|
544
|
-
);
|
|
545
|
-
expect(mockDeleteVerificationByIdentifier).toHaveBeenCalledWith(identifier);
|
|
546
|
-
});
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
// ── POST /api/auth/hub/purchase (D9) ────────────────────────────────────────
|
|
550
|
-
|
|
551
|
-
function claimFor(
|
|
552
|
-
overrides: Partial<Record<"e" | "c" | "s" | "v" | "l", string>> = {},
|
|
553
|
-
now = Date.now(),
|
|
554
|
-
name = "hub-purchase",
|
|
555
|
-
): string {
|
|
556
|
-
const query = new URLSearchParams({
|
|
557
|
-
e: "buyer@example.com",
|
|
558
|
-
c: "spoke_abc",
|
|
559
|
-
s: SESSION_ID,
|
|
560
|
-
v: "1",
|
|
561
|
-
l: "en",
|
|
562
|
-
...overrides,
|
|
563
|
-
}).toString();
|
|
564
|
-
return mintContinuation(query, mockSecret, now, name, 72 * 3600);
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
function purchaseRequest(
|
|
568
|
-
body: unknown,
|
|
569
|
-
headers: Record<string, string> = {
|
|
570
|
-
authorization: BASIC,
|
|
571
|
-
"x-ailk-spoke-key": SPOKE_KEY,
|
|
572
|
-
},
|
|
573
|
-
): Request {
|
|
574
|
-
return new Request(`${HUB}/api/auth/hub/purchase`, {
|
|
575
|
-
method: "POST",
|
|
576
|
-
headers: { "content-type": "application/json", ...headers },
|
|
577
|
-
body: JSON.stringify(body),
|
|
578
|
-
});
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
describe("POST /api/auth/hub/purchase (D9)", () => {
|
|
582
|
-
beforeEach(() => {
|
|
583
|
-
// The plugin's capture: the token reaches the store, nothing is mailed.
|
|
584
|
-
mockSignInMagicLink.mockImplementation(async () => {
|
|
585
|
-
const { purchaseCapture } = jest.requireActual<
|
|
586
|
-
typeof import("../../../../../lib/auth-email")
|
|
587
|
-
>("../../../../../lib/auth-email");
|
|
588
|
-
const store = purchaseCapture.getStore();
|
|
589
|
-
if (store) store.token = "CAPTURED_TOKEN";
|
|
590
|
-
return { status: true };
|
|
591
|
-
});
|
|
592
|
-
});
|
|
593
|
-
|
|
594
|
-
it("AC-4: an authenticated spoke and a fresh claim for a new address → 200 verifyUrl on the pinned origin; nothing mailed", async () => {
|
|
595
|
-
const res = await purchasePOST(
|
|
596
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
597
|
-
);
|
|
598
|
-
|
|
599
|
-
expect(res.status).toBe(200);
|
|
600
|
-
expect(res.headers.get("cache-control")).toBe("no-store");
|
|
601
|
-
const { verifyUrl } = (await res.json()) as { verifyUrl: string };
|
|
602
|
-
const url = new URL(verifyUrl);
|
|
603
|
-
expect(`${url.origin}${url.pathname}`).toBe(
|
|
604
|
-
`${HUB}/api/auth/magic-link/verify`,
|
|
605
|
-
);
|
|
606
|
-
expect(url.searchParams.get("token")).toBe("CAPTURED_TOKEN");
|
|
607
|
-
const callbackURL = url.searchParams.get("callbackURL") ?? "";
|
|
608
|
-
expect(
|
|
609
|
-
openContinuation(callbackURL.slice(`${CONTINUE}/`.length), mockSecret),
|
|
610
|
-
).toBe(SIX_KEYS);
|
|
611
|
-
|
|
612
|
-
expect(mockEntry).toHaveBeenCalledWith(
|
|
613
|
-
{ authorization: BASIC, spokeKey: SPOKE_KEY },
|
|
614
|
-
{
|
|
615
|
-
clientId: "spoke_abc",
|
|
616
|
-
redirectUri: CALLBACK,
|
|
617
|
-
email: "buyer@example.com",
|
|
618
|
-
},
|
|
619
|
-
);
|
|
620
|
-
const [call] = mockSignInMagicLink.mock.calls[0] as [
|
|
621
|
-
{ body: Record<string, unknown>; headers: Headers },
|
|
622
|
-
];
|
|
623
|
-
expect(call.body).toEqual({
|
|
624
|
-
email: "buyer@example.com",
|
|
625
|
-
callbackURL,
|
|
626
|
-
metadata: { agreements: ["account_terms"], locale: "en" },
|
|
627
|
-
});
|
|
628
|
-
expect([...call.headers.entries()]).toEqual([["origin", HUB]]);
|
|
629
|
-
expect(mockMailSend).not.toHaveBeenCalled();
|
|
630
|
-
expect(mockCreateVerificationValue).toHaveBeenCalledWith(
|
|
631
|
-
expect.objectContaining({
|
|
632
|
-
identifier: `purchase-claim:${createHash("sha256").update(SESSION_ID).digest("hex")}`,
|
|
633
|
-
}),
|
|
634
|
-
);
|
|
635
|
-
});
|
|
636
|
-
|
|
637
|
-
it("the same claim again → 410 claim_used, no token", async () => {
|
|
638
|
-
mockFindVerificationValue.mockResolvedValue({
|
|
639
|
-
value: "1",
|
|
640
|
-
expiresAt: new Date(Date.now() + 60_000),
|
|
641
|
-
});
|
|
642
|
-
const res = await purchasePOST(
|
|
643
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
644
|
-
);
|
|
645
|
-
expect(res.status).toBe(410);
|
|
646
|
-
expect(await res.json()).toEqual({ error: "claim_used" });
|
|
647
|
-
expect(mockSignInMagicLink).not.toHaveBeenCalled();
|
|
648
|
-
});
|
|
649
|
-
|
|
650
|
-
it("SEC-11096-4: an existing address → 409 account_exists, no token, the claim not spent", async () => {
|
|
651
|
-
mockFindUserByEmail.mockResolvedValue({ user: { id: "u1" } });
|
|
652
|
-
const res = await purchasePOST(
|
|
653
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
654
|
-
);
|
|
655
|
-
expect(res.status).toBe(409);
|
|
656
|
-
expect(await res.json()).toEqual({ error: "account_exists" });
|
|
657
|
-
expect(mockSignInMagicLink).not.toHaveBeenCalled();
|
|
658
|
-
expect(mockCreateVerificationValue).not.toHaveBeenCalled();
|
|
659
|
-
});
|
|
660
|
-
|
|
661
|
-
it.each([
|
|
662
|
-
["a claim minted for another client", () => claimFor({ c: "spoke_other" })],
|
|
663
|
-
["an expired claim", () => claimFor({}, Date.now() - 73 * 3600 * 1000)],
|
|
664
|
-
["a hub-continue ref", () => claimFor({}, Date.now(), "hub-continue")],
|
|
665
|
-
])("%s → 410 claim_invalid, no entry call", async (_name, build) => {
|
|
666
|
-
const res = await purchasePOST(
|
|
667
|
-
purchaseRequest({ ...AUTHORIZE, claim: build() }),
|
|
668
|
-
);
|
|
669
|
-
expect(res.status).toBe(410);
|
|
670
|
-
expect(await res.json()).toEqual({ error: "claim_invalid" });
|
|
671
|
-
expect(mockEntry).not.toHaveBeenCalled();
|
|
672
|
-
});
|
|
673
|
-
|
|
674
|
-
it("SEC-11096-2: evidence bumped to version 2 after a version-1 claim → 410 terms_changed", async () => {
|
|
675
|
-
mockSignUpEvidence.mockResolvedValue(
|
|
676
|
-
evidence([{ ...ACCOUNT_TERMS, policyVersion: 2 }]),
|
|
677
|
-
);
|
|
678
|
-
const res = await purchasePOST(
|
|
679
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
680
|
-
);
|
|
681
|
-
expect(res.status).toBe(410);
|
|
682
|
-
expect(await res.json()).toEqual({ error: "terms_changed" });
|
|
683
|
-
expect(mockSignInMagicLink).not.toHaveBeenCalled();
|
|
684
|
-
});
|
|
685
|
-
|
|
686
|
-
it("the API refusing the spoke is passed on, and nothing is spent", async () => {
|
|
687
|
-
mockEntry.mockResolvedValue({
|
|
688
|
-
ok: false,
|
|
689
|
-
status: 401,
|
|
690
|
-
body: { ok: false, error: { code: "invalid_client" } },
|
|
691
|
-
});
|
|
692
|
-
const res = await purchasePOST(
|
|
693
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
694
|
-
);
|
|
695
|
-
expect(res.status).toBe(401);
|
|
696
|
-
expect(mockCreateVerificationValue).not.toHaveBeenCalled();
|
|
697
|
-
});
|
|
698
|
-
|
|
699
|
-
it("AC-6: an added password field is 400 invalid_request", async () => {
|
|
700
|
-
const res = await purchasePOST(
|
|
701
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor(), password: "x" }),
|
|
702
|
-
);
|
|
703
|
-
expect(res.status).toBe(400);
|
|
704
|
-
expect(mockEntry).not.toHaveBeenCalled();
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
it("no token captured (Better Auth refused or mailed nothing) → 503, and the claim is not spent", async () => {
|
|
708
|
-
mockSignInMagicLink.mockResolvedValue({ status: true });
|
|
709
|
-
const res = await purchasePOST(
|
|
710
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
711
|
-
);
|
|
712
|
-
expect(res.status).toBe(503);
|
|
713
|
-
expect(mockDeleteVerificationByIdentifier).toHaveBeenCalledWith(
|
|
714
|
-
`purchase-claim:${createHash("sha256").update(SESSION_ID).digest("hex")}`,
|
|
715
|
-
);
|
|
716
|
-
});
|
|
717
|
-
|
|
718
|
-
it("a spend that fails after the claim was marked leaves it spendable: the retry succeeds", async () => {
|
|
719
|
-
const rows = new Map<string, { value: string; expiresAt: Date }>();
|
|
720
|
-
mockFindVerificationValue.mockImplementation(async (id: string) => rows.get(id) ?? null);
|
|
721
|
-
mockCreateVerificationValue.mockImplementation(
|
|
722
|
-
async (row: { identifier: string; value: string; expiresAt: Date }) => {
|
|
723
|
-
rows.set(row.identifier, row);
|
|
724
|
-
return row;
|
|
725
|
-
},
|
|
726
|
-
);
|
|
727
|
-
mockDeleteVerificationByIdentifier.mockImplementation(async (id: string) => {
|
|
728
|
-
rows.delete(id);
|
|
729
|
-
});
|
|
730
|
-
const succeed = mockSignInMagicLink.getMockImplementation();
|
|
731
|
-
mockSignInMagicLink.mockRejectedValueOnce(new Error("boom"));
|
|
732
|
-
|
|
733
|
-
const failed = await purchasePOST(
|
|
734
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
735
|
-
);
|
|
736
|
-
expect(failed.status).toBe(503);
|
|
737
|
-
|
|
738
|
-
mockSignInMagicLink.mockImplementation(succeed!);
|
|
739
|
-
const retried = await purchasePOST(
|
|
740
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
741
|
-
);
|
|
742
|
-
expect(retried.status).toBe(200);
|
|
743
|
-
// And once spent, it stays spent.
|
|
744
|
-
const again = await purchasePOST(
|
|
745
|
-
purchaseRequest({ ...AUTHORIZE, claim: claimFor() }),
|
|
746
|
-
);
|
|
747
|
-
expect(again.status).toBe(410);
|
|
748
|
-
});
|
|
749
|
-
});
|