What is JWT (JSON Web Token) and How it Works

In this article, we will learn:

- What is JWT?

- When should you use JSON Web Tokens?

- What is the JSON Web Token structure?

- How do JSON Web Tokens work?

What is JWT?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. 
A JWT is cryptographically signed so there is a guarantee we can trust it when we receive it, as no middleman can intercept and modify it, or the data it holds, without invalidating it.

JWT, or JSON Web Tokens (RFC 7519), is a standard that is mostly used for securing REST APIs

JWT is the best way to communicate securely between client and server.

JWT follows a stateless authentication mechanism - we can implement JWT authentication entirely stateless without storing session information on the server side.

Read more about JWT at https://jwt.io/introduction

When should we use JSON Web Tokens?

We can use JWT in the below useful scenarios:

Authorization: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign-On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.

Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed.

What is the JSON Web Token Structure?

Basically, JSON Web Tokens consist of three parts separated by dots (.), which are:
  1. Header
  2. Payload
  3. Signature
Therefore, a JWT typically looks like the following.
xxxxx.yyyyy.zzzzz
Let's break down the different parts and understand each of them.

Header

The header typically consists of two parts: 
1. The type of the token, which is JWT
2. The signing algorithm being used, such as HMAC SHA256 or RSA.

For example:

{
  "alg": "HS256",
  "typ": "JWT"
}
Then, this JSON is Base64Url encoded to form the first part of the JWT.

Payload

The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data
An example payload could be:
{
  "sub": "123",
  "name": "Ramesh"
}
The payload is then Base64Url encoded to form the second part of the JSON Web Token.

Signature

To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

For example, if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:
HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)
The signature is used to verify the message wasn't changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.

Use below token and explore yourself using the JWT debugger tool - https://jwt.io/#debugger-io
eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbkBnbWFpbC5jb20iLCJpYXQiOjE2MTY1NjY5NDksImV4cCI6MTYxNzE3MTc0OX0.RVggbCFH2VGRZw9-pptLl7EKgp2BYxfOw8DXoE22MVTGJUBer600dx49UZyd-TeFvBPflOKH9Rbi8SOvzYmIAA


How do JSON Web Tokens work?

Let's understand how JWT works with a sequence diagram.
In the JWT authentication process, the front end (client) firstly sends some credentials to authenticate itself (username and password in our case, since we're working on a web application) to the server.

The server (the Spring app in our case) then checks those credentials, and if they are valid, it generates a JWT and returns it to the client.

After this step client has to provide this token in the request’s Authorization header in the “Bearer TOKEN” form. The back end will check the validity of this token and authorize or reject requests. The token may also store user roles and authorize the requests based on the given authorities.

References

Comments