@cosmicdrift/kumiko-samples 0.343.0 → 0.345.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 (44) hide show
  1. package/package.json +1 -1
  2. package/packages/bundled-features/package.json +1 -1
  3. package/packages/bundled-features/src/audit/changes.json +6 -0
  4. package/packages/bundled-features/src/audit/run-escape-hatch-retention.ts +31 -12
  5. package/packages/bundled-features/src/auth-email-password/changes.json +24 -0
  6. package/packages/bundled-features/src/auth-email-password/constants.ts +1 -0
  7. package/packages/bundled-features/src/auth-email-password/feature.ts +9 -2
  8. package/packages/bundled-features/src/auth-email-password/handlers/invite-signup-complete.write.ts +33 -10
  9. package/packages/bundled-features/src/auth-email-password/handlers/signup-confirm.write.ts +32 -15
  10. package/packages/bundled-features/src/auth-email-password/handlers/switch-tenant-mfa-gate.write.ts +60 -0
  11. package/packages/bundled-features/src/auth-email-password/handlers/token-request-handler.ts +8 -1
  12. package/packages/bundled-features/src/auth-email-password/i18n.ts +2 -0
  13. package/packages/bundled-features/src/auth-email-password/web/auth-client.ts +20 -7
  14. package/packages/bundled-features/src/auth-email-password/web/signup-complete-screen.tsx +11 -1
  15. package/packages/bundled-features/src/billing-foundation/changes.json +7 -0
  16. package/packages/bundled-features/src/billing-foundation/checkout-core.ts +35 -7
  17. package/packages/bundled-features/src/billing-foundation/consumer-protection/confirmation-mail.ts +36 -22
  18. package/packages/bundled-features/src/billing-foundation/consumer-protection/consent-gate.ts +12 -2
  19. package/packages/bundled-features/src/billing-foundation/consumer-protection/consent-locale.ts +1 -1
  20. package/packages/bundled-features/src/billing-foundation/consumer-protection/issue-confirmation.ts +34 -1
  21. package/packages/bundled-features/src/billing-foundation/events.ts +2 -0
  22. package/packages/bundled-features/src/billing-foundation/handlers/billing-plans.query.ts +14 -5
  23. package/packages/bundled-features/src/billing-foundation/handlers/create-checkout-session.write.ts +61 -36
  24. package/packages/bundled-features/src/billing-foundation/index.ts +11 -0
  25. package/packages/bundled-features/src/billing-foundation/types.ts +25 -7
  26. package/packages/bundled-features/src/billing-foundation/validate-options.ts +36 -3
  27. package/packages/bundled-features/src/billing-foundation/web/billing-plans-panel.tsx +1 -1
  28. package/packages/bundled-features/src/billing-foundation/web/cancel-contract-dialog.tsx +1 -2
  29. package/packages/bundled-features/src/billing-foundation/web/checkout-consent-dialog.tsx +16 -6
  30. package/packages/bundled-features/src/billing-foundation/web/index.ts +12 -0
  31. package/packages/bundled-features/src/legal-pages/changes.json +6 -0
  32. package/packages/bundled-features/src/legal-pages/constants.ts +2 -0
  33. package/packages/bundled-features/src/legal-pages/feature.ts +8 -3
  34. package/packages/bundled-features/src/renderer-simple/changes.json +6 -0
  35. package/packages/bundled-features/src/renderer-simple/simple-renderer.ts +10 -0
  36. package/packages/bundled-features/src/step-dispatcher/changes.json +6 -0
  37. package/packages/bundled-features/src/step-dispatcher/feature.ts +2 -1
  38. package/packages/bundled-features/src/step-dispatcher/webhook-runner.ts +10 -0
  39. package/samples/apps/use-all-bundled/e2e-billing-gallery/gallery-features.ts +10 -11
  40. package/samples/apps/use-all-bundled/e2e-billing-gallery/server.ts +2 -2
  41. package/samples/apps/use-all-bundled/feature-manifest.json +2 -1
  42. package/samples/recipes/i18n/README.md +1 -1
  43. package/samples/recipes/rate-limiting/README.md +1 -1
  44. package/samples/recipes/webhook-step/README.md +8 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-samples",
3
- "version": "0.343.0",
3
+ "version": "0.345.0",
4
4
  "description": "Source trees of the Kumiko sample recipes, sample apps and bundled features in repo layout, for tooling such as few-shot corpus builds.",
5
5
  "license": "BUSL-1.1",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-bundled-features",
3
- "version": "0.343.0",
3
+ "version": "0.345.0",
4
4
  "description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
5
5
  "license": "BUSL-1.1",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -1,4 +1,10 @@
