Introduction

A small, URL-safe way to represent claims that need to be transferred between two parties is with JSON Web Tokens (JWT). The claims in a JWT can be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted because they are encoded as a JSON object that can be used as the plaintext of a JSON Web Encryption (JWE) structure or as the payload of a JSON Web Signature (JWS) structure.

JWTs are frequently utilized in situations involving information exchange and authentication, especially in web applications. Because they are digitally signed, they enable safe information transfer between parties and can be validated and relied upon.

The composition of a JWT

Three components make up a JWT, with dots (.) separating them:

  1. Header
  2. Payload
  3. Signature

Header

The type of the token (JWT) and the signing algorithm (RSA or HMAC SHA256) are the two main components of the header.

An example of a header

{
  "alg": "HS256",
  "typ": "JWT"
}

The following is how it appears when encoded in Base64Url:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9

Payload

The claims are in the payload. Claims are assertions regarding an entity (usually the user) and supplementary information. Three categories of claims exist:

An example of a payload

{
  "sub": "1234567890",
  "name": "Jaimin Shethiya",
  "admin": true,
  "userName": "[email protected]",
  "iat": 1516239022
}

The following is how it appears when encoded in Base64Url:

eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkphaW1pbiBTaGV0aGl5YSIsImFkbWluIjp0cnVlLCJ1c2VyTmFtZSI6ImphaW1pbnNoZXRoaXlhQHlhaG9vLmNvbSIsImlhdCI6MTUxNjIzOTAyMn0

Signature

The encoded header, the encoded payload, a secret, and the header's specified algorithm are required to create the signature portion. For instance, the following is how the signature will be generated if the HMAC SHA256 algorithm is used:

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)

An example of a signature

IiRZjTR4MCZkmmpV1oAZf059p0cZDIHmb8EJyGiwPBc

Last JWT

The three components are concatenated with dots (.) to create the final JWT:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkphaW1pbiBTaGV0aGl5YSIsImFkbWluIjp0cnVlLCJ1c2VyTmFtZSI6ImphaW1pbnNoZXRoaXlhQHlhaG9vLmNvbSIsImlhdCI6MTUxNjIzOTAyMn0.IiRZjTR4MCZkmmpV1oAZf059p0cZDIHmb8EJyGiwPBc

The Operation of JWT

A well-liked technique for safely sending data between parties as a JSON object is JSON Web Tokens (JWT). They are extensively utilized in web applications for information exchange and authentication. The following are some benefits and drawbacks of utilizing JWT:

Benefits of JWT

Negative aspects of JWT

Conclusion

Although JWTs provide a scalable and adaptable solution for information sharing and authentication, they also present a unique set of difficulties. It's critical to apply best practices to guarantee security and balance the benefits and drawbacks according to the particular needs of your application.

We learned the new technique and evolved together.

Happy coding!