feat(mam-api): mount requireAuth gate at /api/v1 with auth + cluster carve-outs

This commit is contained in:
Zac Gaetano 2026-05-27 14:13:21 -04:00
parent 88c3aa5149
commit 9de4fe9ab9

View file

@ -8,6 +8,7 @@ import os from 'node:os';
import { exec } from 'node:child_process'; import { exec } from 'node:child_process';
import pool from './db/pool.js'; import pool from './db/pool.js';
import { errorHandler } from './middleware/errors.js'; import { errorHandler } from './middleware/errors.js';
import { requireAuth } from './middleware/auth.js';
import { loadS3ConfigFromDb } from './s3/client.js'; import { loadS3ConfigFromDb } from './s3/client.js';
// Routes // Routes
@ -86,6 +87,17 @@ app.use(session({
// ── Health ──────────────────────────────────────────────────────────────────── // ── Health ────────────────────────────────────────────────────────────────────
app.get('/health', (_req, res) => res.json({ status: 'ok' })); app.get('/health', (_req, res) => res.json({ status: 'ok' }));
// ── Auth gate ─────────────────────────────────────────────────────────────────
// Mount once for everything under /api/v1, with an explicit allowlist for
// the three pre-login auth paths and a carve-out for /cluster/* (node-agent
// uses migration 019's token-binding, not user auth). See spec.
const UNAUTH_PATHS = new Set(['/auth/login', '/auth/setup', '/auth/setup-required']);
app.use('/api/v1', (req, res, next) => {
if (UNAUTH_PATHS.has(req.path)) return next();
if (req.path.startsWith('/cluster')) return next(); // node-agent service auth, not user auth
return requireAuth(req, res, next);
});
// ── API Routes ──────────────────────────────────────────────────────────────── // ── API Routes ────────────────────────────────────────────────────────────────
app.use('/api/v1/assets', assetsRouter); app.use('/api/v1/assets', assetsRouter);
app.use('/api/v1/projects', projectsRouter); app.use('/api/v1/projects', projectsRouter);