Cryptography / JSON Web Tokens (JWT)

JSON Web Tokens (JWT)

What the three dot-separated parts of a JSON Web Token are, why anyone can read the payload but nobody can change it unnoticed, and the mistakes that undo that.

Beginner7 steps~6 min
Header — says how the token was signed
Payload — the claims; readable by anyone
Signature — proves the other two were not changed
Secret key — known only to the server
// THE SHORT VERSION

In 30 seconds

A JWT is a small signed note a server gives you after login. It has three parts joined by dots: a header, a payload of claims such as who you are and when the token expires, and a signature.

The first two parts are only encoded, so anyone can read them. The signature, made with a key only the server knows, is what stops anyone changing them.

So a JWT proves, but does not hide. Never put secrets in it, always set an expiry, and never let the token choose how it is verified.

JSON Web Tokens (JWT), step by step

STEPS01 / 07

This lesson uses HS256, where one shared secret both signs and verifies. RS256 and ES256 sign with a private key and verify with a public one. Encrypted tokens (JWE) are a separate format.

STEP 01 / 07 A signed note in three parts
THE MATH
header.payload.signature eyJhbG… . eyJzdW… . UwABLX…
  1. Step 1: A signed note in three parts. After you log in, the server hands your browser a token and the browser sends it back with every request. A JSON Web Token is that note: three strings joined by dots. The server can check the note itself, so it need not keep a list of who is logged in.
  2. Step 2: Encoded, not encrypted. Header and payload are plain JSON written in base64url, an alphabet of letters, digits, - and _ that is safe in URLs and headers. It is an encoding, not a lock: anyone who holds the token can decode both parts without any key.
  3. Step 3: The payload carries claims. Each entry in the payload is a claim: a statement about the user or about the token. A few names are standard, such as sub (who), iat (issued at) and exp (expires), with times counted in seconds since 1970. Anything else, like role, is the application’s own.
  4. Step 4: Sign with a secret key. The server feeds the first two parts and a secret key into HMAC-SHA256, a keyed fingerprint function, and appends the result as the third part. Only someone who knows the key can produce a signature that fits this exact header and payload.
  5. Step 5: Verify, and catch tampering. On every request the server recomputes the signature from the header and payload it received and compares. If someone edits the payload to say "role":"admin", the recomputed value no longer equals the one on the token, and the request is refused.
  6. Step 6: Tokens expire. A valid signature is not enough: the server also checks the exp claim and refuses tokens past it. The server keeps no record it could delete, so a stolen token works until it expires. Short lifetimes limit the damage; cancelling a token early needs extra machinery such as a deny-list.
  7. Step 7: Three common mistakes. Most JWT failures are in how tokens are checked or filled. A server that lets the token’s own header choose the algorithm may accept "none", meaning no signature at all. Passwords or keys in the payload are readable by anyone. And a token with no expiry is valid for ever once stolen.
VALUES SO FAR
parts3, dot-joinedformat
alg····header
sub····claim
role····claim
iat····claim
exp····claim
key····secret
sig····public
lifetime····rule
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

// AFTER

Learn next

Nothing builds on this one yet. See every cryptography concept.

Spotted a mistake on this page?