What it is
Authentication (authn) establishes who is calling: a person, a device or another service. It ends with a credential the system can check on later requests, such as a session cookie or a signed token.
Authorization (authz) decides whether that caller may perform a specific action on a specific resource: may Alice read invoice 42, may the billing service publish to this topic. It needs the identity from authentication, plus rules and often data about the resource.
Why it exists
Knowing who someone is does not tell you what they may do. Every signed-in user is authenticated; almost none of them may read everyone else’s data. Keeping the two apart lets each be solved well: identity once, in a dedicated system (an identity provider), and permissions on every request, close to the data they protect.
How it works
Authentication happens at login. The user proves identity with something they have or are (a passkey, a hardware key) and often something they know (a password). The identity provider issues a credential: a session id stored server-side, or a signed token listing claims such as the subject (sub), issuer and expiry. Later requests carry the credential, and servers verify it without asking the user again.
Authorization happens on every request. Common models:
- RBAC (role-based): users get roles, roles get permissions. Simple; coarse, and roles multiply.
- ABAC (attribute-based): rules over attributes of the user, the resource and the context (department, record owner, time, IP range).
- ReBAC (relationship-based): permissions follow relationships, as in Google’s Zanzibar paper (“Alice is an editor of folder F, which contains document D”).
Where checks run. A policy enforcement point (the gateway, the service, a sidecar) intercepts the request and asks a policy decision point for allow or deny. The decision point can be code inline in the service, a library evaluating policy files (OPA/Rego, Cedar), or a remote service. Gateways are good at coarse checks that need only the request and the token: is the token valid, does it carry the scope for this route, is the caller within its rate limit. Only the service that loads the object can check ownership, tenancy or record state.
Status codes. RFC 9110 defines 401 Unauthorized as “lacks valid authentication credentials”; the response carries a WWW-Authenticate header that tells the client how to authenticate. 403 Forbidden means the server understood who is asking and refuses. A server may answer 404 instead of 403 to avoid confirming that a resource exists.
When to use it
Always: every endpoint that is not deliberately public needs both. The design decisions are where each check runs, which model fits your rules, and whether to centralize policy. Centralize when many services share rules that change often and must be audited (compliance, multi-tenant SaaS). Keep simple, local checks when rules are few and owned by one service.
When not to
- Don’t put authorization only in the UI (hiding buttons). Anything the server does not check, a client can call directly.
- Don’t rely only on the gateway for anything that depends on the object being accessed.
- Don’t add a remote policy engine call per request if your rules fit in a few lines of code and change rarely; the network hop and the extra dependency cost more than they give.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Checks at the gateway | Junk rejected early, one place for token validation and rate limits | Cannot see objects; services that trust it blindly are exposed if anything bypasses it |
| Checks in each service | Object-level decisions with the data at hand | Rules scattered across codebases, uneven quality |
| Remote policy engine (per request) | One place for rules, testing and audit logs | A network round trip per decision; its outage denies every request (fail closed) |
| Local evaluation of distributed policies | Fast decisions that survive the policy server being down | Rules arrive with a delay; data the rule needs must be available locally |
| 404 instead of 403 | Does not reveal which ids exist | Harder debugging for legitimate users |
Alternatives
- Inline checks in code:
if (invoice.ownerId != caller.id) return Forbid(). Fine for small systems; hard to audit at scale. - Database-enforced rules: row-level security in the database filters rows by the caller. Strong defence in depth; ties authorization to one store.
- Capability tokens: the token itself names exactly what it allows (a pre-signed URL for one object). No lookup; revocation is hard, so keep them short-lived.
Failure modes
- Broken object-level authorization (BOLA/IDOR): endpoints accept an object id and never check it against the caller. First on the OWASP API Security Top 10 (2023).
- Broken function-level authorization: admin endpoints reachable by ordinary users because only the UI hid them.
- Fail open: a policy call times out and the code treats “no answer” as “allow”.
- Policy engine as a single point of failure: per-request remote decisions make its availability an upper bound on yours.
- Confused deputy: service A, with broad permissions, acts for a user on service B without passing the user’s identity, so B authorizes A instead of the user.
- Stale permissions: roles cached in long-lived tokens keep working after they are removed (see the JWT lesson).
In production
Deny by default and grant the least privilege needed. Pass the end user’s identity through service calls (a token or a signed context), not only the calling service’s identity. Write authorization tests for every endpoint: “user B cannot read user A’s resource” is a test case, not a code review hope. Log decisions with the policy version and the reason. If you centralize policy, prefer a library or sidecar evaluating pushed bundles over a remote call per request, and monitor how old each service’s bundle is.
Common misconceptions
- “401 means not allowed.” Despite its name, 401 means not authenticated. Not allowed is 403.
- “A valid token means the request is allowed.” It means you know who is asking. What they may do with this object is a separate decision.
- “The gateway handles security, so services can skip it.” Gateways do not see objects, and internal calls may not pass through the gateway at all.
- “OAuth scopes are permissions on data.” Scopes limit what a client application may do on the user’s behalf; they do not say which records the user may touch.
Interview angle
Expect to separate the two terms crisply, pick 401 vs 403, and then place checks in a design: token validation and rate limits at the gateway, ownership and tenancy in the service, defence in depth with database row-level security for multi-tenant data. Strong answers mention BOLA by name, explain fail-closed behaviour and how to keep a policy engine off the critical path (local evaluation, bundles), and propagate user identity across service calls.
Model assumptions
Steps 1–5 are a scripted illustration, not a simulation: logins, credentials and decisions are drawn as hand-placed packets with illustrative durations (12 ms to the gateway, 4 ms between internal components, 14 ms to the identity provider). The engine has no notion of identities or permissions. Steps 6–9 use the simulation: Poisson arrivals at 300 requests per second, a gateway with 1 ms of work, an invoice service with 32 workers and 3 ms mean service time (lognormal, cv 0.3), a database with 16 connections and 3 ms reads, and a policy engine with 8 workers, 2 ms per decision and 2 ms network distance. A failed policy engine refuses connections, and the service then fails the request (fail closed). Local policy evaluation is modelled as 0.5 ms of extra service time. Numbers are illustrative, not benchmarks of any product.