ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
J
computing · 3 min read

Jwt

JSON Web Token (JWT) is an open, industry-standard protocol (RFC 7519) for securely transmitting information between parties as a JSON object. It is compact,…

Overview

JSON Web Token (JWT) is an open, industry-standard protocol (RFC 7519) for securely transmitting information between parties as a JSON object. It is compact, URL-safe, and designed to be used as a token for authentication and information exchange in distributed systems. JWTs are widely adopted in web applications, APIs, and identity management systems due to their simplicity and interoperability. They enable stateless authentication mechanisms, reducing server-side storage requirements and enabling scalability.

JWTs encode claims—assertions about an entity such as a user or service—into a structured format. These claims are digitally signed or encrypted to ensure integrity and, optionally, confidentiality. The protocol supports multiple signing algorithms, including HMAC (symmetric key) and RSA/ECDSA (asymmetric key), allowing flexibility in security configurations. JWTs are self-contained, meaning all necessary information is embedded within the token itself, eliminating the need for repeated database queries during request processing.

Structure

A JWT consists of three components: a header, a payload, and a signature, concatenated in the form header.payload.signature. Each part is Base64Url-encoded for compactness and compatibility with URL-based protocols.

  1. Header: Defines the cryptographic algorithm (alg) and token type (typ). For example:
   { "alg": "HS256", "typ": "JWT" }  

This header specifies the HMAC SHA-256 algorithm for signing.

  1. Payload (Claims Set): Contains claims, which are key-value pairs of information. Claims are categorized as:
  • Registered: Standardized (e.g., iss for issuer, exp for expiration time).
  • Public: Vendor-specific or application-defined.
  • Private: Custom claims agreed upon by parties.

Example payload:

   { "sub": "1234567890", "username": "johndoe", "exp": 1516239022 }  
  1. Signature: Generated by applying the header’s algorithm to the encoded header and payload using a secret key (HMAC) or private key (RSA/ECDSA). For instance, using HMAC SHA-256:

signature = HMACSHA256(base64UrlEncode(header)+"."+base64UrlEncode(payload), secret_key)

Together, these components form a token that can be validated by the recipient using the public key (asymmetric) or shared secret (symmetric).

Usage

JWTs are primarily used for authentication and authorization. When a user logs in, a server generates a JWT containing claims like user identity and permissions. The client stores this token (e.g., in local storage or a cookie) and includes it in subsequent requests, typically as an Authorization header with the Bearer scheme:

Authorization: Bearer <token>  

Key use cases include:

  • Single Sign-On (SSO): Enables cross-domain authentication by sharing a JWT between services.
  • API Authentication: Validates client identity in microservices architectures without maintaining session state.
  • Information Exchange: Securely transmits non-sensitive data between systems, such as user preferences or entitlements.

JWTs are also integrated with OAuth 2.0 and OpenID Connect, serving as access and ID tokens. For example, an OAuth 2.0 authorization server may issue a JWT access token to grant API access.

Security Considerations

While JWTs provide robust security features, improper implementation can introduce vulnerabilities:

  1. Signature Validation: Always verify the token’s signature to prevent tampering. Failing to enforce algorithm constraints (e.g., rejecting none algorithms) allows attackers to forge unsigned tokens.
  2. Key Management: Symmetric keys (HS256) and private keys (RS256) must be safeguarded against exposure. Key rotation mitigates risks from compromised keys.
  3. Token Lifetime: Short-lived tokens reduce the impact of theft. Refresh tokens may be used to issue new access tokens without reauthenticating.
  4. Claim Validation: Recipients must check critical claims like exp (expiration) and iss (issuer) to prevent token misuse.
  5. Storage Risks: Client-side storage (e.g., local storage) can expose tokens to cross-site scripting (XSS) attacks. Secure, HttpOnly cookies are often preferred for web applications.

Encryption (JWE) is optional and typically reserved for sensitive payloads. Most JWT implementations prioritize signing over encryption, as confidentiality is usually ensured by HTTPS.

Standards and Implementations

JWT is defined by RFC 7519 and part of the JSON Web Signature (JWS) and JSON Web Encryption (JWE) family of standards. Additional specifications include:

  • RFC 7515 (JWS): Defines digital signatures for JWTs.
  • RFC 7516 (JWE): Specifies encryption for confidentiality.
  • RFC 7517 (JWK): Describes JSON Web Key format for key exchange.

Libraries and tools for JWT generation and validation are available in most programming languages, including:

  • Python: PyJWT, Authlib
  • JavaScript: jsonwebtoken (Node.js), Firebase Admin SDK
  • Java: Nimbus JOSE + JWT, Spring Security

JWT’s flexibility and standardization have made it a cornerstone of modern authentication systems, though its usage requires careful attention to cryptographic best practices and threat mitigation.

Frequently asked
What is Jwt about?
JSON Web Token (JWT) is an open, industry-standard protocol (RFC 7519) for securely transmitting information between parties as a JSON object. It is compact,…
What should you know about overview?
JSON Web Token (JWT) is an open, industry-standard protocol (RFC 7519) for securely transmitting information between parties as a JSON object. It is compact, URL-safe, and designed to be used as a token for authentication and information exchange in distributed systems. JWTs are widely adopted in web applications,…
What should you know about structure?
A JWT consists of three components: a header, a payload, and a signature, concatenated in the form header.payload.signature . Each part is Base64Url-encoded for compactness and compatibility with URL-based protocols.
What should you know about usage?
JWTs are primarily used for authentication and authorization. When a user logs in, a server generates a JWT containing claims like user identity and permissions. The client stores this token (e.g., in local storage or a cookie) and includes it in subsequent requests, typically as an Authorization header with the…
What should you know about security Considerations?
While JWTs provide robust security features, improper implementation can introduce vulnerabilities:
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room