Q&A-1Explain the difference between authentication and authorisation, and why it matters.
Answer
Authentication answers who is calling: this request carries a valid token for user u_4821. Authorisation answers what that caller may do to this particular thing: may u_4821 read order 102? They are different questions, they fail differently, and the failure is visible in the response.
401 means the credential is missing, malformed or expired. Per RFC 9110 the response must carry a WWW-Authenticate challenge, and the client's correct reaction is to obtain a new credential and retry. 403 means the server understood the request and refuses it; a different token changes nothing. A client that treats 403 as "refresh and retry" loops forever, and that loop is a real production incident, not a hypothetical one.
The reason the distinction earns its keep is asymmetric quality. Token validation is a solved problem you import from a library, so nearly every API does it. Object-level authorisation is per-endpoint work, so it is routinely missing. OWASP's API Security Top 10 (2023) ranks Broken Object Level Authorization as API1: the top entry on the list.
# Python 3.12
ORDERS = {
101: {"owner": "u_4821", "total": 42.00},
102: {"owner": "u_9007", "total": 17.50},
}
def get_order(order_id: int, caller: str):
order = ORDERS.get(order_id)
if order is None:
return 404, None
return 200, order # authenticated, never authorised
def get_order_scoped(order_id: int, caller: str):
order = ORDERS.get(order_id)
if order is None or order["owner"] != caller:
return 404, None # the lookup is the check
return 200, orderTracing the calls:
get_order(102, "u_4821")→(200, {"owner": "u_9007", "total": 17.50}).u_4821is a legitimate, fully authenticated customer who changed one digit in the URL and read someone else's order. No tooling, no sophistication.get_order_scoped(102, "u_4821")→(404, None).get_order_scoped(101, "u_4821")→(200, {"owner": "u_4821", "total": 42.00}).
Note the denial returns 404, not 403. That is deliberate: a 403 on order 102 confirms order 102 exists. The cost is that clients cannot distinguish "gone" from "not yours", and debugging is harder. Some APIs take the 403 and accept the existence leak. Either is defensible; drifting between them per endpoint is not.
Where the authorisation check lives is the actual design decision:
| placement | answers | wins when | cost |
|---|---|---|---|
| route middleware or decorator | "does this caller hold role X?" | coarse gates: admin-only routes, feature flags | cannot see the object; the row is not loaded yet, so it cannot answer "this order" |
| policy engine (OPA/Rego, Cedar, Casbin) | "does this principal satisfy this policy on this resource?" | the same rules span many services, or auditors must read them; policy changes without a redeploy | a second language and runtime, and the resource attributes must still be fetched and passed in |
| object-level check in the handler or the query | "may this caller act on this row?" | anything where the answer depends on the row's owner, tenant or state: most data endpoints | repeated per endpoint, and the failure mode is forgetting it |
OAuth scopes sit above all three and do not replace the bottom row. A token carrying orders:read says the application may read orders; it says nothing about which orders.
The mitigation for "forgotten per endpoint" is to make the check structural rather than remembered: scope the query by the caller, WHERE id = ? AND owner_id = ?, so an omitted check returns zero rows instead of data. A check you can forget is a check that will be forgotten on the endpoint added at 5pm on a Friday.
Detection, concretely: an integration test that authenticates as user A and requests user B's object ID, asserting 404 or 403. Load tests will never find this: the endpoint is fast and correct for every caller who only requests their own data. Watch token-refresh rate against 403 rate to catch clients looping on the 401/403 confusion, and check that any cache key includes the caller's scope, not just the object ID, or tenant data leaks through the cache instead of the handler.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.