Contents

JWT

JSON Web Token (JWT) are a popular choice for securing APIs and web applications. However, they’re often misunderstood or misused. Draw the three-segment structure and the use/don’t-use line before adopting it.

What is a JWT?

A JWT (most commonly serialized as a signed JWS) is a Base64URL-encoded three-part string header.payload.signature. The header declares the token type and signing algorithm, the payload carries claims, and the signature guarantees integrity and issuer authenticity.

Key boundary: a standard signed JWS does not encrypt the payload—anyone holding the token can Base64URL-decode and read its claims. Never put passwords, secrets, or sensitive PII in a JWS payload; confidentiality comes from HTTPS/TLS in transit (or JWE when encrypted tokens are required).

JWT Structure

Decoded structure of a signed JWT:

{
  "header": {
    "typ": "JWT",
    "alg": "HS256"
  },
  "payload": {
    "sub": "1234567890",
    "iss": "https://auth.example.com",
    "aud": "https://api.example.com",
    "exp": 1795200000
  },
  "signature": "<HMAC-SHA256 signature>"
}

Key distinctions:

  • typ: "JWT" identifies the token media type; Bearer is the HTTP transport scheme (Authorization: Bearer <token>), not a JWT header typ.
  • HS256 uses a symmetric shared secret (HMAC-SHA256); when verifiers should not hold signing keys, use an asymmetric algorithm such as RS256, ES256, or EdDSA.

How JWTs Work and Verification Order

  1. Issuance: After authentication, the issuer signs a JWT with iss, aud, sub, and exp using a pinned algorithm.
  2. Transport: The client sends the token over HTTPS in Authorization: Bearer <token>.
  3. Verification (following RFC 8725):
    • Enforce a server-side allowlist of expected signing algorithms (never trust alg: none or attacker-chosen algorithm switches), then verify the signature.
    • Validate standard claims: exp, nbf, iss (issuer), and aud (audience).
    • A valid signature proves authenticity, not authorization—application logic must still check resource-level permissions for sub.

Advantages of JWTs

  1. Stateless verification: Resource servers can verify token authenticity and expiry without hitting a session store on every request.
  2. Compact: Fits cleanly in HTTP headers, mobile clients, and service-to-service calls.
  3. Tamper-evident: Signatures/MACs detect any modification to header or payload (confidentiality still relies on TLS or JWE).
  4. Extensible claims: Carries standard claims plus small non-sensitive metadata.

Disadvantages of JWTs

  1. Key compromise: If the signing secret or private key leaks, attackers can forge arbitrary valid tokens—rotate keys and guard signing material.
  2. Readable payload: Signed JWS payloads are Base64URL-encoded, not encrypted.
  3. Revocation overhead: Issued tokens remain valid until exp; immediate logout or revocation requires short TTLs plus refresh-token rotation or a revocation list.

When to Use JWTs

JWTs fit stateless identity assertions across services (OAuth 2.0 / OIDC access tokens, microservice auth, mobile APIs). When you need instant server-side revocation or rich mutable session state, server-side sessions are usually simpler and safer.

Conclusion

JWTs are stateless, compact, and tamper-evident, fitting API, web, and mobile auth. Remember that signing ensures integrity rather than confidentiality, and signature verification is separate from authorization.

References