diff --git a/server/config.mjs b/server/config.mjs index 9cfe937..dc1eec9 100644 --- a/server/config.mjs +++ b/server/config.mjs @@ -27,6 +27,16 @@ export const HOST = process.env.HOST || '0.0.0.0'; /** Shared invite code required to register after the first account exists. */ export const INVITE = setting('INVITE'); +/** + * Single-user desktop mode, set by the Windows launcher (LUMINARY_LOCAL=1). + * The server creates one admin account and treats an unauthenticated request as + * that account, so a personal install never shows a signup or sign-in screen. + * Only safe on loopback, which is where the launcher binds; the server warns if + * it is enabled with a reachable HOST. + */ +export const LOCAL_MODE = setting('LOCAL') === '1'; +export const LOCAL_EMAIL = setting('LOCAL_EMAIL', 'local@luminary'); + /** Base URL of the dashboard this editor is bridged to, e.g. http://hub:3000. */ export const GLENLOCH_URL = (process.env.GLENLOCH_URL || '').replace(/\/+$/, ''); diff --git a/server/index.mjs b/server/index.mjs index a22e0e0..135ba97 100644 --- a/server/index.mjs +++ b/server/index.mjs @@ -8,6 +8,8 @@ import { DIST_DIR, HOST, INVITE, + LOCAL_EMAIL, + LOCAL_MODE, MAX_UPLOAD, PORT, RAW_PREVIEW_EDGE, @@ -337,6 +339,32 @@ function sessionCookie(req, token, maxAge) { return `${COOKIE}=${token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${maxAge}${secure}`; } +/** + * LUMINARY_LOCAL=1 (the Windows desktop bundle): one admin account, no signup, + * no sign-in, and an unauthenticated request is the local owner. The password + * is random and never shown, so the account cannot be signed into over a + * network; the launcher also binds loopback only. + */ +let localOwner = null; + +async function prepareLocalOwner() { + if (!LOCAL_MODE) return; + const existing = users.byEmail(LOCAL_EMAIL); + if (existing) { + localOwner = publicUser(existing); + return; + } + const id = crypto.randomUUID(); + users.create({ + id, + email: LOCAL_EMAIL, + name: 'Local', + passHash: await hashPassword(crypto.randomBytes(32).toString('hex')), + role: 'admin', + }); + localOwner = publicUser(users.byId(id)); +} + function currentUser(req) { const cookieToken = parseCookies(req)[COOKIE]; if (cookieToken) { @@ -346,7 +374,10 @@ function currentUser(req) { if (user && !user.disabled) return publicUser(user); } } - return authenticate(req); + const authenticated = authenticate(req); + if (authenticated) return authenticated; + // A personal, loopback-only install has one owner: no credentials needed. + return LOCAL_MODE ? localOwner : null; } function requireAdmin(req, res) { @@ -599,8 +630,10 @@ route('GET', '/api/health', async ({ req, res }) => { ok: true, app: 'luminary', version: VERSION, - needsSetup: users.count() === 0, - registrationOpen: users.count() === 0 || Boolean(INVITE) || invites.pendingCount() > 0, + needsSetup: !LOCAL_MODE && users.count() === 0, + registrationOpen: + !LOCAL_MODE && (users.count() === 0 || Boolean(INVITE) || invites.pendingCount() > 0), + local: LOCAL_MODE, photos: stats.photos, bytes: stats.bytes, // The absolute data path is only for signed-in team members. @@ -610,6 +643,9 @@ route('GET', '/api/health', async ({ req, res }) => { }); route('POST', '/api/auth/register', async ({ req, res }) => { + if (LOCAL_MODE) { + return error(res, 403, 'This is a single-user desktop install; accounts are not used'); + } const body = await readJson(req, 64 * 1024); const email = normalizeEmail(body.email); const password = typeof body.password === 'string' ? body.password : ''; @@ -665,6 +701,18 @@ route('POST', '/api/auth/register', async ({ req, res }) => { }); route('POST', '/api/auth/login', async ({ req, res }) => { + // Desktop mode has no password; hand out the local session so a stale login + // form or bookmark cannot strand the user on a sign-in screen. + if (LOCAL_MODE && localOwner) { + const token = createSession(localOwner.id); + return json( + res, + 200, + { token, user: localOwner }, + { 'Set-Cookie': sessionCookie(req, token, 30 * 24 * 3600) }, + ); + } + const ipKey = `login:ip:${clientIp(req)}`; if (authFailures(ipKey) >= LOGIN_IP_LIMIT) { return error(res, 429, 'Too many sign-in attempts. Try again later.', { 'Retry-After': '900' }); @@ -800,6 +848,9 @@ route('PATCH', '/api/admin/users/:id', async ({ req, res, params }) => { if (!admin) return; const target = users.byId(params.id); if (!target) return error(res, 404, 'No such account'); + if (LOCAL_MODE && localOwner && target.id === localOwner.id) { + return error(res, 403, 'The local account is managed by the desktop launcher'); + } const body = await readJson(req, 64 * 1024); const patch = {}; @@ -872,6 +923,9 @@ route('DELETE', '/api/admin/users/:id', async ({ req, res, params }) => { if (params.id === admin.id) return error(res, 400, 'You cannot delete your own account'); const target = users.byId(params.id); if (!target) return error(res, 404, 'No such account'); + if (LOCAL_MODE && localOwner && target.id === localOwner.id) { + return error(res, 403, 'The local account is managed by the desktop launcher'); + } if (target.role === 'admin' && users.activeAdmins() <= 1) { return error(res, 400, 'Keep at least one active administrator'); } @@ -2567,6 +2621,13 @@ async function sweepOrphanOriginals() { } } +await prepareLocalOwner(); +if (LOCAL_MODE && !['127.0.0.1', 'localhost', '::1'].includes(HOST)) { + console.warn( + ` local: LUMINARY_LOCAL=1 with HOST=${HOST}; every client is treated as the local admin`, + ); +} + server.listen(PORT, HOST, async () => { startQuotaWorker(); const raw = await rawSupport(); @@ -2575,7 +2636,11 @@ server.listen(PORT, HOST, async () => { console.log( ` raw: libraw ${raw.libraw ? `ready (${raw.workers} parallel worker${raw.workers === 1 ? '' : 's'})` : 'missing'} ยท jpeg ${raw.sharp ? 'sharp' : 'png fallback'}`, ); - console.log(` users: ${users.count()} account(s)${users.count() === 0 ? ' โ€” first signup becomes admin' : ''}`); + if (LOCAL_MODE) { + console.log(` local: ${LOCAL_EMAIL} โ€” sign-in disabled (LUMINARY_LOCAL=1)`); + } else { + console.log(` users: ${users.count()} account(s)${users.count() === 0 ? ' โ€” first signup becomes admin' : ''}`); + } if (!raw.libraw) console.log(' note: install libraw-wasm for full raw demosaic (embedded previews still work)'); resumePreviewRebuild(); });