1
1
  [
2
+ {
3
+ "version": "0.345.0",
4
+ "type": "fix",
5
+ "title": "Escape-hatch retention loads only expirable rows",
6
+ "detail": "Escape-hatch audit retention loads only rows that can expire\nThe retention job used to load every escapeHatchUse event to decide which ones to prune. It now reads the storing tenants with one grouped query and loads only events older than each storing tenant's cutoff. The same rows are pruned as before."
7
+ },
2
8
  {
3
9
  "version": "0.343.0",
4
10
  "type": "improvement",
@@ -1,4 +1,4 @@
1
- import { selectMany } from "@cosmicdrift/kumiko-framework/bun-db";
1
+ import { aggregateWhere, selectMany } from "@cosmicdrift/kumiko-framework/bun-db";
2
2
  import {
3
3
  COMPLIANCE_PROFILES,
4
4
  subtractRetentionSpec,
@@ -82,10 +82,20 @@ function targetTenantOf(payload: unknown): TenantId | null {
82
82
  return parsed.success ? parseTenantId(parsed.data.targetTenantId) : null;
83
83
  }
84
84
 
85
- async function pruneByTenantProfiles(db: DbConnection): Promise<number> {
86
- const rows = await selectMany<AuditEventRow>(db, eventsTable, {
87
- aggregateType: ESCAPE_HATCH_USE_AGGREGATE_TYPE,
85
+ async function storingTenantIds(db: DbConnection): Promise<readonly TenantId[]> {
86
+ const groups = await aggregateWhere(
87
+ db,
88
+ eventsTable,
89
+ { measure: { fn: "count" }, groupBy: [{ field: "tenantId" }] },
90
+ { aggregateType: ESCAPE_HATCH_USE_AGGREGATE_TYPE },
91
+ );
92
+ return groups.flatMap((group) => {
93
+ const tenantId = parseTenantId(group.keys[0]);
94
+ return tenantId ? [tenantId] : [];
88
95
  });
96
+ }
97
+
98
+ async function pruneByTenantProfiles(db: DbConnection): Promise<number> {
89
99
  const now = getTemporal().Now.instant();
90
100
  const cutoffByTenant = new Map<TenantId, Temporal.Instant>();
91
101
  const cutoffOf = async (tenantId: TenantId): Promise<Temporal.Instant> => {
@@ -96,15 +106,24 @@ async function pruneByTenantProfiles(db: DbConnection): Promise<number> {
96
106
  return cutoff;
97
107
  };
98
108
 
109
+ // The effective cutoff is never later than the storing tenant's, so only rows older than
110
+ // that one can expire; everything younger stays in the database.
99
111
  const expiredAggregateIds: string[] = [];
100
- for (const row of rows) {
101
- const targetTenantId = targetTenantOf(row.payload);
102
- const storeCutoff = await cutoffOf(row.tenantId);
103
- const cutoff = targetTenantId
104
- ? earlierInstant(storeCutoff, await cutoffOf(targetTenantId))
105
- : storeCutoff;
106
- if (Temporal.Instant.compare(row.createdAt, cutoff) < 0) {
107
- expiredAggregateIds.push(row.aggregateId);
112
+ for (const storingTenantId of await storingTenantIds(db)) {
113
+ const storeCutoff = await cutoffOf(storingTenantId);
114
+ const candidates = await selectMany<AuditEventRow>(db, eventsTable, {
115
+ aggregateType: ESCAPE_HATCH_USE_AGGREGATE_TYPE,
116
+ tenantId: storingTenantId,
117
+ createdAt: { lt: storeCutoff },
118
+ });
119
+ for (const row of candidates) {
120
+ const targetTenantId = targetTenantOf(row.payload);
121
+ const cutoff = targetTenantId
122
+ ? earlierInstant(storeCutoff, await cutoffOf(targetTenantId))
123
+ : storeCutoff;
124
+ if (Temporal.Instant.compare(row.createdAt, cutoff) < 0) {
125
+ expiredAggregateIds.push(row.aggregateId);
126
+ }
108
127
  }
109
128
  }
110
129
 
@@ -1,4 +1,28 @@
1
1
  [
2
+ {
3
+ "version": "0.345.0",
4
+ "type": "fix",
5
+ "title": "invite-signup-complete enforces the MFA gate before issuing a session",
6
+ "detail": "`invite-signup-complete` now applies the same MFA gate as `invite-accept-with-login`. The account is still created and the invitation accepted, but an invitee whose role requires MFA (for example an admin invitation under the `admins` policy) receives the MFA step instead of a session. Member invitations keep receiving a session directly."
7
+ },
8
+ {
9
+ "version": "0.345.0",
10
+ "type": "fix",
11
+ "title": "signup-confirm enforces the MFA gate before issuing a session",
12
+ "detail": "`POST /auth/signup-confirm` now runs the login's MFA gate before it issues the first session. Self-signup makes the new user TenantAdmin of a fresh tenant, so under an MFA policy that covers admins the route used to hand out an admin session without a second factor. The account, the tenant and a bound handover claim are still created, but when the gate applies the route answers with the login contract (`mfaSetupRequired` plus `preauthSetupToken`, or `mfaRequired` plus `challengeToken`) and sets no cookies. `SignupCompleteScreen` then tells the user the account is active and sends them to sign in, where they set up the second factor. Without `mfaStatusChecker` nothing changes. Two exported types change shape: `SignupConfirmData` gains the two MFA variants, and `confirmSignup` now resolves to `SignupConfirmResult` with `kind: \"signed-in\"` (the previous `SignupConfirmSuccess` fields) or `kind: \"mfa-pending\"`. The handler and `invite-signup-complete` also run the gate before they delete the token, so an error in the MFA check leaves the link usable for a retry."
13
+ },
14
+ {
15
+ "version": "0.345.0",
16
+ "type": "fix",
17
+ "title": "switch-tenant enforces the MFA gate before issuing a session",
18
+ "detail": "`POST /auth/switch-tenant` now runs the same MFA gate as login before it issues the new session. A user who is a plain member in one tenant and an admin in another can no longer reach an admin session without a second factor by switching. When the target tenant requires MFA the route answers with the login contract (`mfaRequired` plus `challengeToken`, or `mfaSetupRequired` plus `preauthSetupToken`) and sets no cookies. The gate runs in a new system-only handler, `auth-email-password:write:switch-tenant-mfa-gate`, which resolves the membership and roles itself; `runDevApp` and `runProdApp` wire it as `switchTenantMfaGateHandler` whenever auth-mfa is mounted."
19
+ },
20
+ {
21
+ "version": "0.345.0",
22
+ "type": "fix",
23
+ "title": "Token mail expiry uses the recipient's profile time zone",
24
+ "detail": "Token mails show the expiry in the recipient's own time zone\nPassword reset, email verification and account unlock are requested anonymously, so `ctx.tz.user` only held the tenant default or UTC. These mails now use the recipient's profile time zone when it is a valid IANA zone and fall back to the previous value otherwise. Signup and invite mails keep the fallback because the recipient has no profile yet."
25
+ },
2
26
  {
3
27
  "version": "0.343.0",
4
28
  "type": "improvement",
@@ -46,6 +46,7 @@ export const AuthHandlers = {
46
46
  inviteAccept: "auth-email-password:write:invite-accept",
47
47
  inviteAcceptWithLogin: "auth-email-password:write:invite-accept-with-login",
48
48
  inviteSignupComplete: "auth-email-password:write:invite-signup-complete",
49
+ switchTenantMfaGate: "auth-email-password:write:switch-tenant-mfa-gate",
49
50
  inviteCancel: "auth-email-password:write:invite-cancel",
50
51
  } as const;
51
52
 
@@ -28,6 +28,7 @@ import {
28
28
  createSignupRequestHandler,
29
29
  type SignupRequestOptions,
30
30
  } from "./handlers/signup-request.write.js";
31
+ import { createSwitchTenantMfaGateHandler } from "./handlers/switch-tenant-mfa-gate.write.js";
31
32
  import { createVerifyEmailHandler } from "./handlers/verify-email.write.js";
32
33
 
33
34
  /**
@@ -221,6 +222,10 @@ export function createAuthEmailPasswordFeature(
221
222
  logout: r.writeHandler(logoutWrite),
222
223
  };
223
224
 
225
+ if (opts.mfaStatusChecker) {
226
+ r.writeHandler(createSwitchTenantMfaGateHandler({ mfaStatusChecker: opts.mfaStatusChecker }));
227
+ }
228
+
224
229
  if (opts.passwordReset) {
225
230
  r.writeHandler(createRequestPasswordResetHandler(opts.passwordReset));
226
231
  r.writeHandler(createResetPasswordHandler(opts.passwordReset));
@@ -242,7 +247,7 @@ export function createAuthEmailPasswordFeature(
242
247
 
243
248
  if (opts.signup) {
244
249
  r.writeHandler(createSignupRequestHandler(opts.signup));
245
- r.writeHandler(createSignupConfirmHandler());
250
+ r.writeHandler(createSignupConfirmHandler({ mfaStatusChecker: opts.mfaStatusChecker }));
246
251
  }
247
252
 
248
253
  if (opts.invite) {
@@ -258,7 +263,9 @@ export function createAuthEmailPasswordFeature(
258
263
  strictEmailVerification: strictVerification,
259
264
  }),
260
265
  );
261
- r.writeHandler(createInviteSignupCompleteHandler());
266
+ r.writeHandler(
267
+ createInviteSignupCompleteHandler({ mfaStatusChecker: opts.mfaStatusChecker }),
268
+ );
262
269
  }
263
270
 
264
271
  if (opts.accountUnlock) {
@@ -60,18 +60,23 @@ import {
60
60
  import { passwordSchema } from "../password-policy.js";
61
61
  // kumiko-lint-ignore cross-feature-import provisioning needs cross-feature seeding helpers
62
62
  import { seedUserWithPassword } from "../seeding.js";
63
+ import { gateEnforceMfa, type LoginHandlerOptions, type LoginResult } from "./login.write.js";
63
64
 
64
65
  const InviteSignupCompleteSchema = z.object({
65
66
  token: z.string().min(1),
66
67
  password: passwordSchema,
67
68
  });
68
69
 
69
- export type InviteSignupCompleteData = {
70
- readonly kind: "auth-session";
71
- readonly session: SessionUser;
72
- readonly tenantId: TenantId;
73
- readonly role: string;
74
- };
70
+ export type InviteSignupCompleteData =
71
+ | {
72
+ readonly kind: "auth-session";
73
+ readonly session: SessionUser;
74
+ readonly tenantId: TenantId;
75
+ readonly role: string;
76
+ }
77
+ | Exclude<LoginResult, { readonly kind: "auth-session" }>;
78
+
79
+ export type InviteSignupCompleteOptions = Pick<LoginHandlerOptions, "mfaStatusChecker">;
75
80
 
76
81
  const invitationExecutor = createEventStoreExecutor(
77
82
  tenantInvitationsTable,
@@ -82,7 +87,7 @@ const invitationExecutor = createEventStoreExecutor(
82
87
  const INVITE_SIGNUP_COMPLETE_ESCAPE_HATCH_REASON =
83
88
  "reads the pending invitation by id; the invitee is not yet a member of the invitation's tenant. Creates the user and adds the membership and accepts the invitation in the invitation's tenant, before any caller tenant context exists.";
84
89
 
85
- export function createInviteSignupCompleteHandler() {
90
+ export function createInviteSignupCompleteHandler(opts: InviteSignupCompleteOptions = {}) {
86
91
  return defineWriteHandler<
87
92
  "invite-signup-complete",
88
93
  typeof InviteSignupCompleteSchema,
@@ -190,14 +195,32 @@ export function createInviteSignupCompleteHandler() {
190
195
  );
191
196
  if (!updateResult.isSuccess) return updateResult;
192
197
 
198
+ // buildSessionRoles calls stripForbiddenMembershipRoles internally —
199
+ // a reserved role on the invitation itself must never reach the session.
200
+ const mergedRoles = buildSessionRoles(invitationGlobalRoles, [invitationRole]);
201
+
202
+ // Same gate as invite-accept-with-login: the account exists after this
203
+ // call, but an MFA-gated role gets the MFA step instead of a session.
204
+ // It runs before the token is deleted so a throwing check leaves the link retryable.
205
+ const mfaGate = await gateEnforceMfa(
206
+ ctx,
207
+ { mfaStatusChecker: opts.mfaStatusChecker },
208
+ userId,
209
+ invitationTenantId,
210
+ mergedRoles,
211
+ );
212
+
193
213
  await deleteInviteToken(ctx.redis, { invitationId, token: event.payload.token });
194
214
 
215
+ if (mfaGate !== undefined) {
216
+ committed = true;
217
+ return { isSuccess: true, data: mfaGate };
218
+ }
219
+
195
220
  const session: SessionUser = {
196
221
  id: userId,
197
222
  tenantId: invitationTenantId,
198
- // buildSessionRoles calls stripForbiddenMembershipRoles internally —
199
- // a reserved role on the invitation itself must never reach the session.
200
- roles: buildSessionRoles(invitationGlobalRoles, [invitationRole]),
223
+ roles: mergedRoles,
201
224
  };
202
225
 
203
226
  committed = true;
@@ -56,26 +56,29 @@ import {
56
56
  getSignupHandover,
57
57
  unburnSignupToken,
58
58
  } from "../signup-token-store.js";
59
+ import { gateEnforceMfa, type LoginHandlerOptions, type LoginResult } from "./login.write.js";
59
60
 
60
61
  const SignupConfirmSchema = z.object({
61
62
  token: z.string().min(8),
62
63
  password: passwordSchema,
63
64
  });
64
65
 
65
- // Mirror der login-handler-Shape (kind: "auth-session", session: SessionUser)
66
- // damit die Route-Layer den signup-confirm-success genauso behandeln kann
67
- // wie einen erfolgreichen login: JWT-Mint, Cookies setzen, Session-Body
68
- // returnen. Der zusätzliche tenantKey landet als sibling am data-objekt
69
- // (NICHT in SessionUser — der ist generic, tenantKey ist signup-spezifisch
70
- // für den Post-Signup-Redirect zu /<tenantKey>/).
71
- export type SignupConfirmData = {
72
- readonly kind: "auth-session";
73
- readonly session: SessionUser;
74
- readonly tenantKey: string;
75
- // Present only when a bound tenant-handover grant existed AND its claim
76
- // actually succeeded (see the handler body below).
77
- readonly handover?: { readonly entityType: string; readonly id: string };
78
- };
66
+ // Mirrors the login handler's session shape so the route treats a successful
67
+ // confirm like a login. tenantKey stays a sibling of the session: it is only
68
+ // for the post-signup redirect, SessionUser is generic. When the MFA gate
69
+ // applies, the MFA step is returned instead of a session.
70
+ export type SignupConfirmData =
71
+ | {
72
+ readonly kind: "auth-session";
73
+ readonly session: SessionUser;
74
+ readonly tenantKey: string;
75
+ // Present only when a bound tenant-handover grant existed AND its claim
76
+ // actually succeeded (see the handler body below).
77
+ readonly handover?: { readonly entityType: string; readonly id: string };
78
+ }
79
+ | Exclude<LoginResult, { readonly kind: "auth-session" }>;
80
+
81
+ export type SignupConfirmOptions = Pick<LoginHandlerOptions, "mfaStatusChecker">;
79
82
 
80
83
  const SIGNUP_CONFIRM_PROVISION_REASON =
81
84
  "provisions a new tenant, its first user and membership before any tenant context exists";
@@ -123,7 +126,7 @@ async function claimBoundHandover(
123
126
  return undefined;
124
127
  }
125
128
 
126
- export function createSignupConfirmHandler() {
129
+ export function createSignupConfirmHandler(opts: SignupConfirmOptions = {}) {
127
130
  return defineWriteHandler<"signup-confirm", typeof SignupConfirmSchema, SignupConfirmData>({
128
131
  name: "signup-confirm",
129
132
  schema: SignupConfirmSchema,
@@ -221,11 +224,25 @@ export function createSignupConfirmHandler() {
221
224
  ? await claimBoundHandover(ctx, session, handoverBinding)
222
225
  : undefined;
223
226
 
227
+ // Before the token keys go: a throwing MFA check must leave the link retryable.
228
+ const mfaGate = await gateEnforceMfa(
229
+ ctx,
230
+ { mfaStatusChecker: opts.mfaStatusChecker },
231
+ provisioned.userId,
232
+ provisioned.tenantId,
233
+ session.roles,
234
+ );
235
+
224
236
  // Drop both token lookup keys; the burn key stays for its remaining
225
237
  // TTL as replay protection.
226
238
  await deleteSignupToken(ctx.redis, { email, token: event.payload.token });
227
239
  if (handoverBinding) await deleteSignupHandover(ctx.redis, event.payload.token);
228
240
 
241
+ if (mfaGate !== undefined) {
242
+ committed = true;
243
+ return { isSuccess: true, data: mfaGate };
244
+ }
245
+
229
246
  committed = true;
230
247
  return {
231
248
  isSuccess: true,
@@ -0,0 +1,60 @@
1
+ import {
2
+ buildSessionRoles,
3
+ createSystemUser,
4
+ defineWriteHandler,
5
+ SYSTEM_ROLE,
6
+ type TenantId,
7
+ } from "@cosmicdrift/kumiko-framework/engine";
8
+ import { parseRoles } from "@cosmicdrift/kumiko-framework/utils";
9
+ import * as z from "zod";
10
+ import { UserQueries } from "../../user/index.js";
11
+ import { parseAuthUserRow } from "../auth-user-row.js";
12
+ import { noMembership } from "../errors.js";
13
+ import { gateEnforceMfa, type LoginHandlerOptions } from "./login.write.js";
14
+
15
+ const SwitchTenantMfaGateSchema = z.object({
16
+ userId: z.string().min(1),
17
+ tenantId: z
18
+ .string()
19
+ .min(1)
20
+ .transform((id) => id as TenantId),
21
+ });
22
+
23
+ export type SwitchTenantMfaGateData =
24
+ | { readonly kind: "mfa-gate-clear" }
25
+ | NonNullable<Awaited<ReturnType<typeof gateEnforceMfa>>>;
26
+
27
+ // Dispatched by POST /auth/switch-tenant with the system identity. The handler
28
+ // resolves membership and roles itself, so the gate never trusts caller-supplied
29
+ // roles; system-only access keeps it out of reach of /api/write callers.
30
+ export function createSwitchTenantMfaGateHandler(
31
+ opts: Pick<LoginHandlerOptions, "mfaStatusChecker">,
32
+ ) {
33
+ return defineWriteHandler<
34
+ "switch-tenant-mfa-gate",
35
+ typeof SwitchTenantMfaGateSchema,
36
+ SwitchTenantMfaGateData
37
+ >({
38
+ name: "switch-tenant-mfa-gate",
39
+ schema: SwitchTenantMfaGateSchema,
40
+ access: { roles: [SYSTEM_ROLE] },
41
+ escapeHatch: {
42
+ reason:
43
+ "reads the MFA enrollment of the switch target tenant, not the caller's current tenant, via the mfaStatusChecker callback (it uses ctx.db.unsafeRaw())",
44
+ },
45
+ agent: { expose: false },
46
+ description:
47
+ "Runs the MFA gate of the login for a tenant switch and answers with an MFA challenge, a setup requirement, or clearance.",
48
+ handler: async (event, ctx) => {
49
+ const { userId, tenantId } = event.payload;
50
+ const active = await ctx.resolveActiveMembership(userId, tenantId);
51
+ if (active.kind === "rejected") return noMembership();
52
+ const userRow = parseAuthUserRow(
53
+ await ctx.queryAs(createSystemUser(tenantId), UserQueries.findForAuth, { id: userId }),
54
+ );
55
+ const roles = buildSessionRoles(parseRoles(userRow?.roles ?? null), active.membership.roles);
56
+ const mfaGate = await gateEnforceMfa(ctx, opts, userId, tenantId, roles);
57
+ return { isSuccess: true, data: mfaGate ?? { kind: "mfa-gate-clear" } };
58
+ },
59
+ });
60
+ }
@@ -14,6 +14,7 @@
14
14
 
15
15
  import { createSystemUser, defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
16
16
  import { UnprocessableError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
17
+ import { isValidIanaTimeZone } from "@cosmicdrift/kumiko-framework/time";
17
18
  import type { Temporal } from "temporal-polyfill";
18
19
  import * as z from "zod";
19
20
  import { UserQueries } from "../../user/index.js";
@@ -89,6 +90,12 @@ export type TokenRequestOptions = {
89
90
  readonly locale?: AuthMailLocale;
90
91
  };
91
92
 
93
+ // The requester is anonymous, so ctx.tz.user is only the tenant default; the recipient's
94
+ // own profile zone is the one the expiry should be read in.
95
+ function recipientTimeZone(user: AuthUserRow, fallbackTimeZone: string): string {
96
+ return user.timezone && isValidIanaTimeZone(user.timezone) ? user.timezone : fallbackTimeZone;
97
+ }
98
+
92
99
  export function createTokenRequestHandler<TName extends string, TSuccessKind extends string>(
93
100
  spec: TokenRequestSpec<TName, TSuccessKind>,
94
101
  opts: TokenRequestOptions,
@@ -156,7 +163,7 @@ export function createTokenRequestHandler<TName extends string, TSuccessKind ext
156
163
  token,
157
164
  expiresAt: expiresAt.toString(),
158
165
  issuedAt,
159
- timeZone: ctx.tz.user,
166
+ timeZone: recipientTimeZone(user, ctx.tz.user),
160
167
  ...(opts.appName !== undefined && { appName: opts.appName }),
161
168
  locale,
162
169
  },
@@ -116,6 +116,8 @@ export const defaultTranslations: TranslationsByLocale = {
116
116
  "auth.signupComplete.submitting": "…",
117
117
  "auth.signupComplete.missingToken":
118
118
  "Activation link is missing a token. Please request a new one.",
119
+ "auth.signupComplete.activatedMfaPending":
120
+ "Your account is active. Sign in to set up two-factor authentication.",
119
121
  "auth.signupComplete.activatedTitle": "Account activated",
120
122
  "auth.signupComplete.activated": "Your account is active and you're signed in.",
121
123
  "auth.signupComplete.continue": "Continue",
@@ -345,10 +345,10 @@ export async function requestSignup(
345
345
  return { ok: false, error: await parseTokenFailure(res) };
346
346
  }
347
347
 
348
- // POST /api/auth/signup-confirm. Token aus URL + Password. Erfolgreich:
349
- // Cookies (kumiko_auth + kumiko_csrf) werden gesetzt — User ist sofort
350
- // eingeloggt. Response liefert tenantKey für den Post-Signup-Redirect.
351
- // 422 invalid_signup_token bei abgelaufenem/unbekanntem Token.
348
+ // POST /api/auth/signup-confirm. On success the server sets the auth cookies
349
+ // (auto-login) and returns the tenantKey for the post-signup redirect, unless
350
+ // the MFA gate applies (kind "mfa-pending", no session). 422 invalid_signup_token
351
+ // for an expired or unknown token.
352
352
  export type SignupConfirmSuccess = {
353
353
  readonly user: { readonly id: string; readonly tenantId: string; readonly roles: string[] };
354
354
  readonly tenantKey: string;
@@ -360,10 +360,16 @@ export type SignupConfirmSuccess = {
360
360
  readonly landingPath?: string;
361
361
  };
362
362
 
363
+ // mfa-pending: account exists but the server issued no session because the
364
+ // new roles require a second factor — the user must sign in to enroll.
365
+ export type SignupConfirmResult =
366
+ | ({ readonly kind: "signed-in" } & SignupConfirmSuccess)
367
+ | { readonly kind: "mfa-pending" };
368
+
363
369
  export async function confirmSignup(
364
370
  token: string,
365
371
  password: string,
366
- ): Promise<{ ok: true; data: SignupConfirmSuccess } | { ok: false; error: AuthTokenFailure }> {
372
+ ): Promise<{ ok: true; data: SignupConfirmResult } | { ok: false; error: AuthTokenFailure }> {
367
373
  const res = await fetch("/api/auth/signup-confirm", {
368
374
  method: "POST",
369
375
  credentials: "same-origin",
@@ -371,8 +377,15 @@ export async function confirmSignup(
371
377
  body: JSON.stringify({ token, password }),
372
378
  });
373
379
  if (res.ok) {
374
- const body = (await res.json()) as SignupConfirmSuccess; // @cast-boundary engine-payload
375
- return { ok: true, data: body };
380
+ // @cast-boundary engine-payload
381
+ const body = (await res.json()) as SignupConfirmSuccess & {
382
+ readonly mfaRequired?: boolean;
383
+ readonly mfaSetupRequired?: boolean;
384
+ };
385
+ if (body.mfaRequired === true || body.mfaSetupRequired === true) {
386
+ return { ok: true, data: { kind: "mfa-pending" } };
387
+ }
388
+ return { ok: true, data: { kind: "signed-in", ...body } };
376
389
  }
377
390
  return { ok: false, error: await parseTokenFailure(res) };
378
391
  }
@@ -57,6 +57,7 @@ export function SignupCompleteScreen({
57
57
  const [submitting, setSubmitting] = useState(false);
58
58
  const [error, setError] = useState<string | null>(null);
59
59
  const [continueHref, setContinueHref] = useState<string | null>(null);
60
+ const [mfaPending, setMfaPending] = useState(false);
60
61
 
61
62
  const doSubmit = async (): Promise<void> => {
62
63
  setError(null);
@@ -73,6 +74,11 @@ export function SignupCompleteScreen({
73
74
  const res = await confirmSignup(token, password);
74
75
  setSubmitting(false);
75
76
  if (res.ok) {
77
+ if (res.data.kind === "mfa-pending") {
78
+ setMfaPending(true);
79
+ setContinueHref(loginHref);
80
+ return;
81
+ }
76
82
  // Cookies are already set (auto-login). Show a confirmation with an
77
83
  // explicit continue button instead of navigating away silently —
78
84
  // the user otherwise gets no signal that activation worked.
@@ -124,7 +130,11 @@ export function SignupCompleteScreen({
124
130
  return (
125
131
  <AuthCard title={effectiveTitle}>
126
132
  <p className="text-sm text-muted-foreground" role="status">
127
- {t("auth.signupComplete.activated")}
133
+ {t(
134
+ mfaPending
135
+ ? "auth.signupComplete.activatedMfaPending"
136
+ : "auth.signupComplete.activated",
137
+ )}
128
138
  </p>
129
139
  <Link href={continueHref} variant="button">
130
140
  {t("auth.signupComplete.continue")}
@@ -1,4 +1,11 @@
1
1
  [
2
+ {
3
+ "version": "0.344.0",
4
+ "type": "breaking",
5
+ "title": "Consumer-protection gaps - per-locale legal links, gate order, confirmation mail, exports",
6
+ "detail": "All changes below apply only when `createBillingFoundationFeature` gets `consumerProtection`. Apps without it behave as before.\n`ConsumerProtectionOptions.legalLinks` takes either one `LegalLinkSet` or one set per consent locale (`{ de, en }`, both required). `BillingPlansResult.consumerProtection.legalLinks` is now always `Record<\"de\" | \"en\", LegalLinkSet>`; a single set is returned for both locales. Code that reads `legalLinks.terms` from the billing-plans result must read `legalLinks[locale].terms`. `CheckoutConsentDialog` picks the set for the consent locale.\n`FALLBACK_CONSENT_LOCALE` is now `\"en\"` (was `\"de\"`). A locale other than de/en, or none, resolves to English in `resolveConsentLocale`, the consent gate, the confirmation mail's VAT note and the termination record.\n`create-checkout-session` with consumer protection now checks in this order: provider, billing enabled, redirect origins, price and subscription gates, then the live price (new `UnprocessableError` `price_unavailable` when the provider cannot return it), then the consent, then the provider call. A missing consent no longer hides a disabled provider, an existing subscription or an unknown price. Its payload schema is strict, so unknown fields are rejected. `assertCheckoutAllowed` and `startProviderCheckout` split `openCheckout` in checkout-core.\nNew option `ConsumerProtectionOptions.oneOffItemLabel(ctx, priceId)`: the server resolves the label of a one-off item (for example a credit pack). It is stored on the consent event as `itemLabelKey` / `itemLabelParams` and shown in the confirmation mail as \"Leistung\" / \"Item\". The client cannot set it.\nThe terms text block is read from the system tenant, not from the buyer's tenant, in the consent gate and the confirmation mail. A tenant-owned block with the same slug no longer changes `termsHash` or the mailed text.\nThe confirmation mail shows dates in the recipient's time zone (user zone, else tenant zone, else UTC) with the zone name, renders the terms as Markdown with real headings and shows the translated plan name (`catalog.tierLabelKey`, looked up through the registry i18n, so the key must be qualified with the registering feature; untranslated keys fall back to the raw tier). Its opening sentence starts with a capital letter.\nNew exports: `consentPayloadSchema`, `ConsentPayload`, `OrderItem`, `CONSENT_LOCALES`, `ConsentLocale`, `resolveConsentLocale`, `LegalLinkSet`. The `/web` entry adds `CheckoutConsentDialog`, `CheckoutConsentDialogProps`, `CheckoutConsentPayload`, `CONSENT_TEXT_OUTDATED_CODE`, `resolveConsentLocale`, `ConsentLocale` and `LegalLinkSet`. `CheckoutConsentDialog` `price.count` is optional, and the \"cancel at any time\" note only shows for a recurring price (`price.renewalKey` set).\nThe checkout and cancel dialogs no longer repeat the modal title as a heading, and the cancel-contract button is hidden once a cancellation is scheduled.",
7
+ "migration": "Only apps that pass consumerProtection are affected. BillingPlansResult.consumerProtection.legalLinks is now keyed by locale (legalLinks.de / legalLinks.en). FALLBACK_CONSENT_LOCALE changed from de to en. create-checkout-session rejects unknown payload fields when consumer protection is on."
8
+ },
2
9
  {
3
10
  "version": "0.341.0",
4
11
  "type": "improvement",
@@ -329,17 +329,27 @@ function assertOneOffPriceAllowed(
329
329
  }
330
330
  }
331
331
 
332
- /** The one checkout entry-point create-checkout-session and
333
- * start-plan-checkout both funnel through. `mode: "payment"` (one-off
334
- * top-ups etc.) skips the price/subscription gates entirely — those apply
335
- * to recurring subscriptions only. */
336
- export async function openCheckout(
332
+ type CheckoutCapableProvider = SubscriptionProviderPlugin & {
333
+ readonly createCheckoutSession: NonNullable<SubscriptionProviderPlugin["createCheckoutSession"]>;
334
+ };
335
+
336
+ function canCreateCheckoutSession(
337
+ plugin: SubscriptionProviderPlugin,
338
+ ): plugin is CheckoutCapableProvider {
339
+ return plugin.createCheckoutSession !== undefined;
340
+ }
341
+
342
+ /** All gates that run before a provider checkout call: provider, billing
343
+ * enabled, redirect origins, mode gates, foreign provider customer.
344
+ * `mode: "payment"` (one-off top-ups etc.) skips the price/subscription
345
+ * gates — those apply to recurring subscriptions only. */
346
+ export async function assertCheckoutAllowed(
337
347
  ctx: HandlerContext,
338
348
  options: OpenCheckoutOptions,
339
349
  input: OpenCheckoutInput,
340
- ): Promise<{ readonly url: string; readonly providerName: string }> {
350
+ ): Promise<CheckoutCapableProvider> {
341
351
  const { plugin } = resolveProviderPlugin(ctx, input.providerName);
342
- if (!plugin.createCheckoutSession) {
352
+ if (!canCreateCheckoutSession(plugin)) {
343
353
  throw new Error(
344
354
  `subscription-foundation: provider "${input.providerName}" has no createCheckoutSession-method (e.g. Apple-IAP-only providers). Use the provider's native checkout flow.`,
345
355
  );
@@ -376,7 +386,14 @@ export async function openCheckout(
376
386
  });
377
387
  }
378
388
  }
389
+ return plugin;
390
+ }
379
391
 
392
+ export async function startProviderCheckout(
393
+ ctx: HandlerContext,
394
+ plugin: CheckoutCapableProvider,
395
+ input: OpenCheckoutInput,
396
+ ): Promise<{ readonly url: string; readonly providerName: string }> {
380
397
  const result = await plugin.createCheckoutSession(ctx, {
381
398
  priceId: input.priceId,
382
399
  tenantId: ctx.user.tenantId,
@@ -393,3 +410,14 @@ export async function openCheckout(
393
410
  });
394
411
  return { url: result.url, providerName: input.providerName };
395
412
  }
413
+
414
+ /** The one checkout entry-point start-plan-checkout, switch-plan and the
415
+ * non-consumer-protection create-checkout-session funnel through. */
416
+ export async function openCheckout(
417
+ ctx: HandlerContext,
418
+ options: OpenCheckoutOptions,
419
+ input: OpenCheckoutInput,
420
+ ): Promise<{ readonly url: string; readonly providerName: string }> {
421
+ const plugin = await assertCheckoutAllowed(ctx, options, input);
422
+ return startProviderCheckout(ctx, plugin, input);
423
+ }