inlaycms 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +462 -2
- package/assets/admin/admin.css +2 -0
- package/assets/admin/admin.js +1 -0
- package/assets/admin/editor.js +125 -0
- package/dist/adapters/admin/activity.d.ts +17 -0
- package/dist/adapters/admin/activity.js +59 -0
- package/dist/adapters/admin/assets.d.ts +14 -0
- package/dist/adapters/admin/assets.js +64 -0
- package/dist/adapters/admin/attention.d.ts +15 -0
- package/dist/adapters/admin/attention.js +34 -0
- package/dist/adapters/admin/context.d.ts +40 -0
- package/dist/adapters/admin/context.js +142 -0
- package/dist/adapters/admin/form/walk.d.ts +61 -0
- package/dist/adapters/admin/form/walk.js +565 -0
- package/dist/adapters/admin/pages.d.ts +38 -0
- package/dist/adapters/admin/pages.js +66 -0
- package/dist/adapters/admin/publish.d.ts +12 -0
- package/dist/adapters/admin/publish.js +195 -0
- package/dist/adapters/admin/routes.d.ts +47 -0
- package/dist/adapters/admin/routes.js +255 -0
- package/dist/adapters/admin/screens/backups.d.ts +11 -0
- package/dist/adapters/admin/screens/backups.js +223 -0
- package/dist/adapters/admin/screens/dashboard.d.ts +4 -0
- package/dist/adapters/admin/screens/dashboard.js +160 -0
- package/dist/adapters/admin/screens/editor.d.ts +67 -0
- package/dist/adapters/admin/screens/editor.js +416 -0
- package/dist/adapters/admin/screens/history.d.ts +4 -0
- package/dist/adapters/admin/screens/history.js +105 -0
- package/dist/adapters/admin/screens/lists.d.ts +4 -0
- package/dist/adapters/admin/screens/lists.js +297 -0
- package/dist/adapters/admin/screens/media.d.ts +20 -0
- package/dist/adapters/admin/screens/media.js +186 -0
- package/dist/adapters/admin/screens/pickers.d.ts +29 -0
- package/dist/adapters/admin/screens/pickers.js +134 -0
- package/dist/adapters/admin/screens/preview-pane.d.ts +10 -0
- package/dist/adapters/admin/screens/preview-pane.js +74 -0
- package/dist/adapters/admin/screens/redirects.d.ts +4 -0
- package/dist/adapters/admin/screens/redirects.js +137 -0
- package/dist/adapters/admin/screens/releases.d.ts +7 -0
- package/dist/adapters/admin/screens/releases.js +287 -0
- package/dist/adapters/admin/screens/search.d.ts +22 -0
- package/dist/adapters/admin/screens/search.js +113 -0
- package/dist/adapters/admin/screens/security.d.ts +4 -0
- package/dist/adapters/admin/screens/security.js +85 -0
- package/dist/adapters/admin/screens/users.d.ts +4 -0
- package/dist/adapters/admin/screens/users.js +330 -0
- package/dist/adapters/admin/theme.d.ts +1 -0
- package/dist/adapters/admin/theme.js +1 -0
- package/dist/adapters/admin/translations.d.ts +16 -0
- package/dist/adapters/admin/translations.js +23 -0
- package/dist/adapters/admin/upload.d.ts +8 -0
- package/dist/adapters/admin/upload.js +116 -0
- package/dist/adapters/admin/views/data-table.d.ts +82 -0
- package/dist/adapters/admin/views/data-table.js +117 -0
- package/dist/adapters/admin/views/icons.d.ts +48 -0
- package/dist/adapters/admin/views/icons.js +47 -0
- package/dist/adapters/admin/views/layout.d.ts +51 -0
- package/dist/adapters/admin/views/layout.js +145 -0
- package/dist/adapters/admin/views/sheet.d.ts +15 -0
- package/dist/adapters/admin/views/sheet.js +6 -0
- package/dist/adapters/admin/views/status.d.ts +32 -0
- package/dist/adapters/admin/views/status.js +74 -0
- package/dist/adapters/auth/handler.d.ts +10 -0
- package/dist/adapters/auth/handler.js +63 -0
- package/dist/adapters/auth/request.d.ts +8 -0
- package/dist/adapters/auth/request.js +14 -0
- package/dist/adapters/cli/bin.d.ts +2 -0
- package/dist/adapters/cli/bin.js +42 -0
- package/dist/adapters/cli/main.d.ts +7 -0
- package/dist/adapters/cli/main.js +982 -0
- package/dist/adapters/cli/remote.d.ts +52 -0
- package/dist/adapters/cli/remote.js +161 -0
- package/dist/adapters/mcp/agent-refs.d.ts +9 -0
- package/dist/adapters/mcp/agent-refs.js +25 -0
- package/dist/adapters/mcp/documents.d.ts +15 -0
- package/dist/adapters/mcp/documents.js +26 -0
- package/dist/adapters/mcp/fetch-url.d.ts +19 -0
- package/dist/adapters/mcp/fetch-url.js +244 -0
- package/dist/adapters/mcp/request-state.d.ts +15 -0
- package/dist/adapters/mcp/request-state.js +38 -0
- package/dist/adapters/mcp/results.d.ts +20 -0
- package/dist/adapters/mcp/results.js +61 -0
- package/dist/adapters/mcp/route.d.ts +34 -0
- package/dist/adapters/mcp/route.js +52 -0
- package/dist/adapters/mcp/server.d.ts +44 -0
- package/dist/adapters/mcp/server.js +49 -0
- package/dist/adapters/mcp/tools/history.d.ts +5 -0
- package/dist/adapters/mcp/tools/history.js +84 -0
- package/dist/adapters/mcp/tools/media.d.ts +5 -0
- package/dist/adapters/mcp/tools/media.js +102 -0
- package/dist/adapters/mcp/tools/publish.d.ts +5 -0
- package/dist/adapters/mcp/tools/publish.js +157 -0
- package/dist/adapters/mcp/tools/read.d.ts +5 -0
- package/dist/adapters/mcp/tools/read.js +279 -0
- package/dist/adapters/mcp/tools/redirects.d.ts +5 -0
- package/dist/adapters/mcp/tools/redirects.js +74 -0
- package/dist/adapters/mcp/tools/write.d.ts +5 -0
- package/dist/adapters/mcp/tools/write.js +304 -0
- package/dist/adapters/site/backup-schedule.d.ts +8 -0
- package/dist/adapters/site/backup-schedule.js +31 -0
- package/dist/adapters/site/cache.d.ts +29 -0
- package/dist/adapters/site/cache.js +63 -0
- package/dist/adapters/site/context.d.ts +29 -0
- package/dist/adapters/site/context.js +278 -0
- package/dist/adapters/site/head.d.ts +23 -0
- package/dist/adapters/site/head.js +44 -0
- package/dist/adapters/site/limiter.d.ts +17 -0
- package/dist/adapters/site/limiter.js +39 -0
- package/dist/adapters/site/lock.d.ts +30 -0
- package/dist/adapters/site/lock.js +143 -0
- package/dist/adapters/site/media-routes.d.ts +4 -0
- package/dist/adapters/site/media-routes.js +55 -0
- package/dist/adapters/site/plugin.d.ts +122 -0
- package/dist/adapters/site/plugin.js +436 -0
- package/dist/adapters/site/preview.d.ts +36 -0
- package/dist/adapters/site/preview.js +82 -0
- package/dist/adapters/site/public.d.ts +6 -0
- package/dist/adapters/site/public.js +87 -0
- package/dist/adapters/site/render.d.ts +45 -0
- package/dist/adapters/site/render.js +191 -0
- package/dist/adapters/site/routes.d.ts +17 -0
- package/dist/adapters/site/routes.js +125 -0
- package/dist/adapters/site/sitemap.d.ts +10 -0
- package/dist/adapters/site/sitemap.js +10 -0
- package/dist/adapters/web.d.ts +22 -0
- package/dist/adapters/web.js +75 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +58 -0
- package/dist/core/agent/format.d.ts +62 -0
- package/dist/core/agent/format.js +211 -0
- package/dist/core/agent/schema.d.ts +6 -0
- package/dist/core/agent/schema.js +86 -0
- package/dist/core/archive.d.ts +12 -0
- package/dist/core/archive.js +72 -0
- package/dist/core/auth/auth.d.ts +4911 -0
- package/dist/core/auth/auth.js +61 -0
- package/dist/core/auth/roles.d.ts +10 -0
- package/dist/core/auth/roles.js +38 -0
- package/dist/core/auth/service.d.ts +118 -0
- package/dist/core/auth/service.js +424 -0
- package/dist/core/auth/upload-token.d.ts +8 -0
- package/dist/core/auth/upload-token.js +27 -0
- package/dist/core/backup/backup.d.ts +28 -0
- package/dist/core/backup/backup.js +126 -0
- package/dist/core/backup/restore.d.ts +41 -0
- package/dist/core/backup/restore.js +264 -0
- package/dist/core/backup/schedule.d.ts +12 -0
- package/dist/core/backup/schedule.js +29 -0
- package/dist/core/boot.d.ts +63 -0
- package/dist/core/boot.js +141 -0
- package/dist/core/check.d.ts +32 -0
- package/dist/core/check.js +111 -0
- package/dist/core/content/activity.d.ts +8 -0
- package/dist/core/content/activity.js +101 -0
- package/dist/core/content/delete.d.ts +5 -0
- package/dist/core/content/delete.js +100 -0
- package/dist/core/content/diff.d.ts +6 -0
- package/dist/core/content/diff.js +129 -0
- package/dist/core/content/dry-run.d.ts +24 -0
- package/dist/core/content/dry-run.js +69 -0
- package/dist/core/content/gaps.d.ts +25 -0
- package/dist/core/content/gaps.js +50 -0
- package/dist/core/content/hidden.d.ts +33 -0
- package/dist/core/content/hidden.js +103 -0
- package/dist/core/content/history.d.ts +15 -0
- package/dist/core/content/history.js +62 -0
- package/dist/core/content/patch.d.ts +40 -0
- package/dist/core/content/patch.js +287 -0
- package/dist/core/content/path.d.ts +6 -0
- package/dist/core/content/path.js +38 -0
- package/dist/core/content/publish.d.ts +26 -0
- package/dist/core/content/publish.js +211 -0
- package/dist/core/content/read.d.ts +99 -0
- package/dist/core/content/read.js +141 -0
- package/dist/core/content/reader.d.ts +37 -0
- package/dist/core/content/reader.js +195 -0
- package/dist/core/content/refs.d.ts +44 -0
- package/dist/core/content/refs.js +161 -0
- package/dist/core/content/release-migrations.d.ts +12 -0
- package/dist/core/content/release-migrations.js +154 -0
- package/dist/core/content/save.d.ts +25 -0
- package/dist/core/content/save.js +87 -0
- package/dist/core/content/schema.d.ts +22 -0
- package/dist/core/content/schema.js +127 -0
- package/dist/core/content/search.d.ts +8 -0
- package/dist/core/content/search.js +89 -0
- package/dist/core/content/shipped-input.d.ts +39 -0
- package/dist/core/content/shipped-input.js +221 -0
- package/dist/core/content/shipped.d.ts +112 -0
- package/dist/core/content/shipped.js +437 -0
- package/dist/core/content/slug.d.ts +23 -0
- package/dist/core/content/slug.js +182 -0
- package/dist/core/content/stats.d.ts +11 -0
- package/dist/core/content/stats.js +30 -0
- package/dist/core/content/translate.d.ts +3 -0
- package/dist/core/content/translate.js +68 -0
- package/dist/core/content/usages.d.ts +13 -0
- package/dist/core/content/usages.js +70 -0
- package/dist/core/content/validate.d.ts +21 -0
- package/dist/core/content/validate.js +124 -0
- package/dist/core/content/write.d.ts +10 -0
- package/dist/core/content/write.js +93 -0
- package/dist/core/core.d.ts +227 -0
- package/dist/core/core.js +251 -0
- package/dist/core/db/connection.d.ts +6 -0
- package/dist/core/db/connection.js +12 -0
- package/dist/core/db/migrate.d.ts +9 -0
- package/dist/core/db/migrate.js +42 -0
- package/dist/core/db/migrations/0001_content.d.ts +2 -0
- package/dist/core/db/migrations/0001_content.js +69 -0
- package/dist/core/db/migrations/0002_media.d.ts +2 -0
- package/dist/core/db/migrations/0002_media.js +26 -0
- package/dist/core/db/migrations/0003_redirects.d.ts +2 -0
- package/dist/core/db/migrations/0003_redirects.js +19 -0
- package/dist/core/db/migrations/0004_token_cutoffs.d.ts +3 -0
- package/dist/core/db/migrations/0004_token_cutoffs.js +8 -0
- package/dist/core/db/migrations/0005_site_lock.d.ts +3 -0
- package/dist/core/db/migrations/0005_site_lock.js +10 -0
- package/dist/core/db/migrations/0006_releases.d.ts +3 -0
- package/dist/core/db/migrations/0006_releases.js +34 -0
- package/dist/core/db/schema.d.ts +155 -0
- package/dist/core/db/schema.js +1 -0
- package/dist/core/errors.d.ts +27 -0
- package/dist/core/errors.js +15 -0
- package/dist/core/export/export.d.ts +17 -0
- package/dist/core/export/export.js +185 -0
- package/dist/core/export/format.d.ts +241 -0
- package/dist/core/export/format.js +106 -0
- package/dist/core/export/import.d.ts +54 -0
- package/dist/core/export/import.js +180 -0
- package/dist/core/export/read.d.ts +16 -0
- package/dist/core/export/read.js +234 -0
- package/dist/core/export/replace.d.ts +17 -0
- package/dist/core/export/replace.js +36 -0
- package/dist/core/export/report.d.ts +55 -0
- package/dist/core/export/report.js +50 -0
- package/dist/core/export/schema.d.ts +8 -0
- package/dist/core/export/schema.js +47 -0
- package/dist/core/export/stage.d.ts +25 -0
- package/dist/core/export/stage.js +192 -0
- package/dist/core/fields/builtins.d.ts +70 -0
- package/dist/core/fields/builtins.js +335 -0
- package/dist/core/fields/define.d.ts +86 -0
- package/dist/core/fields/define.js +34 -0
- package/dist/core/fields/walk.d.ts +49 -0
- package/dist/core/fields/walk.js +270 -0
- package/dist/core/fields/widgets.d.ts +27 -0
- package/dist/core/fields/widgets.js +330 -0
- package/dist/core/fields/zod-internals.d.ts +29 -0
- package/dist/core/fields/zod-internals.js +28 -0
- package/dist/core/media/detect.d.ts +5 -0
- package/dist/core/media/detect.js +66 -0
- package/dist/core/media/labels.d.ts +6 -0
- package/dist/core/media/labels.js +17 -0
- package/dist/core/media/library.d.ts +45 -0
- package/dist/core/media/library.js +239 -0
- package/dist/core/media/store.d.ts +11 -0
- package/dist/core/media/store.js +49 -0
- package/dist/core/media/variants.d.ts +10 -0
- package/dist/core/media/variants.js +59 -0
- package/dist/core/messages/en.d.ts +1278 -0
- package/dist/core/messages/en.js +1287 -0
- package/dist/core/messages/index.d.ts +7 -0
- package/dist/core/messages/index.js +14 -0
- package/dist/core/preview.d.ts +16 -0
- package/dist/core/preview.js +42 -0
- package/dist/core/redirects/collapse.d.ts +68 -0
- package/dist/core/redirects/collapse.js +153 -0
- package/dist/core/redirects/csv.d.ts +15 -0
- package/dist/core/redirects/csv.js +68 -0
- package/dist/core/redirects/redirects.d.ts +58 -0
- package/dist/core/redirects/redirects.js +545 -0
- package/dist/core/registry/content.d.ts +26 -0
- package/dist/core/registry/content.js +9 -0
- package/dist/core/registry/define.d.ts +145 -0
- package/dist/core/registry/define.js +41 -0
- package/dist/core/registry/migration.d.ts +44 -0
- package/dist/core/registry/migration.js +4 -0
- package/dist/core/registry/registry.d.ts +18 -0
- package/dist/core/registry/registry.js +255 -0
- package/dist/core/registry/render.d.ts +98 -0
- package/dist/core/registry/render.js +1 -0
- package/dist/core/registry/route.d.ts +42 -0
- package/dist/core/registry/route.js +3 -0
- package/dist/core/release/bundle.d.ts +21 -0
- package/dist/core/release/bundle.js +252 -0
- package/dist/core/release/keys.d.ts +11 -0
- package/dist/core/release/keys.js +60 -0
- package/dist/core/release/manifest.d.ts +103 -0
- package/dist/core/release/manifest.js +138 -0
- package/dist/core/release/pack.d.ts +20 -0
- package/dist/core/release/pack.js +97 -0
- package/dist/core/release/pointer.d.ts +19 -0
- package/dist/core/release/pointer.js +53 -0
- package/dist/core/release/read.d.ts +59 -0
- package/dist/core/release/read.js +256 -0
- package/dist/core/report.d.ts +31 -0
- package/dist/core/report.js +37 -0
- package/dist/core/richtext/html.d.ts +15 -0
- package/dist/core/richtext/html.js +81 -0
- package/dist/core/richtext/markdown.d.ts +19 -0
- package/dist/core/richtext/markdown.js +951 -0
- package/dist/core/richtext/schema.d.ts +98 -0
- package/dist/core/richtext/schema.js +275 -0
- package/dist/core/site/live.d.ts +56 -0
- package/dist/core/site/live.js +145 -0
- package/dist/core/site/paths.d.ts +15 -0
- package/dist/core/site/paths.js +64 -0
- package/dist/core/site/resolve.d.ts +9 -0
- package/dist/core/site/resolve.js +202 -0
- package/dist/core/site/sitemap.d.ts +11 -0
- package/dist/core/site/sitemap.js +24 -0
- package/dist/core/site-lock/site-lock.d.ts +21 -0
- package/dist/core/site-lock/site-lock.js +65 -0
- package/dist/core/site-lock/token.d.ts +7 -0
- package/dist/core/site-lock/token.js +24 -0
- package/dist/core/theme.d.ts +22 -0
- package/dist/core/theme.js +133 -0
- package/dist/core/types.d.ts +253 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.d.ts +1 -0
- package/dist/core/version.js +5 -0
- package/dist/host/activate.d.ts +53 -0
- package/dist/host/activate.js +162 -0
- package/dist/host/app.d.ts +46 -0
- package/dist/host/app.js +129 -0
- package/dist/host/bin.d.ts +5 -0
- package/dist/host/bin.js +8 -0
- package/dist/host/config.d.ts +36 -0
- package/dist/host/config.js +152 -0
- package/dist/host/dev.d.ts +10 -0
- package/dist/host/dev.js +293 -0
- package/dist/host/diff.d.ts +78 -0
- package/dist/host/diff.js +346 -0
- package/dist/host/hooks.d.ts +41 -0
- package/dist/host/hooks.js +49 -0
- package/dist/host/ipc.d.ts +123 -0
- package/dist/host/ipc.js +61 -0
- package/dist/host/preview-proxy.d.ts +24 -0
- package/dist/host/preview-proxy.js +154 -0
- package/dist/host/preview-worker.d.ts +42 -0
- package/dist/host/preview-worker.js +242 -0
- package/dist/host/primary.d.ts +38 -0
- package/dist/host/primary.js +357 -0
- package/dist/host/push.d.ts +125 -0
- package/dist/host/push.js +420 -0
- package/dist/host/serve.d.ts +16 -0
- package/dist/host/serve.js +150 -0
- package/dist/host/store.d.ts +48 -0
- package/dist/host/store.js +109 -0
- package/dist/host/worker.d.ts +67 -0
- package/dist/host/worker.js +310 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +32 -0
- package/dist/jsx/html.d.ts +16 -0
- package/dist/jsx/html.js +65 -0
- package/dist/jsx/jsx-dev-runtime.d.ts +4 -0
- package/dist/jsx/jsx-dev-runtime.js +5 -0
- package/dist/jsx/jsx-runtime.d.ts +17 -0
- package/dist/jsx/jsx-runtime.js +41 -0
- package/dist/site.d.ts +17 -0
- package/dist/site.js +14 -0
- package/package.json +111 -3
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { t } from '../../core/messages/index.js';
|
|
2
|
+
import { html } from '../../jsx/html.js';
|
|
3
|
+
import { adminPage, Alert, authPage } from './views/layout.js';
|
|
4
|
+
/** A labeled input, with its hint and problems tied to it through `aria-describedby`. */
|
|
5
|
+
function field(opts) {
|
|
6
|
+
const errors = opts.errors ?? [];
|
|
7
|
+
const hintId = `${opts.id}-hint`;
|
|
8
|
+
const errorId = `${opts.id}-error`;
|
|
9
|
+
const describedBy = [opts.hint !== undefined ? hintId : null, errors.length > 0 ? errorId : null].filter((x) => x !== null);
|
|
10
|
+
return html `<div class="flex flex-col gap-1"><label for="${opts.id}" class="font-medium">${opts.label}</label>${opts.hint !== undefined ? html `<p class="text-sm" id="${hintId}">${opts.hint}</p>` : ''}${errors.length > 0 ? html `<p class="text-sm text-error" id="${errorId}">${errors.join(' ')}</p>` : ''}<input id="${opts.id}" name="${opts.id}" type="${opts.type}" autocomplete="${opts.autocomplete}"${opts.value !== undefined ? html ` value="${opts.value}"` : ''} class="input w-full" required${describedBy.length > 0 ? html ` aria-describedby="${describedBy.join(' ')}"` : ''}${errors.length > 0 ? html ` aria-invalid="true"` : ''}></div>`;
|
|
11
|
+
}
|
|
12
|
+
const alert = (message) => (message === undefined ? '' : Alert({ kind: 'error', children: message }));
|
|
13
|
+
/** First run: the form that creates the owner account. */
|
|
14
|
+
export function setupPage(ctx, opts) {
|
|
15
|
+
const errors = opts.errors ?? {};
|
|
16
|
+
const problems = Object.values(errors).some((list) => list.length > 0);
|
|
17
|
+
const body = html `<p>${t('auth.setup.intro')}</p>${alert(problems ? t('auth.formProblems') : undefined)}<form method="post" action="/admin/setup" class="flex flex-col gap-4">${[
|
|
18
|
+
field({ id: 'name', label: t('label.user.name'), type: 'text', autocomplete: 'name', value: opts.values?.name ?? '', errors: errors.name ?? [] }),
|
|
19
|
+
field({ id: 'email', label: t('label.user.email'), type: 'email', autocomplete: 'email', value: opts.values?.email ?? '', errors: errors.email ?? [] }),
|
|
20
|
+
field({ id: 'password', label: t('label.user.password'), type: 'password', autocomplete: 'new-password', hint: t('auth.passwordHint'), errors: errors.password ?? [] }),
|
|
21
|
+
field({ id: 'confirm', label: t('auth.passwordConfirm'), type: 'password', autocomplete: 'new-password', errors: errors.confirm ?? [] }),
|
|
22
|
+
]}<button type="submit" class="btn btn-primary">${t('auth.setup.submit')}</button></form>`;
|
|
23
|
+
return authPage(ctx, t('auth.setup.title'), body);
|
|
24
|
+
}
|
|
25
|
+
/** The login form. `oauthQuery` carries a signed OAuth authorize request through the login. */
|
|
26
|
+
export function loginPage(ctx, opts) {
|
|
27
|
+
const body = html `${alert(opts.error)}<form method="post" action="/admin/login" class="flex flex-col gap-4"><input type="hidden" name="next" value="${opts.next}">${opts.oauthQuery !== undefined ? html `<input type="hidden" name="oauth_query" value="${opts.oauthQuery}">` : ''}${[
|
|
28
|
+
field({ id: 'email', label: t('label.user.email'), type: 'email', autocomplete: 'username', value: opts.email ?? '' }),
|
|
29
|
+
field({ id: 'password', label: t('label.user.password'), type: 'password', autocomplete: 'current-password' }),
|
|
30
|
+
]}<button type="submit" class="btn btn-primary">${t('auth.login.submit')}</button></form>`;
|
|
31
|
+
return authPage(ctx, t('auth.login.title'), body);
|
|
32
|
+
}
|
|
33
|
+
/** `/admin` until the dashboard: who is signed in and a link to the publish page. The navigation has the logout button. */
|
|
34
|
+
export function adminHomePage(ctx, actor) {
|
|
35
|
+
const role = actor.role === 'owner' ? t('role.owner') : t('role.editor');
|
|
36
|
+
const body = html `<div class="flex flex-col gap-4"><p>${t('admin.signedInAs', { name: actor.name, role })}</p><p>${t('admin.comingSoon')}</p><p><a href="/admin/publish" data-publish-dialog class="link">${t('admin.publishLink')}</a></p></div>`;
|
|
37
|
+
return adminPage(ctx, { title: t('admin.nav.dashboard'), actor, section: 'dashboard' }, body);
|
|
38
|
+
}
|
|
39
|
+
const SCOPES = {
|
|
40
|
+
openid: 'auth.consent.scope.openid',
|
|
41
|
+
profile: 'auth.consent.scope.profile',
|
|
42
|
+
email: 'auth.consent.scope.email',
|
|
43
|
+
offline_access: 'auth.consent.scope.offline_access',
|
|
44
|
+
};
|
|
45
|
+
const scopeText = (scope) => Object.hasOwn(SCOPES, scope) ? t(SCOPES[scope]) : t('auth.consent.scope.other', { scope });
|
|
46
|
+
/**
|
|
47
|
+
* Asks the signed-in user whether `client` may use their account. It names the client and the host it sends the
|
|
48
|
+
* user back to, since anyone can register a client under any name.
|
|
49
|
+
*/
|
|
50
|
+
export function consentPage(ctx, opts) {
|
|
51
|
+
const { client } = opts;
|
|
52
|
+
const scopes = opts.scopes.length > 0 ? html `<p>${t('auth.consent.scopes')}</p><ul>${opts.scopes.map((s) => html `<li>${scopeText(s)}</li>`)}</ul>` : '';
|
|
53
|
+
const hosts = html `<p>${t('auth.consent.hosts', { client, hosts: opts.hosts.join(', ') })}</p>${opts.hosts.length > 1 ? Alert({ kind: 'error', children: t('auth.consent.manyHosts', { client }) }) : ''}`;
|
|
54
|
+
const body = html `<p>${t('auth.consent.intro', { client, name: opts.name })}</p>${scopes}<p><strong>${t('auth.consent.redirect', {
|
|
55
|
+
host: opts.host,
|
|
56
|
+
})}</strong></p>${hosts}<form method="post" action="/admin/consent" class="flex gap-2"><input type="hidden" name="oauth_query" value="${opts.oauthQuery}"><button type="submit" name="accept" value="true" class="btn btn-primary">${t('auth.consent.allow')}</button> <button type="submit" name="accept" value="false" class="btn">${t('auth.consent.deny')}</button></form>`;
|
|
57
|
+
return authPage(ctx, t('auth.consent.title', { client }), body);
|
|
58
|
+
}
|
|
59
|
+
/** A POST whose `Origin` isn't the site's. */
|
|
60
|
+
export function badOriginPage(ctx) {
|
|
61
|
+
return authPage(ctx, t('auth.badOrigin.title'), html `<p>${t('auth.badOrigin')}</p>`);
|
|
62
|
+
}
|
|
63
|
+
/** A page with a catalog title and one catalog paragraph, such as a failed consent or an unexpected error. */
|
|
64
|
+
export function messagePage(ctx, titleId, bodyId) {
|
|
65
|
+
return authPage(ctx, t(titleId), html `<p>${t(bodyId)}</p>`);
|
|
66
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import { type AdminContext } from './context.js';
|
|
3
|
+
/** The ids of a comma-separated list or of a repeated parameter, without blanks or repeats. */
|
|
4
|
+
export declare function idsOf(raw: unknown): string[];
|
|
5
|
+
/** A return address inside the admin: `raw` when it is a plain `/admin` path, otherwise `/admin`. */
|
|
6
|
+
export declare function backOf(raw: unknown): string;
|
|
7
|
+
/**
|
|
8
|
+
* `GET /admin/publish?ids=&action=` shows what publishing (or unpublishing) would change, with one button that
|
|
9
|
+
* confirms exactly that; `POST /admin/publish` does it, refusing if anything changed since the page was shown.
|
|
10
|
+
* `scope` is a signed-in scope from `registerScreens`, which also checks the origin of the POST.
|
|
11
|
+
*/
|
|
12
|
+
export declare function registerPublishRoutes(scope: FastifyInstance, opts: AdminContext): void;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { InlayError } from '../../core/errors.js';
|
|
2
|
+
import { encodePath } from '../../core/site/paths.js';
|
|
3
|
+
import { t } from '../../core/messages/index.js';
|
|
4
|
+
import { html } from '../../jsx/html.js';
|
|
5
|
+
import { adminActor, formOf, HTML } from './context.js';
|
|
6
|
+
import { adminPage, Alert } from './views/layout.js';
|
|
7
|
+
import { badgeClass } from './views/status.js';
|
|
8
|
+
const PUBLISH_PATH = '/admin/publish';
|
|
9
|
+
const COPY = {
|
|
10
|
+
publish: {
|
|
11
|
+
title: 'admin.publish.title',
|
|
12
|
+
intro: 'admin.publish.intro',
|
|
13
|
+
submit: 'admin.publish.submit',
|
|
14
|
+
nothing: 'admin.publish.nothing',
|
|
15
|
+
busy: 'admin.publish.busy',
|
|
16
|
+
doneTitle: 'admin.publish.done.title',
|
|
17
|
+
done: 'admin.publish.done',
|
|
18
|
+
},
|
|
19
|
+
unpublish: {
|
|
20
|
+
title: 'admin.unpublish.title',
|
|
21
|
+
intro: 'admin.unpublish.intro',
|
|
22
|
+
submit: 'admin.unpublish.submit',
|
|
23
|
+
nothing: 'admin.unpublish.nothing',
|
|
24
|
+
busy: 'admin.unpublish.busy',
|
|
25
|
+
doneTitle: 'admin.unpublish.done.title',
|
|
26
|
+
done: 'admin.unpublish.done',
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
const actionOf = (raw) => (raw === 'unpublish' ? 'unpublish' : 'publish');
|
|
30
|
+
/** The ids of a comma-separated list or of a repeated parameter, without blanks or repeats. */
|
|
31
|
+
export function idsOf(raw) {
|
|
32
|
+
const values = Array.isArray(raw) ? raw : [raw];
|
|
33
|
+
const ids = values.flatMap((value) => (typeof value === 'string' ? value.split(',') : []));
|
|
34
|
+
return [...new Set(ids.map((id) => id.trim()).filter((id) => id !== ''))];
|
|
35
|
+
}
|
|
36
|
+
/** A return address inside the admin: `raw` when it is a plain `/admin` path, otherwise `/admin`. */
|
|
37
|
+
export function backOf(raw) {
|
|
38
|
+
if (typeof raw !== 'string' || !raw.startsWith('/admin'))
|
|
39
|
+
return '/admin';
|
|
40
|
+
// eslint-disable-next-line no-control-regex
|
|
41
|
+
if (raw.includes('//') || raw.includes('\\') || /[\u0000-\u001f\u007f]/.test(raw))
|
|
42
|
+
return '/admin';
|
|
43
|
+
return raw;
|
|
44
|
+
}
|
|
45
|
+
const backLabel = (back) => t(back === undefined ? 'admin.publish.back' : back.startsWith('/admin/documents/') ? 'admin.publish.backToEditor' : 'admin.publish.backToList');
|
|
46
|
+
/** A link to `back`, or to the start page without one. In the publish dialog it closes the dialog instead. */
|
|
47
|
+
const backLink = (back) => html `<p><a href="${back ?? '/admin'}" class="btn btn-sm" data-dialog-cancel data-dialog-label="${t('admin.publish.close')}">${backLabel(back)}</a></p>`;
|
|
48
|
+
/** `{ id: headRev }` from the form, or `null` unless it covers every id with a revision number. */
|
|
49
|
+
function expectOf(raw, ids) {
|
|
50
|
+
if (typeof raw !== 'string')
|
|
51
|
+
return null;
|
|
52
|
+
let parsed;
|
|
53
|
+
try {
|
|
54
|
+
parsed = JSON.parse(raw);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed))
|
|
60
|
+
return null;
|
|
61
|
+
const expect = {};
|
|
62
|
+
for (const id of ids) {
|
|
63
|
+
const rev = parsed[id];
|
|
64
|
+
if (typeof rev !== 'number' || !Number.isInteger(rev))
|
|
65
|
+
return null;
|
|
66
|
+
expect[id] = rev;
|
|
67
|
+
}
|
|
68
|
+
return expect;
|
|
69
|
+
}
|
|
70
|
+
const pageLink = (action, ids, back) => `${PUBLISH_PATH}?${new URLSearchParams({ ids: ids.join(','), action, ...(back !== undefined && { back }) }).toString()}`;
|
|
71
|
+
const itemTitle = (doc) => t('admin.publish.item', { title: doc.title, locale: doc.locale });
|
|
72
|
+
/** Each document, marked when new, with its change lines (none for unpublishing). */
|
|
73
|
+
function changeList(action, changes) {
|
|
74
|
+
return html `<ul class="list-disc space-y-2 pl-6">${changes.map(({ document, isNew, lines }) => html `<li><strong>${itemTitle(document)}</strong>${isNew && action === 'publish' ? html ` <span class="${badgeClass('info')}">${t('admin.publish.new')}</span>` : ''}${action === 'publish' && lines.length > 0 ? html `<ul class="list-disc pl-6">${lines.map((line) => html `<li>${line}</li>`)}</ul>` : ''}</li>`)}</ul>`;
|
|
75
|
+
}
|
|
76
|
+
/** The problems of a failed (dry) run: one list per document, each problem named by its field labels. */
|
|
77
|
+
function problemList(e) {
|
|
78
|
+
const line = (issue) => html `<li>${issue.labels.length > 0 ? `${issue.labels.join(' → ')}: ` : ''}${issue.message}</li>`;
|
|
79
|
+
const documents = e.details.documents ?? [];
|
|
80
|
+
return html `${Alert({ kind: 'error', children: e.message })}${documents.map((doc) => html `<h2 class="mt-4 text-xl font-semibold">${doc.title}</h2><ul class="list-disc pl-6">${doc.issues.map(line)}</ul>`)}${(e.details.issues ?? []).length > 0 ? html `<ul class="list-disc pl-6">${(e.details.issues ?? []).map(line)}</ul>` : ''}`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* An admin page in the dashboard's section. Its content is `#publish-panel`, which `client/admin/publish.ts` loads
|
|
84
|
+
* into the publish dialog, taking the dialog's heading from `data-title`.
|
|
85
|
+
*/
|
|
86
|
+
const sectionPage = (ctx, actor, title, state, body) => adminPage(ctx, { title, actor, section: 'dashboard' }, html `<div id="publish-panel" data-state="${state}" data-title="${title}" class="flex flex-col gap-4">${body}</div>`);
|
|
87
|
+
function confirmForm(action, ids, expect, back) {
|
|
88
|
+
return html `<form method="post" action="${PUBLISH_PATH}" data-busy-label="${t(COPY[action].busy)}" class="flex flex-wrap items-center gap-2 [&>button]:btn [&>button]:btn-primary"><input type="hidden" name="ids" value="${ids.join(',')}"><input type="hidden" name="action" value="${action}"><input type="hidden" name="expect" value="${JSON.stringify(expect)}">${back === undefined ? '' : html `<input type="hidden" name="back" value="${back}">`}<button type="submit">${t(COPY[action].submit)}</button><a href="${back ?? '/admin'}" class="btn btn-ghost" data-dialog-cancel>${t('admin.publish.cancel')}</a></form>`;
|
|
89
|
+
}
|
|
90
|
+
/** Nothing happened because the content moved on since the page was shown; `reload` shows the latest. */
|
|
91
|
+
function changedPage(ctx, actor, reload) {
|
|
92
|
+
return sectionPage(ctx, actor, t('admin.publish.changed.title'), 'changed', html `<p>${t('admin.publish.changed')}</p><p><a href="${reload}" class="btn btn-sm btn-primary">${t('admin.publish.reload')}</a></p>`);
|
|
93
|
+
}
|
|
94
|
+
/** The heading of a failed (dry) run: content that isn't ready to publish, or anything else that stopped it. */
|
|
95
|
+
const failedTitle = (action, e) => t(action === 'publish' && e.code === 'validation' ? 'admin.publish.blocked.title' : 'admin.publish.failed.title');
|
|
96
|
+
const statusOf = (e) => (e.code === 'forbidden' ? 403 : e.code === 'conflict' ? 409 : 400);
|
|
97
|
+
/**
|
|
98
|
+
* `GET /admin/publish?ids=&action=` shows what publishing (or unpublishing) would change, with one button that
|
|
99
|
+
* confirms exactly that; `POST /admin/publish` does it, refusing if anything changed since the page was shown.
|
|
100
|
+
* `scope` is a signed-in scope from `registerScreens`, which also checks the origin of the POST.
|
|
101
|
+
*/
|
|
102
|
+
export function registerPublishRoutes(scope, opts) {
|
|
103
|
+
const { core } = opts;
|
|
104
|
+
const page = (reply, status, body) => reply.code(status).type(HTML).send(body);
|
|
105
|
+
scope.get(PUBLISH_PATH, async (request, reply) => {
|
|
106
|
+
const actor = adminActor(request);
|
|
107
|
+
const ctx = { actor, source: 'admin' };
|
|
108
|
+
const query = request.query;
|
|
109
|
+
const action = actionOf(query.action);
|
|
110
|
+
const reviewPage = (body, state = 'nothing') => sectionPage(opts, actor, t(COPY[action].title), state, body);
|
|
111
|
+
const asked = idsOf(query.ids);
|
|
112
|
+
const back = Object.hasOwn(query, 'back') ? backOf(query.back) : undefined;
|
|
113
|
+
// `ids` with nothing in it is the dashboard's form with every box unticked; without `ids`, publish everything waiting.
|
|
114
|
+
if (Object.hasOwn(query, 'ids') && asked.length === 0) {
|
|
115
|
+
const body = html `<p>${t('admin.publish.noneSelected')}</p>${backLink(back)}`;
|
|
116
|
+
return page(reply, 200, await reviewPage(body));
|
|
117
|
+
}
|
|
118
|
+
const ids = asked.length > 0 ? asked : action === 'publish' ? (await core.listPendingChanges()).map((d) => d.id) : [];
|
|
119
|
+
const nothing = async () => page(reply, 200, await reviewPage(html `<p>${t(COPY[action].nothing)}</p>${backLink(back)}`));
|
|
120
|
+
if (ids.length === 0)
|
|
121
|
+
return nothing();
|
|
122
|
+
let changes;
|
|
123
|
+
try {
|
|
124
|
+
changes = await core.describeChanges(ids);
|
|
125
|
+
const expect = Object.fromEntries(changes.map((c) => [c.document.id, c.document.headRev]));
|
|
126
|
+
const dry = action === 'publish'
|
|
127
|
+
? await core.publish(ids, ctx, { dryRun: true, expect })
|
|
128
|
+
: await core.unpublish(ids, ctx, { dryRun: true });
|
|
129
|
+
const doing = new Set(dry.items.map((item) => item.documentId));
|
|
130
|
+
changes = changes.filter((c) => doing.has(c.document.id));
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
if (!(e instanceof InlayError))
|
|
134
|
+
throw e;
|
|
135
|
+
if (e.code === 'conflict')
|
|
136
|
+
return page(reply, 409, await changedPage(opts, actor, pageLink(action, ids, back)));
|
|
137
|
+
const body = html `<p>${t('admin.publish.problems')}</p>${problemList(e)}`;
|
|
138
|
+
return page(reply, e.code === 'forbidden' ? 403 : 200, await sectionPage(opts, actor, failedTitle(action, e), 'problems', body));
|
|
139
|
+
}
|
|
140
|
+
if (changes.length === 0)
|
|
141
|
+
return nothing();
|
|
142
|
+
const confirming = changes.map((c) => c.document.id);
|
|
143
|
+
const expect = Object.fromEntries(changes.map((c) => [c.document.id, c.document.headRev]));
|
|
144
|
+
const body = html `<p>${t(COPY[action].intro)}</p>${changeList(action, changes)}${confirmForm(action, confirming, expect, back)}`;
|
|
145
|
+
return page(reply, 200, await reviewPage(body, action === 'publish' ? 'review' : 'unpublish'));
|
|
146
|
+
});
|
|
147
|
+
scope.post(PUBLISH_PATH, async (request, reply) => {
|
|
148
|
+
const actor = adminActor(request);
|
|
149
|
+
const ctx = { actor, source: 'admin' };
|
|
150
|
+
const form = formOf(request);
|
|
151
|
+
const action = actionOf(form.action);
|
|
152
|
+
const ids = idsOf(form.ids);
|
|
153
|
+
const back = form.back === undefined ? undefined : backOf(form.back);
|
|
154
|
+
const reload = pageLink(action, ids, back);
|
|
155
|
+
const changed = async () => page(reply, 409, await changedPage(opts, actor, reload));
|
|
156
|
+
if (ids.length === 0)
|
|
157
|
+
return changed();
|
|
158
|
+
const expect = action === 'publish' ? expectOf(form.expect, ids) : {};
|
|
159
|
+
// Without the revisions the user saw, there is no telling what they confirmed.
|
|
160
|
+
if (expect === null)
|
|
161
|
+
return changed();
|
|
162
|
+
// The publish page (no JS, or a page opened before the dialog existed) goes back where it came from; the dialog
|
|
163
|
+
// (`_dialog`) gets the "done" panel, and reloads the page itself when it closes.
|
|
164
|
+
const returning = back !== undefined && form._dialog !== '1';
|
|
165
|
+
let done;
|
|
166
|
+
try {
|
|
167
|
+
const result = action === 'publish' ? await core.publish(ids, ctx, { expect }) : await core.unpublish(ids, ctx);
|
|
168
|
+
done = result.items.map((item) => item.documentId);
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
if (!(e instanceof InlayError))
|
|
172
|
+
throw e;
|
|
173
|
+
if (e.code === 'conflict')
|
|
174
|
+
return changed();
|
|
175
|
+
const body = html `<p>${t('admin.publish.problems')}</p>${problemList(e)}<p><a href="${reload}" class="btn btn-sm">${t('admin.publish.reload')}</a></p>`;
|
|
176
|
+
return page(reply, statusOf(e), await sectionPage(opts, actor, failedTitle(action, e), 'problems', body));
|
|
177
|
+
}
|
|
178
|
+
if (returning) {
|
|
179
|
+
return reply.redirect(`${back}${back.includes('?') ? '&' : '?'}notice=${action === 'publish' ? 'published' : 'unpublished'}`, 303);
|
|
180
|
+
}
|
|
181
|
+
const items = [];
|
|
182
|
+
for (const id of done) {
|
|
183
|
+
const doc = await core.getDocument(id);
|
|
184
|
+
if (!doc)
|
|
185
|
+
continue;
|
|
186
|
+
const live = action === 'publish'
|
|
187
|
+
? (await core.liveDocuments([doc.groupId], doc.locale, { state: 'published' })).get(doc.groupId)
|
|
188
|
+
: undefined;
|
|
189
|
+
const path = live && live.documentId === id ? live.path : null;
|
|
190
|
+
items.push(html `<li>${path === null ? itemTitle(doc) : html `<a href="${encodePath(path)}" class="link">${itemTitle(doc)}</a>`}</li>`);
|
|
191
|
+
}
|
|
192
|
+
const body = html `<p>${t(COPY[action].done)}</p><ul class="list-disc pl-6">${items}</ul>${backLink(back)}`;
|
|
193
|
+
return page(reply, 200, await sectionPage(opts, actor, t(COPY[action].doneTitle), 'done', body));
|
|
194
|
+
});
|
|
195
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import type { Kysely } from 'kysely';
|
|
3
|
+
import type { AuthService } from '../../core/auth/service.js';
|
|
4
|
+
import type { Core } from '../../core/core.js';
|
|
5
|
+
import type { Database } from '../../core/db/schema.js';
|
|
6
|
+
import type { AdminHostHooks } from '../../host/hooks.js';
|
|
7
|
+
import { type AdminAssets } from './assets.js';
|
|
8
|
+
import type { ResolvedTheme } from './theme.js';
|
|
9
|
+
export interface AdminRouteOptions {
|
|
10
|
+
service: AuthService;
|
|
11
|
+
core: Core;
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
secret: string;
|
|
14
|
+
now: () => Date;
|
|
15
|
+
/** The upload limits, for the upload page. */
|
|
16
|
+
uploads: {
|
|
17
|
+
maxBytes: number;
|
|
18
|
+
types: readonly string[];
|
|
19
|
+
};
|
|
20
|
+
/** `logo` is a site URL path, shown as an image in place of the name. */
|
|
21
|
+
branding: {
|
|
22
|
+
name: string;
|
|
23
|
+
logo?: string;
|
|
24
|
+
theme: ResolvedTheme;
|
|
25
|
+
};
|
|
26
|
+
assets: AdminAssets;
|
|
27
|
+
/** What the host can do for the Releases screen (preview, activate); without it the screen can only list and upload. */
|
|
28
|
+
host?: AdminHostHooks;
|
|
29
|
+
/** The release this site runs, when it is a stored release. */
|
|
30
|
+
release?: {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
} | null;
|
|
34
|
+
/** Nothing can be changed (a staged release's preview). */
|
|
35
|
+
readOnly?: boolean;
|
|
36
|
+
/** The site's database and the folder releases are kept in: with both, owners get the Releases screen. */
|
|
37
|
+
db?: Kysely<Database>;
|
|
38
|
+
releasesDir?: string;
|
|
39
|
+
/** The public keys an uploaded release must be signed with, when there is no `host` to check it. */
|
|
40
|
+
trustedKeys?: readonly string[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The pages under `/admin`. They live in their own encapsulated plugin with a local form parser (a `URLSearchParams`),
|
|
44
|
+
* and every response carries the security headers, `no-store` and `noindex`. Setup, login, consent, the upload link
|
|
45
|
+
* and the assets work without a session; everything else is registered through `registerScreens`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function registerAdminRoutes(app: FastifyInstance, opts: AdminRouteOptions): void;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { AUTH_BASE_PATH } from '../../core/auth/auth.js';
|
|
2
|
+
import { InlayError } from '../../core/errors.js';
|
|
3
|
+
import { t } from '../../core/messages/index.js';
|
|
4
|
+
import { LOGIN_PATH, requestActor } from '../auth/request.js';
|
|
5
|
+
import { copyCookies, safeNext, sameOrigin, toWebRequest } from '../web.js';
|
|
6
|
+
import { registerAssetRoutes } from './assets.js';
|
|
7
|
+
import { adminActor, formOf, hostOf, HTML, notFound, registerScreens, SETUP_PATH } from './context.js';
|
|
8
|
+
import { badOriginPage, consentPage, loginPage, messagePage, setupPage } from './pages.js';
|
|
9
|
+
import { registerPublishRoutes } from './publish.js';
|
|
10
|
+
import { registerBackups } from './screens/backups.js';
|
|
11
|
+
import { registerSecurity } from './screens/security.js';
|
|
12
|
+
import { registerDashboard } from './screens/dashboard.js';
|
|
13
|
+
import { registerEditor } from './screens/editor.js';
|
|
14
|
+
import { registerHistory } from './screens/history.js';
|
|
15
|
+
import { registerLists } from './screens/lists.js';
|
|
16
|
+
import { registerMedia } from './screens/media.js';
|
|
17
|
+
import { registerPreviewPane } from './screens/preview-pane.js';
|
|
18
|
+
import { registerPickers } from './screens/pickers.js';
|
|
19
|
+
import { registerRedirects } from './screens/redirects.js';
|
|
20
|
+
import { registerReleases } from './screens/releases.js';
|
|
21
|
+
import { registerSearch } from './screens/search.js';
|
|
22
|
+
import { registerUsers } from './screens/users.js';
|
|
23
|
+
import { registerUploadRoutes } from './upload.js';
|
|
24
|
+
const CONSENT_PATH = '/admin/consent';
|
|
25
|
+
const SETUP_FIELDS = ['name', 'email', 'password', 'confirm'];
|
|
26
|
+
const SECURITY_HEADERS = {
|
|
27
|
+
'content-security-policy': "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'",
|
|
28
|
+
'x-content-type-options': 'nosniff',
|
|
29
|
+
'referrer-policy': 'same-origin',
|
|
30
|
+
'cache-control': 'no-store',
|
|
31
|
+
'x-robots-tag': 'noindex',
|
|
32
|
+
};
|
|
33
|
+
/** The largest urlencoded form the admin accepts; editor forms with rich text can be large. */
|
|
34
|
+
const FORM_LIMIT = 4 * 1024 * 1024;
|
|
35
|
+
/**
|
|
36
|
+
* The status of an error no route handled: 413 for a form or upload over a limit, 400 for any other request
|
|
37
|
+
* Fastify or a plugin refused (`FST_*` with a 4xx status), 500 for anything else.
|
|
38
|
+
*/
|
|
39
|
+
function errorStatus(error) {
|
|
40
|
+
const { code, statusCode } = (typeof error === 'object' && error !== null ? error : {});
|
|
41
|
+
if (typeof code !== 'string' || !code.startsWith('FST_') || typeof statusCode !== 'number')
|
|
42
|
+
return 500;
|
|
43
|
+
if (statusCode === 413)
|
|
44
|
+
return 413;
|
|
45
|
+
return statusCode >= 400 && statusCode < 500 ? 400 : 500;
|
|
46
|
+
}
|
|
47
|
+
const ERROR_PAGES = {
|
|
48
|
+
400: ['admin.error.badRequest.title', 'admin.error.badRequest'],
|
|
49
|
+
413: ['admin.error.tooLarge.title', 'admin.error.tooLarge'],
|
|
50
|
+
500: ['admin.error.title', 'admin.error'],
|
|
51
|
+
};
|
|
52
|
+
/** The raw query string of `url`, without the `?`. */
|
|
53
|
+
function rawQuery(url) {
|
|
54
|
+
const q = url.indexOf('?');
|
|
55
|
+
return q === -1 ? undefined : url.slice(q + 1);
|
|
56
|
+
}
|
|
57
|
+
/** The `url` of better-auth's JSON `{ redirect: true, url }`, which tells the browser where to go next. */
|
|
58
|
+
async function redirectOf(res) {
|
|
59
|
+
const json = res.headers.get('content-type')?.includes('application/json') ? await res.json() : null;
|
|
60
|
+
if (typeof json === 'object' && json !== null && 'redirect' in json && json.redirect === true && 'url' in json) {
|
|
61
|
+
if (typeof json.url === 'string')
|
|
62
|
+
return json.url;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/** The raw query string when it carries a signed OAuth authorize request (`sig=`). */
|
|
67
|
+
function oauthQueryOf(raw) {
|
|
68
|
+
return raw !== undefined && /(?:^|&)sig=/.test(raw) ? raw : undefined;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The pages under `/admin`. They live in their own encapsulated plugin with a local form parser (a `URLSearchParams`),
|
|
72
|
+
* and every response carries the security headers, `no-store` and `noindex`. Setup, login, consent, the upload link
|
|
73
|
+
* and the assets work without a session; everything else is registered through `registerScreens`.
|
|
74
|
+
*/
|
|
75
|
+
export function registerAdminRoutes(app, opts) {
|
|
76
|
+
const { service, baseUrl } = opts;
|
|
77
|
+
/** Signs in through `auth.handler`, so better-auth's rate limits apply. */
|
|
78
|
+
function signIn(request, input) {
|
|
79
|
+
return service.auth.handler(toWebRequest(request, baseUrl, {
|
|
80
|
+
path: `${AUTH_BASE_PATH}/sign-in/email`,
|
|
81
|
+
body: JSON.stringify({ ...input, rememberMe: true }),
|
|
82
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
const page = (reply, status, body) => reply.code(status).type(HTML).send(body);
|
|
86
|
+
const refuseOrigin = async (reply) => page(reply, 403, await badOriginPage(opts));
|
|
87
|
+
const consentFailed = async (reply) => page(reply, 400, await messagePage(opts, 'auth.consent.failed.title', 'auth.consent.failed'));
|
|
88
|
+
const failed = async (reply) => page(reply, 500, await messagePage(opts, 'site.error.title', 'site.error'));
|
|
89
|
+
app.register(async (child) => {
|
|
90
|
+
child.addContentTypeParser('application/x-www-form-urlencoded', { parseAs: 'string', bodyLimit: FORM_LIMIT }, (_request, body, done) => done(null, new URLSearchParams(body)));
|
|
91
|
+
// Set before any handler, so a route's own headers (the logo's stricter CSP, an asset's caching) win.
|
|
92
|
+
child.addHook('onRequest', async (_request, reply) => {
|
|
93
|
+
reply.headers(SECURITY_HEADERS);
|
|
94
|
+
});
|
|
95
|
+
// Anything no route handled, including a form over the size limit: logged, and answered with a plain page.
|
|
96
|
+
child.setErrorHandler(async (error, request, reply) => {
|
|
97
|
+
const status = errorStatus(error);
|
|
98
|
+
if (status === 500)
|
|
99
|
+
request.log.error({ err: error }, 'admin request failed');
|
|
100
|
+
else
|
|
101
|
+
request.log.warn({ err: error }, 'admin request refused');
|
|
102
|
+
const [title, body] = ERROR_PAGES[status];
|
|
103
|
+
return reply.code(status).headers(SECURITY_HEADERS).type(HTML).send(await messagePage(opts, title, body));
|
|
104
|
+
});
|
|
105
|
+
child.get(SETUP_PATH, async (_request, reply) => {
|
|
106
|
+
if (await service.hasOwner())
|
|
107
|
+
return reply.redirect(LOGIN_PATH, 302);
|
|
108
|
+
return page(reply, 200, await setupPage(opts, {}));
|
|
109
|
+
});
|
|
110
|
+
child.post(SETUP_PATH, async (request, reply) => {
|
|
111
|
+
if (!sameOrigin(request, baseUrl))
|
|
112
|
+
return refuseOrigin(reply);
|
|
113
|
+
if (await service.hasOwner())
|
|
114
|
+
return reply.redirect(LOGIN_PATH, 302);
|
|
115
|
+
const form = formOf(request);
|
|
116
|
+
const input = { name: form.name ?? '', email: form.email ?? '', password: form.password ?? '' };
|
|
117
|
+
const values = { name: input.name, email: input.email };
|
|
118
|
+
if (input.password !== (form.confirm ?? '')) {
|
|
119
|
+
return page(reply, 400, await setupPage(opts, { values, errors: { confirm: [t('auth.passwordsDiffer')] } }));
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
await service.createOwner(input);
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
if (!(e instanceof InlayError))
|
|
126
|
+
throw e;
|
|
127
|
+
if (e.messageId === 'error.ownerExists')
|
|
128
|
+
return reply.redirect(LOGIN_PATH, 302);
|
|
129
|
+
if (e.code !== 'validation')
|
|
130
|
+
throw e;
|
|
131
|
+
const errors = {};
|
|
132
|
+
for (const issue of e.details.issues ?? []) {
|
|
133
|
+
const at = issue.path[0];
|
|
134
|
+
const key = (typeof at === 'string' && SETUP_FIELDS.includes(at) ? at : 'email');
|
|
135
|
+
(errors[key] ??= []).push(issue.message);
|
|
136
|
+
}
|
|
137
|
+
return page(reply, 400, await setupPage(opts, { values, errors }));
|
|
138
|
+
}
|
|
139
|
+
const res = await signIn(request, { email: input.email.trim(), password: input.password });
|
|
140
|
+
if (!res.ok)
|
|
141
|
+
return reply.redirect(LOGIN_PATH, 302);
|
|
142
|
+
copyCookies(reply, res);
|
|
143
|
+
return reply.redirect('/admin', 302);
|
|
144
|
+
});
|
|
145
|
+
child.get(LOGIN_PATH, async (request, reply) => {
|
|
146
|
+
const query = request.query;
|
|
147
|
+
const oauthQuery = oauthQueryOf(rawQuery(request.url));
|
|
148
|
+
return page(reply, 200, await loginPage(opts, { next: safeNext(query.next), ...(oauthQuery !== undefined && { oauthQuery }) }));
|
|
149
|
+
});
|
|
150
|
+
child.post(LOGIN_PATH, async (request, reply) => {
|
|
151
|
+
// Otherwise another site could sign a visitor in to an account of its choosing.
|
|
152
|
+
if (!sameOrigin(request, baseUrl))
|
|
153
|
+
return refuseOrigin(reply);
|
|
154
|
+
const form = formOf(request);
|
|
155
|
+
const email = form.email ?? '';
|
|
156
|
+
const next = safeNext(form.next);
|
|
157
|
+
const oauthQuery = oauthQueryOf(form.oauth_query);
|
|
158
|
+
const again = async (status, error) => page(reply, status, await loginPage(opts, { email, next, error, ...(oauthQuery !== undefined && { oauthQuery }) }));
|
|
159
|
+
const res = await signIn(request, {
|
|
160
|
+
email,
|
|
161
|
+
password: form.password ?? '',
|
|
162
|
+
...(oauthQuery !== undefined && { oauth_query: oauthQuery }),
|
|
163
|
+
});
|
|
164
|
+
if (res.status === 429)
|
|
165
|
+
return again(429, t('auth.tooManyAttempts'));
|
|
166
|
+
if (res.status >= 500) {
|
|
167
|
+
request.log.error({ status: res.status }, await res.text());
|
|
168
|
+
return failed(reply);
|
|
169
|
+
}
|
|
170
|
+
// Bad credentials (401), a banned user (403) or a malformed email (400) all read the same.
|
|
171
|
+
if (!res.ok)
|
|
172
|
+
return again(401, t('auth.loginFailed'));
|
|
173
|
+
copyCookies(reply, res);
|
|
174
|
+
// The OAuth redirect better-auth returns itself (the consent page or the client's redirect URI).
|
|
175
|
+
const url = await redirectOf(res);
|
|
176
|
+
return reply.redirect(url ?? next, 302);
|
|
177
|
+
});
|
|
178
|
+
child.get(CONSENT_PATH, async (request, reply) => {
|
|
179
|
+
const query = rawQuery(request.url);
|
|
180
|
+
const actor = await requestActor(request, service);
|
|
181
|
+
if (!actor)
|
|
182
|
+
return reply.redirect(query === undefined ? LOGIN_PATH : `${LOGIN_PATH}?${query}`, 302);
|
|
183
|
+
const params = new URLSearchParams(query ?? '');
|
|
184
|
+
const clientId = params.get('client_id');
|
|
185
|
+
const client = clientId === null ? null : await service.oauthClient(clientId);
|
|
186
|
+
if (!client)
|
|
187
|
+
return page(reply, 400, await messagePage(opts, 'auth.consent.unknownClient.title', 'auth.consent.unknownClient'));
|
|
188
|
+
const oauthQuery = oauthQueryOf(query);
|
|
189
|
+
const host = hostOf(params.get('redirect_uri'));
|
|
190
|
+
if (oauthQuery === undefined || host === undefined)
|
|
191
|
+
return consentFailed(reply);
|
|
192
|
+
const hosts = [...new Set(client.redirectUris.map(hostOf).filter((h) => h !== undefined))];
|
|
193
|
+
const scopes = (params.get('scope') ?? '').split(' ').filter((s) => s !== '');
|
|
194
|
+
return page(reply, 200, await consentPage(opts, { client: client.name, host, hosts, name: actor.name, scopes, oauthQuery }));
|
|
195
|
+
});
|
|
196
|
+
child.post(CONSENT_PATH, async (request, reply) => {
|
|
197
|
+
if (!sameOrigin(request, baseUrl))
|
|
198
|
+
return refuseOrigin(reply);
|
|
199
|
+
const form = formOf(request);
|
|
200
|
+
const oauthQuery = oauthQueryOf(form.oauth_query);
|
|
201
|
+
if (oauthQuery === undefined)
|
|
202
|
+
return consentFailed(reply);
|
|
203
|
+
// better-auth checks the session and the query's signature, and records the consent.
|
|
204
|
+
const res = await service.auth.handler(toWebRequest(request, baseUrl, {
|
|
205
|
+
path: `${AUTH_BASE_PATH}/oauth2/consent`,
|
|
206
|
+
body: JSON.stringify({ accept: form.accept === 'true', oauth_query: oauthQuery }),
|
|
207
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
208
|
+
}));
|
|
209
|
+
if (res.status === 401)
|
|
210
|
+
return reply.redirect(`${LOGIN_PATH}?${oauthQuery}`, 302);
|
|
211
|
+
if (res.status >= 500) {
|
|
212
|
+
request.log.error({ status: res.status }, await res.text());
|
|
213
|
+
return failed(reply);
|
|
214
|
+
}
|
|
215
|
+
if (!res.ok)
|
|
216
|
+
return consentFailed(reply);
|
|
217
|
+
copyCookies(reply, res);
|
|
218
|
+
// The client's redirect URI, with a code or `error=access_denied`; better-auth built it from the signed query.
|
|
219
|
+
const url = await redirectOf(res);
|
|
220
|
+
return url === undefined ? consentFailed(reply) : reply.redirect(url, 302);
|
|
221
|
+
});
|
|
222
|
+
child.post('/admin/logout', async (request, reply) => {
|
|
223
|
+
if (!sameOrigin(request, baseUrl))
|
|
224
|
+
return refuseOrigin(reply);
|
|
225
|
+
const res = await service.auth.handler(toWebRequest(request, baseUrl, {
|
|
226
|
+
path: `${AUTH_BASE_PATH}/sign-out`,
|
|
227
|
+
body: '{}',
|
|
228
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
229
|
+
}));
|
|
230
|
+
copyCookies(reply, res);
|
|
231
|
+
return reply.redirect(LOGIN_PATH, 302);
|
|
232
|
+
});
|
|
233
|
+
registerAssetRoutes(child, opts.assets);
|
|
234
|
+
registerUploadRoutes(child, opts);
|
|
235
|
+
registerScreens(child, opts, (authed) => {
|
|
236
|
+
registerDashboard(authed, opts);
|
|
237
|
+
registerLists(authed, opts);
|
|
238
|
+
registerEditor(authed, opts);
|
|
239
|
+
registerPreviewPane(authed, opts);
|
|
240
|
+
registerHistory(authed, opts);
|
|
241
|
+
registerMedia(authed, opts);
|
|
242
|
+
registerPickers(authed, opts);
|
|
243
|
+
registerRedirects(authed, opts);
|
|
244
|
+
registerUsers(authed, opts);
|
|
245
|
+
registerBackups(authed, opts);
|
|
246
|
+
registerReleases(authed, opts);
|
|
247
|
+
registerSecurity(authed, opts);
|
|
248
|
+
registerPublishRoutes(authed, opts);
|
|
249
|
+
registerSearch(authed, opts);
|
|
250
|
+
// Any other address under /admin: the admin's own 404 page, after the session (and, for a POST, origin) check.
|
|
251
|
+
authed.get('/admin/*', async (request, reply) => notFound(reply, opts, adminActor(request)));
|
|
252
|
+
authed.post('/admin/*', async (request, reply) => notFound(reply, opts, adminActor(request)));
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import { type AdminContext } from '../context.js';
|
|
3
|
+
/** The largest export the upload form takes. */
|
|
4
|
+
export declare const IMPORT_MAX_BYTES: number;
|
|
5
|
+
/**
|
|
6
|
+
* This process's folder for uploaded exports: made once with a random name and mode 0700, so no other user can
|
|
7
|
+
* read, plant or swap files in it. Made again if it is gone or is no longer a private directory.
|
|
8
|
+
*/
|
|
9
|
+
export declare function stagingDir(): Promise<string>;
|
|
10
|
+
/** The owner's Backups screen: the list, a "Back up now" button and the downloads. */
|
|
11
|
+
export declare function registerBackups(scope: FastifyInstance, ctx: AdminContext): void;
|