← Guides

JWT Structure, Expiry and Why Decoding Is Not Verification

Three parts, two dots

A JSON Web Token is three base64url strings joined by dots: header.payload.signature. The header names the signing algorithm and token type. The payload carries the claims. The signature is computed over the first two parts using a key the verifier holds.

Base64url, not base64

Base64url uses - and _ instead of + and /, and drops the = padding, so the result is safe inside a URL or HTTP header. Decoding is a pure mechanical transform: the payload is readable by anyone holding the token. Never put secrets in a JWT.

The claims that matter

  • sub — who the token is about
  • iss / aud — who issued it and who it is meant for; a server should reject tokens with the wrong pair
  • exp — expiry time in seconds since the Unix epoch
  • iat — issue time, useful for rejecting tokens minted in the future
  • nbf — “not before”, the earliest moment the token is valid

All three time claims are seconds, not milliseconds. A token with an exp of 1700000000000 is not valid until the year 55845 — a common mistake when an API hands out milliseconds.

Decoding is not verification

Anyone can decode a payload and change it, then re-encode the result. A server must verify the signature with the correct secret or public key, check the algorithm against an allow-list, and validate the claims. Trusting a payload simply because it looks well-formed is how forged tokens get accepted.

Common implementation bugs include accepting alg: none, using the public key as an HMAC secret, skipping the expiry check on the server, and comparing algorithms as strings without normalising case. Client-side expiry checks only improve the user experience; the server remains the authority.

Where to store a token

In a browser, localStorage is readable by any script on the page, so a single XSS bug leaks the token. An HttpOnly, Secure, SameSite cookie is harder to steal but introduces CSRF concerns that need their own defence. Neither choice removes the need for short lifetimes and server-side revocation.

Choosing a signing algorithm

HS256 signs and verifies with the same secret, so every party that can verify a token can also mint one. That is fine for a single service and dangerous for a public client, where the secret would have to ship with the code. RS256 and ES256 use a private key to sign and a public key to verify, which lets an API publish a JWKS endpoint and rotate keys without redistributing secrets. Asymmetric signatures are larger and slower, so high-volume internal traffic often stays on HMAC. Whichever you pick, pin the algorithm server-side and use the kid header to select a key rather than trusting the header’s alg value.

Lifetime, refresh and revocation

A short access token — five to fifteen minutes — limits the damage from a stolen copy, but forcing the user to log in again every ten minutes is not acceptable. The usual pattern pairs a short-lived access token with a longer refresh token that is stored more carefully and rotated on every use; reusing an old refresh token invalidates the whole family and signals theft. Revocation is the weak spot: a stateless token stays valid until it expires, so immediate logout requires a server-side denylist keyed by jti, a token version stored on the user record, or a session table checked on each request. Decide which of these you need before you promise “log out everywhere”.

Size and transmission limits

Every claim is carried on every request. A token with a full name, email, roles, tenant and feature flags can reach several kilobytes, and base64url adds a third on top. Cookies have a practical limit around 4 KB per domain, and some proxies and gateways cap header size well below that. Keep the payload to what the resource server actually needs, fetch the rest from an endpoint, and remember that a long token in a URL can end up in logs, referrers and browser history.

A short debugging checklist

  • Verify the signature with the key the kid names, not with the first key in the set.
  • Check exp and nbf with a small clock-skew allowance, not an unlimited one.
  • Confirm iss and aud match your own values exactly, including case.
  • Reject tokens whose algorithm is not on your list, before any key is loaded.
  • Log the failure reason server-side; return a generic error to the client.

To inspect a token during debugging, paste it into our JWT decoder. It splits the header and payload, converts the time claims to readable dates, and flags whether the token has expired — all locally in your browser, with a clear reminder that decoding does not verify the signature.

Try it yourself: open the JWT Decoder →