Skip to content
Back to CurriculumLesson 17 of 30
jwt-tokensIntermediate

JSON Web Tokens (JWT): Header, Payload Claims & The Standard Token Validation Checklist

A JSON Web Token (RFC 7519) is a compact, URL-safe means of representing claims to be transferred between two parties. Encoded as three Base64URL-encoded segments separated by dots (Header.Payload.Signature), safe consumption requires executing an exhaustive 6-point cryptographic and temporal validation checklist.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explainerjwt-tokens
JSON Web Tokens (JWT): Header, Payload Claims & The Standard Token Validation Checklist
Steps:
1. Header (Algorithm)
{
  "alg": "RS256",
  "typ": "JWT"
}
2. Payload (Claims)
{
  "sub": "usr_42",
  "role": "admin",
  "exp": 1735689600
}
3. Digital Signature
RS256(header.payload, privateKey)
Cryptographically Sealed
Step 1 of 6
Auto-stepping (1x)

Step 1: Extract the Bearer token from the incoming HTTP Authorization header.

Security Guarantee: JWT payloads are encoded, not encrypted; never store passwords, unhashed secrets, or PII in JWT claims.
Why This Architectural Concept Exists

Microservices need a stateless way to trust caller identity without querying a central database for every single HTTP request. A digitally signed JWT contains the user ID, tenant ID, and permissions directly in its payload, verifiable purely through mathematics.

Intuitive Mental Model

The Airline Boarding Pass: Printed with your passenger name, flight number, seat, and gate, accompanied by a high-density 2D barcode signed by the airline. Any gate agent or baggage handler can verify the pass offline using the barcode scanner without querying the central reservations database.

Architectural System TopologyProtocol Diagram
[ Base64URL(Header) ] . [ Base64URL(Payload) ] . [ Base64URL(Signature) ]
         │                        │                         │
         ▼                        ▼                         ▼
  { "alg": "RS256",        { "sub": "usr_42",        Digital signature computed over:
    "typ": "JWT",            "iss": "cosmos.app",    SHA256(Header + "." + Payload)
    "kid": "key_2026" }      "aud": "cosmos-api",    using IdP's Private Key
                             "exp": 1735689600 }
                                  │
                                  ▼
[ 6-Point Token Validation Checklist: ]
  1. Verify Alg header against whitelist (Reject 'none'!)
  2. Verify Signature with IdP Public Key
  3. Verify exp (Expiration Time > now)
  4. Verify nbf (Not Before <= now)
  5. Verify iss (Issuer matches trusted IdP URL)
  6. Verify aud (Audience matches our API identifier)
CosmosApp Production Milestone

CosmosApp microservices process incoming requests bearing Bearer JWTs. Every microservice runs the 6-point validation pipeline before routing requests. If a token has expired by even 1 second, or was issued for a different audience (e.g. mobile app rather than internal API), it is immediately rejected with HTTP 401.

Step-by-Step Execution Sequence
01

Step 1: Extract the Bearer token from the incoming HTTP Authorization header.

02

Step 2: Split token string into exactly three segments delimited by periods ('.').

03

Step 3: Decode header; verify that 'alg' is in the strictly permitted whitelist (e.g., ['RS256', 'ES256']).

04

Step 4: Verify the cryptographic signature over (Header + '.' + Payload) using the matching public key.

05

Step 5: Inspect temporal claims: ensure current time < exp (with optional 30s clock skew tolerance) and >= nbf.

06

Step 6: Inspect structural claims: ensure iss matches the expected Identity Provider and aud matches this API service.

Production Architecture Blueprint
import { jwtVerify } from 'jose';

// CosmosApp Comprehensive 6-Point JWT Validator:
export async function validateCosmosJwt(rawToken: string, publicKey: CryptoKey) {
  try {
    const { payload, protectedHeader } = await jwtVerify(rawToken, publicKey, {
      issuer: 'https://auth.cosmos.app',
      audience: 'https://api.cosmos.app',
      algorithms: ['RS256', 'ES256'], // Explicit algorithm whitelist!
      clockTolerance: 30,             // 30 seconds max clock skew
    });

    return { valid: true, user: payload };
  } catch (err: any) {
    return { valid: false, error: err.message };
  }
}
Common Architectural Misconceptions
  • ✕JWTs are encrypted by default (Standard JWTs are JWS - signed, not encrypted; anyone who reads the token can decode the Base64 payload in plaintext).
  • ✕Valid signature means the token is trusted (Signature only proves integrity; you must also validate exp, iss, and aud).

Key Architectural Takeaways

  • ✓JWT payloads are encoded, not encrypted; never store passwords, unhashed secrets, or PII in JWT claims.
  • ✓Always validate all standard claims: alg, signature, exp, nbf, iss, and aud.
  • ✓Keep access token expiration windows short (10–15 minutes) to mitigate token theft risk.
Common Architectural Pitfall

Decoding a JWT payload using jsonwebtoken.decode() or atob() without verifying the signature and claims.

Production Best Practice Fix

Always use a cryptographic verification library (jwtVerify) that checks the signature before trusting claims.

Interactive Knowledge Check

Can an attacker read sensitive data (such as an email address or role) stored inside a standard signed JWT (JWS)?