Authentication with JSON Web Token (JWT)
Abbreviations
*[JSON]: JavaScript Object Notation
*[JWT]: JSON Web Token
Introduction
JSON web tokens are one of the more popular ways to secure applications, especially in micro-services, but JWT is much more complex than a simple session based user authentication. In this tutorial I will be breaking down exactly how to set up authentication with JWT and how to ensure the authentication is secure. I will also be showing how to setup refresh tokens with JWT so that your application is even more resilient and secure. Lastly, I will show how to invalidate refresh tokens which is the ultimate last step in securing an application.
Part 1: Simple JWT authentication
create tokens and send tokens to users and then authenticate those tokens on the server
JWT is incredibly powerful
allows the same token to be used across servers (steps 11 creates a second server to show this) as long as they access to the same secret token
Steps
make project folder and cd into it
initialize npm
this will setup the initial project structure and create package.json
install required dependencies
create .env file
this allows you to store whatever you need to keep secret
in this instance
TOKEN_SECRET
install
nodemon
as a developer dependencyanytime a change is made to a file, it will automatically refresh the server instead of manually having to do it
create server.js
this will be the main server in node.js
go into
package.json
and create the scriptdevServer
to startnodemon
you do not have to call it
deServer
, you can call it whatever you want
start up the server
create a request.rest file
you need to have REST Client extension installed
allows you to make REST requests to your api right inside vscode instead of using Postman or Insomnia
how to create a with the crypt library inside of node.js
copy server.js file and rename to authServer.js and change port number
in server.js remove login route
in authServer.js remove get post route and
authenticateToken
middlewareadd start script in
package.json
shutdown down the server and restart server and start authServer
Part 2: Refresh Tokens
increases the security of the server
able to revoke privileges from users we no longer want to have those privileges (similar to logout)
right now the token we built has no expiration, so once a user has been authenticated their token is good for forever, so if a bad player gets a hold of the token, then they would have access to whatever that user has access to --NOT GOOD-- not the most secure
idea is to save the refresh token in a safe spot and for the access token to have a very short expiration time, therefore if some bad player happens to get your access token, then they only access for a few minutes versus forever
access token gets revoked after some many minutes then the user needs to have the refresh token in order to keep access, then once user is done, the refresh token gets invalidated and user logs out
this refresh token gets removed from a list of valid refresh tokens
Reasons for a refresh token
invalidate users that steal access that shouldn't have access
take all your authentication and authorization and remove it away from your normal server
allows scaling of servers separately