

# Obtain a token with the token exchange grant

**Note**  
This is prerelease documentation for a service in preview release. It is subject to change.

The token exchange grant obtains a token for a downstream service that represents an end user who is already signed in. Credential Broker presents a token that identifies the user, called the subject token, to the authorization server and receives a new token for the downstream service. Your application makes one call and writes no token exchange logic. The grant follows the token exchange standard, [RFC 8693](https://www.rfc-editor.org/rfc/rfc8693). For Microsoft Entra ID, Credential Broker uses the JWT bearer grant (`JWT_AUTHORIZATION_GRANT`, [RFC 7523](https://www.rfc-editor.org/rfc/rfc7523)) instead.

## Key characteristics
<a name="credential-broker-obo-characteristics"></a>
+ The token represents the end user, not the application.
+ There is no new consent prompt once a subject token exists.
+ Credential Broker locates the subject token and performs the exchange. Your application never handles the subject token.
+ Supported vendors for token exchange are Microsoft, Okta, Salesforce, and custom OAuth 2.0 authorization servers.

## Example scenario
<a name="credential-broker-obo-scenario"></a>

Employees use a customer-built internal application that runs on Amazon ECS to query their own data through the company's internal HR API, which is protected by the company's own identity provider.

1. **Omar (identity admin)** creates a credential provider pool and a credential provider that allows the token exchange grant and connects to the company's identity provider, and grants the application's role permission to call the broker.

1. **Grace (application developer)** calls the broker with the provider ARN and the scopes the HR API needs.

1. **Sam (end user)** asks for his data and isn't prompted again. The HR API applies its own per-user access control and returns only Sam's data.

## Subject token sources
<a name="credential-broker-obo-sources"></a>

Each token exchange provider names where its subject token comes from, with the `subjectTokenSource` setting. Set exactly one source.

`iamIdentityCenter`  
The subject token comes from the user's IAM Identity Center session. The exchange is silent from the first call. The credential provider pool must be associated with an IAM Identity Center instance.

`credentialProvider`  
The subject token comes from a separate provider that allows the authorization code grant. The user consents once with that provider, and later exchanges are silent.

`credentialProviderSelf`  
The same provider allows both the authorization code grant and the token exchange grant, and the subject token comes from the user's consent with that provider.

Two optional settings control the exchange request. The requester configuration (`tokenExchangeRequesterConfiguration`) sets how the provider authenticates to the authorization server for the exchange, which can be a different client from the one that obtained the subject token. The actor configuration (`tokenExchangeActorConfiguration`) adds an actor token that identifies the party acting on the user's behalf. Omit it to send no actor token.

## Carry the end user's identity in the IAM session
<a name="credential-broker-obo-identity"></a>

Credential Broker identifies the end user from the AWS credentials that sign each call. Your application must call the broker with a per-user session that carries the end user's identity, not with the application's own role session. Credential Broker uses that identity to key the user's tokens and to check that the same user completes a consent session that they started. Use one of the following options.

Option A: IAM federation  
The user signs in to your application through an external OpenID Connect (OIDC) or SAML identity provider. Your application exchanges the user's ID token or SAML assertion for a role session with `AssumeRoleWithWebIdentity` or `AssumeRoleWithSAML`. The session carries the issuer and subject of the user.

Option B: IAM Identity Center  
The user signs in to your application through IAM Identity Center. Your application calls `CreateTokenWithIAM` to obtain the user's IAM Identity Center token, then calls `AssumeRole` with the identity context from that token to create an identity-enhanced session. This option uses trusted identity propagation. For token exchange from an IAM Identity Center source, the credential provider pool must be associated with an IAM Identity Center instance.

Create the per-user session for each user and call Credential Broker with it. Don't share one session across users, and don't cache tokens across users in your application.

## How it works
<a name="credential-broker-obo-how"></a>

**To obtain a token with the token exchange grant**

1. Call `GetOAuthToken` on the token exchange provider with the grant type `TOKEN_EXCHANGE` and the scopes that the downstream service needs, signed with the user's per-user session. When the subject token comes from a consent, also pass the `sessionUri` from that consent.

1. Credential Broker locates the subject token from the provider's subject token source. If no subject token exists for this user, the call fails with `AccessDeniedException` and the reason `SUBJECT_TOKEN_MISSING`. Run the authorization code consent flow once with the source provider, as described in [Obtain a token on behalf of a user with the authorization code grant](credential-broker-3lo.md), and then call `GetOAuthToken` again.

1. Credential Broker exchanges the subject token at the authorization server and returns the downstream access token and its expiration time. Later calls for the same user return the cached token until it expires.

1. Call the downstream service with the access token in the `Authorization: Bearer` header.

A headless caller that can't show a browser can't complete the consent flow. Such a caller stops at the `SUBJECT_TOKEN_MISSING` reason by design. Use an IAM Identity Center source for callers that never interact with a user.

## Security controls in this flow
<a name="credential-broker-obo-security"></a>


**Security controls for the token exchange grant**  

| Control | What it protects against | 
| --- | --- | 
| Per-user IAM session | Credential Broker finds the subject token for the user who signs the call, so a workload can't act as another user. | 
| Subject token held by the broker | Your application never receives or forwards the user's subject token or refresh token. | 
| Scoped downstream token | The exchanged token carries only the scopes that you request for the downstream service. | 
| Separate requester and actor | The exchange can authenticate with its own client and name the acting party, so the authorization server sees who is acting for the user. | 
| Session binding at seeding | When the subject token comes from a consent, session binding protects that consent as described for the authorization code grant. | 
| Encryption | Stored tokens and secrets are encrypted with an AWS owned key by default, or with a customer managed AWS KMS key. For more information, see [Security in Credential Broker](credential-broker-security.md). | 

## Set up
<a name="credential-broker-obo-setup"></a>

1. Create a credential provider pool. For an IAM Identity Center source, associate the pool with an IAM Identity Center instance.

1. Create a credential provider that allows the token exchange grant, with the subject token source and, optionally, the requester and actor configuration. For a consent source, also create or reuse a provider that allows the authorization code grant. You reference the provider by its ARN, in the form `arn:aws:credentialbroker:<region>:<account>:credentialproviderpool/<pool-id>/credentialprovider/<provider-id>`.

1. For an IAM Identity Center source, put an application access scope of `credentialbroker:getoauthtoken` on your application's IAM Identity Center application, with the pool's IAM Identity Center application as an authorized target, by using `PutApplicationAccessScope`.

1. Set up the per-user session described in [Carry the end user's identity in the IAM session](#credential-broker-obo-identity).

1. Grant the per-user role the `credentialbroker:` permissions for this flow. For more information, see [Control access to AWS IAM Credential Broker](credential-broker-permissions.md).

## Related resources
<a name="credential-broker-obo-related"></a>
+ [Credential Broker API Reference](https://docs.aws.amazon.com/credentialbroker/latest/APIReference/)
+ [AWS CLI commands for Credential Broker](https://docs.aws.amazon.com/cli/latest/reference/credentialbroker/)