# ADR-0002: `/api/0pt/*` authenticates via fleet Zitadel Bearer tokens, not NextAuth/Auth.js - **Status:** Accepted and implemented - **Date decided:** ~2026-07 ("Wave 2" — commit `1d76d50`, "feat(auth,foundation): Wave 2 flag-gated Zitadel JWT verify + validation + eslint + tests"), fail-closed default landed commit `1c1e2cb` ("fix(auth): make the AUTH_ENFORCE default fail-closed on `/api/0pt/*`") - **Date of this record:** 2026-08-11 (BACKLOG P1-2 — retroactive) ## Context 0pt's capability surface (`/api/0pt/*`) needs to authenticate requests against a real identity, and needs that identity to carry a SIAX tenant so `x-0pt-verified-tenant` can be trusted downstream. SIAX Cloud already runs a fleet-wide Zitadel IdP (`id-customers.siax.io`) for exactly this purpose — building a second, repo-local identity provider would duplicate it and create a second source of truth for "who is this". ## Decision `middleware.ts` verifies inbound `Authorization: Bearer` tokens against the fleet Zitadel IdP (`src/auth/zitadel.ts`): JWKS discovered from `/.well-known/openid-configuration` (falling back to `/oauth/v2/keys`), tenant derived from a documented claim-key precedence (`urn:zitadel:iam:org:id` → `urn:zitadel:iam:user:resourceowner:id` → `tenant_id` → `tenant`). No NextAuth/Auth.js dependency was added. Enforcement is fail-closed by default: `AUTH_ENFORCE` unset (or any value except the literal string `"false"`) requires a valid, verified token — 401 on missing/invalid, 403 when a client-supplied `X-Tenant-Id` disagrees with the token's own tenant claim. `AUTH_ENFORCE=false` is a local-dev-only escape hatch, and is refused at boot when `NODE_ENV=production` (`assertAuthConfigSafe()` throws rather than serving anonymously). A separate, older HMAC session cookie (`lib/auth.ts`, with a `DEV_LOGIN_KEY` dev-login shortcut) exists in parallel and is explicitly **not** the auth system this ADR is about — it predates the Zitadel work, has no MFA/CSRF/ revocation, and is tracked as its own gap (BACKLOG P0-14, gated on the account owner's decision about what replaces it). ## Consequences - 0pt does not maintain its own user/credential store for the capability layer — identity is entirely delegated to the fleet Zitadel instance. - A live-verified gap was found and is tracked separately: a 2026-08-10 probe found `GET 0pt.app/api/0pt` returning 200 anonymously despite the code being fail-closed (BACKLOG P0-4/P0-5, root cause unresolved as of this record — do not assume the deployed instance enforces this until an anonymous request gets a real 401 there). - `middleware.ts` sits outside `tsconfig.check.json`'s blocking type-check scope (BACKLOG P0-5) — a real gap, not addressed by this ADR. ## See also - `docs/architecture/ARCHITECTURE.md` §8 "Säkerhet" - `tests/auth/{context,zitadel}.test.ts` - `docs/compliance/BACKLOG.md` P0-4, P0-5, P0-14