dragonflight/services/mam-api/src/middleware/auth.js

110 lines
3.7 KiB
JavaScript
Raw Normal View History

/**
* Authentication middleware.
*
* When AUTH_ENABLED=true in the environment, every protected route requires
* either:
* - An active session (set by POST /api/v1/auth/login), or
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
* - A valid Bearer token in the Authorization header.
*
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
* When AUTH_ENABLED is unset or any other value, the middleware is a no-op
* (with a synthetic admin user attached) so the stack can run unprotected.
*
* Exposed on `req` after success:
* req.user { id, username, role, is_client }
* req.tokenBoundHostname hostname binding from api_tokens, or null
*/
import crypto from 'crypto';
import pool from '../db/pool.js';
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
const SYNTHETIC_USER = Object.freeze({
id: null,
username: 'operator',
role: 'admin',
is_client: false,
synthetic: true,
});
export const requireAuth = async (req, res, next) => {
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
if (process.env.AUTH_ENABLED !== 'true') {
req.user = SYNTHETIC_USER;
return next();
}
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
// ── Session-based auth ─────────────────────────────────────────
if (req.session?.userId) {
req.user = {
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
id: req.session.userId,
username: req.session.username,
role: req.session.role,
is_client: !!req.session.isClient,
};
return next();
}
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
// ── Bearer token auth ──────────────────────────────────────────
const authHeader = req.headers.authorization;
if (authHeader?.startsWith('Bearer ')) {
const raw = authHeader.slice(7).trim();
const hash = crypto.createHash('sha256').update(raw).digest('hex');
try {
const { rows } = await pool.query(
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
`SELECT t.user_id AS id,
u.username, u.role, u.is_client,
t.bound_hostname
FROM api_tokens t
JOIN users u ON u.id = t.user_id
WHERE t.token_hash = $1
AND (t.expires_at IS NULL OR t.expires_at > NOW())`,
[hash]
);
if (rows.length > 0) {
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
const row = rows[0];
req.user = {
id: row.id,
username: row.username,
role: row.role,
is_client: !!row.is_client,
};
req.tokenBoundHostname = row.bound_hostname || null;
pool.query(
'UPDATE api_tokens SET last_used_at = NOW() WHERE token_hash = $1',
[hash]
).catch(() => {});
return next();
}
} catch (err) {
return next(err);
}
}
return res.status(401).json({ error: 'Unauthorized' });
};
export const requireAdmin = (req, res, next) => {
if (process.env.AUTH_ENABLED !== 'true') return next();
if (req.user?.role === 'admin') return next();
return res.status(403).json({ error: 'Admin access required' });
};
auth: top-to-bottom rework — local accounts, RBAC + client tag, audit log, env-bootstrap Scope (locked in via planning Q&A): - Identity: local accounts only (PG users table) + existing bearer tokens for headless callers. - Transport: httpOnly cookie session for browser, Bearer for API. - RBAC: admin / editor / viewer roles, plus an orthogonal is_client flag for external (agency, talent, customer) accounts. - Bootstrap: ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD env seed the first admin on a clean install. Set ADMIN_BOOTSTRAP_RESET to force-reset the named user (break-glass). - Rate limit: in-memory, 10 fails per 15min per (IP, username). - Password policy: \u22658 chars, mixed case, digit, symbol; small blocklist of common passwords; cannot equal username. - Self-service: change own display name + password. Everything else (role, is_client, other-user mgmt) is admin only. - Audit log: append-only table, indexed by actor + event_type + created_at, populated by every auth/admin event. Files added: - services/mam-api/src/db/migrations/022-auth-rework.sql users.is_client + last_login_at + failed_attempts; audit_log table with FK to users (ON DELETE SET NULL). - services/mam-api/src/middleware/audit.js Fire-and-forget audit() helper. Caller never awaits, failure logs but never throws — auditing cannot break the request that triggered it. - services/mam-api/src/middleware/passwordPolicy.js Shared checkPassword(pw, { username }) used by setup, user create/update, and self-service password change. - services/mam-api/src/tasks/bootstrapAdmin.js Runs after migrations. No-ops unless ADMIN_BOOTSTRAP_USER + ADMIN_BOOTSTRAP_PASSWORD are set AND (users table empty OR ADMIN_BOOTSTRAP_RESET=true). - services/mam-api/src/routes/audit.js Admin-only GET /audit (paginated, filter by event_type / actor / target / date) and GET /audit/event-types. - services/web-ui/public/modal-account-settings.jsx Profile + Password tabs. Triggered by sidebar user button. Files rewritten: - services/mam-api/src/routes/auth.js - POST /login: regenerate(), no manual save(); audit success/ fail/lockout; updates last_login_at + failed_attempts. - POST /logout: destroys session, audits logout. - GET /me: returns is_client + last_login_at. Synthetic admin when AUTH_ENABLED=false. - GET /setup-status: drives login.html UI state. - POST /setup: blocked once any user exists; password policy. - POST /password: self-service. Requires current pw, runs policy, audits, invalidates other sessions implicitly via users.js if changed by admin. - PATCH /me: self-service display_name update. - services/mam-api/src/routes/users.js - is_client field in create/update/list/get. - Guardrails: cannot delete or demote last admin, cannot delete self, admins cannot be flagged is_client. - Password change invalidates all sessions for that user (DELETE FROM sessions WHERE sess->>'userId' = id). - Audit on every mutation. - Password policy enforced. - services/mam-api/src/middleware/auth.js - requireAuth now exposes req.user.is_client. - New requireRole(["admin","editor"], { rejectClients: true }) helper. Applied to cluster, sdk, capture routes (infra). - Synthetic user when AUTH_ENABLED=false has is_client=false. - services/mam-api/src/index.js - Loads bootstrap admin after migrations. - Wires /api/v1/audit. - Cleans up an earlier comment block. - services/web-ui/public/login.html - Password hint added next to setup-mode password field. - services/web-ui/public/shell.jsx - Sidebar user footer is a button that opens AccountSettings. - CLIENT badge next to role when is_client=true. - Nav filters: clients lose ingest tree + jobs + editor; viewers lose ingest + editor; only admins see the Admin section. Power button hidden when synthetic user. - services/web-ui/public/screens-admin.jsx - Users table: new Client column with inline toggle. - InviteUserModal: Client checkbox + password hint, gated off when role=admin. - Last login column replaces Created in primary view. - CSV export includes client + last_login. - services/web-ui/public/data.jsx - ZAMPP_DATA.ME carries is_client + display_name. - services/web-ui/public/index.html - Loads dist/modal-account-settings.js. - services/web-ui/public/styles-rest.css - .user-row grid widened to 6 columns. - docker-compose.yml - Plumbs SESSION_COOKIE_SECURE + ADMIN_BOOTSTRAP_* env vars. Deploy: cd /opt/wild-dragon git pull origin main # In .env: # AUTH_ENABLED=true # SESSION_SECRET=<openssl rand -hex 48> # ADMIN_BOOTSTRAP_USER=admin # ADMIN_BOOTSTRAP_PASSWORD=<strong> docker compose build mam-api web-ui docker compose up -d --force-recreate --no-deps mam-api web-ui
2026-05-26 23:21:07 -04:00
/**
* Role gate: `requireRole(['admin','editor'])` rejects with 403 unless the
* user's role is in the allow list. Always passes when AUTH_ENABLED=false.
* Optionally pass { rejectClients: true } to also block users with
* is_client=true regardless of role used for routes that touch the
* recorder / cluster / infra surface.
*/
export const requireRole = (allowed, opts = {}) => {
const set = new Set(Array.isArray(allowed) ? allowed : [allowed]);
const { rejectClients = false } = opts;
return (req, res, next) => {
if (process.env.AUTH_ENABLED !== 'true') return next();
if (!req.user || !set.has(req.user.role)) {
return res.status(403).json({ error: `Requires role: ${[...set].join(' or ')}` });
}
if (rejectClients && req.user.is_client) {
return res.status(403).json({ error: 'This action is not available to client accounts' });
}
next();
};
};