Three questions, three systems
| Question | Name | Typical answer |
|---|---|---|
| Who are you? | Authentication (AuthN) | Password + OTP, passkey, Google sign-in |
| What may you do? | Authorization (AuthZ) | Roles, policies, relationships |
| What did you do? | Audit | Immutable log of sensitive actions |
The HTTP codes follow the same split: 401 Unauthorized means "I don't know who you are" (missing or invalid credentials); 403 Forbidden means "I know who you are, and you may not do this".
Keep them separate in your design. An identity provider (IdP) authenticates and issues tokens; each service authorizes using the claims in those tokens plus its own rules; everything sensitive is audited.
Passwords, done right (or avoided)
If you store passwords:
- Never store them in plain text or with a fast hash (MD5, SHA-256). Attackers with a GPU try billions of fast hashes per second.
- Use a slow, salted, memory-hard hash: Argon2id (preferred), scrypt or bcrypt. Tune it to take ~50–250 ms per hash on your servers.
- Each user gets a unique random salt (the libraries do this for you), so identical passwords produce different hashes.
- Check new passwords against lists of breached passwords; don't force weird composition rules.
- Rate-limit login per account and per IP, and add MFA.
Better still, avoid passwords: "Sign in with Google/Apple", one-time codes by email or SMS (with strict rate limits), or passkeys (WebAuthn), where the user's device holds a private key and proves possession with a fingerprint or face. Passkeys can't be phished or reused across sites, because there's no shared secret.
Sessions vs tokens
After login, the user needs to prove who they are on every request without sending the password again.
| Server-side session | Self-contained token (JWT) | |
|---|---|---|
| What the client holds | Random session ID | Signed claims: sub, exp, roles… |
| Validation | Look up in a session store (Redis) | Verify signature locally; no lookup |
| Revocation | Delete the session: instant | Hard: valid until it expires |
| Scaling | Needs a shared, fast store | Stateless; great across many services |
| Size | Tiny cookie | Hundreds of bytes to kilobytes |
| Best for | Browser apps on your own domain | APIs, mobile apps, service-to-service |
The standard compromise: short access tokens + refresh tokens
- Access token: a JWT valid for ~5–15 minutes, sent with every API call, verified locally by each service.
- Refresh token: long-lived (days to months), opaque, stored securely, used only at the auth server to get new access tokens. It's checked against a database, so it can be revoked instantly.
- Rotation: each refresh returns a new refresh token and invalidates the old one. If an old one is ever reused, assume theft and revoke the whole chain.
Revocation latency for access tokens is then at most their lifetime. For "log out everywhere now" or a banned user, keep a small deny list of token IDs (or a per-user "tokens issued before T are invalid" timestamp) in a fast cache.
Putting secrets or personal data inside a JWT. A JWT is signed, not encrypted: anyone holding it can base64-decode and read every claim. Put only an ID and coarse claims in it. And always verify the signature algorithm on the server (never accept alg: none), the issuer, the audience and the expiry.
Storing tokens in the browser
- HttpOnly, Secure, SameSite cookies can't be read by JavaScript, so an XSS bug can't steal them. Add CSRF protection (SameSite=Lax or Strict plus anti-CSRF tokens for state-changing requests).
- localStorage is readable by any script on the page; a single XSS leaks the token. Avoid it for long-lived tokens.
- Many teams use a backend-for-frontend (BFF): the browser holds only a session cookie; the BFF holds the OAuth tokens and calls APIs on the user's behalf.
OAuth 2.0 and OpenID Connect
OAuth 2.0 is a protocol for delegated authorization: letting an app act on your behalf at another service without giving it your password. "Let this photo-printing site read my Google Photos" is OAuth. OpenID Connect (OIDC) adds an ID token on top, so the same flow also tells the app who you are: "Sign in with Google" is OIDC.
The flow you should know is the authorization code flow with PKCE:
Key ideas to explain in an interview:
- Scopes limit what the token can do (
photos.read, not "everything"). - The code travels through the browser, but the tokens are fetched directly by the app, so they don't leak through browser history or logs.
- PKCE proves that whoever exchanges the code is whoever started the flow, which stops intercepted codes being used by a malicious app.
- Client credentials flow is for machine-to-machine calls with no user involved.
SAML is the older XML-based single-sign-on standard still common in enterprises; modern B2B products typically support both SAML and OIDC for "Sign in with your company account".