Enforcing access at an API
A token that passes validation and carries the right scope has answered two of the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests.'s questions. The third, whether this person may act on this album, is the API's own, and this lesson follows the photo API from a validated token to its decision and the response it chooses.
Three questions in order
Two requests reach the photo API a second apart. Both come from the printer, and both carry the same valid token for user-2048, Ava's account at the photo service, with photos.read. All domains, accounts, and tokens in these examples are fictional.
GET /albums/42/photos HTTP/1.1
Host: api.photos.example
Authorization: Bearer demo-access-token-7
GET /albums/43/photos HTTP/1.1
Host: api.photos.example
Authorization: Bearer demo-access-token-7Album 42 is Ava's. Album 43 belongs to someone else. Everything Token formats and validation and Scopes and audiences checked is identical for both requests: the issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts., the signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message., the audience, the expiry, and the scope. Only one of them should succeed, and nothing in the token can tell the API which.
The photo API's decision has three parts, and each depends on the one before it:
- Is the token valid here? These are the validation checks from Token formats and validation, or an equivalent answer from the authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. about a reference token. If not, the answer is 401.
- Does the token's scope cover this operation? Listing an album's photos needs
photos.read. If not, the answer is 403. - May this subject act on this object? Is
user-2048allowed to see album 43? If not, the photo API answers 404 rather than 403, so the refusal does not reveal that album 43 exists.
The first two need only the token and the route. The third needs the API's own data about who owns what and what has been shared, which is why it is the check most often missing.
What the request asks for
- Album 42
- Ava's, token with photos.read
- Album 43
- Someone else's, same token
- Album 9999
- Does not exist, same token
- Album 42
- Token without photos.read
- Delete a photo
- Album shared for viewing, photos.delete
Valid here, scope, then this object
- All three pass: 200
- May not see it: 404
- Not found: the same 404
- Scope missing: 403 insufficient_scope
- Can see it, may not change it: 403
Nothing in the token can tell the API which album request should succeed; only its own data about who owns what can.
Mapping operations to scopes
The photo API keeps a table of every operation it offers and the scope each one requires:
| Operation | Required scope |
|---|---|
GET /photos | photos.read |
GET /albums/{albumId}/photos | photos.read |
POST /albums | photos.write |
DELETE /photos/{photoId} | photos.delete |
The table is the API's policy, written down, and two habits make it trustworthy.
The first is deny by default. Every route appears in the table, and a route that does not appear is refused. If a developer adds GET /albums/{albumId}/export and forgets the table, the safe outcome is that nobody can call it until someone decides which scope it needs, not that every valid token can.
The second is enforcement in one shared place, such as middleware that runs before any route handler. A check written at the top of each handler depends on every developer remembering it, every time. A check in the shared path applies to routes nobody has thought about yet.
When a token lacks the required scope, the API answers 403 with insufficient_scope and names the scope the operation needs, the response the Using access tokens lesson showed from the client's side.
Checking the object
An API that checks the operation but not the object has broken object-level authorization: anyone allowed to read some album can read every album, just by changing the number in the URLA web address, such as https://photos.example/api/albums/42?limit=10. It names how to connect (https), the domain, the path to a particular page or resource, and any extra parameters after the question mark.. It is one of the most common serious flaws in real APIs, because each request looks legitimate. The token is valid, the scope is right, and the code that loads an album by its identifier works exactly as written.
The fix is to ask about the object on every request, using the subject from the validated token. Here is a sketch of the album route in TypeScript, written for an invented web framework:
// Each route declares the scope it requires. Shared middleware validates
// the token and checks that scope before any handler runs, and it refuses
// any route that declares none.
app.get('/albums/:albumId/photos', { scope: 'photos.read' }, async (req, res) => {
// null when the token was issued to a client acting for itself
const user = req.auth.user;
if (!user) return res.status(403).end();
// Search only albums this user owns or that are shared with them, never by ID alone.
const album = await albums.findVisibleTo(user.id, req.params.albumId);
if (!album) return res.status(404).end();
return res.json(await photos.listForAlbum(album.id));
});The important line is the lookup. findVisibleTo searches only the albums that user-2048 owns or that someone has shared with that account, so album 43 is simply not found. Writing the query this way, rather than loading album 43 and comparing its owner afterwards, makes the safe version the only version. There is no code path that holds someone else's album and then forgets to check. The user ID comes from the validated token, never from a parameter, header, or body field the client controls.
The check above it handles a different case. A token from the client credentials grant represents the client alone. When the printer obtains one for itself, for a back-office job with no person present, the token says the printer is asking and says nothing about any person. If the photo API accepts such tokens, it must never let one stand in for a user, and the danger is subtler than it looks. In a JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in., the subject of a client-only token identifies the client. If a client could register under an identifier that matched a person's subject, say user-2048, its own token would carry "sub": "user-2048", and a careless ownership check would pass.
Current security guidance says authorization servers should not let clients choose identifiers that could be confused with people's, and where that cannot be avoided, they must give APIs another reliable way to tell the two kinds of token apart. The API has to use that distinction. In the sketch, the middleware leaves req.auth.user empty for a client-only token, and the route refuses it.
Choosing the response
The status codeThe three-digit number at the start of an HTTP response that says how the request went, such as 200 for success, 302 for a redirect, or 401 when authentication is needed. tells the client what kind of refusal it received. It can also tell an attacker things the API meant to keep private. The bearer tokenA token that works for whoever presents it: the API checks that the token is genuine and current, not who is holding it. That is why bearer tokens must be kept from being copied. standard defines the first two rows below. The others are the photo API's own design, and a common one:
| Situation | Response |
|---|---|
| No token, or a token that fails validation | 401, with invalid_token when a token was sent |
| A valid token without the operation's scope | 403, with insufficient_scope |
| A client-only token on a route that needs a user | 403 |
| An album that does not exist, or that this user may not see | 404, the same for both |
| An album this user can see but may not change, such as deleting a photo from an album shared for viewing | 403 |
The 404 row is deliberate. If the API answered 403 for album 43 and 404 for album 9999, anyone could probe numbers and learn which albums exist from the difference. Answering 404 for both means a caller learns about albums only when it can already see them. The same thinking applies to the rest of the response: a refusal that takes noticeably longer, or carries a different message, gives away what the status code hides. When the caller can already see the object, as in the last row, 403 hides nothing and is the clearer answer.
Each decision is worth recording, because refusals are often the first sign of misuse. A useful record holds the time, a request ID, the route, the subject, the client ID, the token's jti when it has one, the scope required, the outcome, and a reason category such as insufficient_scope or object_not_visible. It never holds the token or the Authorization header. A burst of object_not_visible refusals as one client walks through album numbers is exactly the pattern those records exist to show.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Watch an API check token, scope and object, in that orderRun a local photo API that owns albums 42 and 43, call it as Ava, as Ben and as a back-office client to see 401, 403 and identical 404 answers, and see the hosted photo API apply the same first checks.