This tutorial shows how to add authorization to Node.js Express API with PlusAuth. If you do not have a PlusAuth account, register from here.
Create PlusAuth Client
After you sign up or log in to PlusAuth, you need to create a client to get the necessary configuration keys in the dashboard. Go to Clients and create a client with the type of Server to Server Application
Configure Client
Get Client Properties
You will need your Client Id
and Client Secret
for interacting with PlusAuth. You can retrieve them from the created client's details.
Configure APIs
Create API
API is a definition in PlusAuth equals to your services which you want to secure. You need to create an API to add authorization to your app. Go to Api's and create a new API. Provide a name and audience to your api. Audience
must be a URL that identifies your api, like https://example.com/api
.
Create API Permissions
After you create API, you can create permissions for it. Permissions allow you to define how resources can be accessed with a specific access token. Go to Dashboard > Api's and click on the Permissions
button on the row at the data table which contains your API.
Authorize Client
Finally, authorize your client in your api to grant permissions. Go to Dashboard > Api's , then select your api and navigate to Authorized Clients
. Add your client to the Authorized Clients
list and grant permissions to it.
Configure Node.js to add Authorization
Create a Node.js Express application or download the sample project from the link on the top of the page.
Install the dependencies
To get started, install the following dependencies.
- body-parser - Node.js body parsing middleware
- node-jsonwebtoken - JsonWebToken implementation for node.js
- jwks-rsa - Library to retrieve signing keys from a JWKS
- dotenv - Module to load environment variables from a
.env
file
Create the .env file
Create the .env
file in the root of your app and add your PlusAuth variables and values to it.
Configure Express Application
We will configure our Express application in a simple way. We will be using body-parser
for request body parsing middleware
Configure Authorization Middleware
We will use jsonwebtoken
and jwks-rsa
to add authorization middleware.
checkJwt
middleware looks for access_token
in the request header. If the access token is not provided or not valid, the response status will be 401 Unauthorized
. In case token validation succeeds, the middleware checks for the requested scope as the second step. If the requested scope is not provided in the token, the response status will be 403 Forbidden
.
Create and Protect API Endpoints
Finally, we will create API endpoints. We will be using checkJwt
middleware here to validate jwt and check scopes for protecting resources with permissions.
See it in action
Start your app and follow the Using API section to see it in action.
Using API
You need to obtain an access token
to call your API. This tutorial shows how OAuth Client Credentials Flow
works for server-to-server communication where there is no user and login process. You will need your client's Client Id
and Client Secret
properties to acquire an access token in Client Credentials Flow
. Also you must include Audience
and Scope
parameters to access your API.
Obtain Access Token
You can obtain an access token using the command line or another application. Create a POST request and enter the required parameters.
You may have noticed that the values defined in Configure Client and Configure APIs sections are used here. If you have used different values make sure to update this file accordingly.
Call Your API
- Calling Endpoint Without Access Token
If you request your protected endpoint without an access token, you will get a 401 Unauthorized
error response.
- Calling Endpoint With Access Token
If you request your protected endpoint with a valid access token, you will get a 200 OK
response.
If you request your protected endpoint with insufficient scope, you will get 403 Forbidden
error response.
As you see, the access token needs to have users:read
scope to access the endpoint.