software-engineer-blog logoSoftware Engineer Blog

Module 12 · Security

Unit 45 of 49

Unit 45 · Module 12 · Security

Sessions, cookies and tokens — how an app remembers you

Every "logged in" is one of two bets: the server remembers, or the token proves it.

Unit 45 of the free 49-unit computer-science course, in security. 4 topics to watch or read, 3 interview questions answered in full and a short self-check.

Watch or read

4 topics make up this unit. Take each one whichever way suits you, then answer the questions below.

Sessions vs JWT

After you log in, every request has to prove it's still you. There are two ways to do it: let the server remember you (a session) or carry the proof yourself (a JWT). One is a coat-check ticket, the other a signed wristband — and the difference decides how you revoke access, scale, and store state. Here's the real trade-off, with the code in Python, TypeScript, and Java.

Read

Access token vs refresh token

Why one login gives you two tokens: access tokens are short-lived and sent everywhere (small blast radius if stolen), refresh tokens are long-lived and server-tracked (enabling real logout). Together they escape the single-token security-vs-UX trade-off.

ReelRead

OAuth vs JWT

Read

Bearer tokens in the Authorization header

Authorization: Bearer eyJhbGciOi… is not one thing, it is two — a scheme and a credential. Here is what the word Bearer actually promises, why the token comes in exactly two flavours (opaque and JWT) and what that choice costs you, why OAuth 2.0 is not a third flavour, and the three pieces of code that issue it, send it and verify it.

ReelRead

Interview questions this unit unlocks

Asked out loud, answered out loud. Read the answer, then say it in your own words.

Sessions or JWTs — what is the real trade-off?

Revocation against lookup. A session id is a meaningless handle; the server holds the state, so signing someone out is deleting a row, at the cost of a store lookup on every request. A JWT carries signed claims, so any service can verify it with no shared state — and nothing can un-issue it before it expires. Everything else, including the "stateless scales better" argument, is smaller than that one difference.

The pattern that resolves it: a short-lived access token (minutes) plus a long-lived refresh token that IS checked against a store. You get stateless verification and a revocation point.

What is in the Authorization header, and what does "Bearer" mean?

A scheme and a credential: `Authorization: Bearer <token>`. Bearer means exactly what it says — whoever bears the token is treated as the subject, with nothing binding it to a device, a key or a client. That is why it must only ever travel over TLS, why it belongs in a header rather than a URL that lands in logs and referrers, and why its lifetime should be short.

How should a cookie holding a session be configured?

HttpOnly, so a cross-site script cannot read it. Secure, so it never leaves over plain HTTP. SameSite=Lax or Strict, which is what stops a third-party page from making an authenticated request on the user's behalf. Scope the Path and Domain as narrowly as the app allows, and give it an expiry rather than leaving it as a session cookie the browser may restore.

SameSite=Lax is a large part of why CSRF is less common than it was — but it is a default, not a substitute for a token on state-changing requests.

Self-check — 3 questions

Answer alone, at 2am, with no interviewer in the room.

Part of Everything You Need to Know About Computer Science.