SHASHANGKA SHEKHAR'S BLOG

Author, TechWriter

Learn About API Authorization In Node.js

Hits: 36

In this post we are going to secure our NodeJS API’s with customized security token generated by logged user and HTTP actions.

How it Works:

Let’s take a quick look on how the work is going to be done.

  1. Generating a customized token by each HTTP request.
  2. Pass it through request header(x-access-token)
  3. Server extract the token from each request
  4. Verify the custom token by regenerating in server
  5. If token match then system check the user permission from database
  6. Otherwise system will response with status code of 403/404

Prerequisites:

It’s highly recommended to review those previous post.

Let’s get started:

Previously we have created User table in database, now we need to create another table named ”UserAuthorization/Authorization”.

Add new Stored Procedure to validate user action.

We are done with the database work. From our previous sample of application we are going to start. Download it from GitHub, then open the application using Visual Studio 2017. In our application we need to modify both client-side and server-side. First we are going to modify in client-side.

Client-Side:

Let’s get started with the client-side first. Here we are going to generate the token based on several hash process with HMAC SHA256 & Base64 encoding.

Here HMAC is for Hash Message Authentication Code & SHA256 is the hash function.

Get more: https://en.wikipedia.org/wiki/HMAC

 Token Generation:

We need to add this two library in our layout page to enable the hash/encoding process.

Below function is generating the token in client-side which is passed through each HTTP request header.

Create a .js file in public/javascripts folder with the name of “authorization.js”, copy the code snippet then paste it.

 Code Explanation:

From token generation function we can see the hash is generated by cryptographic hash function

Here first argument is the message to hash & the second one is secret key to authenticate the message.

Get more: https://code.google.com/archive/p/crypto-js

Finally the token is encoded by Base64 encoding type.

 Layout.html

Now call the “authorization.js” file to layout page like below.

AngularJS Controller:

Let’s modify our existing “UserController” to add the token in request header with each HTTP method.

Finally the UserController:

Server-Side:

After completing the client-side token generation and sending it with the HTTP request it’s time to work with server-end modifications.

We need to install utf8 npm packages to encode/decode our generated token.

  • utf8 – UTF-8 encoder/decoder for Node.js

To Install the package right click on project Go to > Open Command Prompt Here then type “npm install utf8” command, press Enter.

Verify Token:

Let’s create a .js file in data folder by naming it “verifyToken.js”. In this process Server extract the token from each request then perform two step validation

  1. Comparing the custom token by regenerating in server and
  2. Validating the user permission from database on each request

 Code Explanation:

Extracting the token from request header

Regenerating Token in Server-side to compare

Finally verifyToken.js:

Secure API’s:

Let’s including varifyToken module.

Now we need to add verifyToken middleware as second argument in express route to verify token.

Get More: https://expressjs.com/en/guide/using-middleware.html

 Finally API’s:

Publishing the App:

Go to task explorer in Visual Studio like below image

Run the task, this will copy all our application file to published folder.

Open command prompt here (Shift + Right Mouse) then type “nodemon”. We are starting our application using nodemon. If we have any change in our application nodemon will automatically restart the application.

Now open browser type the URL: http://localhost:3000

OutPut:

In below table view as we can see the access configuration set by particular user.  Let’s test our application according to this configurations.

Delete Action:

Here we can see the request method “DELETE” is forbidden with the status code of 403.

Also the custom response message is displayed in response tab.

Update Action:

Here we can see the request method “PUT” is executed successfully with the status code of 200.

Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help J

References:

  1. https://expressjs.com/en/guide/using-middleware.html
  2. https://code.google.com/archive/p/crypto-js
  3. https://en.wikipedia.org/wiki/HMAC
  4. https://www.codeproject.com/Articles/1110226/Authorization-Filters-in-ASP-NET-Web-API-Angular

Post a comment