In this tutorial, you will see how to create a simple financial service in NodeJs using the Express framework.
Configure PlusAuth
First of all, you need to create a Fintech Service client from the PlusAuth Dashboard. Unlike other application types, you need to define your JWKS (JSON Web Key Set) while creating a financial client for validating client assertion. You will learn more about this later in this article. If you would like to learn about the internals, you can look at OpenID Connect Client Authentication section.
Generating a JWK set
There are many tools that will help you to generate JWKS. We will be using jose for this tutorial. Here is a simple node script that will generate a jwks and write them to corresponding files. Don't forget to install jose
library before using the following script.
After you run the above script, you will have two files which are our public and private keys. Make sure to keep your private key in a safe place. Let's copy the public key to JWKS field in the PlusAuth dashboard client creation popup and finish the creation of the client.
Configure Client
In order to continue this tutorial, you need to configure the redirect and post-logout redirect URIs of the client. You can also do this later.
Let's assume our application will be run on localhost:3000
, and we will have /auth/callback
route for OAuth2 redirect callback URI. Go to client details from the dashboard and add http://localhost:3000/auth/callback
to Redirect Uri's.
For logout redirect URI, lets use /auth/logout/callback
endpoint. Your post-logout redirect URI will be http://localhost:3000/auth/logout/callback
. Let's add it to Post Logout Redirect Uris of the client and save the form.
Create Node.js Application
We will be using Express web framework with pug templating engine. For environment-specific configuration, we will be using dotenv
library.
Install dependencies
So, here are the dependencies that we will be using.
- express : NodeJS web framework
- express-session : Session middleware for express apps
- pug : A templating engine
- dotenv : Environment variable loader utility
- openid-client : OpenID Connect client library with FAPI support
- passport : Authentication middleware for NodeJS
or with yarn
Create .env
file
Don't forget to replace values defined in the format of <PLACEHOLDER>
according to your needs.
Add express-session
middleware
In our app, we will be using cookies to store user-session information.
Initialize FAPI client
We will create an OIDC client with FAPI support and will be using it in authorization middlewares. If you remember, we generated JWKS in the step Generating a JWK set. We will use generated private key here. So make sure you load it correctly.
Add passport authentication middleware
Let's include passport
with the OIDC client's strategy and configure it accordingly to our needs.
Create routes for login, logout, and user profile
Login
We will be using the request object to pass authorization options in a FAPI conformant way to the PlusAuth.
Logout
Check auth middleware
User Profile
Create views
We will be using pug as a template engine.
Here is a simple layout with bootstrap.
And following is our main page.
Profile
On the profile page, we will be printing authenticated user information as JSON with a welcome message.
See the results
Now you are ready to run your application. After installing dependencies, run your application. If you have followed this tutorial exactly the application should run at http://localhost:3000
The source code is served at GitHub. You can reach it from here