How it works
The explanation, with examples you can run.
Server-side sessions
The server creates a long random ID (at least 128 bits from a secure random generator), stores the session data against it, and sends it as a cookie with HttpOnly, Secure and SameSite. Each request looks the session up. Logging out, or kicking a stolen session, is deleting it. Issue a new ID at login and on privilege changes, so an ID planted before login (session fixation) is worthless afterwards, and expire sessions after idle time and an absolute maximum.
For a classic web app with one backend, this is usually the best choice.
JWTs
A JWT is three base64url-encoded parts joined by dots: header, payload (the claims), signature. Any service holding the issuer's key can verify it locally, which is useful across many services or between organisations. That is also its main weakness: a valid token stays valid until exp, unless every verifier also checks a revocation list.
It is easy to forget that the payload is readable by anyone:
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
JWT pitfalls (RFC 8725)
- Trusting the header's
alg. Pin the algorithm (or small set) you accept. Rejectnone. Never let a token switch from an asymmetric algorithm to HS256, where an attacker could sign with your public key as the HMAC secret. - Skipping claim checks. Verify
exp,nbf,issandaud. A token issued for another service (aud) must not work on yours. - Weak HMAC secrets. HS256 secrets must be long and random; short ones can be brute-forced offline from a single token.
- Secrets in the payload. Don't put personal data you wouldn't show the user, and never passwords or keys.
- Long lifetimes. Keep access tokens short (minutes), and use a refresh token that the server can revoke.
- Storing tokens in
localStorage. Any XSS on your page can read them. AnHttpOnlycookie can't be read by script, though cookies bring CSRF considerations instead.
Choosing
Use server-side sessions for a browser talking to its own backend. Use short-lived signed tokens when many independent services must verify identity without a shared session store, or across organisations, which is the case OAuth and OpenID Connect handle in the next topic.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.