What token exchange solves
Picture a request that passes through several services. A user calls the orders API. The orders API needs to call the billing API on the same user’s behalf. What token should it send?
Forwarding the user’s original access token is tempting but risky. That token was issued for the orders API. If billing accepts it, any service holding it can call billing too, and a leak in one place opens every API the token works for.
OAuth 2.0 Token Exchange, RFC 8693, gives a cleaner answer. The orders API sends the user’s token to the authorization server and asks for a new token meant for billing. The server checks that this exchange is allowed and issues a token with the right audience and scope.
How a token exchange request works
Token exchange is a grant at the normal token endpoint. The calling service authenticates as itself and sends the token it holds as the subject token.
POST /realms/acme/protocol/openid-connect/token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic b3JkZXJzLWFwaTpzYW1wbGUtc2VjcmV0
grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange
&subject_token=SAMPLE_ACCESS_TOKEN
&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token
&audience=billing-api{
"access_token": "SAMPLE_EXCHANGED_TOKEN",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
"expires_in": 300
}The response is a normal token response plus issued_token_type, which RFC 8693 section 2.2.1 requires. It tells the caller what kind of token it received.
The parameters, one by one
subject_token and subject_token_type
The subject token represents the party the new token is about, usually the signed-in user. Its type is a URN. The common ones are urn:ietf:params:oauth:token-type:access_token, refresh_token, id_token and jwt, all under the same prefix. Both parameters are required.
audience and resource
audience names the target service by a logical name, such as a client id. resource names it by URL, as in RFC 8707. Both are optional and may be repeated. A server may support one or both.
requested_token_type and scope
requested_token_type asks for a particular kind of token back. Leave it out and the server picks, usually an access token. scope narrows what the new token may do.
actor_token and actor_token_type
These are for delegation: the actor token represents the party acting on the subject’s behalf. The issued token may then carry an act claim naming the actor. Without them, the exchange is closer to impersonation, where the new token simply represents the subject.
Delegation and the act claim
RFC 8693 separates two ideas. In impersonation, the new token represents the subject and nobody else; a service holding it looks exactly like the user to the next API. In delegation, the new token represents the subject and also names who is acting for them.
Delegation shows up in the token as the act claim, defined in RFC 8693 section 4.1. It is a JSON object that identifies the actor, usually with a sub. Chains are nested: an actor inside an actor shows each hop.
{
"aud": "billing-api",
"sub": "1f8f4b3a-sample",
"act": { "sub": "orders-api" }
}Section 4.4 also defines may_act, a claim in the subject token that says who is allowed to act for the subject. Whether your server issues or checks these claims depends on the server; read the issued token to find out.
Checking the exchanged token
After an exchange, check the result before you rely on it.
issued_token_typein the response matches what you asked for.audnames the target service, and only it.scopeis no wider than the original token’s scope.subis still the user, not the calling service.expis short, since the token serves one call chain.
What this tool does and does not do
The builder writes the whole exchange request as curl and as raw HTTP, with every RFC 8693 parameter available.
- It offers the standard token type URNs in lists, so you do not have to type them.
- It sends optional parameters only when you fill them in.
- It requires client authentication, since the caller must prove who it is.
- It builds the Keycloak token endpoint from a server URL and realm, or takes a custom URL.
It does not send the request, check your server’s exchange policy or decode the tokens. Read the result with the JWT decoder to see the new audience and scope.
How to use the builder
- Enter your Keycloak server URL and realm, or a custom token endpoint.
- Paste the subject token: usually the access token your service received.
- Pick its type; access_token is the usual choice.
- Set the audience of the service you need to call.
- Enter the calling client’s id and credentials.
- Copy the curl command, run it and check
issued_token_typeand the new token’saud.
Where token exchange fits
Service chains in a back end
This is the main use. Each hop exchanges the token it received for one scoped to the next service. Every token is valid for one audience, so a leak in one service does not unlock the others.
Narrowing a token
A gateway can trade a broad token for a narrower one before it calls an internal API. The internal API then sees only the scope it needs.
Where it does not fit
Token exchange is not a way for a browser or mobile app to get extra tokens. The caller authenticates as a confidential client, so it belongs on a server. A service acting as itself, with no user, should use client credentials instead.
How Keycloak handles token exchange
Keycloak serves token exchange at its normal token endpoint, https://<host>/realms/<realm>/protocol/openid-connect/token.
- Since Keycloak 26.2, standard token exchange is a supported feature. It is turned on per client with Standard token exchange in the client’s capability settings.
- The calling client authenticates, so it needs Client authentication on.
- The older token exchange, which also covers cases such as exchanging with external identity providers, is still a preview feature.
The exact rules for which audiences a client may request depend on your Keycloak version and setup. Test the exchange in your realm and read the token you get back.
Common errors and how to fix them
| Error | Usual cause | Fix |
|---|---|---|
unsupported_grant_type | Token exchange is not enabled, or the grant URN has a typo. | Turn on Standard token exchange for the client and copy the URN exactly. |
invalid_client | The calling client did not authenticate or used a wrong secret. | Send the client’s credentials. |
invalid_target | The audience or resource is unknown or not allowed. | Check the target’s client id and the exchange permissions. |
invalid_request | A required parameter is missing, often subject_token_type. | Send both subject_token and subject_token_type. |
invalid_grant | The subject token expired or is not valid for this exchange. | Use a fresh token from the current request. |
The OAuth error lookup has more detail on each code.
Security notes
An exchange can create powerful tokens, so treat exchange rights like any other permission. Allow each client to exchange only for the audiences it needs.
Prefer narrowing over widening. A token for a downstream service should carry less scope than the one the user gave the front service.
RFC 9700 asks for audience-restricted access tokens, so a token is accepted only by the service it was meant for. Exchanging per hop is one way to get there.
Best practices
- Exchange once per downstream call chain and cache the result until it expires.
- Always set
audienceorresourceso the new token has a clear target. - Check
audin the receiving service and reject tokens meant for others. - Keep the calling client’s credentials on the server, ideally as a signed JWT instead of a shared secret.