@kidecms/core 0.29.3 → 0.31.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/admin/components/BlockEditor.tsx +64 -45
- package/admin/components/CheckboxField.tsx +12 -3
- package/admin/components/ContentEditor.tsx +1 -1
- package/admin/components/DocumentActions.tsx +69 -0
- package/admin/components/DocumentsDataTable.tsx +28 -1
- package/admin/components/FieldControl.astro +35 -4
- package/admin/components/PasskeySettings.tsx +121 -0
- package/admin/components/PasskeySignIn.tsx +57 -0
- package/admin/components/SharedSectionBlockField.tsx +1 -1
- package/admin/components/SidebarUserMenu.tsx +7 -1
- package/admin/components/TwoFactorSettings.tsx +206 -0
- package/admin/components/UnsavedGuard.tsx +9 -2
- package/admin/components/block-fields.tsx +42 -25
- package/admin/components/block-values.ts +52 -0
- package/admin/layouts/AdminLayout.astro +42 -2
- package/admin/views/EditView.astro +75 -39
- package/admin/views/ListView.astro +43 -3
- package/admin/views/edit/EditScripts.astro +88 -2
- package/core/api.ts +49 -5
- package/core/auth-config.ts +53 -11
- package/core/auth-engine.ts +920 -0
- package/core/auth.ts +46 -152
- package/core/create-admin.ts +9 -5
- package/core/define.ts +75 -35
- package/core/generator.ts +80 -10
- package/core/index.ts +31 -3
- package/core/schema.ts +3 -1
- package/core/seed.ts +12 -2
- package/internals/integration.ts +34 -3
- package/internals/mcp.ts +3 -3
- package/internals/shared-deps.ts +126 -0
- package/internals/upgrade.ts +3 -0
- package/internals/virtual.d.ts +3 -2
- package/middleware/auth.ts +58 -18
- package/package.json +26 -21
- package/routes/admin/account.astro +138 -0
- package/routes/admin/login/verify.astro +90 -0
- package/routes/admin/login.astro +84 -33
- package/routes/api/auth/[...all].ts +153 -0
- package/routes/api/auth/_better-auth.ts +26 -0
- package/routes/api/auth/account.ts +69 -0
- package/routes/api/auth/forgot-password.ts +38 -32
- package/routes/api/auth/invite.ts +66 -98
- package/routes/api/auth/login/verify.ts +50 -0
- package/routes/api/auth/login.ts +68 -118
- package/routes/api/auth/logout.ts +13 -28
- package/routes/api/auth/reset-password.ts +17 -56
- package/routes/api/auth/setup.ts +14 -20
- package/routes/api/auth/sso/[provider]/start.ts +33 -25
- package/routes/api/forms/submit/[slug].ts +27 -2
package/internals/integration.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, watch,
|
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { analyzeSharedDeps, formatConflicts } from "./shared-deps";
|
|
7
8
|
|
|
8
9
|
// All runtime files resolve relative to this module, never the project root, so
|
|
9
10
|
// the integration works identically embedded (workspace package in the project
|
|
@@ -113,9 +114,17 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
|
|
|
113
114
|
return {
|
|
114
115
|
name: "kide-cms",
|
|
115
116
|
hooks: {
|
|
116
|
-
"astro:config:setup": ({ command, updateConfig, injectRoute, injectScript, addMiddleware }) => {
|
|
117
|
+
"astro:config:setup": ({ command, updateConfig, injectRoute, injectScript, addMiddleware, logger }) => {
|
|
117
118
|
const root = process.cwd();
|
|
118
119
|
|
|
120
|
+
// One copy per library the admin shares with the project — see shared-deps.ts.
|
|
121
|
+
const sharedDeps = analyzeSharedDeps({
|
|
122
|
+
projectRoot: root,
|
|
123
|
+
packageFile: packagePath("package.json"),
|
|
124
|
+
dependencies: JSON.parse(readFileSync(packagePath("package.json"), "utf8")).dependencies ?? {},
|
|
125
|
+
});
|
|
126
|
+
if (sharedDeps.conflicts.length > 0) logger.warn(formatConflicts(sharedDeps.conflicts));
|
|
127
|
+
|
|
119
128
|
// Generate a wrapper CSS: Tailwind sources + the core admin chrome +
|
|
120
129
|
// the project's theme file. Chrome (layers, focus, editor styles,
|
|
121
130
|
// variants) is core-owned and updates with the package; the project
|
|
@@ -180,8 +189,9 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
|
|
|
180
189
|
"virtual:kide/admin-css": wrapperCss,
|
|
181
190
|
"virtual:kide/custom-fields": customFieldsBarrel,
|
|
182
191
|
},
|
|
183
|
-
// Two React copies (project + package) break hooks/context identity
|
|
184
|
-
|
|
192
|
+
// Two React copies (project + package) break hooks/context identity, and two
|
|
193
|
+
// copies of any admin UI library break Vite's pre-bundling (shared-deps.ts).
|
|
194
|
+
dedupe: ["react", "react-dom", ...sharedDeps.dedupe],
|
|
185
195
|
},
|
|
186
196
|
// The package ships TypeScript/Astro source — Vite must compile it
|
|
187
197
|
// rather than treat it as an external Node dependency in SSR.
|
|
@@ -223,6 +233,14 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
|
|
|
223
233
|
|
|
224
234
|
// Inject admin pages
|
|
225
235
|
injectRoute({ pattern: "/admin/login", entrypoint: new URL("../routes/admin/login.astro", import.meta.url) });
|
|
236
|
+
injectRoute({
|
|
237
|
+
pattern: "/admin/login/verify",
|
|
238
|
+
entrypoint: new URL("../routes/admin/login/verify.astro", import.meta.url),
|
|
239
|
+
});
|
|
240
|
+
injectRoute({
|
|
241
|
+
pattern: "/admin/account",
|
|
242
|
+
entrypoint: new URL("../routes/admin/account.astro", import.meta.url),
|
|
243
|
+
});
|
|
226
244
|
injectRoute({
|
|
227
245
|
pattern: "/admin/forgot-password",
|
|
228
246
|
entrypoint: new URL("../routes/admin/forgot-password.astro", import.meta.url),
|
|
@@ -283,6 +301,19 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
|
|
|
283
301
|
pattern: "/api/cms/auth/invite",
|
|
284
302
|
entrypoint: new URL("../routes/api/auth/invite.ts", import.meta.url),
|
|
285
303
|
});
|
|
304
|
+
injectRoute({
|
|
305
|
+
pattern: "/api/cms/auth/login/verify",
|
|
306
|
+
entrypoint: new URL("../routes/api/auth/login/verify.ts", import.meta.url),
|
|
307
|
+
});
|
|
308
|
+
injectRoute({
|
|
309
|
+
pattern: "/api/cms/auth/account",
|
|
310
|
+
entrypoint: new URL("../routes/api/auth/account.ts", import.meta.url),
|
|
311
|
+
});
|
|
312
|
+
// Better Auth's own endpoints (OAuth callbacks, two-factor, passkeys) — allowlisted inside.
|
|
313
|
+
injectRoute({
|
|
314
|
+
pattern: "/api/cms/auth/[...all]",
|
|
315
|
+
entrypoint: new URL("../routes/api/auth/[...all].ts", import.meta.url),
|
|
316
|
+
});
|
|
286
317
|
injectRoute({
|
|
287
318
|
pattern: "/api/cms/assets/upload",
|
|
288
319
|
entrypoint: new URL("../routes/api/assets/upload.ts", import.meta.url),
|
package/internals/mcp.ts
CHANGED
|
@@ -199,7 +199,7 @@ server.registerTool(
|
|
|
199
199
|
{
|
|
200
200
|
title: "List documents",
|
|
201
201
|
description:
|
|
202
|
-
"List documents in a collection with optional filters, search, sort, locale, status, limit, and offset. With locale, availability 'exact' lists only documents that exist in that locale (their _sourceLocale or a translation); the default 'fallback' lists every document.",
|
|
202
|
+
"List documents in a collection with optional filters, search, sort, locale, status, limit, and offset. With locale, availability 'exact' lists only documents that exist in that locale (their _sourceLocale or a translation), 'missing' only those that don't; the default 'fallback' lists every document.",
|
|
203
203
|
inputSchema: {
|
|
204
204
|
collection: z.string().min(1),
|
|
205
205
|
where: jsonObjectSchema.optional(),
|
|
@@ -209,7 +209,7 @@ server.registerTool(
|
|
|
209
209
|
offset: z.number().int().min(0).default(0),
|
|
210
210
|
status: statusSchema.optional(),
|
|
211
211
|
locale: z.string().min(1).optional(),
|
|
212
|
-
availability: z.enum(["fallback", "exact"]).optional(),
|
|
212
|
+
availability: z.enum(["fallback", "exact", "missing"]).optional(),
|
|
213
213
|
},
|
|
214
214
|
},
|
|
215
215
|
async (args) => toResult(await workerCall("listDocuments", args)),
|
|
@@ -226,7 +226,7 @@ server.registerTool(
|
|
|
226
226
|
search: z.string().optional(),
|
|
227
227
|
status: statusSchema.optional(),
|
|
228
228
|
locale: z.string().min(1).optional(),
|
|
229
|
-
availability: z.enum(["fallback", "exact"]).optional(),
|
|
229
|
+
availability: z.enum(["fallback", "exact", "missing"]).optional(),
|
|
230
230
|
},
|
|
231
231
|
},
|
|
232
232
|
async (args) => toResult(await workerCall("countDocuments", args)),
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Libraries the admin shares with the host project.
|
|
2
|
+
//
|
|
3
|
+
// The admin's React islands import the package's own UI dependencies
|
|
4
|
+
// (@base-ui/react, lucide-react, TipTap, …). When the project installs its own
|
|
5
|
+
// copy of one of them — typically by adding shadcn/ui components for the public
|
|
6
|
+
// site — the browser graph contains two copies under one bare specifier. Vite
|
|
7
|
+
// pre-bundles each specifier once, so the two copies keep invalidating each
|
|
8
|
+
// other's bundle: requests 504 with "Outdated Optimize Dep", admin islands fail
|
|
9
|
+
// to hydrate, and every rich field renders empty (the data itself is fine).
|
|
10
|
+
//
|
|
11
|
+
// Resolution: when the project's copy satisfies the range this package declares,
|
|
12
|
+
// dedupe to it (one copy, and the admin runs on a version it supports). When it
|
|
13
|
+
// doesn't, deduping could break the admin, so warn with the exact fix instead.
|
|
14
|
+
|
|
15
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
|
|
18
|
+
/** Package dependencies that never reach the browser — no hydration risk. */
|
|
19
|
+
const SERVER_ONLY = new Set([
|
|
20
|
+
"ai",
|
|
21
|
+
"@ai-sdk/openai",
|
|
22
|
+
"@modelcontextprotocol/sdk",
|
|
23
|
+
"drizzle-kit",
|
|
24
|
+
"tsx",
|
|
25
|
+
"tw-animate-css",
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
type Version = [number, number, number];
|
|
29
|
+
|
|
30
|
+
const parseVersion = (value: string): Version | null => {
|
|
31
|
+
const match = value.trim().match(/^v?(\d+)\.(\d+)\.(\d+)/);
|
|
32
|
+
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const compare = (a: Version, b: Version) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether `version` falls inside `range`. Supports the forms package manifests
|
|
39
|
+
* actually use here: `^x.y.z`, `~x.y.z` and exact `x.y.z`. Returns null for
|
|
40
|
+
* anything else, so callers can stay silent instead of guessing.
|
|
41
|
+
*/
|
|
42
|
+
export const satisfiesRange = (version: string, range: string): boolean | null => {
|
|
43
|
+
const v = parseVersion(version);
|
|
44
|
+
const operator = range.trim().match(/^[\^~]/)?.[0] ?? "";
|
|
45
|
+
const base = parseVersion(range.trim().slice(operator.length));
|
|
46
|
+
if (!v || !base) return null;
|
|
47
|
+
if (!/^[\^~]?v?\d+\.\d+\.\d+$/.test(range.trim())) return null;
|
|
48
|
+
if (operator === "") return compare(v, base) === 0;
|
|
49
|
+
if (compare(v, base) < 0) return false;
|
|
50
|
+
if (operator === "~") return v[0] === base[0] && v[1] === base[1];
|
|
51
|
+
// Caret: the left-most non-zero component is fixed.
|
|
52
|
+
if (base[0] > 0) return v[0] === base[0];
|
|
53
|
+
if (base[1] > 0) return v[0] === 0 && v[1] === base[1];
|
|
54
|
+
return v[0] === 0 && v[1] === 0 && v[2] === base[2];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type SharedDepConflict = { name: string; range: string; projectVersion: string };
|
|
58
|
+
|
|
59
|
+
export type SharedDepReport = {
|
|
60
|
+
/** Deduped to the project's copy (compatible with this package's range). */
|
|
61
|
+
dedupe: string[];
|
|
62
|
+
/** Project copies outside this package's range — the admin keeps its own. */
|
|
63
|
+
conflicts: SharedDepConflict[];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Directory of the copy of `name` that `fromFile` resolves through its own
|
|
68
|
+
* node_modules chain. Deliberately not `require.resolve.paths()`: that appends
|
|
69
|
+
* NODE_PATH, which `pnpm dev`/`pnpm exec` point at pnpm's hoisted store, so every
|
|
70
|
+
* package would appear to have a project copy.
|
|
71
|
+
*/
|
|
72
|
+
const locatePackage = (name: string, fromFile: string): string | null => {
|
|
73
|
+
let dir = path.dirname(path.resolve(fromFile));
|
|
74
|
+
for (;;) {
|
|
75
|
+
const manifest = path.join(dir, "node_modules", name, "package.json");
|
|
76
|
+
if (existsSync(manifest)) return realpathSync(path.dirname(manifest));
|
|
77
|
+
const parent = path.dirname(dir);
|
|
78
|
+
if (parent === dir) return null;
|
|
79
|
+
dir = parent;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const readVersion = (dir: string) => {
|
|
84
|
+
try {
|
|
85
|
+
return String(JSON.parse(readFileSync(path.join(dir, "package.json"), "utf8")).version ?? "");
|
|
86
|
+
} catch {
|
|
87
|
+
return "";
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Compare this package's browser dependencies with the copies the project
|
|
93
|
+
* resolves from its root. Only packages the project actually has a separate copy
|
|
94
|
+
* of are reported.
|
|
95
|
+
*/
|
|
96
|
+
export const analyzeSharedDeps = (options: {
|
|
97
|
+
projectRoot: string;
|
|
98
|
+
/** A file inside this package, used as the resolution origin for its own deps. */
|
|
99
|
+
packageFile: string;
|
|
100
|
+
dependencies: Record<string, string>;
|
|
101
|
+
}): SharedDepReport => {
|
|
102
|
+
const projectFile = path.join(options.projectRoot, "package.json");
|
|
103
|
+
const report: SharedDepReport = { dedupe: [], conflicts: [] };
|
|
104
|
+
|
|
105
|
+
for (const [name, range] of Object.entries(options.dependencies)) {
|
|
106
|
+
if (SERVER_ONLY.has(name)) continue;
|
|
107
|
+
const projectDir = locatePackage(name, projectFile);
|
|
108
|
+
const packageDir = locatePackage(name, options.packageFile);
|
|
109
|
+
if (!projectDir || !packageDir || projectDir === packageDir) continue;
|
|
110
|
+
|
|
111
|
+
const projectVersion = readVersion(projectDir);
|
|
112
|
+
const compatible = satisfiesRange(projectVersion, range);
|
|
113
|
+
if (compatible === true) report.dedupe.push(name);
|
|
114
|
+
else if (compatible === false) report.conflicts.push({ name, range, projectVersion });
|
|
115
|
+
}
|
|
116
|
+
return report;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const formatConflicts = (conflicts: SharedDepConflict[]) =>
|
|
120
|
+
[
|
|
121
|
+
"This project installs its own copy of libraries the admin uses, at versions the admin doesn't support:",
|
|
122
|
+
...conflicts.map((c) => ` ${c.name} ${c.projectVersion} (admin requires ${c.range})`),
|
|
123
|
+
' Two copies make Vite re-bundle dependencies in a loop (504 "Outdated Optimize Dep"), so admin',
|
|
124
|
+
" editors fail to load and fields look empty. Align the project's versions, then run `pnpm dev:clean`:",
|
|
125
|
+
` pnpm add ${conflicts.map((c) => `${c.name}@"${c.range}"`).join(" ")}`,
|
|
126
|
+
].join("\n");
|
package/internals/upgrade.ts
CHANGED
|
@@ -271,6 +271,9 @@ const changedFilesJson = (repoDir: string, fromCommit: string, toCommit: string)
|
|
|
271
271
|
const isManagedRuntimePath = (file: string, corePath: string) => {
|
|
272
272
|
// Legacy seed-data file may carry user customizations — careful review, not auto-apply.
|
|
273
273
|
if (file === `${corePath}/internals/seed.data.ts`) return false;
|
|
274
|
+
// The @kidecms/core manifest carries the managed runtime's own dependencies; without it
|
|
275
|
+
// an upgrade ships code that imports packages the project never installs.
|
|
276
|
+
if (file === `${corePath}/package.json`) return true;
|
|
274
277
|
|
|
275
278
|
const managedPrefixes = [
|
|
276
279
|
`${corePath}/admin`,
|
package/internals/virtual.d.ts
CHANGED
|
@@ -14,8 +14,9 @@ declare module "virtual:kide/api" {
|
|
|
14
14
|
|
|
15
15
|
declare module "virtual:kide/schema" {
|
|
16
16
|
const cmsTables: Record<string, { main: any; translations?: any }>;
|
|
17
|
-
export const
|
|
18
|
-
export const
|
|
17
|
+
export const cmsAuthSessions: any;
|
|
18
|
+
export const cmsAuthAccounts: any;
|
|
19
|
+
export const cmsAuthVerifications: any;
|
|
19
20
|
export const cmsRateLimits: any;
|
|
20
21
|
export { cmsTables };
|
|
21
22
|
}
|
package/middleware/auth.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { defineMiddleware } from "astro:middleware";
|
|
2
2
|
import type { APIContext, MiddlewareNext } from "astro";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getSsoDenial,
|
|
5
|
+
publicOrigin,
|
|
6
|
+
registerAuthConfig,
|
|
7
|
+
resolveAdminAuth,
|
|
8
|
+
resolveAdminSession,
|
|
9
|
+
runWithRequestScope,
|
|
10
|
+
} from "../core";
|
|
4
11
|
import type { RequestScope, SessionUser } from "../core";
|
|
5
12
|
import { eq } from "drizzle-orm";
|
|
6
13
|
|
|
7
14
|
import config from "virtual:kide/config";
|
|
8
|
-
import
|
|
15
|
+
import "virtual:kide/runtime";
|
|
9
16
|
import { getDb } from "virtual:kide/db";
|
|
10
17
|
|
|
11
18
|
let hasUsers: boolean | null = null;
|
|
@@ -26,6 +33,7 @@ const normalizeCustomUser = (value: Record<string, unknown> | null): SessionUser
|
|
|
26
33
|
};
|
|
27
34
|
|
|
28
35
|
export const onRequest = defineMiddleware(async (context, next) => {
|
|
36
|
+
registerAuthConfig(config);
|
|
29
37
|
// Establish a per-request scope for EVERY request (including public pages and custom public
|
|
30
38
|
// API routes) so deferred work (audit/search/webhook-enqueue) is kept alive only for THIS
|
|
31
39
|
// request — never routed to the module-level script fallback, whose promises aren't attached
|
|
@@ -57,7 +65,7 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
|
|
|
57
65
|
|
|
58
66
|
// `?preview` exposes draft content on public pages; require a session (unauth → strip it).
|
|
59
67
|
if (context.url.searchParams.has("preview")) {
|
|
60
|
-
const previewUser = await
|
|
68
|
+
const previewUser = (await resolveAdminSession(context.request))?.user ?? null;
|
|
61
69
|
if (!previewUser) {
|
|
62
70
|
const clean = new URL(context.url);
|
|
63
71
|
clean.searchParams.delete("preview");
|
|
@@ -69,16 +77,15 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
|
|
|
69
77
|
const isAdminRoute = pathname.startsWith("/admin");
|
|
70
78
|
const isAdminApiRoute = pathname.startsWith("/api/cms");
|
|
71
79
|
const isLoginPage = pathname === "/admin/login";
|
|
72
|
-
const isLoginApi = pathname === "/api/cms/auth/login";
|
|
73
80
|
const isForgotPasswordPage = pathname === "/admin/forgot-password";
|
|
74
|
-
const isForgotPasswordApi = pathname === "/api/cms/auth/forgot-password";
|
|
75
81
|
const isResetPasswordPage = pathname === "/admin/reset-password";
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
// Every /api/cms/auth/* route guards itself: Kide's own (login, setup, invite, …)
|
|
83
|
+
// and Better Auth's (OAuth callbacks, two-factor, passkeys) through the catch-all.
|
|
84
|
+
const isAuthApi = pathname.startsWith("/api/cms/auth/");
|
|
85
|
+
const isTwoFactorPage = pathname === "/admin/login/verify";
|
|
78
86
|
const isSetupPage = pathname === "/admin/setup";
|
|
79
87
|
const isSetupApi = pathname === "/api/cms/auth/setup";
|
|
80
88
|
const isInvitePage = pathname === "/admin/invite";
|
|
81
|
-
const isInviteApi = pathname === "/api/cms/auth/invite";
|
|
82
89
|
|
|
83
90
|
// Public despite the /api/cms prefix: cmsImageUrl() puts these URLs on public pages.
|
|
84
91
|
// Only reads files under public/, which are served unauthenticated anyway.
|
|
@@ -90,7 +97,8 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
|
|
|
90
97
|
|
|
91
98
|
// Security headers for all admin routes
|
|
92
99
|
const isAuthPath =
|
|
93
|
-
|
|
100
|
+
isAuthApi ||
|
|
101
|
+
isTwoFactorPage ||
|
|
94
102
|
isLoginPage ||
|
|
95
103
|
isSetupPage ||
|
|
96
104
|
isInvitePage ||
|
|
@@ -191,24 +199,21 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
|
|
|
191
199
|
return context.redirect("/admin/login");
|
|
192
200
|
}
|
|
193
201
|
|
|
194
|
-
// Always allow
|
|
202
|
+
// Always allow the sign-in pages, auth API, and cron/webhook endpoints (they have
|
|
195
203
|
// their own auth: bearer secret for cron, HMAC signature for webhooks)
|
|
196
204
|
const isCronApi = pathname === "/api/cms/cron/publish" || pathname === "/api/cms/cron/tasks";
|
|
197
205
|
const isWebhookApi = pathname.startsWith("/api/cms/webhooks/");
|
|
198
206
|
const isFormSubmit = pathname.startsWith("/api/cms/forms/submit/");
|
|
199
207
|
if (
|
|
200
208
|
isLoginPage ||
|
|
201
|
-
|
|
209
|
+
isTwoFactorPage ||
|
|
202
210
|
isForgotPasswordPage ||
|
|
203
|
-
isForgotPasswordApi ||
|
|
204
211
|
isResetPasswordPage ||
|
|
205
|
-
|
|
206
|
-
isSsoAuthApi ||
|
|
212
|
+
isAuthApi ||
|
|
207
213
|
isSetupApi ||
|
|
208
214
|
isCronApi ||
|
|
209
215
|
isWebhookApi ||
|
|
210
216
|
isInvitePage ||
|
|
211
|
-
isInviteApi ||
|
|
212
217
|
isFormSubmit
|
|
213
218
|
) {
|
|
214
219
|
return serve();
|
|
@@ -216,10 +221,19 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
|
|
|
216
221
|
|
|
217
222
|
const auth = resolveAdminAuth(config);
|
|
218
223
|
const customProvider = config.admin?.auth?.provider;
|
|
224
|
+
const session =
|
|
225
|
+
auth.provider === "custom" && typeof customProvider === "object"
|
|
226
|
+
? null
|
|
227
|
+
: await resolveAdminSession(context.request);
|
|
219
228
|
const user =
|
|
220
229
|
auth.provider === "custom" && typeof customProvider === "object"
|
|
221
230
|
? normalizeCustomUser(await customProvider.getSession(context.request))
|
|
222
|
-
:
|
|
231
|
+
: (session?.user ?? null);
|
|
232
|
+
// A sliding-session refresh re-issues the cookie; pass it through.
|
|
233
|
+
const withSessionCookies = (response: Response) => {
|
|
234
|
+
for (const cookie of session?.setCookies ?? []) response.headers.append("Set-Cookie", cookie);
|
|
235
|
+
return response;
|
|
236
|
+
};
|
|
223
237
|
|
|
224
238
|
// Non-httpOnly hint for the public-site edit bar: lets the injected client skip
|
|
225
239
|
// the session check entirely for anonymous visitors. Carries no auth value — the
|
|
@@ -238,13 +252,31 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
|
|
|
238
252
|
};
|
|
239
253
|
|
|
240
254
|
if (!user) {
|
|
255
|
+
const denial = getSsoDenial(context.request);
|
|
241
256
|
// API routes → 401
|
|
242
257
|
if (isAdminApiRoute) {
|
|
243
|
-
|
|
258
|
+
const error = denial
|
|
259
|
+
? denial.kind === "revoked"
|
|
260
|
+
? "sso_access_revoked"
|
|
261
|
+
: "sso_reauthentication_required"
|
|
262
|
+
: null;
|
|
263
|
+
return new Response(JSON.stringify({ error: "Unauthorized", ...(error ? { code: error } : {}) }), {
|
|
244
264
|
status: 401,
|
|
245
265
|
headers: { "Content-Type": "application/json" },
|
|
246
266
|
});
|
|
247
267
|
}
|
|
268
|
+
// The provider refused the user: say so instead of a bare login page.
|
|
269
|
+
if (denial?.kind === "revoked") {
|
|
270
|
+
return withEditorHint(context.redirect("/admin/login?error=sso_access_revoked"), false);
|
|
271
|
+
}
|
|
272
|
+
// Back through the provider — usually a silent hop while its own session is alive.
|
|
273
|
+
if (denial?.kind === "reauth") {
|
|
274
|
+
const returnTo = `${pathname}${context.url.search}`;
|
|
275
|
+
return withEditorHint(
|
|
276
|
+
context.redirect(`/api/cms/auth/sso/${denial.providerId}/start?returnTo=${encodeURIComponent(returnTo)}`),
|
|
277
|
+
false,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
248
280
|
// Admin pages → redirect to login
|
|
249
281
|
return withEditorHint(context.redirect("/admin/login"), false);
|
|
250
282
|
}
|
|
@@ -252,5 +284,13 @@ const handle = async (context: APIContext, next: MiddlewareNext) => {
|
|
|
252
284
|
// Attach user to locals for downstream use
|
|
253
285
|
context.locals.user = user;
|
|
254
286
|
|
|
255
|
-
|
|
287
|
+
// `mfa.require`: password users enroll an authenticator before anything else.
|
|
288
|
+
if (session?.mfaEnrollmentRequired && pathname !== "/admin/account") {
|
|
289
|
+
if (isAdminApiRoute) {
|
|
290
|
+
return Response.json({ error: "Two-factor authentication must be set up first." }, { status: 403 });
|
|
291
|
+
}
|
|
292
|
+
return withSessionCookies(context.redirect("/admin/account?mfa=required"));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return withSessionCookies(withEditorHint(await serve(), true));
|
|
256
296
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kidecms/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "Kide CMS runtime — core library, admin UI, routes, and middleware for Astro. Shipped as TypeScript source.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cms",
|
|
@@ -51,34 +51,39 @@
|
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@ai-sdk/openai": "^3.0.
|
|
55
|
-
"@base-ui/react": "^1.
|
|
54
|
+
"@ai-sdk/openai": "^3.0.114",
|
|
55
|
+
"@base-ui/react": "^1.8.0",
|
|
56
|
+
"@better-auth/passkey": "1.7.7",
|
|
57
|
+
"@better-auth/utils": "0.4.2",
|
|
56
58
|
"@dnd-kit/core": "^6.3.1",
|
|
57
59
|
"@dnd-kit/sortable": "^10.0.0",
|
|
58
60
|
"@dnd-kit/utilities": "^3.2.2",
|
|
59
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
60
|
-
"@radix-ui/react-dropdown-menu": "^2.1.
|
|
61
|
-
"@radix-ui/react-popover": "^1.1.
|
|
61
|
+
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
62
|
+
"@radix-ui/react-dropdown-menu": "^2.1.24",
|
|
63
|
+
"@radix-ui/react-popover": "^1.1.23",
|
|
64
|
+
"@simplewebauthn/browser": "^13.3.0",
|
|
62
65
|
"@tanstack/react-table": "^8.21.3",
|
|
63
|
-
"@tiptap/core": "3.
|
|
64
|
-
"@tiptap/extension-image": "3.
|
|
65
|
-
"@tiptap/extension-link": "3.
|
|
66
|
-
"@tiptap/extension-placeholder": "3.
|
|
67
|
-
"@tiptap/markdown": "3.
|
|
68
|
-
"@tiptap/pm": "3.
|
|
69
|
-
"@tiptap/react": "3.
|
|
70
|
-
"@tiptap/starter-kit": "3.
|
|
71
|
-
"@tiptap/suggestion": "3.
|
|
72
|
-
"ai": "^6.0.
|
|
66
|
+
"@tiptap/core": "3.31.3",
|
|
67
|
+
"@tiptap/extension-image": "3.31.3",
|
|
68
|
+
"@tiptap/extension-link": "3.31.3",
|
|
69
|
+
"@tiptap/extension-placeholder": "3.31.3",
|
|
70
|
+
"@tiptap/markdown": "3.31.3",
|
|
71
|
+
"@tiptap/pm": "3.31.3",
|
|
72
|
+
"@tiptap/react": "3.31.3",
|
|
73
|
+
"@tiptap/starter-kit": "3.31.3",
|
|
74
|
+
"@tiptap/suggestion": "3.31.3",
|
|
75
|
+
"ai": "^6.0.286",
|
|
76
|
+
"better-auth": "1.7.7",
|
|
73
77
|
"class-variance-authority": "^0.7.1",
|
|
74
78
|
"clsx": "^2.1.1",
|
|
75
79
|
"cmdk": "^1.1.1",
|
|
76
|
-
"drizzle-kit": "^0.31.
|
|
77
|
-
"lucide-react": "^
|
|
80
|
+
"drizzle-kit": "^0.31.10",
|
|
81
|
+
"lucide-react": "^1.47.0",
|
|
78
82
|
"nanoid": "^6.0.1",
|
|
79
|
-
"tailwind-merge": "^3.
|
|
80
|
-
"tsx": "^4.
|
|
81
|
-
"tw-animate-css": "^1.4.0"
|
|
83
|
+
"tailwind-merge": "^3.7.0",
|
|
84
|
+
"tsx": "^4.23.13",
|
|
85
|
+
"tw-animate-css": "^1.4.0",
|
|
86
|
+
"uqr": "^0.1.3"
|
|
82
87
|
},
|
|
83
88
|
"peerDependencies": {
|
|
84
89
|
"astro": "^7.0.0",
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
---
|
|
2
|
+
import AdminLayout from "../../admin/layouts/AdminLayout.astro";
|
|
3
|
+
import AdminCard from "../../admin/components/AdminCard.astro";
|
|
4
|
+
import PasskeySettings from "../../admin/components/PasskeySettings";
|
|
5
|
+
import TwoFactorSettings from "../../admin/components/TwoFactorSettings";
|
|
6
|
+
import { Input } from "../../admin/components/ui/input";
|
|
7
|
+
import { Label } from "../../admin/components/ui/label";
|
|
8
|
+
import { buttonVariants } from "../../admin/components/ui/button";
|
|
9
|
+
import { canRead } from "../../admin/lib/access";
|
|
10
|
+
import config from "virtual:kide/config";
|
|
11
|
+
import {
|
|
12
|
+
enforcedSsoProvider,
|
|
13
|
+
getAccountSecurity,
|
|
14
|
+
initDateFormat,
|
|
15
|
+
MIN_PASSWORD_LENGTH,
|
|
16
|
+
mfaRequiredFor,
|
|
17
|
+
resolveAdminAuth,
|
|
18
|
+
} from "../../core";
|
|
19
|
+
|
|
20
|
+
export const prerender = false;
|
|
21
|
+
|
|
22
|
+
initDateFormat(config, Astro.cookies.get("tz")?.value);
|
|
23
|
+
|
|
24
|
+
const user = Astro.locals.user;
|
|
25
|
+
if (!user) return Astro.redirect("/admin/login");
|
|
26
|
+
|
|
27
|
+
const auth = resolveAdminAuth(config);
|
|
28
|
+
const security = await getAccountSecurity(user.id);
|
|
29
|
+
const mfaRequired = mfaRequiredFor(auth, user.role);
|
|
30
|
+
// Domain-bound users sign in through their provider only; a stored password is unused.
|
|
31
|
+
const managedBy = enforcedSsoProvider(auth, user.email);
|
|
32
|
+
const usesPassword = security.hasPassword && !managedBy;
|
|
33
|
+
const mustEnroll = Astro.url.searchParams.get("mfa") === "required" && !security.twoFactorEnabled;
|
|
34
|
+
const ssoLabels = new Map(auth.ssoProviders.map((provider) => [provider.id, provider.label]));
|
|
35
|
+
const accessibleCollections = config.collections.filter((c) => canRead(config, user, c.slug));
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
<AdminLayout title="Account | Kide CMS" collections={accessibleCollections} activeCollection="account">
|
|
39
|
+
<section class="max-w-3xl space-y-6 px-4 py-6 lg:py-8 lg:pr-8 lg:pl-12">
|
|
40
|
+
<div class="space-y-1">
|
|
41
|
+
<h1 class="text-2xl font-semibold tracking-tight">Account & security</h1>
|
|
42
|
+
<p class="text-muted-foreground text-sm">{user.name} · {user.email} · {user.role}</p>
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
{
|
|
46
|
+
mustEnroll && (
|
|
47
|
+
<div class="rounded-lg border border-amber-300 bg-amber-50 px-4 py-3 text-sm text-amber-900 dark:border-amber-700 dark:bg-amber-500/10 dark:text-amber-100">
|
|
48
|
+
Your role requires two-factor authentication. Set up an authenticator app to continue to the admin.
|
|
49
|
+
</div>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
<AdminCard
|
|
54
|
+
title="Sign-in methods"
|
|
55
|
+
description={managedBy ? `Your organization manages your sign-in through ${managedBy.label}.` : undefined}
|
|
56
|
+
>
|
|
57
|
+
<ul class="grid gap-2 text-sm">
|
|
58
|
+
{usesPassword && <li>Email and password</li>}
|
|
59
|
+
{security.ssoProviders.map((id) => <li>{ssoLabels.get(id) ?? id} (single sign-on)</li>)}
|
|
60
|
+
{
|
|
61
|
+
security.passkeys.length > 0 && (
|
|
62
|
+
<li>{security.passkeys.length === 1 ? "1 passkey" : `${security.passkeys.length} passkeys`}</li>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
</ul>
|
|
66
|
+
<form method="post" action="/api/cms/auth/account" class="mt-4">
|
|
67
|
+
<input type="hidden" name="_action" value="revoke-other-sessions" />
|
|
68
|
+
<button class={buttonVariants({ variant: "outline" })} type="submit">Sign out on other devices</button>
|
|
69
|
+
</form>
|
|
70
|
+
</AdminCard>
|
|
71
|
+
|
|
72
|
+
{
|
|
73
|
+
usesPassword && (
|
|
74
|
+
<AdminCard title="Password">
|
|
75
|
+
<form method="post" action="/api/cms/auth/account" class="grid max-w-sm gap-4">
|
|
76
|
+
<input type="hidden" name="_action" value="change-password" />
|
|
77
|
+
<div class="grid gap-2">
|
|
78
|
+
<Label htmlFor="currentPassword">Current password</Label>
|
|
79
|
+
<Input
|
|
80
|
+
id="currentPassword"
|
|
81
|
+
name="currentPassword"
|
|
82
|
+
type="password"
|
|
83
|
+
autoComplete="current-password"
|
|
84
|
+
required
|
|
85
|
+
/>
|
|
86
|
+
</div>
|
|
87
|
+
<div class="grid gap-2">
|
|
88
|
+
<Label htmlFor="newPassword">New password</Label>
|
|
89
|
+
<Input
|
|
90
|
+
id="newPassword"
|
|
91
|
+
name="newPassword"
|
|
92
|
+
type="password"
|
|
93
|
+
autoComplete="new-password"
|
|
94
|
+
placeholder={`Min ${MIN_PASSWORD_LENGTH} characters`}
|
|
95
|
+
required
|
|
96
|
+
/>
|
|
97
|
+
</div>
|
|
98
|
+
<div class="grid gap-2">
|
|
99
|
+
<Label htmlFor="confirmPassword">Confirm new password</Label>
|
|
100
|
+
<Input id="confirmPassword" name="confirmPassword" type="password" autoComplete="new-password" required />
|
|
101
|
+
</div>
|
|
102
|
+
<div>
|
|
103
|
+
<button class={buttonVariants()} type="submit">
|
|
104
|
+
Change password
|
|
105
|
+
</button>
|
|
106
|
+
</div>
|
|
107
|
+
</form>
|
|
108
|
+
</AdminCard>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
{
|
|
113
|
+
auth.mfa.totp && usesPassword && (
|
|
114
|
+
<AdminCard
|
|
115
|
+
title="Two-factor authentication"
|
|
116
|
+
description={
|
|
117
|
+
security.twoFactorEnabled
|
|
118
|
+
? "On — signing in with your password also asks for a code from your authenticator app."
|
|
119
|
+
: "Add a code from an authenticator app to every password sign-in."
|
|
120
|
+
}
|
|
121
|
+
>
|
|
122
|
+
<TwoFactorSettings client:load enabled={security.twoFactorEnabled} required={mfaRequired} />
|
|
123
|
+
</AdminCard>
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
{
|
|
128
|
+
auth.mfa.passkeys && (
|
|
129
|
+
<AdminCard
|
|
130
|
+
title="Passkeys"
|
|
131
|
+
description="Sign in with your fingerprint, face or device PIN instead of a password."
|
|
132
|
+
>
|
|
133
|
+
<PasskeySettings client:load passkeys={security.passkeys} dateLocale={config.admin?.dateFormat} />
|
|
134
|
+
</AdminCard>
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
</section>
|
|
138
|
+
</AdminLayout>
|