Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

11.1 KB
Raw Blame
import { Router } from 'express';
import { z } from 'zod';
import {
  getPasswordRiskStatus,
  buildClearCookie,
  buildSessionCookie,
  clearLoginFailures,
  clearPassword,
  createSession,
  extractTokens,
  getAuthStatus,
  isAuthEnabled,
  isLoginRateLimited,
  listSessions,
  parseSessionCookies,
  recordLoginFailure,
  revokeSession,
  revokeSessionById,
  revokeSessionsByLabel,
  sessionCookieNameFor,
  setPassword,
  verifyPassword,
  verifyRequestSession,
  verifySession,
} from '../services/auth.js';
import { getAgentKeyStatus, rotateAgentKey, setAgentKeyEnabled } from '../services/agentKey.js';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
import { logSecurityEvent } from '../lib/securityAuditLog.js';
import { isRemoteRequest } from '../lib/requestOrigin.js';

const router = Router();

const loginSchema = z.object({ password: z.string().min(1).max(256) }).strict();
const setPasswordSchema = z.object({
  newPassword: z.string().min(8).max(256),
  currentPassword: z.string().max(256).optional(),
}).strict();
const clearPasswordSchema = z.object({ currentPassword: z.string().min(1).max(256) }).strict();
// Batch revoke is label-scoped so it can only ever drop non-interactive
// credentials (never the caller's browser session, which carries no label).
const revokeSessionsQuerySchema = z.object({ label: z.literal('agent') }).strict();
const sessionIdParamSchema = z.object({ id: z.string().min(1).max(64).regex(/^[a-f0-9]+$/) }).strict();

// Whether the request reached us over HTTPS (so the cookie should carry the
// Secure flag). `req.secure` reflects the actual socket; we don't trust
// X-Forwarded-Proto since PortOS isn't behind a reverse proxy in its normal
// deployment topology.
const isSecure = (req) => !!req.secure;

// Source address for the audit trail. `req.ip` is the socket peer (no reverse
// proxy in front of PortOS), so a forged header cannot name another machine.
// A Tailcat ingress request reports the loopback hop as its address; keep its
// throttle bucket and audit lines apart from genuine local logins.
const sourceOf = (req) => (isRemoteRequest(req) ? 'tailcat-ingress' : (req.ip || req.socket?.remoteAddress || 'unknown'));

// The session cookie name is port-scoped from the browser-facing `Host` (see
// sessionCookieNameFor in lib/portosAuthCore.js) — e.g. `portos_auth_15555`
// behind a `tailcat forward 15555:5555` — so this install never shares (or is
// blocked by a `Secure`) cookie slot with another PortOS on the same host.
const sessionCookieFor = (req, token) =>
  buildSessionCookie(token, { secure: isSecure(req), name: sessionCookieNameFor(req.headers?.host) });

// Names of this install's OTHER session cookies on the request (e.g. a legacy
// bare `portos_auth` minted before the port-scoped name) — only those whose
// token is live HERE, so we never clear a sibling install's cookie that
// happens to share the host.
const ownedStaleCookieNames = async (req) => {
  const own = sessionCookieNameFor(req.headers?.host);
  const names = [];
  for (const { name, value } of parseSessionCookies(req.headers?.cookie)) {
    if (name === own || names.includes(name)) continue;
    if (await verifySession(value)) names.push(name);
  }
  return names;
};

const clearCookiesFor = (req, extraNames = []) => {
  const secure = isSecure(req);
  const names = [sessionCookieNameFor(req.headers?.host), ...extraNames];
  return [...new Set(names)].map((name) => buildClearCookie({ secure, name }));
};

// GET /api/auth/status — always reachable. The UI uses this to know whether
// to render the login gate at all.
router.get('/status', asyncHandler(async (_req, res) => {
  res.json(await getAuthStatus());
}));

router.get('/password-risk', asyncHandler(async (_req, res) => {
  res.json(await getPasswordRiskStatus());
}));

// GET /api/auth/whoami — confirm the current cookie/header is still valid.
// Returns { authenticated: true|false, required: true|false } so the client
// can disambiguate "auth off" from "auth on, you're signed in" from "auth on,
// you're not signed in".
router.get('/whoami', asyncHandler(async (req, res) => {
  const required = await isAuthEnabled();
  if (!required) {
    res.json({ authenticated: true, required: false });
    return;
  }
  const authenticated = !!(await verifyRequestSession(req));
  res.json({ authenticated, required: true });
}));

// POST /api/auth/login
router.post('/login', asyncHandler(async (req, res) => {
  const { password } = validateRequest(loginSchema, req.body || {});
  if (!(await isAuthEnabled())) {
    throw new ServerError('Authentication is not enabled', { status: 400, code: 'AUTH_NOT_ENABLED' });
  }
  // Throttle check runs BEFORE scrypt so a sidecar can't pin the CPU by
  // looping bad guesses. The IP comes from Express's `req.ip` (we don't
  // sit behind a reverse proxy in normal PortOS deployments).
  const clientIp = sourceOf(req);
  if (isLoginRateLimited(clientIp)) {
    throw new ServerError('Too many login attempts — try again in a minute', {
      status: 429,
      code: 'AUTH_RATE_LIMITED',
    });
  }
  if (!(await verifyPassword(password))) {
    recordLoginFailure(clientIp);
    logSecurityEvent('login.failed', { refused: true, ip: clientIp });
    // One line when the window fills, not one per throttled request — the 429s
    // that follow carry no new information and would flood the log under a guess loop.
    if (isLoginRateLimited(clientIp)) logSecurityEvent('login.throttled', { refused: true, ip: clientIp });
    throw new ServerError('Invalid password', { status: 401, code: 'AUTH_BAD_PASSWORD' });
  }
  // Success clears the throttle so a user who mistyped a few times then got
  // it right isn't kept locked out.
  clearLoginFailures(clientIp);
  const { token, id } = await createSession();
  logSecurityEvent('login.ok', { ip: clientIp, session: id });
  res.setHeader('Set-Cookie', sessionCookieFor(req, token));
  res.json({ authenticated: true });
}));

