Beta

Create a tenant

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

BTL Admin

The problem token exchange solves

When the printer asks for the photos in 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 forwarding the printer's token, calling with the photo API's own client credentials, and naming you in a header each lose something. It settled on a different 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 your files only when the request really concerns you, so it needs a token addressed to it, about user-2048, carrying no more access than you gave the printer, and issued by an authority it already trusts. The photo API cannot produce that token. The authorization server can.

The authorization server as a token service

The idea is older than OAuth. A security token service, or STS, is a service that 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 and SOAP 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. For the printer's request, the photo API is a resource server. When it asks the authorization server for a new token, it is a client. It is registered as photo-api and authenticates 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:

GrantWhat the client brings
Authorization codeA code returned through your 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 you. 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 ID token from OpenID Connect, 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. Much of the care in token exchange lives there. An exchange that works as intended never adds access you 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.

Try it in the Lab

PUT IT INTO PRACTICE

Check your understanding

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

0 of 2 answered correctly

Enable JavaScript to answer these questions and save progress in this browser.

QUESTION 1 OF 2The 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?

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

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