Notes
Factors and MFA
Factors come in three kinds: something you know like a password, something you have like a phone or security key, and something you are like a fingerprint. Multi-factor means two different kinds, not two passwords. The strongest are phishing-resistant: passkeys and hardware keys, in episode twelve. SMS codes beat nothing but can be phished, so prefer app codes or passkeys. MFA is the highest-value control against stolen passwords.
- knowledge: password, PIN
- possession: phone, security key, authenticator app
- inherence: fingerprint, face
- MFA = two different factors, not two passwords
- strongest: a phishing- resistant factor (passkey, security key)
- SMS codes: better than nothing, but phishable
Server-side sessions
There are two models. The classic one is server-side sessions. On login the server stores the state and hands the client a random session identifier in a cookie. Each request, the server looks it up. The advantage is control: logout and revocation are instant, because you delete the session server-side. The cost is that the server, or a shared cache like Redis, holds the state. For most web apps this is the right default, and simpler to reason about than tokens.
- on login, server stores session state, gives the client a random session ID in a cookie
- each request: server looks up the ID
- logout and revocation are instant: delete server-side
- state lives server-side (store or cache)
- great default for web apps
Stateless tokens
The other model is stateless tokens, usually JWTs. On login the server issues a signed token containing claims about the user, and the client sends it on each request. The server verifies the signature without any lookup, which scales nicely across many services and APIs with no shared session store. The cost is revocation: a valid token works until it expires, so you cannot instantly log someone out. The standard answer is a short-lived access token plus a longer-lived refresh token you can revoke.
- on login, server issues a signed token (JWT) with claims; client sends it back
- server verifies the signature, no lookup needed
- scales across services without shared session state
- cost: hard to revoke before expiry
- fix: short-lived access + refresh token
Configure them right
Either model fails if handled badly. A session cookie must be HttpOnly so scripts cannot read it, Secure so it only goes over HTTPS, and SameSite to limit cross-site sending, with a random identifier of at least 128 bits, rotated on login to prevent fixation. A JWT must have its signature and algorithm verified, the algorithm pinned and none rejected, and its expiry, issuer and audience checked. And mind where a token is stored, because script storage is readable by any cross-site scripting bug.
- session cookie: HttpOnly, Secure, SameSite, sensible expiry, random 128-bit ID
- rotate the session ID on login (prevents fixation)
- JWT: verify signature AND alg; check exp, iss, aud; short lifetime
- store tokens carefully; avoid localStorage for sensitive apps (XSS steals it)
The flows
The flows need as much care as the mechanism. Login should rate-limit and lock out repeated failures, and return an identical response for a wrong username and a wrong password, so it does not reveal which accounts exist. Check passwords against breached lists. Logout must destroy the server session or revoke the token, not just redirect. Enforce idle and absolute timeouts. And require step-up auth, re-entering a password or MFA, before sensitive actions like changing an email or making a payment.
- login: rate-limit, lockout, identical response for bad user vs bad password
- breached-password check
- logout: destroy session or revoke the token
- idle and absolute timeouts
- step-up auth for sensitive actions (password or MFA)
Account recovery
Account recovery is often the weakest link, because a reset flow that bypasses MFA undoes your login security. Do it properly: email a single-use token that expires within about fifteen minutes, long and random, and store only its hash so a database leak does not expose live tokens. Do not reveal whether an email has an account. Invalidate sessions when the password changes. And recovery should be as strong as login; if an attacker resets a password with just an email, MFA bought you little.
- email a single-use token that expires quickly (15 min)
- random and long; store only its hash
- do not reveal whether an email exists
- invalidate sessions on reset
- recovery as strong as login: keep MFA where possible
- rate-limit reset requests
Do not build your own
The last principle is the most practical: do not build your own identity system unless identity is your product. It has countless edge cases, MFA, recovery, lockout, federation, auditing, each a chance to get it wrong. Use a mature identity provider such as Okta, Entra, Cognito or self-hosted Keycloak, or a vetted framework like Spring Security, and build only what is unique to your app. Never store passwords in plaintext or invent your own token crypto.
- identity has countless edge cases: MFA, recovery, lockout, federation, audit
- use a provider (Okta, Entra, Cognito, Keycloak) or a vetted framework
- build only what is unique to you
- never store plaintext passwords or roll your own crypto
Common mistakes
Mistakes. Passwords only, no MFA. A session identifier not rotated on login, allowing fixation. A JWT accepted with the algorithm none or an unverified signature. No real logout or revocation. A reset flow that bypasses MFA or reveals which emails have accounts. Tokens in script-readable storage in apps prone to cross-site scripting. And a whole identity system built from scratch when a proven one exists. Each turns up regularly in real assessments.
- passwords only, no MFA
- session ID not rotated on login (fixation)
- JWT alg:none or unverified signature
- no real logout/revocation
- reset bypasses MFA or reveals accounts
- tokens in localStorage in XSS-prone apps
- home-made identity
Say it like this
In the interview: for a web app I default to server-side sessions, because revocation is instant, with an HttpOnly, Secure, SameSite cookie, rotated on login. For stateless APIs I use JWTs, with short-lived access tokens, revocable refresh tokens, and strict checks of the algorithm and the expiry, issuer and audience claims. I require MFA, build a recovery flow that does not bypass it, and use a proven identity provider rather than my own. Passwords are hashed with Argon2 or bcrypt.
- "Server sessions for web apps: instant revocation, HttpOnly
- Secure SameSite cookie, rotated on login. JWTs for stateless APIs: short-lived
- access plus revocable refresh, verified alg and claims. MFA
- everywhere, strong recovery, and a proven identity
- provider rather than my own."