create-ailk 0.35.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/component-catalog.json +12 -1
- package/component-catalog.md +6 -5
- package/dist/lib/env-catalog.js +8 -0
- package/dist/surfaces.js +6 -3
- package/dist/topology.d.ts +4 -4
- package/dist/topology.js +104 -60
- package/package.json +3 -3
- package/templates/.env.example +21 -0
- package/templates/apps/api/.env.example +7 -0
- package/templates/apps/api/CLAUDE.md +9 -4
- package/templates/apps/mcp/CLAUDE.md +4 -2
- package/templates/apps/web/.env.example +16 -0
- package/templates/apps/web/CLAUDE.md +46 -5
- package/templates/apps/web/app/[locale]/(auth)/__tests__/password-reset.test.tsx +2 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/page.test.tsx +52 -1
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/remembered-method.test.ts +43 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/sign-in-card.test.tsx +108 -1
- package/templates/apps/web/app/[locale]/(auth)/sign-in/page.tsx +36 -3
- package/templates/apps/web/app/[locale]/(auth)/sign-in/remembered-method.ts +37 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-card.tsx +90 -33
- package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-text.ts +9 -4
- package/templates/apps/web/app/[locale]/(authed)/__tests__/gate.test.tsx +79 -348
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/billing.test.tsx +12 -1
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/hub-mode.test.tsx +55 -2
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/layout-sign-out.test.tsx +10 -5
- package/templates/apps/web/app/[locale]/(authed)/account/account-frame.tsx +1 -1
- package/templates/apps/web/app/[locale]/(authed)/account/billing/page.tsx +60 -6
- package/templates/apps/web/app/[locale]/(authed)/account/billing-source.ts +32 -3
- package/templates/apps/web/app/[locale]/(authed)/account/connect/__tests__/page.test.tsx +8 -1
- package/templates/apps/web/app/[locale]/(authed)/account/connect/page.tsx +7 -3
- package/templates/apps/web/app/[locale]/(authed)/account/general/__tests__/phone-verification.test.tsx +215 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/__tests__/seam-imports.test.ts +65 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/page.tsx +47 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/phone-actions.ts +122 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/phone-verification.tsx +214 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/__tests__/invitation-id.test.ts +35 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/__tests__/page.test.tsx +266 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/actions.ts +116 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/invitation-id.ts +12 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/page.tsx +154 -0
- package/templates/apps/web/app/[locale]/(authed)/account/layout.tsx +6 -4
- package/templates/apps/web/app/[locale]/(authed)/account/security/page.tsx +3 -1
- package/templates/apps/web/app/[locale]/(authed)/layout.tsx +15 -13
- package/templates/apps/web/app/[locale]/(authed)/signed-out-gate.generated.tsx +20 -52
- package/templates/apps/web/app/[locale]/layout.tsx +24 -75
- package/templates/apps/web/app/[locale]/purchase/__tests__/success.test.tsx +50 -0
- package/templates/apps/web/app/[locale]/purchase/success/page.tsx +48 -7
- package/templates/apps/web/app/api/auth/hub/__tests__/entry-routes.test.ts +749 -0
- package/templates/apps/web/app/api/auth/hub/authorize/__tests__/route.test.ts +166 -7
- package/templates/apps/web/app/api/auth/hub/authorize/route.ts +96 -30
- package/templates/apps/web/app/api/auth/hub/continuation.ts +51 -2
- package/templates/apps/web/app/api/auth/hub/email/route.ts +111 -0
- package/templates/apps/web/app/api/auth/hub/entry-shared.ts +163 -0
- package/templates/apps/web/app/api/auth/hub/purchase/route.ts +230 -0
- package/templates/apps/web/app/api/auth/hub/purchase-link/route.ts +174 -0
- package/templates/apps/web/app/api/auth/hub/sign-up/route.ts +60 -0
- package/templates/apps/web/app/api/auth/hub/sign-up-evidence.ts +97 -0
- package/templates/apps/web/app/api/hub-session/__tests__/routes.test.ts +475 -7
- package/templates/apps/web/app/api/hub-session/callback/route.ts +13 -47
- package/templates/apps/web/app/api/hub-session/email/route.ts +125 -0
- package/templates/apps/web/app/api/hub-session/one-tap/nonce/route.ts +49 -0
- package/templates/apps/web/app/api/hub-session/one-tap/route.ts +106 -0
- package/templates/apps/web/app/api/hub-session/open/route.ts +111 -0
- package/templates/apps/web/app/api/hub-session/sign-in/route.ts +23 -40
- package/templates/apps/web/app/api/hub-session/sign-out/route.ts +1 -10
- package/templates/apps/web/auth.ts +133 -6
- package/templates/apps/web/components/sign-out-trigger.tsx +1 -1
- package/templates/apps/web/lib/__tests__/auth-email.test.ts +141 -0
- package/templates/apps/web/lib/__tests__/auth-phone.test.ts +445 -0
- package/templates/apps/web/lib/__tests__/auth-sms.test.ts +140 -0
- package/templates/apps/web/lib/__tests__/fixtures/tsconfig.flow.json +18 -0
- package/templates/apps/web/lib/__tests__/site-header.test.ts +13 -0
- package/templates/apps/web/lib/__tests__/telemetry-scrub.test.ts +19 -1
- package/templates/apps/web/lib/auth-email.ts +62 -6
- package/templates/apps/web/lib/auth-env.ts +13 -0
- package/templates/apps/web/lib/auth-sms.ts +180 -0
- package/templates/apps/web/lib/hub-session/__tests__/hub-session.test.ts +28 -0
- package/templates/apps/web/lib/hub-session/__tests__/spoke-sign-in.test.tsx +332 -0
- package/templates/apps/web/lib/hub-session/config.ts +7 -1
- package/templates/apps/web/lib/hub-session/entry.ts +238 -0
- package/templates/apps/web/lib/hub-session/index.ts +1 -4
- package/templates/apps/web/lib/hub-session/one-tap.tsx +148 -0
- package/templates/apps/web/lib/hub-session/session.ts +120 -2
- package/templates/apps/web/lib/hub-session/spoke-sign-in.tsx +277 -0
- package/templates/apps/web/lib/site-header.ts +14 -1
- package/templates/apps/web/lib/telemetry-scrub.ts +11 -0
- package/templates/apps/web/lib/topology.generated.ts +39 -0
- package/templates/apps/web/messages/en.json +112 -20
- package/templates/apps/web/middleware.ts +34 -2
- package/templates/apps/web/security-headers.config.ts +21 -0
- package/versions.json +47 -47
- package/templates/apps/web/app/[locale]/(authed)/sign-in-gate.tsx +0 -162
- package/templates/apps/web/components/signed-in-nav.tsx +0 -79
- package/templates/apps/web/lib/hub-session/__tests__/sign-in-gate.test.tsx +0 -180
- package/templates/apps/web/lib/hub-session/sign-in-gate.tsx +0 -162
|
@@ -31,7 +31,7 @@ import { type BetterAuthPlugin, betterAuth } from "better-auth";
|
|
|
31
31
|
import { prismaAdapter } from "better-auth/adapters/prisma";
|
|
32
32
|
import type { AuthMiddleware } from "better-auth/api";
|
|
33
33
|
import { nextCookies } from "better-auth/next-js";
|
|
34
|
-
import { magicLink, mcp } from "better-auth/plugins";
|
|
34
|
+
import { magicLink, mcp, phoneNumber } from "better-auth/plugins";
|
|
35
35
|
|
|
36
36
|
import { sealMcpCallbacks } from "./app/api/auth/mcp/continuation";
|
|
37
37
|
import {
|
|
@@ -44,15 +44,18 @@ import {
|
|
|
44
44
|
import {
|
|
45
45
|
getActiveProviderIds,
|
|
46
46
|
isSignUpOpen,
|
|
47
|
+
phoneVerificationConfigured,
|
|
47
48
|
resetSendConfigured,
|
|
48
49
|
} from "./lib/auth-env";
|
|
49
50
|
import { resolveSecret } from "./lib/auth-secret";
|
|
51
|
+
import { isAllowedPhoneNumber, makeSendPhoneOtp } from "./lib/auth-sms";
|
|
50
52
|
import { type McpMembership, type McpOAuthDeps, mcpOAuthPlugin } from "./lib/mcp-oauth";
|
|
51
53
|
import {
|
|
52
54
|
signUpAgreementHooks,
|
|
53
55
|
signUpAgreementsPlugin,
|
|
54
56
|
withAuthTransactionCapture,
|
|
55
57
|
} from "./lib/sign-up-agreements";
|
|
58
|
+
import { TOPOLOGY } from "./lib/topology.generated";
|
|
56
59
|
|
|
57
60
|
/**
|
|
58
61
|
* Reset-token lifetime (#5962 § D11). One hour — the library's own default,
|
|
@@ -80,6 +83,97 @@ const VERIFY_TOKEN_TTL_SECONDS = 86_400;
|
|
|
80
83
|
*/
|
|
81
84
|
const VERIFY_EMAIL_DISABLED_PATHS = ["/verify-email"];
|
|
82
85
|
|
|
86
|
+
/**
|
|
87
|
+
* The phone plugin's paths AILK turns off whenever it is installed (#11106
|
|
88
|
+
* D9): phone is never a sign-in method or a password-reset channel, only a
|
|
89
|
+
* verified contact a signed-in person sets for themselves.
|
|
90
|
+
*/
|
|
91
|
+
export const PHONE_DISABLED_PATHS = [
|
|
92
|
+
"/sign-in/phone-number",
|
|
93
|
+
"/phone-number/request-password-reset",
|
|
94
|
+
"/phone-number/reset-password",
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
/** The code's shape and life (#11106 D9), set explicitly so they are reviewable. */
|
|
98
|
+
const PHONE_OTP_LENGTH = 6;
|
|
99
|
+
const PHONE_OTP_TTL_SECONDS = 300;
|
|
100
|
+
const PHONE_OTP_ATTEMPTS = 3;
|
|
101
|
+
|
|
102
|
+
let guardPhoneStep: AuthMiddleware | undefined;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The phone step's guard (#11106 D9, SEC-11106-2). `/phone-number/send-otp`
|
|
106
|
+
* and `/phone-number/verify` answer only a session whose user's email is
|
|
107
|
+
* verified — 401 signed out, 403 unverified — so a throwaway account cannot
|
|
108
|
+
* text anyone. `/phone-number/verify` also needs `updatePhoneNumber: true`:
|
|
109
|
+
* without it the plugin signs the phone's owner in (it creates a session),
|
|
110
|
+
* which would make phone a sign-in method after all.
|
|
111
|
+
*/
|
|
112
|
+
export const phoneVerificationGuard = {
|
|
113
|
+
id: "ailk-phone-verification-guard",
|
|
114
|
+
hooks: {
|
|
115
|
+
before: [
|
|
116
|
+
{
|
|
117
|
+
matcher: (ctx) =>
|
|
118
|
+
ctx.path === "/phone-number/send-otp" || ctx.path === "/phone-number/verify",
|
|
119
|
+
// `better-auth/api` is ESM-only (#507): imported when the hook first runs.
|
|
120
|
+
handler: async (ctx) => {
|
|
121
|
+
const { APIError, createAuthMiddleware, getSessionFromCtx } = await import(
|
|
122
|
+
"better-auth/api"
|
|
123
|
+
);
|
|
124
|
+
guardPhoneStep ??= createAuthMiddleware(async (inner) => {
|
|
125
|
+
const session = await getSessionFromCtx(inner);
|
|
126
|
+
if (!session) {
|
|
127
|
+
throw new APIError("UNAUTHORIZED", { code: "SESSION_REQUIRED", message: "SESSION_REQUIRED" });
|
|
128
|
+
}
|
|
129
|
+
if (session.user.emailVerified !== true) {
|
|
130
|
+
throw new APIError("FORBIDDEN", { code: "EMAIL_NOT_VERIFIED", message: "EMAIL_NOT_VERIFIED" });
|
|
131
|
+
}
|
|
132
|
+
const body = inner.body as Record<string, unknown> | undefined;
|
|
133
|
+
if (inner.path === "/phone-number/verify" && body?.updatePhoneNumber !== true) {
|
|
134
|
+
throw new APIError("BAD_REQUEST", {
|
|
135
|
+
code: "UPDATE_PHONE_NUMBER_REQUIRED",
|
|
136
|
+
message: "UPDATE_PHONE_NUMBER_REQUIRED",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
});
|
|
141
|
+
return guardPhoneStep(ctx);
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
} satisfies BetterAuthPlugin;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Whether this build carries the phone step (#11106 D9): on a hub (never a
|
|
150
|
+
* spoke, which keeps no users) whose relay secret and origin are set. The
|
|
151
|
+
* Account page offers the step on the same predicate.
|
|
152
|
+
*/
|
|
153
|
+
export function phoneStepActive(): boolean {
|
|
154
|
+
return phoneVerificationConfigured() && TOPOLOGY !== "spoke";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The phone plugin and its guard, as `createAuth` installs them. */
|
|
158
|
+
export function phoneVerificationPlugins() {
|
|
159
|
+
return [
|
|
160
|
+
phoneNumber({
|
|
161
|
+
sendOTP: makeSendPhoneOtp({
|
|
162
|
+
sessionUserId: async (ctx) => {
|
|
163
|
+
const { getSessionFromCtx } = await import("better-auth/api");
|
|
164
|
+
const session = await getSessionFromCtx(ctx as Parameters<typeof getSessionFromCtx>[0]);
|
|
165
|
+
return session?.user.id ?? null;
|
|
166
|
+
},
|
|
167
|
+
}),
|
|
168
|
+
otpLength: PHONE_OTP_LENGTH,
|
|
169
|
+
expiresIn: PHONE_OTP_TTL_SECONDS,
|
|
170
|
+
allowedAttempts: PHONE_OTP_ATTEMPTS,
|
|
171
|
+
phoneNumberValidator: isAllowedPhoneNumber,
|
|
172
|
+
}),
|
|
173
|
+
phoneVerificationGuard,
|
|
174
|
+
];
|
|
175
|
+
}
|
|
176
|
+
|
|
83
177
|
// mcp-oauth:begin
|
|
84
178
|
/**
|
|
85
179
|
* The site's MCP authorization server (#10357 D4): better-auth's `mcp` plugin
|
|
@@ -374,10 +468,26 @@ async function rejectNonHttpImage(user: { image?: unknown }): Promise<void> {
|
|
|
374
468
|
throw new APIError("BAD_REQUEST", { code: "INVALID_IMAGE_URL", message: "INVALID_IMAGE_URL" });
|
|
375
469
|
}
|
|
376
470
|
|
|
471
|
+
/**
|
|
472
|
+
* The phone fields a user create may carry, nulled (#11106 D7, SEC-11106-1).
|
|
473
|
+
* The phone plugin leaves `phoneNumber` input-able on create, so a
|
|
474
|
+
* `/sign-up/email`, magic-link or social sign-up could otherwise claim a
|
|
475
|
+
* number — squatting a victim's under the unique index, or probing which are
|
|
476
|
+
* registered with no text sent. Only the plugin's verify step writes either.
|
|
477
|
+
* A hook's `data` is merged over the row, so a key is dropped by nulling it.
|
|
478
|
+
*/
|
|
479
|
+
function phoneFieldsCleared(user: Record<string, unknown>): Record<string, null> {
|
|
480
|
+
const cleared: Record<string, null> = {};
|
|
481
|
+
for (const key of ["phoneNumber", "phoneNumberVerified"]) {
|
|
482
|
+
if (key in user) cleared[key] = null;
|
|
483
|
+
}
|
|
484
|
+
return cleared;
|
|
485
|
+
}
|
|
486
|
+
|
|
377
487
|
/**
|
|
378
488
|
* The sign-up hooks, with the image check on every user write: on update, and
|
|
379
489
|
* on create (the sign-up endpoint accepts `image` too) ahead of the agreement
|
|
380
|
-
* hook.
|
|
490
|
+
* hook. Every create also drops the phone fields (#11106 D7).
|
|
381
491
|
*/
|
|
382
492
|
function databaseHooks(): ReturnType<typeof signUpAgreementHooks> {
|
|
383
493
|
const hooks = signUpAgreementHooks();
|
|
@@ -388,7 +498,12 @@ function databaseHooks(): ReturnType<typeof signUpAgreementHooks> {
|
|
|
388
498
|
create: {
|
|
389
499
|
before: async (user, context) => {
|
|
390
500
|
await rejectNonHttpImage(user);
|
|
391
|
-
|
|
501
|
+
const cleared = phoneFieldsCleared(user);
|
|
502
|
+
const result = await createBefore?.({ ...user, ...cleared }, context);
|
|
503
|
+
if (Object.keys(cleared).length === 0 || result === false) return result;
|
|
504
|
+
return result && typeof result === "object" && "data" in result
|
|
505
|
+
? { ...result, data: { ...result.data, ...cleared } }
|
|
506
|
+
: { data: cleared };
|
|
392
507
|
},
|
|
393
508
|
},
|
|
394
509
|
update: { before: rejectNonHttpImage },
|
|
@@ -654,9 +769,14 @@ export function createAuth(deps: CreateAuthDeps = {}) {
|
|
|
654
769
|
* (#10357 D4): `listTenancy`'s rule — every workspace of every organization
|
|
655
770
|
* they belong to, with that membership's role.
|
|
656
771
|
*/
|
|
772
|
+
/**
|
|
773
|
+
* A deactivated membership counts as none (#11106 D6): its organization's
|
|
774
|
+
* workspaces leave the consent list and the token self-check, and return on
|
|
775
|
+
* reactivation.
|
|
776
|
+
*/
|
|
657
777
|
const readMemberships = async (userId: string): Promise<McpMembership[]> => {
|
|
658
778
|
const memberships = await prisma.membership.findMany({
|
|
659
|
-
where: { userId },
|
|
779
|
+
where: { userId, deactivatedAt: null },
|
|
660
780
|
select: {
|
|
661
781
|
role: true,
|
|
662
782
|
organization: {
|
|
@@ -679,6 +799,10 @@ export function createAuth(deps: CreateAuthDeps = {}) {
|
|
|
679
799
|
};
|
|
680
800
|
const mcpServer = baseURL ? mcpAuthorizationServer({ readMemberships }) : [];
|
|
681
801
|
|
|
802
|
+
/** The phone step (#11106 D9); its sign-in and reset paths close with it. */
|
|
803
|
+
const phoneActive = phoneStepActive();
|
|
804
|
+
const phonePlugins = phoneActive ? phoneVerificationPlugins() : [];
|
|
805
|
+
|
|
682
806
|
const instance = betterAuth({
|
|
683
807
|
/**
|
|
684
808
|
* `transaction: true` (#8765 D4): Better Auth opens one `$transaction` for
|
|
@@ -725,6 +849,7 @@ export function createAuth(deps: CreateAuthDeps = {}) {
|
|
|
725
849
|
disabledPaths: [
|
|
726
850
|
...VERIFY_EMAIL_DISABLED_PATHS,
|
|
727
851
|
...(baseURL ? MCP_DISABLED_PATHS : []),
|
|
852
|
+
...(phoneActive ? PHONE_DISABLED_PATHS : []),
|
|
728
853
|
],
|
|
729
854
|
emailAndPassword: {
|
|
730
855
|
enabled: passwordMethodActive,
|
|
@@ -925,7 +1050,8 @@ export function createAuth(deps: CreateAuthDeps = {}) {
|
|
|
925
1050
|
* `/sign-up/email` without every sign-up agreement is refused before the
|
|
926
1051
|
* handler looks the address up.
|
|
927
1052
|
*
|
|
928
|
-
* The MCP authorization server whenever `baseURL` is pinned (#10357 D4)
|
|
1053
|
+
* The MCP authorization server whenever `baseURL` is pinned (#10357 D4),
|
|
1054
|
+
* and the phone step under its predicate (#11106 D9).
|
|
929
1055
|
*/
|
|
930
1056
|
plugins: magicLinkActive
|
|
931
1057
|
? [
|
|
@@ -933,8 +1059,9 @@ export function createAuth(deps: CreateAuthDeps = {}) {
|
|
|
933
1059
|
signUpAgreementsPlugin(),
|
|
934
1060
|
magicLink(makeMagicLinkOptions({ send: authEmailTransport })),
|
|
935
1061
|
...mcpServer,
|
|
1062
|
+
...phonePlugins,
|
|
936
1063
|
]
|
|
937
|
-
: [nextCookies(), signUpAgreementsPlugin(), ...mcpServer],
|
|
1064
|
+
: [nextCookies(), signUpAgreementsPlugin(), ...mcpServer, ...phonePlugins],
|
|
938
1065
|
});
|
|
939
1066
|
return instance;
|
|
940
1067
|
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*
|
|
11
11
|
* The locale layout (RSC) reads the session and authors the "Sign out" nav
|
|
12
12
|
* action as `kind: "flow"`; Nav hands the rendered trigger `<Button>` back
|
|
13
|
-
* through `renderFlowTrigger` — composed in
|
|
13
|
+
* through `renderFlowTrigger` — composed in a client Nav until #11095 D6, on the client side
|
|
14
14
|
* of the RSC boundary (#7402) — and this component clones it with the real
|
|
15
15
|
* sign-out handler. Same `createAuthClient()` idiom as `SignInGate`.
|
|
16
16
|
*/
|
|
@@ -28,9 +28,11 @@ import {
|
|
|
28
28
|
confirmSignUpEmail,
|
|
29
29
|
MAGIC_LINK_TTL_SECONDS,
|
|
30
30
|
makeMagicLinkOptions,
|
|
31
|
+
makeSendAccountOpen,
|
|
31
32
|
makeSendResetPassword,
|
|
32
33
|
makeSendVerificationEmail,
|
|
33
34
|
makeSendPasswordChangedNotice,
|
|
35
|
+
purchaseCapture,
|
|
34
36
|
} from "../auth-email";
|
|
35
37
|
|
|
36
38
|
const ORIGINAL_ENV = process.env;
|
|
@@ -825,3 +827,142 @@ describe("confirmSignUpEmail (#10820 D7)", () => {
|
|
|
825
827
|
expect(JSON.stringify(lines)).not.toContain(NONCE);
|
|
826
828
|
});
|
|
827
829
|
});
|
|
830
|
+
|
|
831
|
+
// ── #11096 D9 — the purchase capture and the account-open kind ──────────────
|
|
832
|
+
|
|
833
|
+
describe("sendMagicLink inside purchaseCapture (#11096 D9)", () => {
|
|
834
|
+
const AGREED = { agreements: ["account_terms"], locale: "en" };
|
|
835
|
+
|
|
836
|
+
beforeEach(() => {
|
|
837
|
+
process.env.AILK_AUTH_SIGNUP_OPEN = "true";
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
it("binds the agreements as for any sign-up request, hands the token to the capture and mails nothing", async () => {
|
|
841
|
+
const send = jest.fn().mockResolvedValue({ ok: true });
|
|
842
|
+
const bind = jest.fn().mockResolvedValue(AGREED);
|
|
843
|
+
const { lines, log } = makeLog();
|
|
844
|
+
const { ctx, background, runInBackgroundOrAwait } = makeCtx(null);
|
|
845
|
+
const { sendMagicLink } = makeMagicLinkOptions({ send, log, bind });
|
|
846
|
+
const capture = { token: null as string | null };
|
|
847
|
+
|
|
848
|
+
await purchaseCapture.run(capture, () =>
|
|
849
|
+
sendMagicLink(
|
|
850
|
+
{ email: "new@example.com", url: LINK, token: "MLTOKEN", metadata: AGREED },
|
|
851
|
+
ctx,
|
|
852
|
+
),
|
|
853
|
+
);
|
|
854
|
+
await Promise.all(background);
|
|
855
|
+
|
|
856
|
+
expect(bind).toHaveBeenCalledWith(
|
|
857
|
+
ctx.context.internalAdapter,
|
|
858
|
+
"MLTOKEN",
|
|
859
|
+
AGREED,
|
|
860
|
+
MAGIC_LINK_TTL_SECONDS,
|
|
861
|
+
);
|
|
862
|
+
expect(capture.token).toBe("MLTOKEN");
|
|
863
|
+
expect(send).not.toHaveBeenCalled();
|
|
864
|
+
expect(runInBackgroundOrAwait).not.toHaveBeenCalled();
|
|
865
|
+
expect(JSON.stringify(lines)).not.toContain("MLTOKEN");
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
it("captures nothing and mails nothing for an address that already has an account", async () => {
|
|
869
|
+
const send = jest.fn().mockResolvedValue({ ok: true });
|
|
870
|
+
const bind = jest.fn().mockResolvedValue(AGREED);
|
|
871
|
+
const { ctx } = makeCtx({ id: "user_1", email: "old@example.com" });
|
|
872
|
+
const { sendMagicLink } = makeMagicLinkOptions({ send, log: makeLog().log, bind });
|
|
873
|
+
const capture = { token: null as string | null };
|
|
874
|
+
|
|
875
|
+
await purchaseCapture.run(capture, () =>
|
|
876
|
+
sendMagicLink(
|
|
877
|
+
{ email: "old@example.com", url: LINK, token: "MLTOKEN", metadata: AGREED },
|
|
878
|
+
ctx,
|
|
879
|
+
),
|
|
880
|
+
);
|
|
881
|
+
|
|
882
|
+
expect(capture.token).toBeNull();
|
|
883
|
+
expect(send).not.toHaveBeenCalled();
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
it("captures nothing when the binding fails", async () => {
|
|
887
|
+
const send = jest.fn();
|
|
888
|
+
const bind = jest.fn().mockRejectedValue(new Error("db down"));
|
|
889
|
+
const { ctx } = makeCtx(null);
|
|
890
|
+
const { sendMagicLink } = makeMagicLinkOptions({ send, log: makeLog().log, bind });
|
|
891
|
+
const capture = { token: null as string | null };
|
|
892
|
+
|
|
893
|
+
await purchaseCapture.run(capture, () =>
|
|
894
|
+
sendMagicLink(
|
|
895
|
+
{ email: "new@example.com", url: LINK, token: "MLTOKEN", metadata: AGREED },
|
|
896
|
+
ctx,
|
|
897
|
+
),
|
|
898
|
+
);
|
|
899
|
+
|
|
900
|
+
expect(capture.token).toBeNull();
|
|
901
|
+
expect(send).not.toHaveBeenCalled();
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
it("outside a capture the same call mails, exactly as before", async () => {
|
|
905
|
+
const send = jest.fn().mockResolvedValue({ ok: true });
|
|
906
|
+
const bind = jest.fn().mockResolvedValue(AGREED);
|
|
907
|
+
const { ctx, background } = makeCtx(null);
|
|
908
|
+
const { sendMagicLink } = makeMagicLinkOptions({ send, log: makeLog().log, bind });
|
|
909
|
+
|
|
910
|
+
await sendMagicLink(
|
|
911
|
+
{ email: "new@example.com", url: LINK, token: "MLTOKEN", metadata: AGREED },
|
|
912
|
+
ctx,
|
|
913
|
+
);
|
|
914
|
+
await Promise.all(background);
|
|
915
|
+
|
|
916
|
+
expect(send).toHaveBeenCalledTimes(1);
|
|
917
|
+
});
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
describe("makeSendAccountOpen (#11096 D9)", () => {
|
|
921
|
+
const OPEN_URL = "https://spoke.example/account?claim=CLAIM";
|
|
922
|
+
|
|
923
|
+
it("posts one signed account-open body to the buyer: no agreements, no locale", async () => {
|
|
924
|
+
const send = jest.fn().mockResolvedValue({ ok: true });
|
|
925
|
+
const { lines, log } = makeLog();
|
|
926
|
+
|
|
927
|
+
await makeSendAccountOpen({ send, log })({
|
|
928
|
+
to: "buyer@example.com",
|
|
929
|
+
url: OPEN_URL,
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
expect(send).toHaveBeenCalledTimes(1);
|
|
933
|
+
const [rawBody, headers] = send.mock.calls[0] as [string, Record<string, string>];
|
|
934
|
+
expect(JSON.parse(rawBody)).toEqual({
|
|
935
|
+
kind: "account-open",
|
|
936
|
+
userId: "purchase",
|
|
937
|
+
to: "buyer@example.com",
|
|
938
|
+
url: OPEN_URL,
|
|
939
|
+
});
|
|
940
|
+
const timestamp = headers["x-ailk-auth-email-timestamp"]!;
|
|
941
|
+
expect(headers["x-ailk-auth-email-signature"]).toBe(
|
|
942
|
+
`sha256=${createHmac("sha256", "webhook-secret")
|
|
943
|
+
.update(`${timestamp}.${rawBody}`, "utf8")
|
|
944
|
+
.digest("hex")}`,
|
|
945
|
+
);
|
|
946
|
+
const serialized = JSON.stringify(lines);
|
|
947
|
+
expect(serialized).not.toContain("buyer@example.com");
|
|
948
|
+
expect(serialized).not.toContain("CLAIM");
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
it("never throws when the transport fails, and answers false", async () => {
|
|
952
|
+
const send = jest.fn().mockRejectedValue(new Error("down"));
|
|
953
|
+
await expect(
|
|
954
|
+
makeSendAccountOpen({ send, log: makeLog().log })({
|
|
955
|
+
to: "buyer@example.com",
|
|
956
|
+
url: OPEN_URL,
|
|
957
|
+
}),
|
|
958
|
+
).resolves.toBe(false);
|
|
959
|
+
});
|
|
960
|
+
|
|
961
|
+
it("answers true only when the relay accepted the mail", async () => {
|
|
962
|
+
const accepted = jest.fn().mockResolvedValue({ ok: true, status: 202 });
|
|
963
|
+
const refused = jest.fn().mockResolvedValue({ ok: false, status: 500 });
|
|
964
|
+
const input = { to: "buyer@example.com", url: OPEN_URL };
|
|
965
|
+
await expect(makeSendAccountOpen({ send: accepted, log: makeLog().log })(input)).resolves.toBe(true);
|
|
966
|
+
await expect(makeSendAccountOpen({ send: refused, log: makeLog().log })(input)).resolves.toBe(false);
|
|
967
|
+
});
|
|
968
|
+
});
|