Skip to content

refactor(auth): split resolveAuth into attachAuth + requireAuth (P2-D)#77

Merged
CryptoJones merged 1 commit into
masterfrom
feat/auth-middleware-split
May 18, 2026
Merged

refactor(auth): split resolveAuth into attachAuth + requireAuth (P2-D)#77
CryptoJones merged 1 commit into
masterfrom
feat/auth-middleware-split

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Part of architect audit issue #73 — iteration P2-D.

Summary

  • Split combined resolveAuth into attachAuth (best-effort) and requireAuth (strict 403).
  • attachAuth mounted globally on /v1 — populates req.authKey, req.isMaster, req.companyId on every request without rejecting.
  • requireAuth exported but NOT globally mounted. Per-controller adoption tracked separately; mounting it globally before controllers drop their inline checks regressed 58 tests, so the rollout is two-phased.
  • /v1/whoami consumes the attached fields directly. Distinguishes "header missing" (403) from "header present but unknown" (200, authenticated:false) — the unified resolveAuth collapsed both into 403, defeating the endpoint's purpose.
  • resolveAuth kept as a thin compat wrapper chaining attachAuth → requireAuth.

Test plan

  • npm test — 291 pass / 4 skip.
  • /v1/whoami returns 200 with authenticated:false for an unknown authKey (regression-tested in whoami.test.js).
  • Existing controllers continue to enforce auth via their inline checks (unchanged behavior).
  • Follow-up PRs migrate controllers off the inline check, then mount requireAuth globally.

This code proudly made in Nebraska. GO BIG RED! 🌽 https://xkcd.com/2347/

Architect audit P2-D. Split the combined resolveAuth middleware into
two single-responsibility middlewares so endpoints can opt out of the
strict 403 enforcement:

- attachAuth: best-effort populates req.authKey, req.isMaster,
  req.companyId. Never rejects on missing/unknown key (returns 500
  only on a DB error during lookup). Mounted globally on /v1.
- requireAuth: 403s on missing key or unknown non-master key. Exported
  but not globally mounted yet — existing controllers still have their
  inline authKey check, so global mount would short-circuit
  per-controller scoping nuances. Per-controller adoption follows in
  P2-D2…N PRs.

/v1/whoami now consumes the attached fields directly and distinguishes
"header missing" (403) from "header present but unknown" (200 with
authenticated:false) — the previous resolveAuth wrapper collapsed
those into a uniform 403, which defeated the endpoint's purpose.

resolveAuth kept as a backwards-compat wrapper that chains
attachAuth → requireAuth, so anywhere it's still mounted directly
keeps working unchanged.

Tests: 291 pass / 4 skip. The earlier 58-test regression came from
mounting requireAuth globally before per-controller migration —
backed off; mount is opt-in until controllers drop their inline
checks (tracked separately).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit a758aca into master May 18, 2026
3 checks passed
@CryptoJones CryptoJones deleted the feat/auth-middleware-split branch May 18, 2026 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant