The problem token exchange solves
When an 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. 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 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. 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.
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-exchangeEach grant brings something different to the token endpoint:
| Grant | What the client brings |
|---|---|
| Authorization code | A code returned through the person's browser, and the PKCE verifier for that attempt. |
| Refresh token | A refresh token from an earlier token response. |
| JWT or SAML assertion | A signed statement from an issuer the server already trusts. |
| Token exchange | A 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 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.. Token exchange generalizes that. The input can be an access token, a refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again., 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., a SAMLThe Security Assertion Markup Language, a federation standard widely used for workplace single sign-on, in which the identity provider sends the application a signed assertion about the user. 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.
Try it in the Lab
- Try two ways to call the storage service on Ava's behalf, then exchange her tokenForward Ava's token to a storage service, call it with the photo API's own credentials, and see what each loses. Then exchange her token for one made for storage.