@cosmicdrift/kumiko-framework 0.125.2 → 0.126.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 +2 -2
- package/src/api/auth-middleware.ts +92 -6
- package/src/api/auth-routes.ts +2 -0
- package/src/api/index.ts +1 -0
- package/src/api/server.ts +3 -0
- package/src/engine/boot-validator/gdpr-storage.ts +34 -1
- package/src/engine/boot-validator/index.ts +2 -0
- package/src/engine/extensions/tenant-data.ts +19 -0
- package/src/engine/index.ts +5 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-framework",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.126.0",
|
|
4
4
|
"description": "Framework core — engine, pipeline, API, DB, and every other bit that makes Kumiko go.",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -189,7 +189,7 @@
|
|
|
189
189
|
"zod": "^4.4.3"
|
|
190
190
|
},
|
|
191
191
|
"devDependencies": {
|
|
192
|
-
"@cosmicdrift/kumiko-dispatcher-live": "0.
|
|
192
|
+
"@cosmicdrift/kumiko-dispatcher-live": "0.126.0",
|
|
193
193
|
"bun-types": "^1.3.13",
|
|
194
194
|
"pino-pretty": "^13.1.3"
|
|
195
195
|
},
|
|
@@ -55,6 +55,10 @@ export type AuthSessionChecker = (
|
|
|
55
55
|
// and short-circuits the JWT path on a hit.
|
|
56
56
|
export type PatResolver = (rawToken: string) => Promise<SessionUser | null>;
|
|
57
57
|
|
|
58
|
+
export type TenantLifecycleStatusResolver = (
|
|
59
|
+
tenantId: TenantId,
|
|
60
|
+
) => Promise<{ readonly status: string } | null>;
|
|
61
|
+
|
|
58
62
|
export type AuthMiddlewareOptions = {
|
|
59
63
|
// Called after JWT-verify when the token carries a sid. If the checker
|
|
60
64
|
// reports anything other than "live", the request is rejected with 401.
|
|
@@ -74,6 +78,10 @@ export type AuthMiddlewareOptions = {
|
|
|
74
78
|
// a SessionUser with id="anonymous" and roles=["anonymous"], scoped to a
|
|
75
79
|
// tenantId resolved through the chain documented on AnonymousAccessConfig.
|
|
76
80
|
readonly anonymousAccess?: AnonymousAccessConfig;
|
|
81
|
+
// Consulted after tenantId is resolved (JWT/PAT/anonymous). Returns 410
|
|
82
|
+
// when the tenant is in teardown (destroyRequested/destroying/destroyed).
|
|
83
|
+
// cancel-destruction is exempt while status=destroyRequested.
|
|
84
|
+
readonly resolveTenantLifecycleStatus?: TenantLifecycleStatusResolver;
|
|
77
85
|
};
|
|
78
86
|
|
|
79
87
|
// Resolves the tenant for an unauthenticated request. Returns null when no
|
|
@@ -127,14 +135,15 @@ type MiddlewareRejectCode =
|
|
|
127
135
|
| "tenant_required"
|
|
128
136
|
| "tenant_not_found"
|
|
129
137
|
| "tenant_mismatch"
|
|
130
|
-
| "invalid_tenant_format"
|
|
138
|
+
| "invalid_tenant_format"
|
|
139
|
+
| "tenant_unavailable";
|
|
131
140
|
|
|
132
141
|
// @wrapper-known error-helper
|
|
133
142
|
function middlewareReject(
|
|
134
143
|
c: Context,
|
|
135
144
|
opts: {
|
|
136
145
|
code: MiddlewareRejectCode;
|
|
137
|
-
status: 400 | 401 | 403 | 404;
|
|
146
|
+
status: 400 | 401 | 403 | 404 | 410;
|
|
138
147
|
message: string;
|
|
139
148
|
i18nKey: string;
|
|
140
149
|
details?: Record<string, unknown>;
|
|
@@ -187,7 +196,13 @@ function extractToken(
|
|
|
187
196
|
}
|
|
188
197
|
|
|
189
198
|
export function authMiddleware(jwt: JwtHelper, options: AuthMiddlewareOptions = {}) {
|
|
190
|
-
const {
|
|
199
|
+
const {
|
|
200
|
+
sessionChecker,
|
|
201
|
+
strictMode = false,
|
|
202
|
+
anonymousAccess,
|
|
203
|
+
patResolver,
|
|
204
|
+
resolveTenantLifecycleStatus,
|
|
205
|
+
} = options;
|
|
191
206
|
|
|
192
207
|
return async (c: Context, next: Next) => {
|
|
193
208
|
const extracted = extractToken(c);
|
|
@@ -219,7 +234,7 @@ export function authMiddleware(jwt: JwtHelper, options: AuthMiddlewareOptions =
|
|
|
219
234
|
i18nKey: "auth.errors.missingToken",
|
|
220
235
|
});
|
|
221
236
|
}
|
|
222
|
-
return await handleAnonymous(c, anonymousAccess, next);
|
|
237
|
+
return await handleAnonymous(c, anonymousAccess, next, resolveTenantLifecycleStatus);
|
|
223
238
|
}
|
|
224
239
|
return middlewareReject(c, {
|
|
225
240
|
code: "missing_token",
|
|
@@ -234,7 +249,7 @@ export function authMiddleware(jwt: JwtHelper, options: AuthMiddlewareOptions =
|
|
|
234
249
|
// Personal Access Token, not a JWT. Short-circuit the JWT path entirely.
|
|
235
250
|
// Cookie transport is never a PAT (the browser holds the JWT).
|
|
236
251
|
if (patResolver && transport === "bearer" && token.startsWith(PAT_TOKEN_PREFIX)) {
|
|
237
|
-
return await handlePat(c, patResolver, token, next);
|
|
252
|
+
return await handlePat(c, patResolver, token, next, resolveTenantLifecycleStatus);
|
|
238
253
|
}
|
|
239
254
|
|
|
240
255
|
let payload: Awaited<ReturnType<JwtHelper["verify"]>>;
|
|
@@ -286,6 +301,12 @@ export function authMiddleware(jwt: JwtHelper, options: AuthMiddlewareOptions =
|
|
|
286
301
|
...(payload.claims ? { claims: payload.claims } : {}),
|
|
287
302
|
...(payload.jti ? { sid: payload.jti } : {}),
|
|
288
303
|
};
|
|
304
|
+
const lifecycleReject = await rejectIfTenantTeardown(
|
|
305
|
+
c,
|
|
306
|
+
payload.tenantId,
|
|
307
|
+
resolveTenantLifecycleStatus,
|
|
308
|
+
);
|
|
309
|
+
if (lifecycleReject) return lifecycleReject;
|
|
289
310
|
c.set(USER_KEY, user);
|
|
290
311
|
c.set(AUTH_TRANSPORT_KEY, transport);
|
|
291
312
|
await next();
|
|
@@ -311,6 +332,7 @@ async function handlePat(
|
|
|
311
332
|
patResolver: PatResolver,
|
|
312
333
|
token: string,
|
|
313
334
|
next: Next,
|
|
335
|
+
resolveTenantLifecycleStatus?: TenantLifecycleStatusResolver,
|
|
314
336
|
): Promise<Response | undefined> {
|
|
315
337
|
const patUser = await patResolver(token);
|
|
316
338
|
if (!patUser) {
|
|
@@ -335,6 +357,12 @@ async function handlePat(
|
|
|
335
357
|
}
|
|
336
358
|
c.set(USER_KEY, patUser);
|
|
337
359
|
c.set(AUTH_TRANSPORT_KEY, "bearer");
|
|
360
|
+
const lifecycleReject = await rejectIfTenantTeardown(
|
|
361
|
+
c,
|
|
362
|
+
patUser.tenantId,
|
|
363
|
+
resolveTenantLifecycleStatus,
|
|
364
|
+
);
|
|
365
|
+
if (lifecycleReject) return lifecycleReject;
|
|
338
366
|
await next();
|
|
339
367
|
// skip: PAT path completed — next() ran; explicit return keeps the
|
|
340
368
|
// Response|undefined union honest (same as handleAnonymous).
|
|
@@ -357,6 +385,7 @@ async function handleAnonymous(
|
|
|
357
385
|
c: Context,
|
|
358
386
|
config: AnonymousAccessConfig,
|
|
359
387
|
next: Next,
|
|
388
|
+
resolveTenantLifecycleStatus?: TenantLifecycleStatusResolver,
|
|
360
389
|
): Promise<Response | undefined> {
|
|
361
390
|
// Step 1+2: parse client-supplied tenant. Reject malformed values before
|
|
362
391
|
// they touch any downstream consumer (DB, cache, audit row).
|
|
@@ -394,6 +423,12 @@ async function handleAnonymous(
|
|
|
394
423
|
}
|
|
395
424
|
|
|
396
425
|
// Step 5: synthesise + continue.
|
|
426
|
+
const lifecycleReject = await rejectIfTenantTeardown(
|
|
427
|
+
c,
|
|
428
|
+
resolved.tenantId,
|
|
429
|
+
resolveTenantLifecycleStatus,
|
|
430
|
+
);
|
|
431
|
+
if (lifecycleReject) return lifecycleReject;
|
|
397
432
|
c.set(USER_KEY, createAnonymousUser(resolved.tenantId));
|
|
398
433
|
await next();
|
|
399
434
|
// skip: anonymous path completed — Hono middleware contract returns void
|
|
@@ -479,8 +514,59 @@ async function resolveTenant(
|
|
|
479
514
|
|
|
480
515
|
type RejectArgs = {
|
|
481
516
|
code: MiddlewareRejectCode;
|
|
482
|
-
status: 400 | 401 | 403 | 404;
|
|
517
|
+
status: 400 | 401 | 403 | 404 | 410;
|
|
483
518
|
message: string;
|
|
484
519
|
i18nKey: string;
|
|
485
520
|
details?: Record<string, unknown>;
|
|
486
521
|
};
|
|
522
|
+
|
|
523
|
+
const TENANT_LIFECYCLE_BLOCKED = new Set([
|
|
524
|
+
"destroyRequested",
|
|
525
|
+
"destroying",
|
|
526
|
+
"destroyFailed",
|
|
527
|
+
"destroyed",
|
|
528
|
+
]);
|
|
529
|
+
const TENANT_LIFECYCLE_CANCEL_QN = "tenant-lifecycle:write:cancel-destruction";
|
|
530
|
+
|
|
531
|
+
async function requestsCancelDestruction(c: Context): Promise<boolean> {
|
|
532
|
+
if (c.req.method !== "POST") return false;
|
|
533
|
+
try {
|
|
534
|
+
const path = c.req.path;
|
|
535
|
+
const body = (await c.req.raw.clone().json()) as {
|
|
536
|
+
type?: string;
|
|
537
|
+
commands?: Array<{ type?: string }>;
|
|
538
|
+
};
|
|
539
|
+
if (path === "/api/write") {
|
|
540
|
+
return body.type === TENANT_LIFECYCLE_CANCEL_QN;
|
|
541
|
+
}
|
|
542
|
+
if (path === "/api/batch") {
|
|
543
|
+
return (
|
|
544
|
+
Array.isArray(body.commands) &&
|
|
545
|
+
body.commands.some((command) => command.type === TENANT_LIFECYCLE_CANCEL_QN)
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
} catch {
|
|
549
|
+
return false;
|
|
550
|
+
}
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
async function rejectIfTenantTeardown(
|
|
555
|
+
c: Context,
|
|
556
|
+
tenantId: TenantId,
|
|
557
|
+
resolveTenantLifecycleStatus: TenantLifecycleStatusResolver | undefined,
|
|
558
|
+
): Promise<Response | undefined> {
|
|
559
|
+
if (!resolveTenantLifecycleStatus) return undefined;
|
|
560
|
+
const lifecycle = await resolveTenantLifecycleStatus(tenantId);
|
|
561
|
+
if (!lifecycle || !TENANT_LIFECYCLE_BLOCKED.has(lifecycle.status)) return undefined;
|
|
562
|
+
if (lifecycle.status === "destroyRequested" && (await requestsCancelDestruction(c))) {
|
|
563
|
+
return undefined;
|
|
564
|
+
}
|
|
565
|
+
return middlewareReject(c, {
|
|
566
|
+
code: "tenant_unavailable",
|
|
567
|
+
status: 410,
|
|
568
|
+
message: `tenant "${tenantId}" is unavailable (${lifecycle.status})`,
|
|
569
|
+
i18nKey: "auth.errors.tenantUnavailable",
|
|
570
|
+
details: { tenantId, status: lifecycle.status },
|
|
571
|
+
});
|
|
572
|
+
}
|
package/src/api/auth-routes.ts
CHANGED
|
@@ -243,6 +243,8 @@ export type AuthRoutesConfig = {
|
|
|
243
243
|
// Wired by run-prod-app when the PAT feature is mounted; unwired = no PAT
|
|
244
244
|
// rate limiting.
|
|
245
245
|
patRateLimiter?: LoginRateLimiter;
|
|
246
|
+
// Tenant-lifecycle 410 gate — wired by tenant-lifecycle / run-prod-app.
|
|
247
|
+
resolveTenantLifecycleStatus?: import("./auth-middleware").TenantLifecycleStatusResolver;
|
|
246
248
|
// Password-reset flow. When wired, POST /auth/request-password-reset and
|
|
247
249
|
// POST /auth/reset-password are mounted as public routes. The framework
|
|
248
250
|
// dispatches to the feature-level handlers (authoring QNs typically come
|
package/src/api/index.ts
CHANGED
package/src/api/server.ts
CHANGED
|
@@ -567,6 +567,9 @@ export function buildServer(options: ServerOptions): KumikoServer {
|
|
|
567
567
|
...(options.auth?.sessionChecker ? { sessionChecker: options.auth.sessionChecker } : {}),
|
|
568
568
|
...(options.auth?.sessionStrictMode ? { strictMode: options.auth.sessionStrictMode } : {}),
|
|
569
569
|
...(options.auth?.patResolver ? { patResolver: options.auth.patResolver } : {}),
|
|
570
|
+
...(options.auth?.resolveTenantLifecycleStatus
|
|
571
|
+
? { resolveTenantLifecycleStatus: options.auth.resolveTenantLifecycleStatus }
|
|
572
|
+
: {}),
|
|
570
573
|
...(options.anonymousAccess ? { anonymousAccess: options.anonymousAccess } : {}),
|
|
571
574
|
});
|
|
572
575
|
app.use("/api/*", async (c, next) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EXT_USER_DATA } from "../extension-names";
|
|
1
|
+
import { EXT_TENANT_DATA, EXT_USER_DATA } from "../extension-names";
|
|
2
2
|
import type { FeatureDefinition } from "../types";
|
|
3
3
|
import type { PiiAnnotations } from "../types/fields";
|
|
4
4
|
|
|
@@ -120,3 +120,36 @@ export function validateGdprPiiHookCoverage(features: readonly FeatureDefinition
|
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
|
+
|
|
124
|
+
// V4: tenantOwned-entity-without-hook gate. Mirrors validateGdprPiiHookCoverage
|
|
125
|
+
// but for EXT_TENANT_DATA when tenant-lifecycle is mounted.
|
|
126
|
+
export function validateTenantDataHookCoverage(features: readonly FeatureDefinition[]): void {
|
|
127
|
+
const featureNames = new Set(features.map((f) => f.name));
|
|
128
|
+
if (!featureNames.has("tenant-lifecycle")) {
|
|
129
|
+
// skip: this guard only applies to apps that mount tenant-lifecycle
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const hookedEntities = new Set<string>();
|
|
134
|
+
for (const f of features) {
|
|
135
|
+
for (const usage of f.extensionUsages) {
|
|
136
|
+
if (usage.extensionName === EXT_TENANT_DATA) hookedEntities.add(usage.entityName);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (const feature of features) {
|
|
141
|
+
for (const [entityName, entity] of Object.entries(feature.entities ?? {})) {
|
|
142
|
+
if (hookedEntities.has(entityName)) continue;
|
|
143
|
+
const tenantSubjectFields = Object.entries(entity.fields)
|
|
144
|
+
.filter(([, field]) => {
|
|
145
|
+
const annot = field as PiiAnnotations;
|
|
146
|
+
return Boolean(annot.tenantOwned);
|
|
147
|
+
})
|
|
148
|
+
.map(([name]) => name);
|
|
149
|
+
if (tenantSubjectFields.length === 0) continue;
|
|
150
|
+
throw new Error(
|
|
151
|
+
`[kumiko:boot] Entity "${entityName}" (feature "${feature.name}") has tenant-subject fields (${tenantSubjectFields.join(", ")}) but no feature registers an EXT_TENANT_DATA destroy hook for it — tenant destroy never erases this data. Register r.useExtension(EXT_TENANT_DATA, "${entityName}", { destroy }) or a documented no-op if crypto-shredding covers it.`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
validateGdprHookCompleteness,
|
|
32
32
|
validateGdprPiiHookCoverage,
|
|
33
33
|
validateGdprStoragePersistence,
|
|
34
|
+
validateTenantDataHookCoverage,
|
|
34
35
|
} from "./gdpr-storage";
|
|
35
36
|
import { validateI18nSurfaceKeys } from "./i18n-keys";
|
|
36
37
|
import { validateOwnershipRules } from "./ownership";
|
|
@@ -184,6 +185,7 @@ export function validateBoot(features: readonly FeatureDefinition[]): void {
|
|
|
184
185
|
validateGdprStoragePersistence(features);
|
|
185
186
|
validateGdprHookCompleteness(features);
|
|
186
187
|
validateGdprPiiHookCoverage(features);
|
|
188
|
+
validateTenantDataHookCoverage(features);
|
|
187
189
|
|
|
188
190
|
if (hasEncryptedFields) {
|
|
189
191
|
// Availability check, not env-presence: eagerly building the keyring
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Hook-Signatur-Types für EXT_TENANT_DATA (DSGVO tenant-scoped destroy).
|
|
2
|
+
//
|
|
3
|
+
// Mirror of engine/extensions/user-data.ts at tenant granularity.
|
|
4
|
+
// tenant-lifecycle orchestrates destroy via registry.getExtensionUsages(EXT_TENANT_DATA).
|
|
5
|
+
|
|
6
|
+
import type { DbRunner } from "../../db/connection";
|
|
7
|
+
import type { Registry, TenantId } from "../types";
|
|
8
|
+
|
|
9
|
+
export interface TenantDataHookCtx {
|
|
10
|
+
readonly db: DbRunner;
|
|
11
|
+
readonly registry: Registry;
|
|
12
|
+
readonly tenantId: TenantId;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type TenantDataDestroyHook = (ctx: TenantDataHookCtx) => Promise<void>;
|
|
16
|
+
|
|
17
|
+
export interface TenantDataExtensionHooks {
|
|
18
|
+
readonly destroy: TenantDataDestroyHook;
|
|
19
|
+
}
|
package/src/engine/index.ts
CHANGED
|
@@ -81,6 +81,11 @@ export {
|
|
|
81
81
|
EXT_USER_DATA_ORDER,
|
|
82
82
|
FILE_PROVIDER_CONFIG_KEY,
|
|
83
83
|
} from "./extension-names";
|
|
84
|
+
export type {
|
|
85
|
+
TenantDataDestroyHook,
|
|
86
|
+
TenantDataExtensionHooks,
|
|
87
|
+
TenantDataHookCtx,
|
|
88
|
+
} from "./extensions/tenant-data";
|
|
84
89
|
export type {
|
|
85
90
|
TenantUserModel,
|
|
86
91
|
UserDataDeleteHook,
|