A JavaScript program to create a JSON Web Token (JWT), powered by jsonwebtoken library.
- π§ What is JWT ?
- βοΈ Prerequisites
- π§Ά How to Run (using
Yarn
) - π How to Run (using
Docker
) - π How to Run (pull from DockerHub)
- π³ How to Run (using
docker-compose
)
JWT stands for JSON Web Token that is used to authenticate a user/session to backend services and also to authenticate between backend services.
A JWT consists of three parts:
- Header (Metadata of JWT which are usually encoding algorithm and token type)
- Payload (Data that are encoded in JWT, do not store any sensitive data)
- Signature (A random string that is generated by Header + Payload + Secret)
The signing algorithm which is usually HS256, utilizes the header, the payload, and the secret to generate a string named signature. The figure below illustrates the signing process :
(image from : StackOverflow - What is secret key for JWT based authentication and how to generate it?)
In the server-side which stores the secret (used to generate a signature), the JWT verification process occurrs. The figure below gives an illustration :
(image from : StackOverflow - What is secret key for JWT based authentication and how to generate it?)
The JWT from the client is decoded to a header and a paylaod. Then both of them are combined with the secret in the server-side to geenrate a test signature. As a result, if test siganture matches the original signature that comes within the JWT from the client, it is verified succesfully which means the JWT is valid. Otherwise, it's invalid and data probably have been modified.
References :
- Lovia - JSON Web Token
- StackOverflow - What is secret key for JWT based authentication and how to generate it?
Prerequisites means what you should prepare/have before diving into a project. You should have NodeJS installed in your laptop/computer.
This project was created and tested using NodeJS v14.17.5. Hence, NodeJS version 14.17.5 or above should work.
- Clone this repository :
git clone https://github.com/kevinadhiguna/js-create-jwt
- Install dependencies :
yarn
or
npm install
- Create environment variables (
.env
file) by copying.env.example
file :
cp .env.example .env
- Generate secret and put it into
.env
file :
openssl rand 64 | base64 # (linux/macOS users)
or
node -e "console.log(require('crypto').randomBytes(64).toString('base64'))" # (all users)
- Generate a new JWT :
yarn generate
or
npm run generate
You can either choose to build docker image by yourself or pull the docker image from DockerHub.
If you want to build docker image:
- Clone this repository :
git clone https://github.com/kevinadhiguna/js-create-jwt
- Build the docker image :
docker build -t js-jwt .
Note: you can replace js-jwt
with docker image name that you want.
- Generate secret :
openssl rand 64 | base64 # (linux/macOS users)
or
node -e "console.log(require('crypto').randomBytes(64).toString('base64'))" # (all users)
Copy the result (secret) so that you can paste it as JWT_SECRET
value in the next command.
- Run the docker image :
Let's say the secret isw4KuiqUDzvjIFBA4jHpUfjAeOOCH1DHJOODHkGXBUYLtK0bnp26GDM6WQvRUZtu2pgp3WiL5oFgz6XoSN7Q4VA==
. Then pass it as theJWT_SECRET
value.
docker run -e JWT_SECRET=w4KuiqUDzvjIFBA4jHpUfjAeOOCH1DHJOODHkGXBUYLtK0bnp26GDM6WQvRUZtu2pgp3WiL5oFgz6XoSN7Q4VA== js-jwt
If you want to pull the docker image from DockerHub, here are the steps:
- Pull the docker image:
docker pull kevinadhiguna/js-create-jwt:latest
- Generate secret :
openssl rand 64 | base64 # (linux/macOS users)
or
node -e "console.log(require('crypto').randomBytes(64).toString('base64'))" # (all users)
Copy the result (secret) so that you can paste it as JWT_SECRET
value in the next command.
- Run the docker image :
Let's say the secret isw4KuiqUDzvjIFBA4jHpUfjAeOOCH1DHJOODHkGXBUYLtK0bnp26GDM6WQvRUZtu2pgp3WiL5oFgz6XoSN7Q4VA==
. Then pass it as theJWT_SECRET
value.
docker run -e JWT_SECRET=w4KuiqUDzvjIFBA4jHpUfjAeOOCH1DHJOODHkGXBUYLtK0bnp26GDM6WQvRUZtu2pgp3WiL5oFgz6XoSN7Q4VA== kevinadhiguna/js-create-jwt:latest
- Clone this repository :
git clone https://github.com/kevinadhiguna/js-create-jwt
- Generate secret :
openssl rand 64 | base64 # (linux/macOS users)
or
node -e "console.log(require('crypto').randomBytes(64).toString('base64'))" # (all users)
Copy the result (secret) so that you can paste it as JWT_SECRET
value in .env
file.
- Run docker-compose :
docker-compose up