Authenticate a Forest Admin API against an OAuth protected API Backend
In some cases, you may want to authenticate a Forest Admin user against your API backend that is hosted elsewhere and protected via OAuth. Your API backend may be using a hosted authentication provider such as Auth0, Okta, or Ping, or you may have implemented OAuth yourself.
To achieve authentication between the Forest Admin API and your API backend, the client credentials grant (https://tools.ietf.org/html/rfc6749#section-4.4) is appropriate. When implemented, a client credentials grant eliminates the need to transmit a static or pre-shared, per-user, API key between Forest Admin and your API backend.
Requirements
An admin backend running on forest-express-sequelize/forest-express-mongoose
An API protected via OAuth
Authentication Flow Overview
When you need to call your backend API from a Forest Admin route (via a Smart Action or route override, for example), your Forest Admin API must first call your OAuth token endpoint, including the appropriate Client ID, Client Secret, and Forest Admin username (retrieved via Forest Admin’s
request.user.email
field). The Client ID and Client Secret should be stored securely and protected against disclosure. The implementation of this step is likely specific to your authentication provider. See Auth0 documentation: https://auth0.com/docs/flows/guides/client-credentials/call-api-client-credentials.Upon success, your authentication provider returns a signed access token (typically as a JWT) that includes your Forest Admin username as a custom claim. You must configure your authentication provider to include this custom claim in the token response. See Auth0 documentation: https://auth0.com/docs/scopes/current/sample-use-cases#add-custom-claims-to-a-token.
The signed token is received by your Forest Admin API and stored in memory or in a database if the user needs to make additional authenticated calls to your API backend. Future calls using this token should inspect the expiry date of the token to determine if a token refresh is required.
Finally, the Forest Admin API makes a call to your API backend, including the signed token received from the authentication provider in step 2. Your API validates the signed token and inspects the custom claims to retrieve the username of the Forest Admin user. This username should either match or be mapped to an appropriate user entry in your backend so that the correct authorization (via role or access control restrictions) can be applied.
Sample Code
In the following example, we override the CREATE
user route to authenticate the Forest Admin user against our own API backend protected via OAuth.
Last updated