Data Forest logo
Home page  /  Glossary / 
JWT (JSON Web Tokens)

JWT (JSON Web Tokens)

JSON Web Tokens (JWT) are an open standard (RFC 7519) for securely transmitting information between parties as a JSON object. This token format is compact, URL-safe, and can be verified and trusted because it is digitally signed. JWTs are widely used in authentication and information exchange scenarios, especially in modern web applications and APIs.

Structure of JWT

A JWT is composed of three parts: the header, the payload, and the signature. These parts are encoded in Base64Url format and concatenated with periods (`.`) as separators, resulting in a string that looks like this:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
  1. Header
    The header typically consists of two parts:
    • alg: The signing algorithm being used, such as HMAC SHA256 or RSA.
    • typ: The type of token, which is usually JWT.

      An example header in JSON format looks like this:
json
{
  "alg": "HS256",
  "typ": "JWT"
}
  1. Payload
    The payload contains the claims, which are statements about an entity (typically the user) and additional metadata. Claims can be categorized as:
    • Registered Claims: These are a set of predefined claims that are not mandatory but recommended, such as `iss` (issuer), `exp` (expiration time), `sub` (subject), and `aud` (audience).
    • Public Claims: These can be defined at will and should be registered in the IANA JSON Web Token Registry or be collision-resistant.
    • Private Claims: These are custom claims created to share information between parties that agree on using them.

      An example payload might look like this:
json
{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
}
  1. Signature
    To create the signature part, you need to take the encoded header, the encoded payload, a secret, and the algorithm specified in the header. The resulting string is a unique identifier for the token. For example, if you are using HMAC SHA256, the signature can be created as follows:
HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)

How JWT Works

  1. Authentication: When a user successfully logs in using their credentials, the server generates a JWT that encodes the user’s information and sends it back to the client. The client can then store this token (usually in local storage or a cookie) and send it along with subsequent requests, typically in the `Authorization` header, like so:    
Authorization: Bearer <token>
  1. Authorization: When the server receives a request with a JWT, it verifies the token’s signature using the secret key or public key, depending on the algorithm used. If the token is valid, the server can trust the claims it contains and grant access to the user’s requested resource.
  2. Session Management: Unlike traditional sessions that require server-side storage, JWTs are stateless. This means that the server does not need to keep track of user sessions, as all necessary information is stored in the token itself. This can significantly improve scalability in applications with many users.

Advantages of Using JWT

  1. Compact: JWTs are compact in size, making them easy to transmit via URL, POST parameters, or HTTP headers.  
  2. Self-Contained: JWTs contain all the information needed to authenticate a user, reducing the need for multiple database queries.  
  3. Cross-Domain: Being a standard, JWTs can be used across different domains and applications, making them suitable for microservices architectures.
  4. Flexibility: They can contain custom claims that allow the implementation of various authorization mechanisms, accommodating unique application requirements.

Mathematical Representation of JWT Claims

Claims in a JWT can represent various properties that can be quantified. For example, if we want to analyze token usage in an application, we can define:

  • `N`: the number of tokens issued over a period.
  • `A`: the average time to expiration of these tokens.
  • `U`: the number of successful authentications using these tokens.

The effectiveness of JWTs in an application can be represented by:

E = N / (A * U)

In this formula, `E` signifies the efficiency of the JWT mechanism in supporting user authentications relative to the number of tokens issued and their lifetimes.

JWTs are particularly prevalent in single-page applications (SPAs) and mobile applications due to their ability to simplify the authentication flow. They are also useful in APIs where stateless authentication is crucial for maintaining high performance and scalability.

Many modern frameworks and libraries, including those in Node.js (e.g., Express with the `jsonwebtoken` package) and front-end frameworks (e.g., React, Angular), support JWT out of the box, making integration straightforward for developers.

Furthermore, JWTs have become an essential component in the implementation of OAuth 2.0 and OpenID Connect, which are widely adopted standards for authorization and authentication in modern web applications. Their versatility, performance benefits, and self-contained nature have made them a popular choice for managing secure communications across various platforms.

In summary, JSON Web Tokens (JWT) are a robust and versatile method for securely transmitting information between parties in a web application. Their structure, which includes a header, payload, and signature, allows for compact and efficient data representation, making them ideal for authentication and authorization in modern applications. Through their ability to maintain state and share information across domains, JWTs have established themselves as a critical component of contemporary web security practices.

Web Applications
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Latest publications

All publications
Article preview
January 20, 2025
15 min

Corporate Automation: Swapping Excel Chaos for Smart AI Systems

Acticle preview
January 14, 2025
12 min

Digital Transformation Market: AI-Driven Evolution

Article preview
January 7, 2025
17 min

Digital Transformation Tools: The Tech Heart of Business Evolution

All publications
top arrow icon