Q&A-1Explain authentication versus authorization with an API example.
Answer
The 90-second answer
Authentication answers "who is this caller, and can they prove it?" Authorization answers "may this proven caller do this specific thing to this specific object?" They fail differently and return different status codes: failed authentication is 401 with a WWW-Authenticate challenge; failed authorization is 403. If your API returns 403 for a missing token, you have merged the two, and clients cannot tell whether to re-login or give up.
Assume a multi-tenant API taking a JWT bearer token. A client sends Authorization: Bearer <JWT>. The gateway verifies the signature against the IdP's JWKS and checks iss, aud, exp. That is authentication: it produces a subject, sub=user_8f21. Nothing about invoices has been decided yet. Authorization is the next step: load the policy for invoices:read on tenant A's invoice 42, evaluate it against that subject, and only then touch the database.
Worked trace
Request: GET /v1/tenants/A/invoices/42
Token payload (RFC 9068 access token):
{"iss":"https://idp.example.com","sub":"user_8f21","aud":"api://billing",
"exp":1735689600,"scope":"invoices:read"}| # | Condition | Result |
|---|---|---|
| 1 | No Authorization header | 401, WWW-Authenticate: Bearer realm="billing" |
| 2 | Signature invalid or exp passed | 401, WWW-Authenticate: Bearer error="invalid_token" |
| 3 | aud is api://reports, not api://billing | 401: token was not minted for this API |
| 4 | Valid token, scope lacks invoices:read | 403 |
| 5 | Scope ok, sub is not a member of tenant A | 403 (or 404, if you don't want to leak that invoice 42 exists) |
Steps 1–3 are authentication. Steps 4–5 are authorization. The tenant in step 5 comes from the verified token or a server-side membership lookup: never from the URL path alone, or you have an IDOR.
Failure modes
- Confused deputy: service B accepts a token minted for service A and acts with B's own privileges. Prevented by
audvalidation; detected by alerting onaud-mismatch rejections, which should sit near zero in steady state. - Scope creep: one broad token reused across endpoints. Detected by logging granted scopes against required scopes per route and flagging routes where granted far exceeds required.
- Tenant isolation break: policy checks the resource id but not the tenant. Detected by a cross-tenant test suite in CI, plus a per-tenant 403/404 ratio that spikes when a client starts guessing ids.
- Deny-by-default cost: a new endpoint with no matching policy returns 403 in production. Detected by a policy-coverage metric (routes with a matching rule / total routes) and by canarying new routes.
Trade-offs
Sessions vs tokens: a server-side session revokes instantly but needs a shared store (Redis) or sticky routing; a JWT is stateless and scales horizontally, but revocation needs a denylist or a short TTL. Commonly 5–15 minutes plus a refresh token. Centralized policy (an OPA sidecar evaluating locally, no network hop) keeps rules consistent and auditable; per-service checks are simpler until two services disagree about who may delete an invoice. Deny-by-default is the right default, and its cost is exactly the 403-on-new-route above: pay it with coverage tests rather than by flipping to allow-by-default.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.