@cosmicdrift/kumiko-bundled-features 0.176.0 → 0.176.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-bundled-features",
3
- "version": "0.176.0",
3
+ "version": "0.176.1",
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>",
@@ -120,12 +120,12 @@
120
120
  "./step-dispatcher": "./src/step-dispatcher/index.ts"
121
121
  },
122
122
  "dependencies": {
123
- "@cosmicdrift/kumiko-dispatcher-live": "0.176.0",
124
- "@cosmicdrift/kumiko-framework": "0.176.0",
125
- "@cosmicdrift/kumiko-headless": "0.176.0",
126
- "@cosmicdrift/kumiko-renderer": "0.176.0",
127
- "@cosmicdrift/kumiko-renderer-web": "0.176.0",
128
- "@cosmicdrift/kumiko-types": "0.176.0",
123
+ "@cosmicdrift/kumiko-dispatcher-live": "0.176.1",
124
+ "@cosmicdrift/kumiko-framework": "0.176.1",
125
+ "@cosmicdrift/kumiko-headless": "0.176.1",
126
+ "@cosmicdrift/kumiko-renderer": "0.176.1",
127
+ "@cosmicdrift/kumiko-renderer-web": "0.176.1",
128
+ "@cosmicdrift/kumiko-types": "0.176.1",
129
129
  "@mollie/api-client": "^4.5.0",
130
130
  "imapflow": "^1.3.3",
131
131
  "mailparser": "^3.9.8",
@@ -351,6 +351,37 @@ describe("invite-accept-with-login (Branch 2: anon + existing email)", () => {
351
351
  expect(memberships).toHaveLength(2);
352
352
  });
353
353
 
354
+ test("Bob accepts with a timezone → JWT retains the timezone", async () => {
355
+ await asRawClient(stack.db).unsafe(
356
+ `UPDATE "${userTable.tableName}" SET timezone = $1 WHERE id = $2`,
357
+ ["Europe/Berlin", bobId],
358
+ );
359
+ try {
360
+ const token = await inviteEmail(BOB_EMAIL, "Editor");
361
+
362
+ const res = await stack.http.raw("POST", "/api/auth/invite-accept-with-login", {
363
+ token,
364
+ email: BOB_EMAIL,
365
+ password: BOB_PASSWORD,
366
+ });
367
+ expect(res.status).toBe(200);
368
+ const body = (await res.json()) as { token?: string };
369
+ expect(body.token).toBeTypeOf("string");
370
+ if (!body.token) throw new Error("invite acceptance did not return a token");
371
+
372
+ const payload = await stack.jwt.verify(body.token);
373
+ expect(payload.timezone).toBe("Europe/Berlin");
374
+ } finally {
375
+ // Bob is shared across this describe block (other tests rely on his
376
+ // membership state persisting) — restore his timezone so later tests
377
+ // don't silently inherit it.
378
+ await asRawClient(stack.db).unsafe(
379
+ `UPDATE "${userTable.tableName}" SET timezone = NULL WHERE id = $1`,
380
+ [bobId],
381
+ );
382
+ }
383
+ });
384
+
354
385
  test("Wrong password → 422 invalid_invite_token (anti-enum)", async () => {
355
386
  const token = await inviteEmail(BOB_EMAIL, "Editor");
356
387
  const res = await stack.http.raw("POST", "/api/auth/invite-accept-with-login", {
@@ -28,7 +28,7 @@ import {
28
28
  } from "@cosmicdrift/kumiko-framework/engine";
29
29
  import { InternalError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
30
30
  import { z } from "zod";
31
- import { decryptStoredPii, verifyPassword } from "../../shared";
31
+ import { decryptStoredPii, sessionTimezoneField, verifyPassword } from "../../shared";
32
32
  // kumiko-lint-ignore cross-feature-import invite-flow
33
33
  import {
34
34
  INVITATION_STATUS,
@@ -96,7 +96,11 @@ export function createInviteAcceptWithLoginHandler() {
96
96
  readonly role: string;
97
97
  readonly version: number;
98
98
  };
99
- type UserAuthRow = { readonly id: string; readonly passwordHash: string | null };
99
+ type UserAuthRow = {
100
+ readonly id: string;
101
+ readonly passwordHash: string | null;
102
+ readonly timezone?: string | null;
103
+ };
100
104
 
101
105
  let committed = false;
102
106
  try {
@@ -173,6 +177,7 @@ export function createInviteAcceptWithLoginHandler() {
173
177
  // buildSessionRoles calls stripForbiddenMembershipRoles internally —
174
178
  // a reserved role on the invitation itself must never reach the session.
175
179
  roles: buildSessionRoles([], [invitationRole]),
180
+ ...sessionTimezoneField(userRow.timezone),
176
181
  };
177
182
 
178
183
  committed = true;
@@ -14,9 +14,12 @@ import {
14
14
  expectErrorIncludes,
15
15
  seedRow,
16
16
  } from "@cosmicdrift/kumiko-framework/testing";
17
+ import { AuthHandlers as AuthEmailPasswordHandlers } from "../../auth-email-password/constants";
18
+ import { createAuthEmailPasswordFeature } from "../../auth-email-password/feature";
17
19
  import { createConfigFeature } from "../../config";
18
20
  import { createConfigResolver } from "../../config/resolver";
19
21
  import { configValuesTable } from "../../config/table";
22
+ import { hashPassword } from "../../shared";
20
23
  import { createTenantFeature } from "../../tenant";
21
24
  import { tenantMembershipsTable } from "../../tenant/membership-table";
22
25
  import { tenantEntity } from "../../tenant/schema/tenant";
@@ -25,7 +28,7 @@ import { createUserFeature } from "../../user/feature";
25
28
  import { userEntity, userTable } from "../../user/schema/user";
26
29
  import { base32Decode } from "../base32";
27
30
  import { AuthMfaHandlers } from "../constants";
28
- import { createAuthMfaFeature } from "../feature";
31
+ import { createAuthMfaFeature, mfaStatusCheckerFromFeature } from "../feature";
29
32
  import { signMfaChallengeToken } from "../mfa-challenge-token";
30
33
  import { userMfaEntity } from "../schema/user-mfa";
31
34
  import { currentTotpCode } from "../totp";
@@ -49,18 +52,26 @@ beforeAll(async () => {
49
52
  const encryption = createTestEnvelopeCipher();
50
53
  configureEntityFieldEncryption(encryption);
51
54
  const resolver = createConfigResolver({ cipher: encryption });
55
+ const authMfaFeature = createAuthMfaFeature({
56
+ setupTokenSecret: SETUP_TOKEN_SECRET,
57
+ issuer: "Kumiko Test",
58
+ challengeTokenSecret: CHALLENGE_TOKEN_SECRET,
59
+ });
52
60
  stack = await setupTestStack({
53
61
  features: [
54
62
  createConfigFeature(),
55
63
  createUserFeature(),
56
64
  createTenantFeature(),
57
- createAuthMfaFeature({
58
- setupTokenSecret: SETUP_TOKEN_SECRET,
59
- issuer: "Kumiko Test",
60
- challengeTokenSecret: CHALLENGE_TOKEN_SECRET,
65
+ authMfaFeature,
66
+ createAuthEmailPasswordFeature({
67
+ mfaStatusChecker: mfaStatusCheckerFromFeature(authMfaFeature),
61
68
  }),
62
69
  ],
63
70
  extraContext: { configResolver: resolver, configEncryption: encryption },
71
+ authConfig: {
72
+ membershipQuery: "tenant:query:memberships",
73
+ loginHandler: AuthEmailPasswordHandlers.login,
74
+ },
64
75
  });
65
76
  await unsafeCreateEntityTable(stack.db, userEntity);
66
77
  await unsafeCreateEntityTable(stack.db, tenantEntity);
@@ -76,8 +87,10 @@ async function enableMfaFor(idSeed: number): Promise<{
76
87
  user: ReturnType<typeof createTestUser>;
77
88
  secret: Buffer;
78
89
  recoveryCodes: string[];
90
+ password: string;
79
91
  }> {
80
92
  const user = createTestUser({ id: idSeed, roles: ["User"] });
93
+ const password = `password-${idSeed}-long-enough`;
81
94
  const start = await stack.http.writeOk<{
82
95
  setupToken: string;
83
96
  otpauthUri: string;
@@ -103,14 +116,14 @@ async function enableMfaFor(idSeed: number): Promise<{
103
116
  id: user.id,
104
117
  tenantId: user.tenantId,
105
118
  email: `user-${user.id}@example.com`,
106
- passwordHash: "h",
119
+ passwordHash: await hashPassword(password),
107
120
  displayName: `Test User ${idSeed}`,
108
121
  locale: "de",
109
122
  emailVerified: true,
110
123
  roles: "[]",
111
124
  status: USER_STATUS.Active,
112
125
  });
113
- return { user, secret, recoveryCodes: start.recoveryCodes };
126
+ return { user, secret, recoveryCodes: start.recoveryCodes, password };
114
127
  }
115
128
 
116
129
  function challengeFor(userId: string, tenantId: TenantId): string {
@@ -131,6 +144,50 @@ describe("mfa verify — completes a two-step login", () => {
131
144
  expect(res.session.tenantId).toBe(user.tenantId);
132
145
  });
133
146
 
147
+ test("a password login followed by MFA verify retains the user's timezone", async () => {
148
+ const { user, password, secret } = await enableMfaFor(9);
149
+ await asRawClient(stack.db).unsafe(
150
+ `UPDATE "${userTable.tableName}" SET timezone = $1 WHERE id = $2`,
151
+ ["Europe/Berlin", user.id],
152
+ );
153
+
154
+ const loginRes = await stack.http.raw("POST", "/api/auth/login", {
155
+ email: `user-${user.id}@example.com`,
156
+ password,
157
+ });
158
+ expect(loginRes.status).toBe(200);
159
+ const login = (await loginRes.json()) as { challengeToken?: string };
160
+ expect(login.challengeToken).toBeTypeOf("string");
161
+ if (!login.challengeToken) throw new Error("MFA login did not return a challenge token");
162
+
163
+ const verified = await stack.http.writeOk<{ session: SessionUser }>(
164
+ AuthMfaHandlers.verify,
165
+ { challengeToken: login.challengeToken, code: currentTotpCode(secret) },
166
+ GUEST,
167
+ );
168
+ expect(verified.session.timezone).toBe("Europe/Berlin");
169
+ });
170
+
171
+ test("a password login followed by MFA verify omits an unset timezone", async () => {
172
+ const { user, password, secret } = await enableMfaFor(10);
173
+
174
+ const loginRes = await stack.http.raw("POST", "/api/auth/login", {
175
+ email: `user-${user.id}@example.com`,
176
+ password,
177
+ });
178
+ expect(loginRes.status).toBe(200);
179
+ const login = (await loginRes.json()) as { challengeToken?: string };
180
+ expect(login.challengeToken).toBeTypeOf("string");
181
+ if (!login.challengeToken) throw new Error("MFA login did not return a challenge token");
182
+
183
+ const verified = await stack.http.writeOk<{ session: SessionUser }>(
184
+ AuthMfaHandlers.verify,
185
+ { challengeToken: login.challengeToken, code: currentTotpCode(secret) },
186
+ GUEST,
187
+ );
188
+ expect(verified.session.timezone).toBeUndefined();
189
+ });
190
+
134
191
  test("a wrong TOTP code is rejected", async () => {
135
192
  const { user } = await enableMfaFor(2);
136
193
  const challengeToken = challengeFor(user.id, user.tenantId);
@@ -8,7 +8,7 @@ import {
8
8
  import { InternalError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
9
9
  import { parseRoles } from "@cosmicdrift/kumiko-framework/utils";
10
10
  import { z } from "zod";
11
- import { burnToken } from "../../shared";
11
+ import { burnToken, sessionTimezoneField } from "../../shared";
12
12
  import { USER_STATUS, UserQueries } from "../../user";
13
13
  import { MFA_VERIFY_LOCKOUT_MINUTES, MFA_VERIFY_MAX_ATTEMPTS } from "../constants";
14
14
  import { findUserMfaRow } from "../db/queries";
@@ -135,7 +135,7 @@ export function createMfaVerifyHandler(opts: MfaVerifyOptions) {
135
135
  const systemUser = createSystemUser(tenantId, ["SystemAdmin"]);
136
136
  const userRow = (await ctx.queryAs(systemUser, UserQueries.findForAuth, {
137
137
  id: userId,
138
- })) as { roles?: string | null; status?: string } | null; // @cast-boundary engine-payload
138
+ })) as { roles?: string | null; status?: string; timezone?: string | null } | null; // @cast-boundary engine-payload
139
139
 
140
140
  // Re-check status + membership the way login.write.ts does after its
141
141
  // password check — the challenge token only proves "password was
@@ -168,7 +168,12 @@ export function createMfaVerifyHandler(opts: MfaVerifyOptions) {
168
168
  // read-time backstop against a rebuild-resurrected role.
169
169
  const mergedRoles = buildSessionRoles(globalRoles, membership.roles);
170
170
 
171
- const baseSession: SessionUser = { id: userId, tenantId, roles: mergedRoles };
171
+ const baseSession: SessionUser = {
172
+ id: userId,
173
+ tenantId,
174
+ roles: mergedRoles,
175
+ ...sessionTimezoneField(userRow?.timezone),
176
+ };
172
177
  const claims = await ctx.resolveAuthClaims(baseSession);
173
178
  const session: SessionUser =
174
179
  Object.keys(claims).length > 0 ? { ...baseSession, claims } : baseSession;
@@ -16,5 +16,6 @@ export { isWithinGracePeriod } from "./grace-period";
16
16
  export { isIdentityV3Hash, verifyIdentityV3Hash } from "./identity-v3-hash";
17
17
  export { mapWithConcurrency } from "./map-with-concurrency";
18
18
  export { hashPassword, verifyDummyPassword, verifyPassword } from "./password-hashing";
19
+ export { sessionTimezoneField } from "./session-timezone-field";
19
20
  export type { SystemQueryFn } from "./system-query";
20
21
  export { type BurnResult, burnToken, unburnToken } from "./token-burn-store";
@@ -0,0 +1,7 @@
1
+ import type { SessionUser } from "@cosmicdrift/kumiko-framework/engine";
2
+
3
+ export function sessionTimezoneField(
4
+ timezone: string | null | undefined,
5
+ ): Pick<SessionUser, "timezone"> | Record<string, never> {
6
+ return timezone !== null && timezone !== undefined ? { timezone } : {};
7
+ }