Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

The problem token exchange solves

When an API has to call another service on a person's behalf, the token it received is addressed to the wrong place, and its own credentials say nothing about the person. Token exchange lets it ask the authorization server for a new token made for the next service. This lesson follows the photo API's call to the storage service for Ava's photos and sets out what an exchange can change and where it stops being the right tool.

The authorization server as a token service

When the printer asks for the photos in Ava's album 42, the photo API checks the printer's token and then needs the image files, which live in the storage service at https://storage.photos.example. Service-to-service access followed that second request and found that each obvious way to make it loses something. Forwarding the printer's token fails at storage, because the token is addressed to the photo API. Calling with the photo API's own client credentials gives storage a token about the photo API, not about Ava, so storage cannot check whose files are being read. Naming Ava in a header leaves storage with only the caller's word for who she is. That lesson settled on a fourth approach: the photo API asks the authorization server for a new token, made for the storage service, in exchange for the token it received.

That approach is token exchange, and the storage service sets what it has to deliver. Storage should hand over files only when the request really concerns their owner, so it needs a token addressed to it, about Ava's account user-2048, carrying no more access than Ava gave the printer, and issued by an authority it already trusts. The photo API cannot produce that token. The authorization server can.

The printer's token, passed on

aud
The photo API
sub
user-2048
Subject vouched for by
Authorization server

Refused: wrong audience

The photo API's own token

aud
Storage
sub
photo-api
Subject vouched for by
No one: not about Ava

Cannot tell whose files

Own token plus a user header

aud
Storage
sub
user-2048 in a header
Subject vouched for by
The caller alone

Only the caller's word

A token made for storage

aud
Storage
sub
user-2048
Subject vouched for by
Authorization server

Storage checks the files are Ava's

The photo API cannot produce that token. The authorization server can.

Of four ways the photo API could call the storage service for Ava, only the exchanged token is addressed to storage and vouched for as being about Ava.

The idea is older than OAuth. A security token service, or STS, validates security tokens presented to it and issues new ones in response. Enterprise web services used them for years through WS-Trust, a protocol built on XML messages. OAuth 2.0 Token Exchange, published as RFC 8693 in January 2020, defines the same kind of service with the OAuth token endpoint, form-encoded requests and JSON responses.

An authorization server is well placed for the job. It issued the printer's token and holds the key that signed it. It knows the registered clients, the scopes and the APIs those scopes belong to. Given the printer's token, it can confirm that the token is genuine and still valid, see what it allows, and decide whether a narrower token for the storage service is appropriate.

One role shifts along the way. Toward the printer, the photo API is a resource server. When it asks the authorization server for a new token, it is a client, registered as photo-api and authenticating with its own credential like any confidential client. The token it presents came from someone else's request, and its client authentication says who is asking to exchange it.

A grant at the token endpoint

Token exchange is an extension grant, like the device authorization and assertion grants. The photo API sends a token request to the same token endpoint the printer uses, and names the grant with a URN, shown here before form encoding:

grant_type=urn:ietf:params:oauth:grant-type:token-exchange

Each grant brings something different to the token endpoint:

What each grant brings to the token endpoint
GrantWhat the client brings
Authorization codeA code returned through the person's browser, and the PKCE verifier for that attempt.
Refresh tokenA refresh token from an earlier token response.
JWT or SAML assertionA signed statement from an issuer the server already trusts.
Token exchangeA security token the client holds, and a description of where the new token will be used.

The token being exchanged is called the subject token, because it represents the party the new token will be about, here Ava. The request can also say where the new token will be used and what it should allow there. The response is an ordinary token response with one addition: a field saying what kind of token was issued.

What an exchange can change

JWT and SAML assertion grants turned one kind of signed statement into an access token. Token exchange generalizes that. The input can be an access token, a refresh token, a JWT, a SAML assertion or an OpenID Connect ID token, issued by this authorization server or by another one it trusts. The output does not have to be an access token either, which is why the response names its type.

When the output is an access token, it can differ from the input in several ways at once. It can be addressed to a different audience, carry less scope, live for less time, and record who is acting for whom. Each of those is a decision the authorization server makes for every exchange.

The specification defines the request and the response, and deliberately says little about how to make those decisions. Which tokens a server accepts, which clients may exchange them and what the new tokens contain are left to each deployment, and much of the care in token exchange lives there. An exchange that works as intended never adds access Ava did not give. It turns the printer's token into something narrower: usable at a different service, about the same person, with no more access and for less time.

When token exchange is not the right tool

Token exchange asks the authorization server for a new token at every hop, and every new token is still an access token for one API. Two situations stretch that design, and the IETF OAuth working group has drafts for both. Neither is an RFC yet, so their details may still change.

The first is a long call chain inside one platform. Suppose a single request from the printer passes through ten internal services on the photo service's side. Exchanging at each hop means ten round trips to the authorization server, and each service still learns only whom the request is about and who called it, not what the request as a whole is for. OAuth Transaction Tokens, an IETF draft, take a different approach. When the request enters the platform, the service at the edge makes one exchange at a Transaction Token Service and receives a short-lived, signed transaction token. It records the subject, the transaction's identifier, and context such as the purpose of the request, and its audience is the whole platform rather than one API. It travels with the request in its own HTTP header, alongside each service's own authentication, and lives only as long as the transaction. A transaction token is not an access token: it grants nothing by itself, it is never sent outside the platform, and services in the chain cannot change what it says about Ava.

The second is a chain that leaves the authorization server's world. If the photo API had to call the print lab, which runs its own authorization server, no token from the photo service's server would be accepted there. Identity and authorization chaining across domains, another IETF draft, combines two grants you already know. The photo API exchanges Ava's token at its own authorization server for a short-lived JWT addressed to the print lab's authorization server, then presents that JWT there with the JWT bearer grant and receives a token the print lab's API accepts. A related draft, the Identity Assertion Authorization Grant, applies the same pattern in organizations that use single sign-on: the company's identity provider turns the ID token an application already holds into a signed grant for another application's authorization server, so the company's sign-in policy, not a fresh consent screen, decides whether one application may call the other on an employee's behalf.

Inside one authorization server's domain, for a hop or two, token exchange remains the simplest tool.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

The photo API sends the printer's token to the token endpoint and asks for a token for the storage service. What role is the photo API playing in that request? 1 of 3

Choose one answer.

Which new token shows an exchange of the printer's token working as intended? 2 of 3

Choose one answer.

One request from the printer passes through ten internal services on the photo service's side. Why might the platform use a transaction token instead of exchanging at each hop? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity