Skip to content
Get started
Bastionary › Knowledge base › Tokens and APIs

Resource indicators (RFC 8707): scoping tokens to the right API

By Bastionary · Tokens and APIs

Most OAuth deployments hand out one access token that works everywhere. The user logs in, the client gets a token, and that token is accepted by the billing API, the messaging API, the admin API, and the analytics API alike. It is convenient right up until the token leaks. Then the single token that opened every door becomes a master key in an attacker's hands, and the blast radius is your entire backend. The design that produced that outcome treated "which API is this token for" as a question not worth asking.

Resource indicators (RFC 8707) make it a required question. They let a client say, at request time, which specific resource server a token is intended for, and the authorization server mints a token whose audience is scoped to exactly that API. A token for the billing API is rejected by the messaging API, because it was never minted for it. A leaked token unlocks one service, not all of them.

The over-privilege problem, concretely

Consider a token with aud unset or set to a catch-all, carrying scope=read write. Every resource server that validates it has to decide what those scopes mean for itself, and most do the permissive thing. The messaging API sees a valid signature and valid scopes and serves the request — it has no way to know the token was obtained for billing. The audience claim is the mechanism that would stop this, and without resource indicators clients have no standard way to request a correctly-scoped audience.

Audience scoping and scope are orthogonal, and you need both. Scope answers "what actions" (read vs. write); audience answers "at which service." A token can have scope=write and still be the wrong token to present, if its audience is the billing API and you are calling messaging. Treat an audience mismatch as a hard rejection independent of whether the scopes would have allowed the action.

Requesting an audience-scoped token

The client adds one or more resource parameters to the authorization or token request, each an absolute URI identifying the target API.

POST /token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https://app.example.com/callback
&client_id=app-7f2
&resource=https://billing.example.com
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

The authorization server validates that this client is permitted to obtain tokens for https://billing.example.com, then issues a token whose aud is that URI.

{
  "iss": "https://auth.example.com",
  "sub": "user-4821",
  "aud": "https://billing.example.com",
  "scope": "invoices:read invoices:write",
  "exp": 1776700600
}

Now the billing API accepts it and the messaging API does not, because the messaging API's validation rule includes audience: 'https://messaging.example.com' and this token's aud does not match.

Enforcing audience on the resource server

The resource server must check aud on every request. This is one line in most JWT libraries, and omitting it is the single most common way audience scoping is defeated — the token is minted correctly and then nobody checks.

import { jwtVerify, createRemoteJWKSet } from 'jose';

const JWKS = createRemoteJWKSet(
  new URL('https://auth.example.com/.well-known/jwks.json')
);

async function verifyForBilling(token: string) {
  const { payload } = await jwtVerify(token, JWKS, {
    issuer: 'https://auth.example.com',
    audience: 'https://billing.example.com', // reject tokens for any other API
    algorithms: ['RS256'],
  });
  return payload;
}
A token with no audience claim must be rejected by an audience-enforcing resource server, not accepted as "audience not specified, therefore fine." Treating a missing aud as a pass is how a legacy master token keeps working after you thought you had locked things down. Require the claim and require it to match.

Requesting tokens for multiple resources

A client that genuinely needs to call two APIs can send multiple resource values. But multi-audience tokens partially undo the benefit — a token good for two APIs leaks access to two APIs. The better pattern for anything beyond a trivial case is to obtain narrow, single-audience tokens and refresh per resource, so each token's exposure is bounded to one service.

POST /token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token
&refresh_token=8xLOxBtZp8
&resource=https://messaging.example.com

The same refresh token yields a messaging-scoped access token now and a billing-scoped one on the next refresh — each one audience-bound, none of them a master key.

Where this connects to the rest of your token strategy

Resource indicators are the request-time half of a larger principle: a token should carry the least authority that gets the job done. They pair naturally with two other mechanisms:

  • Token exchange is the service-to-service counterpart. A gateway holding a broad session can exchange it for a narrow, single-audience token per downstream call — the same audience discipline, applied between your own services. See token exchange (RFC 8693) for the delegation mechanics.
  • Short lifetimes. Audience scoping bounds where a token works; short expiry bounds how long. Together they turn a leaked token into a small, local, brief problem instead of a global, permanent one.

Rolling it out without breaking everything

The migration from master tokens to audience-scoped ones is where teams stumble, because flipping audience enforcement on globally will reject every existing broad token at once. Stage it:

  1. Start minting audience-scoped tokens: have clients send resource and the authorization server set aud.
  2. Have resource servers log audience mismatches without rejecting, so you can see which callers still present wrong-audience tokens.
  3. Fix those callers to request the correct resource.
  4. Only once the logs are clean, switch resource servers to reject on mismatch.

Done in that order, the cutover is boring, which is the goal for anything in the authentication path.

The principle underneath

A token is a capability, and a capability that works everywhere is a capability you cannot reason about. Resource indicators restore the ability to answer "if this token leaks, what exactly can the holder reach?" — and the answer becomes "one API, for a few minutes" instead of "everything, indefinitely." That is the difference between a contained incident and a breach.

Deciding which clients may request which resources, setting the audience on every token, and enforcing it across every service are policies that live at your authorization server and in your resource servers together. Keeping that policy coherent across a fleet is markedly easier when the server minting the tokens is one you operate yourself, where "every token must name its audience" is a rule you can require rather than a behavior you hope your provider supports.

Run it yourself

Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.

Get started