Skip to main content

Build an authentication application using Identity Pools APIs

Learn how to create an authentication application using Identity Pools APIs using Nodejs and React

Overview

In this tutorial, we will be creating a Node.js application for displaying custom consents. After authenticating, the user will be redirected to a custom consent page where the user will be presented with the scopes that the OAuth client is requesting. The end user will have an option to customize the requested consents or reject them altogether. We will be using SecureAuth and its authorization service as an authorization server.

After a user is authenticated with the configured identity source by the OAuth server, the user is redirected to consent page where the user can accept or reject consents. The SecureAuth platform allows the client to set up a custom consent page. The SecureAuth platform allows you to create a custom consent application and integrate into the OAuth flow. A custom consent page may be required if the customization capability provided by SecureAuth isn't enough or there are other UX interactions or data points that need to be displayed in the consent page. In such scenarios custom consent application integration model can be utilized. One of the main use case for a custom consent page is for Open Banking scenarios which need interaction with external APIs as well.

An overview of the flow can be seen below.

Custom consent overview

First, the user-agent is redirected to SecureAuth authorization server, where the user logs in and is authenticated. Once the user is authenticated, the SecureAuth authorization server redirects the user to the application hosting the custom consent page. The application calls the SecureAuth scope grant request API to retrieve details on the account access consents. The application displays the consents to the user and the user can accept all, reject all, or accept only some of the consents. Once a selection is made the application redirects the user back to the SecureAuth authorization server.

Prerequisites

Build Node.js Application

The Node.js application we will build consists primarily of two JavaScript files and some templates for rendering the HTML. app.js is just boilerplate for setting up our Express.js application and specifying that we are using Handlebars.js for our view engine. There should be nothing unfamiliar here but if you need a refresher check out the ExpressJS documentation.

Define Basic Route & Environment Variables

Inside the routes folder lets look at the index.js file. It contains our handlers and the code for obtaining the consent request, getting an authorization token, and accepting or denying the consent request.

First, we get our environment variables. The System workspace needs to obtain an access token so that it can fetch the scopes and related metadata. To do that, we will need the following:

  • TENANT_ID - the tenant ID which can be seen in SecureAuth platform under Platform.

  • AUTHORIZATION_SERVER_URL - the issuer URL which can be found in the .well-known endpoint. In the next section we will add these environment variables to our .env file.

  • CLIENT_ID - this is the client ID of your system workspace client application.

  • CLIENT_SECRET - the client secret for your client application in the system workspace.

const tenant_id = process.env.TENANT_ID;
const issuer_url = process.env.AUTHORIZATION_SERVER_URL;
const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;

Client Authentication

The Demo Application, by default, is set up for using the Client Secret Basic client authentication method. This is not required. You may change this to an alternative token endpoint authentication method. Please, update your Node.js application according to your change.

We will use the CLIENT_ID and CLIENT_SECRET to create the "Bearer" token credentials to access SecureAuth APIs.

const auth_token = Buffer.from(`${client_id}:${client_secret}`, 'utf-8').toString('base64');

Token Purpose

When obtaining an access token using the Client Secret Basic method, this is not the access token that the OAuth client uses for accessing the users protected resources. Instead, this is for the custom consent application to use for obtaining an access token that is used for getting the available consents for displaying to the user.

Next, add a route for checking that our application is running and responding to requests.

router.get('/', function (req, res, next) {
  res.render('health');
});

Set Environment Variables in Node.js Application

When we enabled a custom consent page in our SecureAuth platform client, an application was created for us in the System workspace. This application should have grant type client credentials and Client Secret Basic as the Token Endpoint Authentication Method (if your system workspace has different settings then change them to the previous values).

Additionally, under the Scopes tab of your client application settings, ensure that the manage_consents and manage_scope_grants scopes are selected.

From the OAuth tab of the system workspace application, copy the CLIENT ID and CLIENT SECRET. Also, make note of your Tenant ID in the top right. Then, in your client application in the new workspace that we created, on the left navigation menu choose OAuth > Authorization Server and from the General tab copy the Authorization Server URL (this is your issuer URL and can also be found in the .well-known endpoint). Add these values to the environment variables in the .env file.

Running the Node.js Application

To run the application, enter the following command from the root of the project in the terminal after you have added the required environment variables to .env.

npm start

Tip

After running the application, verify that there are no errors and check that the application is running by visiting http://localhost:4001/. You should see that the application is running.

Visiting http://localhost:4001/ directly will show an error since the application is expecting values to the passed in during the callback from SecureAuth. You should see a page with a card title showing Running.

Now, go back to the SecureAuth platform and select View all workspaces. Then, select the three dots in the upper right corner of the card for the client application that we created. Select Demo Application. This initiates the OAuth flow.

Sign in using the username and password that you chose when the workspace was created (when you added the Sandbox IDP). Once you sign in, you should be redirected to the custom consent screen. Once you accept the consent you should be redirected back to SecureAuth platform and your access and ID tokens should be visible.

Summary

This wraps up our tutorial for the Node.js application that provides a custom consent page to end users after the user has authenticated with the Identity Source. After going through the tutorial you have accomplished the following:

  • Build a simple Node.js application to provide a custom consent page.

  • Prepare SecureAuth for redirecting a user to a custom consent page.