@cosmicdrift/kumiko-samples 0.420.0 → 0.421.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/package.json +1 -1
- package/packages/bundled-features/package.json +1 -1
- package/packages/bundled-features/src/auth-email-password/seeding.ts +29 -1
- package/packages/bundled-features/src/tenant/seeding.ts +70 -0
- package/samples/recipes/prod-boot-options/README.md +8 -1
- package/samples/recipes/prod-boot-options/src/app.ts +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-samples",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.421.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": "MIT",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-bundled-features",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.421.0",
|
|
4
4
|
"description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
createSystemUser,
|
|
25
25
|
type SessionUser,
|
|
26
26
|
SYSTEM_TENANT_ID,
|
|
27
|
+
SYSTEM_USER_ID,
|
|
27
28
|
type TenantId,
|
|
28
29
|
} from "@cosmicdrift/kumiko-framework/engine";
|
|
29
30
|
import { ConflictError } from "@cosmicdrift/kumiko-framework/errors";
|
|
@@ -32,7 +33,12 @@ import { hashPassword, verifyPassword } from "../shared/index.ts";
|
|
|
32
33
|
// kumiko-lint-ignore cross-feature-import bootstrap admin seed checks whether the tenant already has members
|
|
33
34
|
import { tenantMembershipsTable } from "../tenant/membership-table.ts";
|
|
34
35
|
// kumiko-lint-ignore cross-feature-import auth-tests need user+tenant seed-helpers
|
|
35
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
addTenantMembershipRoles,
|
|
38
|
+
type SeedTenantHooks,
|
|
39
|
+
seedTenant,
|
|
40
|
+
seedTenantMembership,
|
|
41
|
+
} from "../tenant/seeding.ts";
|
|
36
42
|
// kumiko-lint-ignore cross-feature-import signup create-only guard reads the user projection by email
|
|
37
43
|
import { USER_STATUS, userTable } from "../user/schema/user.ts";
|
|
38
44
|
// kumiko-lint-ignore cross-feature-import auth-tests need user+tenant seed-helpers
|
|
@@ -509,3 +515,25 @@ export async function seedBootstrapAdmin(
|
|
|
509
515
|
}
|
|
510
516
|
return seeded;
|
|
511
517
|
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Boot top-up for the bootstrap owner: adds the roles the owner's existing membership in the tenant
|
|
521
|
+
* lacks. Only a membership the system user created and last wrote is touched, so a human role change since then is not reverted at boot; the mail is unique per account, so the account is the owner named in the env.
|
|
522
|
+
*/
|
|
523
|
+
export async function ensureBootstrapAdminRoles(
|
|
524
|
+
db: DbConnection,
|
|
525
|
+
options: {
|
|
526
|
+
readonly email: string;
|
|
527
|
+
readonly tenantId: TenantId;
|
|
528
|
+
readonly roles: readonly string[];
|
|
529
|
+
},
|
|
530
|
+
): Promise<{ readonly added: readonly string[] }> {
|
|
531
|
+
const owner = await findExistingUserRowForEmail(db, options.email);
|
|
532
|
+
if (typeof owner?.["id"] !== "string") return { added: [] };
|
|
533
|
+
return addTenantMembershipRoles(db, {
|
|
534
|
+
userId: owner["id"],
|
|
535
|
+
tenantId: options.tenantId,
|
|
536
|
+
roles: options.roles,
|
|
537
|
+
onlyIfInsertedBy: SYSTEM_USER_ID,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
@@ -50,6 +50,7 @@ import {
|
|
|
50
50
|
createDeferringNotify,
|
|
51
51
|
createLifecycleHooks,
|
|
52
52
|
} from "@cosmicdrift/kumiko-framework/pipeline";
|
|
53
|
+
import { parseRoles } from "@cosmicdrift/kumiko-framework/utils";
|
|
53
54
|
import { assertAssignableMembershipRoles } from "./membership-roles.ts";
|
|
54
55
|
import { findLiveMembershipStreamId } from "./membership-stream-history.ts";
|
|
55
56
|
import { tenantMembershipEntity, tenantMembershipsTable } from "./membership-table.ts";
|
|
@@ -374,3 +375,72 @@ function extractMembershipId(data: unknown): string {
|
|
|
374
375
|
`seedTenantMembership: executor.create returned no string id (got ${JSON.stringify(data)})`,
|
|
375
376
|
);
|
|
376
377
|
}
|
|
378
|
+
|
|
379
|
+
// ponytail: checks creator and last writer only; a human demotion followed by a system write
|
|
380
|
+
// (invite acceptance) passes. Scan the membership's event stream for non-system actors if that matters.
|
|
381
|
+
function wasOnlyWrittenBy(
|
|
382
|
+
row: { readonly insertedById: string | null; readonly modifiedById: string | null },
|
|
383
|
+
actorId: string,
|
|
384
|
+
): boolean {
|
|
385
|
+
return (
|
|
386
|
+
row.insertedById === actorId && (row.modifiedById === null || row.modifiedById === actorId)
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Add-only top-up for an EXISTING membership: roles the membership lacks are appended through the
|
|
392
|
+
* event-store executor (one update event), nothing is removed. No event when all roles are present;
|
|
393
|
+
* no membership is created.
|
|
394
|
+
*/
|
|
395
|
+
export async function addTenantMembershipRoles(
|
|
396
|
+
db: DbRunner,
|
|
397
|
+
options: {
|
|
398
|
+
readonly userId: string;
|
|
399
|
+
readonly tenantId: TenantId;
|
|
400
|
+
readonly roles: readonly string[];
|
|
401
|
+
/** Only touch a membership this id created and last wrote (boot seeds pass SYSTEM_USER_ID), so a human role change since then is not reverted. */
|
|
402
|
+
readonly onlyIfInsertedBy?: string;
|
|
403
|
+
},
|
|
404
|
+
): Promise<{ readonly added: readonly string[] }> {
|
|
405
|
+
assertAssignableMembershipRoles(options.roles);
|
|
406
|
+
// One re-read on version_conflict: a concurrent boot (second pod) may have added the same roles.
|
|
407
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
408
|
+
const existing = await fetchOne<{
|
|
409
|
+
id: string;
|
|
410
|
+
version: number;
|
|
411
|
+
roles: unknown;
|
|
412
|
+
insertedById: string | null;
|
|
413
|
+
modifiedById: string | null;
|
|
414
|
+
}>(db, tenantMembershipsTable, { userId: options.userId, tenantId: options.tenantId });
|
|
415
|
+
if (!existing) return { added: [] };
|
|
416
|
+
if (
|
|
417
|
+
options.onlyIfInsertedBy !== undefined &&
|
|
418
|
+
!wasOnlyWrittenBy(existing, options.onlyIfInsertedBy)
|
|
419
|
+
) {
|
|
420
|
+
return { added: [] };
|
|
421
|
+
}
|
|
422
|
+
const currentRoles = parseRoles(existing.roles);
|
|
423
|
+
const added = options.roles.filter((role) => !currentRoles.includes(role));
|
|
424
|
+
if (added.length === 0) return { added };
|
|
425
|
+
const result = await executor.update(
|
|
426
|
+
{
|
|
427
|
+
id: existing.id,
|
|
428
|
+
version: existing.version,
|
|
429
|
+
changes: { roles: JSON.stringify([...currentRoles, ...added]) },
|
|
430
|
+
},
|
|
431
|
+
createSystemUser(options.tenantId),
|
|
432
|
+
createTenantDb({
|
|
433
|
+
db,
|
|
434
|
+
tenantId: options.tenantId,
|
|
435
|
+
mode: "system",
|
|
436
|
+
reason: "boot seed tops up the owner's membership roles in the seeded tenant",
|
|
437
|
+
}),
|
|
438
|
+
);
|
|
439
|
+
if (result.isSuccess) return { added };
|
|
440
|
+
if (result.error.code === "version_conflict" && attempt === 0) continue;
|
|
441
|
+
throw new Error(
|
|
442
|
+
`addTenantMembershipRoles failed: ${result.error.code} — ${JSON.stringify(result.error.details ?? {})}`,
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
return { added: [] };
|
|
446
|
+
}
|
|
@@ -25,7 +25,14 @@ the boot with a clear message when the env is incomplete.
|
|
|
25
25
|
signals to the host.
|
|
26
26
|
- **`seeds`**: `{ demoTenant, demoAdminFromEnv, sysadminFromEnv, run }`
|
|
27
27
|
replaces hand-written tenant and admin seeds; needs the auth features (not
|
|
28
|
-
mounted here).
|
|
28
|
+
mounted here). `bootstrapAdminFromEnv: { ensureRoles: true }` adds roles
|
|
29
|
+
that `roles` gained since the first boot to the owner's existing membership
|
|
30
|
+
on every boot (additive, never removes).
|
|
31
|
+
- **`previewLogin`**: `{ loginKey }` mounts `GET /__kumiko/preview-login?exp=&sig=`,
|
|
32
|
+
which signs the bootstrap owner in (302 to `/`, the normal auth cookies) when
|
|
33
|
+
`sig` is the hex HMAC-SHA256 of `kumiko-preview-login:<exp>` under `loginKey`
|
|
34
|
+
and `exp` lies at most about 90 seconds ahead. Any other link answers 403.
|
|
35
|
+
Without the option or with an empty key the route does not exist.
|
|
29
36
|
- **`billing`**: `{ stripe: { priceToTier }, defaultTier }` mounts the Stripe
|
|
30
37
|
bridge and the tier-sync webhook route; needs `billing-foundation` and
|
|
31
38
|
`tier-engine`.
|
|
@@ -15,7 +15,7 @@ export function appFeatures(): RunProdAppOptions["features"] {
|
|
|
15
15
|
|
|
16
16
|
type BootOptions = Pick<
|
|
17
17
|
RunProdAppOptions,
|
|
18
|
-
"mail" | "kmsMode" | "fileStorage" | "demoMode" | "signals"
|
|
18
|
+
"mail" | "kmsMode" | "fileStorage" | "demoMode" | "signals" | "previewLogin"
|
|
19
19
|
>;
|
|
20
20
|
|
|
21
21
|
export function bootOptions(envSource: Record<string, string | undefined>): BootOptions {
|
|
@@ -28,5 +28,7 @@ export function bootOptions(envSource: Record<string, string | undefined>): Boot
|
|
|
28
28
|
notice: { text: { en: "This demo is read-only.", de: "Diese Demo ist schreibgeschützt." } },
|
|
29
29
|
},
|
|
30
30
|
signals: { drainTimeoutMs: 20_000 },
|
|
31
|
+
// Preview deployments only: the link signs the owner in without the MFA gate.
|
|
32
|
+
previewLogin: { loginKey: envSource["PREVIEW_LOGIN_KEY"] ?? "" },
|
|
31
33
|
};
|
|
32
34
|
}
|