Course map

Topic 159 / Core

Authentication and authorization

Chapter 10.2 · Identity and Access

What and why

Topic 39 shipped the minimum enough to call an API safely; this topic is the full system behind it. Authentication answers who (or what) is calling — a person, a service, a script. Authorization answers what that identity may do — which actions, on which resources. The two are independent decisions, made in that order: a system can authenticate a caller correctly and still be wrong about what they're allowed to touch, and the most common real-world access-control bug is checking the first and skipping the second. Builds on: → 158 (defense in depth — identity is the first control in the stack).

Diagram

Where each decision is made, and what happens when it fails:

Decision Question Failure response What it must not reveal
Authentication Is this a known, verified identity? 401 Unauthorized Whether the account exists, if credentials were wrong
Authorization May this identity do this, to this object? 403 Forbidden Internal structure of what was denied

How it works

  1. Collect a credential on every request (header, cookie, or bearer token — the mechanisms from topic 39).
  2. Verify the credential against a known identity — this is authentication, step one and nothing more.
  3. Once identity is established, decide authorization separately: does this identity have rights to this action on this object — never inferred from "the credential was valid."
  4. Return the two failure modes distinctly: a bad or missing credential is 401; a valid credential with insufficient rights is 403 — collapsing them either leaks information or hides a real bug.
  5. Re-run the authorization check on every request that touches a resource, never caching "this user is allowed" across requests for different objects.

Practical example

(illustrative). DELETE /tasks/42 arrives with a valid session token for user 7. Step 2 passes — the token is genuine, authentication succeeds. Step 3 then checks: does task 42 belong to user 7, or does user 7 hold a role permitting deletion of any task? If neither holds, the correct response is 403, not a silent 200 that deletes someone else's task, and not a 401 that incorrectly implies the login itself failed.

When to use it

  • Use when: every request that reads or mutates anything beyond public data — this is the baseline, not an optional hardening pass.
  • Avoid when: nothing — the judgment here is in how much machinery to build (sessions vs. tokens vs. full OAuth, → 161), never in whether to separate the two checks.
  • Failure scenarios: authorization inferred from authentication alone ("logged in, so allowed"); a 403 case accidentally returning 404, leaking whether a resource exists to an unauthorized caller — a deliberate design choice in some systems, a bug in most; UI-only authorization with no server-side enforcement.
  • Simpler alternative: for a single-user or fully internal tool with no sensitive per-user data, authentication alone (no per-object authorization) can be an explicit, documented trade-off — the same exception topic 39 names.

Practice

(Pattern kind — implement minimal, spot the misuse; 20 min, no lab services). Take an endpoint from Project 1 or 3 and add both checks explicitly as separate functions: authenticate(request) -> identity and authorize(identity, action, resource) -> bool. Then deliberately break the ordering — call the handler with authorization skipped — and show the specific unauthorized action it now allows. Done when the two functions are visibly separate in code and you can point to the exact line that would let the bug back in.

Key takeaway

Authentication and authorization are two independent questions, asked in that order, on every request; collapsing them is the single most common access-control mistake. Done when you can: name, for any endpoint, what proves identity and what separately proves permission. Used next in → 160 (identity lifecycle), → 161 (delegated authentication), → 163 (authorization models); Project 3.

References

None — this topic makes no external claims.

Source directory

Search the guide