Technology Encyclopedia Home >What are the structure and components of JSON Web Token?

What are the structure and components of JSON Web Token?

A JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The structure and components of a JWT are as follows:

Structure

A JWT consists of three parts separated by dots (.):

  1. Header
  2. Payload
  3. Signature

Components

  1. Header:

    • Typically consists of two parts: the type of token (typ) and the signing algorithm used (alg).
    • Example:
      {
        "typ": "JWT",
        "alg": "HS256"
      }
      
  2. Payload:

    • Contains the claims or assertions about the subject (user) and additional metadata.
    • Common claims include iss (issuer), exp (expiration time), sub (subject), aud (audience), and custom claims.
    • Example:
      {
        "sub": "1234567890",
        "name": "John Doe",
        "iat": 1516239022,
        "exp": 1516242622
      }
      
  3. Signature:

    • Created by encoding the header and payload with a secret key using the algorithm specified in the header.
    • Ensures the integrity and authenticity of the token.
    • Example (signature is a base64url-encoded string):
      HMACSHA256(
        base64UrlEncode(header) + "." +
        base64UrlEncode(payload),
        secret
      )
      

Example of a Complete JWT

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Usage in Cloud Services

JWTs are widely used in various cloud services for authentication and authorization purposes. For instance, Tencent Cloud provides services like Tencent Cloud Identity and Access Management (IAM), which can integrate with JWT for secure access control.

By understanding the structure and components of JWT, developers can effectively implement secure authentication mechanisms in their applications.