The three segments
A JWT is three Base64url strings joined by dots: header.payload.signature.
- Header — the algorithm and token type:
{"alg":"HS256","typ":"JWT"}. It tells the verifier how the signature was made. - Payload — the claims: who the user is (
sub), who issued the token (iss), when it expires (exp), plus any custom claims like roles or plan tiers. - Signature — the proof that the header and payload were not altered, computed with a secret key or a private key.
Both header and payload are just Base64url-encoded JSON — not encrypted, not obfuscated. Paste any token into the JWT decoder and read it. Anyone with the token — a browser extension, a proxy log, a screenshot — can read the claims too.
Signed, not encrypted
This is the single most misunderstood property of JWTs. The signature makes the token tamper-proof: change one character of the payload and verification fails. It does nothing for confidentiality: the contents are readable by anyone holding the token.
The practical consequences:
- Never put secrets in a JWT payload — passwords, API keys, personal data nobody needs to see.
- Do put identity and authorization claims that the verifier needs to read quickly (
sub,roles,exp). - If the payload must be secret, use encrypted JWTs (JWE) — a different, rarer beast.
How verification actually works
Two families of algorithms:
- HS256 (HMAC + SHA-256): one shared secret signs and verifies. Simple, but every service that verifies must hold the secret — so any one of them can also mint tokens.
- RS256 / ES256 (asymmetric): a private key signs, a public key verifies. Services can check tokens without being able to forge them. This is the standard choice when multiple services consume the same tokens.
The standard claims worth knowing
| Claim | Meaning |
|---|---|
| exp | Expiration time (Unix seconds). Verifiers must reject expired tokens. |
| iat | Issued at — when the token was created. |
| nbf | Not before — token is invalid earlier than this. |
| iss | Issuer — who created the token. |
| aud | Audience — which service the token is intended for. |
| sub | Subject — usually the user ID. |
Check the dates in any token with the decoder — it renders exp/iat/nbf as real dates and flags expired tokens.
The alg=none trap
A famous vulnerability class: some naive libraries accepted a header of {"alg":"none"} with an empty signature — meaning "trust the payload as-is". An attacker edits their role to admin, sets alg to none, and walks in. Well-maintained libraries reject none unless explicitly enabled (it exists for debugging), but the lesson stands: the verifier must pin the algorithm it expects, not take the header's word for it.
Why tokens expire — and why that is good
A JWT cannot be "logged out" the way a server-side session can: it is valid until exp, wherever it travels. That is why real systems use short-lived access tokens (minutes) plus a separate refresh token (longer, stored server-side and revocable). If you see JWTs valid for weeks, the system has no way to revoke a stolen one — a design smell worth flagging.
Storage notes
localStorageis readable by any script on the page — a single XSS hole leaks every token.HttpOnlycookies are not readable by JavaScript, which blunts XSS theft — but need CSRF protection.- There is no universally right answer; the wrong answer is storing a long-lived token where any script can read it.