@kidecms/core 0.30.1 → 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/PasskeySettings.tsx +121 -0
- package/admin/components/PasskeySignIn.tsx +57 -0
- package/admin/components/SidebarUserMenu.tsx +7 -1
- package/admin/components/TwoFactorSettings.tsx +206 -0
- package/admin/views/EditView.astro +14 -0
- package/core/api.ts +36 -0
- 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 +21 -0
- package/internals/virtual.d.ts +3 -2
- package/middleware/auth.ts +58 -18
- package/package.json +7 -2
- 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/internals/integration.ts
CHANGED
|
@@ -233,6 +233,14 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
|
|
|
233
233
|
|
|
234
234
|
// Inject admin pages
|
|
235
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
|
+
});
|
|
236
244
|
injectRoute({
|
|
237
245
|
pattern: "/admin/forgot-password",
|
|
238
246
|
entrypoint: new URL("../routes/admin/forgot-password.astro", import.meta.url),
|
|
@@ -293,6 +301,19 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
|
|
|
293
301
|
pattern: "/api/cms/auth/invite",
|
|
294
302
|
entrypoint: new URL("../routes/api/auth/invite.ts", import.meta.url),
|
|
295
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
|
+
});
|
|
296
317
|
injectRoute({
|
|
297
318
|
pattern: "/api/cms/assets/upload",
|
|
298
319
|
entrypoint: new URL("../routes/api/assets/upload.ts", import.meta.url),
|
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",
|
|
@@ -53,12 +53,15 @@
|
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@ai-sdk/openai": "^3.0.114",
|
|
55
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
61
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
60
62
|
"@radix-ui/react-dropdown-menu": "^2.1.24",
|
|
61
63
|
"@radix-ui/react-popover": "^1.1.23",
|
|
64
|
+
"@simplewebauthn/browser": "^13.3.0",
|
|
62
65
|
"@tanstack/react-table": "^8.21.3",
|
|
63
66
|
"@tiptap/core": "3.31.3",
|
|
64
67
|
"@tiptap/extension-image": "3.31.3",
|
|
@@ -70,6 +73,7 @@
|
|
|
70
73
|
"@tiptap/starter-kit": "3.31.3",
|
|
71
74
|
"@tiptap/suggestion": "3.31.3",
|
|
72
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",
|
|
@@ -78,7 +82,8 @@
|
|
|
78
82
|
"nanoid": "^6.0.1",
|
|
79
83
|
"tailwind-merge": "^3.7.0",
|
|
80
84
|
"tsx": "^4.23.13",
|
|
81
|
-
"tw-animate-css": "^1.4.0"
|
|
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>
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
---
|
|
2
|
+
import "virtual:kide/admin-css";
|
|
3
|
+
|
|
4
|
+
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../../../admin/components/ui/card";
|
|
5
|
+
import { Input } from "../../../admin/components/ui/input";
|
|
6
|
+
import { Label } from "../../../admin/components/ui/label";
|
|
7
|
+
import { buttonVariants } from "../../../admin/components/ui/button";
|
|
8
|
+
|
|
9
|
+
export const prerender = false;
|
|
10
|
+
|
|
11
|
+
// Reached only mid-sign-in: Better Auth's short-lived two-factor cookie must be present.
|
|
12
|
+
if (!/(?:^|;\s*)(?:__Secure-)?kide\.two_factor=/.test(Astro.request.headers.get("cookie") ?? "")) {
|
|
13
|
+
return Astro.redirect("/admin/login?error=two-factor-expired");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const useBackupCode = Astro.url.searchParams.get("method") === "backup";
|
|
17
|
+
const ERROR_MESSAGES: Record<string, string> = {
|
|
18
|
+
missing: "Enter the code.",
|
|
19
|
+
invalid: useBackupCode ? "That backup code isn't valid." : "That code isn't valid. Check the time on your device.",
|
|
20
|
+
"rate-limited": "Too many attempts. Try again in a few minutes.",
|
|
21
|
+
};
|
|
22
|
+
const error = Astro.url.searchParams.get("error");
|
|
23
|
+
const errorMessage = error ? (ERROR_MESSAGES[error] ?? null) : null;
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
<!doctype html>
|
|
27
|
+
<html lang="en">
|
|
28
|
+
<head>
|
|
29
|
+
<meta charset="utf-8" />
|
|
30
|
+
<meta name="viewport" content="width=device-width" />
|
|
31
|
+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
|
32
|
+
<link rel="icon" href="/favicon.ico" />
|
|
33
|
+
<title>Two-factor authentication | Kide CMS</title>
|
|
34
|
+
</head>
|
|
35
|
+
<body class="bg-background text-foreground min-h-screen antialiased">
|
|
36
|
+
<div class="flex min-h-screen items-center justify-center px-4">
|
|
37
|
+
<div class="w-full max-w-sm space-y-6">
|
|
38
|
+
<h1 class="text-center text-xl font-semibold tracking-tight">Kide CMS</h1>
|
|
39
|
+
<Card>
|
|
40
|
+
<CardHeader>
|
|
41
|
+
<CardTitle>Two-factor authentication</CardTitle>
|
|
42
|
+
<CardDescription>
|
|
43
|
+
{
|
|
44
|
+
useBackupCode
|
|
45
|
+
? "Enter one of the backup codes you saved when you set up two-factor authentication."
|
|
46
|
+
: "Enter the 6-digit code from your authenticator app."
|
|
47
|
+
}
|
|
48
|
+
</CardDescription>
|
|
49
|
+
</CardHeader>
|
|
50
|
+
<CardContent>
|
|
51
|
+
{
|
|
52
|
+
errorMessage && (
|
|
53
|
+
<div class="border-destructive/50 bg-destructive/10 text-destructive mb-4 rounded-lg border px-3 py-2 text-sm">
|
|
54
|
+
{errorMessage}
|
|
55
|
+
</div>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
<form method="post" action="/api/cms/auth/login/verify" class="grid gap-4">
|
|
59
|
+
<input type="hidden" name="method" value={useBackupCode ? "backup" : "totp"} />
|
|
60
|
+
<div class="grid gap-2">
|
|
61
|
+
<Label htmlFor="code">{useBackupCode ? "Backup code" : "Code"}</Label>
|
|
62
|
+
<Input
|
|
63
|
+
id="code"
|
|
64
|
+
name="code"
|
|
65
|
+
type="text"
|
|
66
|
+
inputMode={useBackupCode ? "text" : "numeric"}
|
|
67
|
+
autoComplete="one-time-code"
|
|
68
|
+
pattern={useBackupCode ? undefined : "[0-9 ]*"}
|
|
69
|
+
required
|
|
70
|
+
autoFocus
|
|
71
|
+
/>
|
|
72
|
+
</div>
|
|
73
|
+
<label class="text-muted-foreground flex items-center gap-2 text-sm">
|
|
74
|
+
<input type="checkbox" name="trustDevice" class="accent-primary size-4" />
|
|
75
|
+
Trust this device for 30 days
|
|
76
|
+
</label>
|
|
77
|
+
<button class={buttonVariants({ size: "lg", className: "w-full" })} type="submit">Verify</button>
|
|
78
|
+
<a
|
|
79
|
+
class="text-muted-foreground hover:text-foreground text-center text-sm"
|
|
80
|
+
href={useBackupCode ? "/admin/login/verify" : "/admin/login/verify?method=backup"}
|
|
81
|
+
>
|
|
82
|
+
{useBackupCode ? "Use your authenticator app instead" : "Use a backup code instead"}
|
|
83
|
+
</a>
|
|
84
|
+
</form>
|
|
85
|
+
</CardContent>
|
|
86
|
+
</Card>
|
|
87
|
+
</div>
|
|
88
|
+
</div>
|
|
89
|
+
</body>
|
|
90
|
+
</html>
|
package/routes/admin/login.astro
CHANGED
|
@@ -5,7 +5,8 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../..
|
|
|
5
5
|
import { Input } from "../../admin/components/ui/input";
|
|
6
6
|
import { Label } from "../../admin/components/ui/label";
|
|
7
7
|
import { buttonVariants } from "../../admin/components/ui/button";
|
|
8
|
-
import
|
|
8
|
+
import PasskeySignIn from "../../admin/components/PasskeySignIn";
|
|
9
|
+
import { getSsoProvider, resolveAdminAuth } from "../../core";
|
|
9
10
|
import config from "virtual:kide/config";
|
|
10
11
|
|
|
11
12
|
export const prerender = false;
|
|
@@ -15,10 +16,33 @@ const ERROR_MESSAGES: Record<string, string> = {
|
|
|
15
16
|
missing: "Email and password are required.",
|
|
16
17
|
disabled: "Password login is disabled.",
|
|
17
18
|
"rate-limited": "Too many attempts. Try again in a few minutes.",
|
|
19
|
+
"two-factor-expired": "The sign-in took too long. Start again.",
|
|
20
|
+
not_invited: "There's no Kide account for that user. Ask an admin to invite you.",
|
|
21
|
+
domain_not_allowed: "That account's email domain isn't allowed here.",
|
|
22
|
+
account_not_linked:
|
|
23
|
+
"An account with this email exists, but your identity provider didn't confirm the address. Ask an admin to check the SSO setup.",
|
|
24
|
+
signup_disabled: "There's no Kide account for that user. Ask an admin to invite you.",
|
|
25
|
+
"sso-unknown": "That sign-in option doesn't exist.",
|
|
26
|
+
"sso-unavailable": "Single sign-on isn't available right now. Try again later or contact an admin.",
|
|
27
|
+
access_denied: "Sign-in was cancelled.",
|
|
28
|
+
sso_access_revoked: "Your organization account no longer has access. Contact your administrator.",
|
|
18
29
|
};
|
|
19
|
-
const
|
|
20
|
-
|
|
30
|
+
const STATUS_MESSAGES: Record<string, string> = {
|
|
31
|
+
"password-reset": "Your password was changed. Sign in with the new one.",
|
|
32
|
+
};
|
|
33
|
+
// Better Auth reports SSO refusals as upper-case codes (NOT_INVITED); Kide's own are kebab-case.
|
|
34
|
+
const rawError = Astro.url.searchParams.get("error");
|
|
35
|
+
const error = rawError && rawError === rawError.toUpperCase() ? rawError.toLowerCase() : rawError;
|
|
21
36
|
const auth = resolveAdminAuth(config);
|
|
37
|
+
const requiredSso = error === "sso-required" ? getSsoProvider(config, Astro.url.searchParams.get("sso") ?? "") : null;
|
|
38
|
+
const errorMessage = requiredSso
|
|
39
|
+
? `Your organization signs in with ${requiredSso.label}. Use the button above.`
|
|
40
|
+
: error
|
|
41
|
+
? (ERROR_MESSAGES[error] ?? "Sign-in failed. Try again.")
|
|
42
|
+
: null;
|
|
43
|
+
const status = Astro.url.searchParams.get("status");
|
|
44
|
+
const statusMessage = status ? (STATUS_MESSAGES[status] ?? null) : null;
|
|
45
|
+
const hasSso = auth.ssoProviders.length > 0;
|
|
22
46
|
---
|
|
23
47
|
|
|
24
48
|
<!doctype html>
|
|
@@ -37,7 +61,9 @@ const auth = resolveAdminAuth(config);
|
|
|
37
61
|
<Card>
|
|
38
62
|
<CardHeader>
|
|
39
63
|
<CardTitle>Sign in</CardTitle>
|
|
40
|
-
<CardDescription>
|
|
64
|
+
<CardDescription>
|
|
65
|
+
{auth.password.enabled ? "Enter your credentials to continue" : "Continue with your organization account"}
|
|
66
|
+
</CardDescription>
|
|
41
67
|
</CardHeader>
|
|
42
68
|
<CardContent>
|
|
43
69
|
{
|
|
@@ -47,13 +73,51 @@ const auth = resolveAdminAuth(config);
|
|
|
47
73
|
</div>
|
|
48
74
|
)
|
|
49
75
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
76
|
+
{
|
|
77
|
+
statusMessage && (
|
|
78
|
+
<div class="border-border bg-muted mb-4 rounded-lg border px-3 py-2 text-sm">{statusMessage}</div>
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
{
|
|
82
|
+
hasSso && (
|
|
83
|
+
<div class="grid gap-3">
|
|
84
|
+
{auth.ssoProviders.map((provider) => (
|
|
85
|
+
<a
|
|
86
|
+
class={buttonVariants({
|
|
87
|
+
variant: auth.password.enabled ? "outline" : "default",
|
|
88
|
+
size: "lg",
|
|
89
|
+
className: "w-full",
|
|
90
|
+
})}
|
|
91
|
+
href={`/api/cms/auth/sso/${provider.id}/start`}
|
|
92
|
+
>
|
|
93
|
+
Continue with {provider.label}
|
|
94
|
+
</a>
|
|
95
|
+
))}
|
|
96
|
+
</div>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
{
|
|
100
|
+
auth.password.enabled && (
|
|
101
|
+
<>
|
|
102
|
+
{hasSso && (
|
|
103
|
+
<div class="text-muted-foreground my-4 flex items-center gap-3 text-xs">
|
|
104
|
+
<div class="bg-border h-px flex-1" />
|
|
105
|
+
<span>or</span>
|
|
106
|
+
<div class="bg-border h-px flex-1" />
|
|
107
|
+
</div>
|
|
108
|
+
)}
|
|
109
|
+
<form method="post" action="/api/cms/auth/login" class="grid gap-4">
|
|
54
110
|
<div class="grid gap-2">
|
|
55
111
|
<Label htmlFor="email">Email</Label>
|
|
56
|
-
<Input
|
|
112
|
+
<Input
|
|
113
|
+
id="email"
|
|
114
|
+
name="email"
|
|
115
|
+
type="email"
|
|
116
|
+
placeholder="admin@example.com"
|
|
117
|
+
autoComplete="username webauthn"
|
|
118
|
+
required
|
|
119
|
+
autoFocus={!hasSso}
|
|
120
|
+
/>
|
|
57
121
|
</div>
|
|
58
122
|
<div class="grid gap-2">
|
|
59
123
|
<div class="flex items-center justify-between gap-3">
|
|
@@ -64,36 +128,23 @@ const auth = resolveAdminAuth(config);
|
|
|
64
128
|
</a>
|
|
65
129
|
)}
|
|
66
130
|
</div>
|
|
67
|
-
<Input
|
|
131
|
+
<Input
|
|
132
|
+
id="password"
|
|
133
|
+
name="password"
|
|
134
|
+
type="password"
|
|
135
|
+
placeholder="Enter password"
|
|
136
|
+
autoComplete="current-password"
|
|
137
|
+
required
|
|
138
|
+
/>
|
|
68
139
|
</div>
|
|
69
140
|
<button class={buttonVariants({ size: "lg", className: "w-full" })} type="submit">
|
|
70
141
|
Sign in
|
|
71
142
|
</button>
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
</form>
|
|
76
|
-
{
|
|
77
|
-
auth.ssoProviders.length > 0 && (
|
|
78
|
-
<div class="mt-4 grid gap-3">
|
|
79
|
-
{auth.password.enabled && (
|
|
80
|
-
<div class="text-muted-foreground flex items-center gap-3 text-xs">
|
|
81
|
-
<div class="bg-border h-px flex-1" />
|
|
82
|
-
<span>or</span>
|
|
83
|
-
<div class="bg-border h-px flex-1" />
|
|
84
|
-
</div>
|
|
85
|
-
)}
|
|
86
|
-
{auth.ssoProviders.map((provider) => (
|
|
87
|
-
<a
|
|
88
|
-
class={buttonVariants({ variant: "outline", size: "lg", className: "w-full" })}
|
|
89
|
-
href={`/api/cms/auth/sso/${provider.id}/start`}
|
|
90
|
-
>
|
|
91
|
-
Continue with {provider.label}
|
|
92
|
-
</a>
|
|
93
|
-
))}
|
|
94
|
-
</div>
|
|
143
|
+
</form>
|
|
144
|
+
</>
|
|
95
145
|
)
|
|
96
146
|
}
|
|
147
|
+
{auth.mfa.passkeys && <PasskeySignIn client:load />}
|
|
97
148
|
</CardContent>
|
|
98
149
|
</Card>
|
|
99
150
|
</div>
|