JWT
A JSON Web Token (JWT, pronounced “jot”) is a compact, signed string that carries a small JSON document of claims, such as “this is user 42, issued by auth.shop.com, valid until 15:00, allowed to read orders”. Anyone can read a JWT, because it is only Base64URL-encoded, but nobody can change it without breaking the signature. That lets an API trust the token by checking the signature locally, without calling the login server on every request. JWTs are the usual format for OAuth 2.0 access tokens and OpenID Connect ID tokens.
Inside a token, and how it is verified
- A JWT is three Base64URL strings joined by dots: header, payload, signature. This one arrived in an
Authorization: Bearerheader. - The header says how it was signed (alg) and which key was used (kid). Decoding needs no secret.
- The payload holds the claims: who (sub), issued by whom (iss), for which API (aud), until when (exp), with what permissions. It is readable by anyone, so no secrets here.
- The signature covers the first two parts. Only the auth server holds the private key that made it.
- The API fetches the matching public key from the issuer's JWKS endpoint, checks the signature, then the claims. Keys are cached, so there is no database or network call per request.
- Change a single character of the payload and the signature no longer matches. The token is rejected.
Signing algorithms
| Algorithm | Key | Who can verify | Use when |
|---|---|---|---|
| HS256 (HMAC) | One shared secret | Anyone with the secret, who could also mint tokens | A single service signs and verifies its own tokens |
| RS256 (RSA) | Private key signs, public key verifies | Anyone with the public key | Many APIs trust one identity provider. The common default. |
| ES256 / EdDSA | Elliptic-curve key pair | Same as RS256 | Same as RS256, with smaller keys and signatures |
Claims to check, every time
- The signature, using an algorithm you chose (an allow-list), never “whatever the header says”.
exp(expired?) andnbf(not yet valid?), with a small clock-skew allowance.iss: issued by the identity provider you trust.aud: meant for this API, not a token issued for another service.- Scopes or roles, for the specific action.
Use a maintained library (PyJWT, jose / jsonwebtoken in Node, golang-jwt/jwt, Nimbus JOSE in Java) and pass these checks explicitly.
JWT vs server-side sessions
| JWT (self-contained) | Session ID (reference) | |
|---|---|---|
| Verification | Local, no lookup | Lookup in a session store per request |
| Revocation | Hard: valid until exp |
Easy: delete the session |
| Size | Hundreds of bytes to a few KB | ~32 bytes |
| Good for | APIs, service-to-service, many verifiers | One web app with a browser login |
Because a JWT cannot be “logged out”, keep access tokens short-lived (5 to 15 minutes) and use refresh tokens (see OAuth) to get new ones.
Common mistakes
- Accepting
"alg": "none"or letting the token choose between HS256 and RS256. Both have led to complete authentication bypasses. - Putting secrets or personal data in the payload. It is encoded, not encrypted. Use JWE if you really need encryption.
- Long-lived tokens in
localStorage. Any XSS bug can steal them. Prefer short lifetimes; for browser apps, consider HttpOnly cookies or a backend-for-frontend. - Skipping
audchecks, so a token for the reporting API also works on the payments API. - Using JWTs as session storage for large, changing data. Every request then carries kilobytes of stale state.
Learning Resources
- RFC 7519: JSON Web Token and RFC 8725: JWT Best Current Practices
- jwt.io: decoder and library list (decode only tokens you don’t mind sharing)
- JWT Handbook: Auth0, free ebook
- OWASP JSON Web Token Cheat Sheet
- Critical vulnerabilities in JSON Web Token libraries: the classic
algconfusion write-up