Audience restrictions and validation
Naming a resource achieves nothing on its own. It protects Ava only if 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. writes the right audience into each tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. and refuses resources it should not serve, and if every 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. refuses a token that does not name it.
Setting the audience
Token formats and validation placed the audience check among the API's validation steps. With resource indicators, each side has a few more decisions to make, starting with the authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. server.
When a client names a resource, the authorization server should restrict the token to it. 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., that restriction is the aud claim, and an introspectionA way for an API to ask the authorization server what a token means: whether it is still active and what access it carries. APIs need it when a token is a random string that only the authorization server can interpret. response reports it in a member with the same name. Normally the value is the requested resource itself, as Selecting the intended resource noted. The specification for resource indicators also lets a server map the requested value to another identifier it uses for that API, such as a more general URI. The photo service keeps things simple and writes the resource value unchanged, so the photo API's tokens carry "aud": "https://api.photos.example".
Whatever mapping a server uses, each API still needs its own audience value, the requirement the Scopes and audiences lesson noted in the JWT access token profile. A mapping that sent two APIs to the same identifier would make their tokens interchangeable, however carefully each API checked.
Refusing a resource
Resource indicators come with one error, invalid_target. A server returns it when the requested resource is invalid, unknown, or malformed, when a required resource is missing, or when the combination of resources and scopes makes no sense. All domains in these examples are fictional. At the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., it arrives like any other token error:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
{
"error": "invalid_target"
}At the authorization endpoint, the error returns through the browser to the client's redirect URI, with the attempt's state and iss, like the other authorization errors. Either way, retrying the same request will not help. Something is wrong with the client's configuration, or with where its values came from.
A server that knows every API it issues tokens for can refuse any other resource, and that closes a gap the Token theft and replay lesson described: the counterfeit API. Imagine a version of the printer that lets Ava connect a photo service by typing its address, and an attacker who persuades her to enter https://photos-api.attacker.example. A printer that asks for a token for that resource gets nothing from a careful authorization server, because it does not recognize the resource. And if a careless server did issue one, its audience would name the attacker's address. When the attacker replayed it at the real photo API, the API would see that the token was not meant for it and refuse.
That protection depends on the resource value being the API's real address. A client that sends tokens to https://api.photos.example can check through TLSTransport Layer Security, the protection behind HTTPS. It encrypts traffic between two connected systems, detects changes on the way, and uses certificates so the client can confirm which server it reached. that it is talking to that host. When a server uses abstract identifiers instead, the client has to learn by some other trusted route which addresses belong to each identifier.
Checking the audience at the API
The photo API knows its own identifier from its configuration. It never takes the expected value from the incoming request, such as its Host header, because the caller controls what the request says. The aud claim can hold a single string or an array of strings, and the API accepts the token only when its own identifier is one of them:
const PHOTO_API = 'https://api.photos.example';
function audienceAccepted(aud: unknown): boolean {
const audiences = typeof aud === 'string' ? [aud]
: Array.isArray(aud) ? aud
: [];
return audiences.includes(PHOTO_API);
}The comparison is exact. There is no prefix match, no case folding, and no partial credit for a token from the same authorization server. A token whose audience is https://share.photos.example fails here, even though the same server signed it with the same key. When the check fails, the API answers as it does for any token it cannot accept:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token"Issued for this API
- Signature
- Verifies
- aud
- https://api.photos.example
- Exact match
- Yes
Accepted
Same server, same key
- Signature
- Verifies
- aud
- https://share.photos.example
- Exact match
- No
401 invalid_token
Replayed by the attacker
- Signature
- Verifies
- aud
- https://photos-api.attacker.example
- Exact match
- No
401 invalid_token
The audience limits where a token works, not who presents it.
An API that relies on introspection makes the same comparison with the aud member of the response. A token being active means only that the server still honors it, not that it was issued for this API.
Two further questions follow the basic check. First, do the token's scopes or authorization details mean anything here? A token that names this API but carries only scopes for another one has nothing to authorize. Second, does the token name other APIs as well? A token with several audiences means another API holds a working copy. An API can accept such tokens, refuse them, or accept them only for less sensitive operations, as its own policy decides.
What the audience does not cover
Token theft and replay followed a copied token and showed what an audience leaves open. It limits where a token works, not who presents it, so anyone who copies a token for the photo API can use it at the photo API until it expires. And a compromised photo API can still use the tokens it receives there, although they work nowhere else.
The audience also depends on the client sending each token only to the API it names, as Using access tokens described. If the printer sent its photo token to the sharing API, the sharing API would correctly refuse it, but a party the audience was meant to exclude would already hold a copy.
Who presents a token is the question that binding answers. A token bound to the client that holds it is useless to anyone else, and the lessons on Demonstrating Proof of Possession (DPoP) and Mutual TLS follow the two standard ways of binding one. Together with audience restriction, they shrink what a copied token is worth: it works at one API, for one client, for a short time.
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
- Make your photo API refuse tokens meant for another APIGet tokens addressed to the photo API and the print API, watch the hosted photo API refuse the print token, see what a shared introspection credential reveals, and refuse an unknown resource.