Configure Access Token Exchange with Token Vault

Single-Page Applications (SPAs) can call backend services in a microservice architecture, passing along only an Auth0 access token. These backend services then use the access token exchange to exchange an Auth0 access token for an external provider’s access token to call external APIs on the user’s behalf.

To use the access token exchange with Token Vault, you need to: 

  • Configure your SPA with the authorization_code grant type.

  • Create a backend API that the SPA can request an Auth0 access token for by specifying it as the audience.

  • Create a Custom API Client that is linked to the backend API with the Token Vault grant type enabled.

Configure your SPA

Configure your SPA with the authorization_code grant type. This enables the SPA to request an Auth0 access token scoped to the backend API from the Auth0 Authorization Server.

To configure your SPA with the authorization_code grant type:

  • Navigate to Applications > Applications

  • Select the application you want to configure. 

  • Under Advanced Settings > Grant Types, select the Authorization Code grant type.

  • Select Save Changes.

Create backend API

Create a backend API with a unique identifier and the desired scopes that will perform the access token exchange with the Auth0 Authorization Server:

To create a backend API in the Auth0 Dashboard: 

  1. Navigate to Applications > APIs, and click Create API

  2. To create your API, follow the instructions in Register APIs. Note: Once you set an identifier for your API, you cannot change it. 

  3. Click Create

  4. Once you’ve created your API, you need to add scopes for the API. Navigate to the Permissions tab. Under Add a Permission, add your scopes.

Create Custom API Client

For the access token exchange, you need to create a Custom API Client linked to the backend API. The SPA will be able to request an access token to the backend API by specifying it as the audience in the authorization request to the Auth0 Authorization Server. The Custom API Client has the same identifier as your backend API and has the Token Vault grant type enabled. 

When the backend API performs the access token exchange, it authenticates itself by passing the Custom API Client’s credentials to the Auth0 Authorization Server, proving that it is the same entity that was registered in the Auth0 Dashboard.

To create a Custom API Client in the Auth0 Dashboard:

  1. Navigate to Applications > APIs and select your backend API. 

  2. Select Add Application and enter an application name.

  3. Click Add. Once the application has been successfully created, click Configure Application and scroll to Application Properties. The Application Type is a Custom API Client. 

  4. Under Advanced Settings > Grant Types, the Token Vault grant type should already be enabled for the Custom API Client. 

Once you’ve successfully created the Custom API Client, the user will be redirected to it instead of the SPA after logging in.