DC-046 DC-047 pluggable auth providers + email magic link
Pluggable AuthProvider framework for any future auth method (OIDC, SAML,
passkeys) to plug in without touching the auth path again. Two
implementations ship:
* TOTP — refactored from routes/auth/totp.js into src/auth/providers/totp.js
as one AuthProvider impl. Legacy /api/v1/totp/* routes stay mounted for
back-compat; new /api/v1/auth/login/totp/* routes use the new shape.
* EmailMagicLink — src/auth/providers/email.js. Initiate issues a 32-byte
base64url token, stores its SHA-256 hash in data/email-tokens.json
(atomic lockfile-based mutation, automatic TTL cleanup), and delivers via
nodemailer if providers.email.{host,port,username,password} is set OR
falls back to log.info('auth', 'email magic link issued', ...) for dev.
Verify accepts the token, marks it used, creates the same DashCaddy
session cookie that TOTP uses (single global cookie model).
createAuthProviderRegistry() composes both implementations and exposes
them via /api/v1/auth/login/{methods, :provider/initiate, :provider/verify,
recovery-info} and /api/v1/auth/disable/:provider. PUBLIC_ROUTES + CSRF
exemptions updated to use :provider placeholder (parameterized for future
providers).
Test fix: src/utilities/middleware.js PUBLIC_ROUTES and csrf-protection.js
both switched to the :provider form because the prior literal 'totp'
wouldn't match the parameterized mount path Express 4.22 produces.
Test fix: __tests__/public-routes-drift.test.js extractMountPath() was
broken for Express 4.22's new ^/path/?(?=/|$) source format (no \?\?
terminator in the literal-mount case). Rewrote the parser to normalize
escaped slashes + trailing lookaheads instead of relying on regex
matching against the raw source.
New: __tests__/auth-provider-registry.test.js — 9 tests covering registry
composition, getProvider round-trip, listEnabled no-secrets-leak guarantee,
enabled-flag respect, listAll vs listEnabled distinction, email provider
dev-console fallback (token written to JSON store + log.info with
deliveredVia: 'dev-console' + response masked), verify rejects unknown
tokens via AuthenticationError.
Tests: 1241/1241 passing across 46 suites (was 1232; +9 new).
DNS2 deploy: this commit + bump VERSION to 1.16.0 + publish tarball to
get.dashcaddy.net + docker build + bash start.sh. The image-layer migration
from DC-050 also runs on first container recreate post-merge.
This commit is contained in:
@@ -187,8 +187,8 @@ function walkRouter(router, basePrefix, mounted) {
|
||||
mounted.add(path);
|
||||
}
|
||||
} else if (layer.name === 'router' && layer.handle.stack) {
|
||||
// Sub-router mounted via router.use(subRouter)
|
||||
// Express strips the mount path from layer.regex; reconstruct it from layer.regex
|
||||
// Sub-router mounted via router.use(subRouter) — may or may not
|
||||
// include a path prefix.
|
||||
const mountPath = extractMountPath(layer);
|
||||
walkRouter(layer.handle, basePrefix + mountPath, mounted);
|
||||
} else if (layer.regex && layer.handle !== undefined) {
|
||||
@@ -199,6 +199,12 @@ function walkRouter(router, basePrefix, mounted) {
|
||||
const mountPath = extractMountPath(layer);
|
||||
walkRouter(layer.handle, basePrefix + mountPath, mounted);
|
||||
}
|
||||
} else if (layer.regexp && layer.handle && layer.handle.stack) {
|
||||
// Newer Express versions (5.x) store the mount regex in `regexp`
|
||||
// rather than `regex` — handle the prefixed router.use('/auth', sub)
|
||||
// case here. Falls back to bare mount if no prefix detected.
|
||||
const mountPath = extractMountPath({ regex: layer.regexp });
|
||||
walkRouter(layer.handle, basePrefix + mountPath, mounted);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,17 +217,46 @@ function walkRouter(router, basePrefix, mounted) {
|
||||
// reconstruct from the FastWildcard options.
|
||||
// Since Express internals here are brittle, fall back to a regex source match.
|
||||
function extractMountPath(layer) {
|
||||
if (layer.regex && layer.regex.fast_slash) return '';
|
||||
if (!layer.regex || !layer.regex.source) return '';
|
||||
// The source is something like '^\\/foo\\/?(?=\\/|$)' for mount path '/foo'.
|
||||
// Match the first path segment after the optional leading slash.
|
||||
const m = layer.regex.source.match(/^\\\/\(([^)]+)\)/);
|
||||
if (m) {
|
||||
// Convert path-to-regexp syntax like ':foo' or '*' back to a placeholder.
|
||||
// For simple mounts (no params) this gives us the literal segment.
|
||||
return '/' + m[1];
|
||||
// Newer Express stores compiled regex on `regexp`, older on `regex`.
|
||||
// Accept both so we work across Express 4 and 5.
|
||||
const regex = layer.regexp || layer.regex;
|
||||
if (regex && regex.fast_slash) return '';
|
||||
if (!regex || !regex.source) return '';
|
||||
// The regex source from Node's path-to-regexp serialized form has:
|
||||
// - escaped slashes (a literal `\` followed by `/`)
|
||||
// - a leading anchor `^`
|
||||
// - optional end-of-string terminators like `\\??(?=\\/|$)` or
|
||||
// trailing `\\/?(?=\\/|$)` lookaheads
|
||||
// Strip all of those to recover the original mount path string.
|
||||
let src = regex.source.replace(/\\\//g, '/'); // unescape slashes
|
||||
src = src.replace(/^\^/, ''); // drop leading ^
|
||||
src = src.replace(/\(\?=[^)]*\)\??$/, ''); // drop trailing lookahead
|
||||
src = src.replace(/\\\?$/, ''); // drop trailing `\\?`
|
||||
src = src.replace(/[\\/?]+$/, ''); // drop trailing /, /?, /
|
||||
|
||||
// Use layer.keys when available — they're the parsed parameter names
|
||||
// from path-to-regexp and always match the original mount path
|
||||
// segments in order. A mount like `/auth/:id` produces keys = [{name:'id'}].
|
||||
if (Array.isArray(layer.keys) && layer.keys.length) {
|
||||
const segments = src.split('/').filter(Boolean);
|
||||
let keyIdx = 0;
|
||||
return '/' + segments.map(seg => {
|
||||
if (seg.startsWith(':') || seg === '*') {
|
||||
const k = layer.keys[keyIdx++];
|
||||
return seg === '*'
|
||||
? '*'
|
||||
: ':' + (k ? k.name : seg.slice(1));
|
||||
}
|
||||
return seg;
|
||||
}).join('/');
|
||||
}
|
||||
return '';
|
||||
|
||||
// Simple case (no path-to-regexp params): return whatever remains.
|
||||
// Sources we see in practice:
|
||||
// /auth (router.use('/auth', sub))
|
||||
// /auth (router.use('/auth/?', sub))
|
||||
// /auth/totp (with literal nested segment)
|
||||
return src || '';
|
||||
}
|
||||
|
||||
// Check if path is a prefix in PUBLIC_ROUTES (e.g., '/api/v1/auth/gate/' grants all under it)
|
||||
|
||||
Reference in New Issue
Block a user