refactor(auth): split resolveAuth into attachAuth + requireAuth (P2-D)#77
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of architect audit issue #73 — iteration P2-D.
Summary
resolveAuthintoattachAuth(best-effort) andrequireAuth(strict 403).attachAuthmounted globally on/v1— populatesreq.authKey,req.isMaster,req.companyIdon every request without rejecting.requireAuthexported 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/whoamiconsumes the attached fields directly. Distinguishes "header missing" (403) from "header present but unknown" (200,authenticated:false) — the unifiedresolveAuthcollapsed both into 403, defeating the endpoint's purpose.resolveAuthkept as a thin compat wrapper chainingattachAuth → requireAuth.Test plan
npm test— 291 pass / 4 skip./v1/whoamireturns 200 withauthenticated:falsefor an unknown authKey (regression-tested inwhoami.test.js).requireAuthglobally.This code proudly made in Nebraska. GO BIG RED! 🌽 https://xkcd.com/2347/