Understanding JSON Web Tokens (JWT)

Understanding JSON Web Tokens (JWT)

November 30, 2024

authenticationjwt

JSON Web Tokens (JWT) are a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted. This document delves into the structure, usage, and benefits of JWTs in modern web applications.

napkin-selection%20(10).png

What is a JSON Web Token?

A JSON Web Token is a string consisting of three parts: a header, a payload, and a signature. These parts are separated by dots (.), and the overall structure looks like this:

1eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
2

1. Header

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

Example of a header:

1{
2  "alg": "HS256",
3  "typ": "JWT"
4}
5

2. Payload

The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims.

Example of a payload:

1{
2  "sub": "1234567890",
3  "name": "John Doe",
4  "iat": 1516239022
5}
6

3. Signature

To create the signature part, you take the encoded header, the encoded payload, a secret, and the algorithm specified in the header. For example, if you are using the HMAC SHA256 algorithm, the signature will be created as follows:

1HMACSHA256(
2  base64UrlEncode(header) + "." +
3  base64UrlEncode(payload),
4  secret)
5

How JWTs are Used

JWTs are commonly used in authentication and information exchange. When a user logs in, a JWT is generated and sent to the client. The client stores this token (usually in local storage or cookies) and includes it in the Authorization header of subsequent requests to access protected routes or resources.

Authentication

  1. User logs in with credentials.
  2. Server validates credentials and generates a JWT.
  3. Server sends the JWT back to the client.
  4. Client stores the JWT and includes it in the Authorization header for future requests.

napkin-selection%20(9).png

Information Exchange

JWTs can also be used to transmit information securely between parties. Since the information is signed, the recipient can verify that the sender is who it claims to be and that the message wasn't changed along the way.

Benefits of Using JWTs

  • Compact: JWTs are small in size, making them easy to pass in URLs, POST parameters, or HTTP headers.
  • Self-contained: They contain all the necessary information about the user, reducing the need for database lookups.
  • Secure: JWTs can be signed and encrypted, ensuring data integrity and confidentiality.
  • Cross-domain: They can be used across different domains, making them suitable for microservices architecture.

napkin-selection%20(8).png

Conclusion

JSON Web Tokens provide a powerful and flexible way to handle authentication and information exchange in web applications. Their structure allows for secure and efficient communication between parties, making them a popular choice in modern development practices. Understanding how to implement and utilize JWTs can significantly enhance the security and performance of applications.

Related Posts

Handling JWT Tokens in Google OAuth 2.0

Read more