// POST /api/auth/logout — best-effort revoke + clear cookie. Idempotent so a
// double-click on Sign Out doesn't 401.
router.post('/logout', asyncHandler(async (req, res) => {
  // Revoke every live token the request carries (port-scoped cookie, a
  // pre-upgrade legacy cookie, Bearer) so signing out can't leave a second
  // valid cookie behind. revokeSession is a no-op for foreign tokens.
  const stale = await ownedStaleCookieNames(req);
  const tokens = extractTokens(req);
  for (const token of tokens) await revokeSession(token);
  if (tokens.length) logSecurityEvent('logout', { ip: sourceOf(req) });
  res.setHeader('Set-Cookie', clearCookiesFor(req, stale));
  res.json({ ok: true });
}));

// POST /api/auth/password — set or rotate the password. When auth is already
// on, the caller must include their current password. When it's off, this is
// the first-time-set path and `currentPassword` is ignored. Returns a fresh
// session cookie so the user stays signed in.
router.post('/password', asyncHandler(async (req, res) => {
  const body = validateRequest(setPasswordSchema, req.body || {});
  const alreadyEnabled = await isAuthEnabled();
  // The centralized host-control gate requires a local connection for first
  // setup, or an operator session for rotation. setPassword additionally
  // verifies the current password before changing an enabled configuration.
  const stale = await ownedStaleCookieNames(req);
  const { token } = await setPassword({
    newPassword: body.newPassword,
    currentPassword: alreadyEnabled ? body.currentPassword : null,
  });
  logSecurityEvent(alreadyEnabled ? 'password.rotated' : 'password.set', {
    ip: sourceOf(req),
    note: 'all other sessions revoked',
  });
  const secure = isSecure(req);
  res.setHeader('Set-Cookie', [
    sessionCookieFor(req, token),
    ...stale.map((name) => buildClearCookie({ secure, name })),
  ]);
  res.json({ enabled: true });
}));

// DELETE /api/auth/password — turn auth off. Requires the current password
// so an attacker holding only a session token (without the password) can't
// silently disable the gate.
router.delete('/password', asyncHandler(async (req, res) => {
  const { currentPassword } = validateRequest(clearPasswordSchema, req.body || {});
  const stale = await ownedStaleCookieNames(req);
  await clearPassword({ currentPassword });
  logSecurityEvent('password.cleared', { ip: sourceOf(req), note: 'auth disabled, all sessions revoked' });
  res.setHeader('Set-Cookie', clearCookiesFor(req, stale));
  res.json({ enabled: false });
}));

// GET /api/auth/sessions — list live sessions for Settings → Security. Gated
// exactly like the rest of /api/auth/* (not in the always-public set), so
// this only reveals anything to a caller who is already authenticated.
// Never returns the token or its hash — just what identifies a session on
// screen (label, expiry) and what addresses it for a scoped revoke (id).
router.get('/sessions', asyncHandler(async (_req, res) => {
  const sessions = await listSessions();
  res.json({ sessions, count: sessions.length });
}));

// DELETE /api/auth/sessions?label=agent — revoke every agent-labelled session
// in one call (Settings → Security "Revoke all"). Leaves unlabelled browser
// sessions, including the caller's, untouched.
router.delete('/sessions', asyncHandler(async (req, res) => {
  const { label } = validateRequest(revokeSessionsQuerySchema, req.query);
  const revoked = await revokeSessionsByLabel(label);
  logSecurityEvent('sessions.revoked', { ip: sourceOf(req), note: `${revoked} ${label} session(s)` });
  res.json({ ok: true, revoked });
}));

// DELETE /api/auth/sessions/:id — revoke exactly one session (e.g. the
// agent's loopback credential) without touching the caller's own browser
// session, unlike DELETE /api/auth/password (which revokes every session).
router.delete('/sessions/:id', asyncHandler(async (req, res) => {
  const { id } = validateRequest(sessionIdParamSchema, req.params);
  const revoked = await revokeSessionById(id);
  if (!revoked) {
    throw new ServerError('Session not found', { status: 404, code: 'AUTH_SESSION_NOT_FOUND' });
  }
  logSecurityEvent('session.revoked', { ip: sourceOf(req), session: id });
  res.json({ ok: true });
}));

// Local agent API key (services/agentKey.js): a revocable session token kept in
// a mode-0600 file so an agent PortOS did not spawn can call the API. GET shows
// state only — never the token. Turning it on or rotating it mints an operator
// session, so both sit on the host-control list beside POST /password.
const agentKeyToggleSchema = z.object({ enabled: z.boolean() }).strict();

router.get('/agent-key', asyncHandler(async (_req, res) => {
  res.json(await getAgentKeyStatus());
}));

router.put('/agent-key', asyncHandler(async (req, res) => {
  const { enabled } = validateRequest(agentKeyToggleSchema, req.body || {});
  const status = await setAgentKeyEnabled(enabled);
  logSecurityEvent(enabled ? 'agent-key.enabled' : 'agent-key.disabled', { ip: sourceOf(req) });
  res.json(status);
}));

router.post('/agent-key/rotate', asyncHandler(async (req, res) => {
  const status = await rotateAgentKey();
  logSecurityEvent('agent-key.rotated', { ip: sourceOf(req) });
  res.json(status);
}));

export default router;