Obtain a token on behalf of a user with the authorization code grant
This is prerelease documentation for a service in preview release. It is subject to change.
The authorization code grant obtains a token that represents an end user. The user signs in to the external service, such as GitHub or Jira, and grants consent once in a browser. Use this flow when your application acts on a specific user's behalf and needs that user's approval. After the first consent, Credential Broker returns the cached token and refreshes it silently until the refresh token expires or is revoked.
Your application never receives the authorization code, the client secret, or the refresh token. The authorization server sends the code to Credential Broker, and Credential Broker exchanges the code only after a step called session binding confirms that the user who completes consent is the same user who started it. For the security background of this step, see Understand session binding.
Key characteristics
The token represents the end user, not the application.
The user consents once in a browser at the external service.
Your application sends the user to the authorization URL. There is no broker-hosted consent portal in this release.
The authorization code goes to Credential Broker, never to your application.
Session binding ties the consent to the signed-in identity of the user who started it.
Example scenario
A customer-built web application that runs on Amazon ECS reads a user's own Jira tickets on the user's behalf, with the user's consent.
Nadia (identity admin) registers an OAuth app in Jira, creates a credential provider pool and a credential provider that allows the authorization code grant, registers the provider's callback URL in the Jira app, and grants the application's role permission to call the broker.
Tom (application developer) adds the consent flow and a session binding handler to the application. Tom never sees the client secret.
Lena (end user) signs in to the application, approves access to her Jira tickets once in her browser, and is not prompted again while her consent is valid.
Carry the end user's identity in the IAM session
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
AssumeRoleWithWebIdentityorAssumeRoleWithSAML. 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
CreateTokenWithIAMto obtain the user's IAM Identity Center token, then callsAssumeRolewith 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
Every call that your application makes to Credential Broker in this flow is signed with the user's per-user session.
To obtain a token with the authorization code grant
-
Call
GetOAuthTokenwith the provider ARN, the grant typeAUTHORIZATION_CODE, the scopes, and thesessionUrifrom an earlier session if you have one. If Credential Broker holds a valid token for this user, provider, and scopes, the response returns the access token and you are done. Otherwise, the response returns no token. -
Call
StartOAuthSessionwith the provider ARN, the scopes, asessionBindingUrl, and an optionalcustomStatevalue. Credential Broker records the user who started the session and returns anauthorizationUrland asessionUri. Store thesessionUriandcustomStatein the user's application session.The
sessionBindingUrlmust exactly match one of the allowed session binding URLs configured on the provider. UsecustomStatefor your own protection against cross-site request forgery, for example a random value tied to the user's application session. If a token became available after your first call, the response returns the access token instead of an authorization URL. -
Send the user's browser to the
authorizationUrl. This URL opens Credential Broker, which redirects the browser to the authorization server. The user signs in to the external service and grants consent. -
The authorization server redirects to the provider's Credential Broker callback URL with the authorization code. Credential Broker keeps the code and doesn't exchange it yet. If the user denies consent, the callback receives an error instead of a code.
-
Credential Broker redirects the browser to your
sessionBindingUrlwith thesessionUriand yourcustomState. In the handler at that URL, confirm thatcustomStatematches the value you stored. -
From the handler, call
CompleteOAuthSessionBindingwith thesessionUri, signed as the currently signed-in user. Credential Broker checks that this user is the user who started the session. If the identities match, Credential Broker exchanges the authorization code with the client secret at the authorization server, stores the access token and refresh token, and returns thesessionUri. -
Call
GetOAuthTokenagain with the same provider ARN, grant type, and scopes, and thesessionUri. The response returns the access token and its expiration time. Call the external service with the access token in theAuthorization: Bearerheader.
After the first consent, your application repeats only the first step. To discard the
stored tokens and require the user to consent again, call GetOAuthToken with
forceAuthentication set to true, then start a new session.
Security controls in this flow
| Control | What it protects against |
|---|---|
| Per-user IAM session | Tokens are keyed to the end user who signs the call, so one user can't retrieve another user's token. |
| Session binding | Credential Broker exchanges the code only when
the user who completes consent is the user who started it. A mismatch fails with
AccessDeniedException and the reason
SESSION_BINDING_IDENTITY_MISMATCH, and no token is kept. |
| Single-use sessions | A session that is already bound, has
expired, or isn't waiting for binding fails with ConflictException and the
reason SESSION_STATE_CONFLICT. A replayed session is rejected. |
| Allowed session binding URLs | Credential Broker redirects the browser only to a URL that you allowed on the provider (up to 10). |
| Code held by the broker | The authorization code, client secret, and refresh token never reach your application. |
| Browser session protection | Credential Broker uses PKCE and browser session cookies between its authorization and callback endpoints. |
customState | Lets your application reject a response to a session that it didn't start. |
| 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. |
Set up
Register an OAuth app with the external service.
Create a credential provider pool.
Create a credential provider that allows the authorization code grant, with the allowed session binding URLs set. You reference this provider by its ARN, in the form
arn:aws:credentialbroker:<region>:<account>:credentialproviderpool/<pool-id>/credentialprovider/<provider-id>. For the configuration steps specific to this grant, see Complete setup for the authorization code grant.Register the provider's callback URLs, which
GetCredentialProviderreturns ascallbackUrls, as redirect URIs in the external service's OAuth app.Set up the per-user session described in Carry the end user's identity in the IAM session.
Grant the per-user role the
credentialbroker:permissions for this flow. For more information, see Control access to AWS IAM Credential Broker.
Complete setup for the authorization code grant
If a provider allows the authorization code grant, complete the following two additional settings:
-
Allowed session binding URLs – When a provider allows the authorization code grant, you must provide the return URLs that your application uses for session binding. Credential Broker accepts a session binding URL at runtime only if it is in this list.
-
A registered broker callback URL – Credential Broker hosts a per-provider callback URL. Read this URL from the
GetCredentialProviderresponse, and then register it as an authorized redirect URI on your OAuth client at the outbound identity provider. The identity provider rejects the consent redirect if this URL is not registered.
The broker callback URL has the following form.
https://credentialbroker.<region>.api.aws/oauth2/callback/<poolId>/<providerId>
To define a pool or provider with AWS CloudFormation, see the Credential Broker resource reference in the AWS CloudFormation User Guide. This guide does not reproduce the CloudFormation resource schema.