$ echo $TOKEN | cut -d. -f2 | base64 -d # RFC 7519

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

$ jwt decode $TOKEN
Three parts, one signature check
eyJhbGciOiJSUzI1NiIs….eyJzdWIiOiJ1c2VyXzQyIiwi….dBjftJeZ4CVP…w9QpYk HEADER PAYLOAD SIGNATURE {"alg": "RS256","typ": "JWT","kid": "2026-01"} {"sub": "user_42","iss": "https://auth.shop.com","aud": "orders-api","exp": 1795000000,"scope": "orders:read"} RS256(b64url(header) + "." +b64url(payload),auth server PRIVATE key) API: public key for kid 2026-01 (JWKS) → signature ✓ → exp ✓ iss ✓ aud ✓ → 200 OK tampered: "sub": "admin" → recomputed signature ≠ token signature → 401
  1. A JWT is three Base64URL strings joined by dots: header, payload, signature. This one arrived in an Authorization: Bearer header.
  2. The header says how it was signed (alg) and which key was used (kid). Decoding needs no secret.
  3. 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.
  4. The signature covers the first two parts. Only the auth server holds the private key that made it.
  5. 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.
  6. 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

  1. The signature, using an algorithm you chose (an allow-list), never “whatever the header says”.
  2. exp (expired?) and nbf (not yet valid?), with a small clock-skew allowance.
  3. iss: issued by the identity provider you trust.
  4. aud: meant for this API, not a token issued for another service.
  5. 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 aud checks, 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

See also: OAuth, HTTPS.