JSON Web Tokens: structure, validation, and the mistakes that make them forgeable

What a JWT’s header, payload, and signature contain, how to decode one safely, the validation checks a server must do (algorithm, signature, issuer, audience, expiry), and the classic failures such as alg none and key confusion.

Lesson 30 of 69 · 13 min read · practice only with authorization

Diagram

Validating a token in order

Follow the path. One example at a time.

Step 1 of 4

Decode

Split on dots and Base64URL-decode the header and payload; this proves nothing yet.

Illustration: Decode
$ cut -d. -f2 <<< "$JWT" | base64 -d 2>/dev/null | jq
{ "sub": "ana", "aud": "orders-api", "iss": "https://login.example.org", "exp": 1793700000 }

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Decoding is not verifying

    What fails
    Anyone can read and craft the payload; only the signature proves origin.
    Who is affected
    Every user whose claims the server trusts unchecked.
    What you would notice
    Code that decodes without verify or with verification disabled.
    What you change
    Use the library’s verify path with fixed algorithms.
  2. Algorithm confusion

    What fails
    Accepting alg none, or treating an RSA public key as an HMAC secret, lets tokens be forged.
    Who is affected
    All users of the API.
    What you would notice
    Libraries configured to accept any algorithm in the header.
    What you change
    Pin algorithms per issuer and keep libraries current.
  3. Audience and lifetime

    What fails
    A valid token for one API replayed against another, or a token that never expires, extends access.
    Who is affected
    Users whose tokens are stolen.
    What you would notice
    No aud check, or exp far in the future.
    What you change
    Check aud, keep access tokens short-lived, and use refresh tokens with rotation.

Inspecting a token safely

Decode tokens locally with a command-line tool rather than pasting real tokens into websites. jwt.io is fine for lab tokens; production tokens are credentials.

$ python3 - <<'PY'
import jwt, sys
print(jwt.get_unverified_header(open("token.txt").read().strip()))
PY
{'alg': 'RS256', 'kid': '2026-10-key', 'typ': 'JWT'}

Where tokens live

Tokens in localStorage are readable by any script on the page; HttpOnly cookies are not but need CSRF protection. The bearer tokens in the browser page compares the options.

Revocation

Stateless tokens cannot be revoked individually without a denylist or short lifetimes. Keep access tokens to minutes and revoke refresh tokens server-side.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A server reads the user id from a JWT payload without verifying the signature. What can an attacker do?

Go deeper

Specific techniques, exercises, and glossary cards for this topic.

  1. 01Where a bearer token lives in the browserA token in local storage is readable by any script on the page. Includes the diagram and a defensive fix.