View a markdown version of this page

Obtain a token on behalf of a user with the authorization code grant - AWS Identity and Access Management

Obtain a token on behalf of a user with the authorization code grant

Note

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.

  1. 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.

  2. Tom (application developer) adds the consent flow and a session binding handler to the application. Tom never sees the client secret.

  3. 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 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

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
  1. Call GetOAuthToken with the provider ARN, the grant type AUTHORIZATION_CODE, the scopes, and the sessionUri from 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.

  2. Call StartOAuthSession with the provider ARN, the scopes, a sessionBindingUrl, and an optional customState value. Credential Broker records the user who started the session and returns an authorizationUrl and a sessionUri. Store the sessionUri and customState in the user's application session.

    The sessionBindingUrl must exactly match one of the allowed session binding URLs configured on the provider. Use customState for 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.

  3. 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.

  4. 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.

  5. Credential Broker redirects the browser to your sessionBindingUrl with the sessionUri and your customState. In the handler at that URL, confirm that customState matches the value you stored.

  6. From the handler, call CompleteOAuthSessionBinding with the sessionUri, 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 the sessionUri.

  7. Call GetOAuthToken again with the same provider ARN, grant type, and scopes, and the sessionUri. The response returns the access token and its expiration time. Call the external service with the access token in the Authorization: Bearer header.

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

Security controls for the authorization code grant
ControlWhat it protects against
Per-user IAM sessionTokens are keyed to the end user who signs the call, so one user can't retrieve another user's token.
Session bindingCredential 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 sessionsA 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 URLsCredential Broker redirects the browser only to a URL that you allowed on the provider (up to 10).
Code held by the brokerThe authorization code, client secret, and refresh token never reach your application.
Browser session protectionCredential Broker uses PKCE and browser session cookies between its authorization and callback endpoints.
customStateLets your application reject a response to a session that it didn't start.
EncryptionStored 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

  1. Register an OAuth app with the external service.

  2. Create a credential provider pool.

  3. 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.

  4. Register the provider's callback URLs, which GetCredentialProvider returns as callbackUrls, as redirect URIs in the external service's OAuth app.

  5. Set up the per-user session described in Carry the end user's identity in the IAM session.

  6. 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 GetCredentialProvider response, 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>
Note

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.