CS Fundamentals Trainer · Guides

Authentication and sessions: passwords, cookies, tokens and OAuth without the folklore

How to store passwords, what a session is and how session cookies are protected, when a signed token (JWT) is a good idea and when it is not, how OAuth 2.0 and OpenID Connect divide the work between authorization and login, multi-factor authentication and passkeys, and the mistakes that show up in most breach reports.

5 min read

Authentication answers "who is this?". Authorization answers "what may they do?". They are separate problems, and mixing them up is behind many bugs. This guide covers the first one: how a system recognises a user and keeps recognising them for the next request.

Passwords

Never store a password; store the output of a password hashing function designed to be slow and memory-hungry: Argon2id (first choice), scrypt or bcrypt. A general-purpose hash such as SHA-256 is far too fast: a GPU can try billions per second.

Sessions

HTTP has no memory, so after a login the server issues something the browser sends back each time. The classic design is the server-side session: a long random identifier (at least 128 bits from a secure random generator) goes into a cookie, and the server looks it up in a store (database, Redis) to find the user. Because the state is on the server, you can revoke a session instantly.

Cookie attributes that matter:

Rules in the flow: issue a new session id on login (otherwise an attacker who planted an id before login, called session fixation, shares it), destroy the session on logout, and ask for the password again before sensitive actions (change email, add a payment method).

Tokens and JWTs

A JSON Web Token is a signed (and sometimes encrypted) JSON document: header.payload.signature. The server can check a signature without a lookup, which is why JWTs are popular for APIs and across services. The trade-offs are real:

Use JWTs when several independent services must validate identity without calling a central store; otherwise a server-side session is the boring, safe choice.

OAuth 2.0 and OpenID Connect

OAuth 2.0 is about delegated authorization: letting an application act on your behalf at another service without handing it your password. It gives the app an access token with limited scopes. It does not say who you are.

OpenID Connect (OIDC) adds an identity layer on top: an ID token (a JWT) that says who logged in, issued by an identity provider. "Sign in with X" is OIDC.

For web and mobile applications, use the authorization code flow with PKCE:

  1. The app sends the browser to the provider with a random state (CSRF protection) and a PKCE code_challenge.
  2. The user signs in at the provider and consents.
  3. The provider redirects back with a one-time code.
  4. The app's server exchanges the code plus the PKCE code_verifier for tokens.
  5. The app validates the ID token (iss, aud, exp, nonce) and creates its own session.

The implicit flow (tokens in the URL fragment) and the password grant are deprecated. Register exact redirect URIs, and never accept an arbitrary redirect_uri.

Multi-factor authentication and passkeys

A second factor defeats the most common attack, reuse of leaked passwords. In order of strength: passkeys/WebAuthn and hardware keys (phishing-resistant, because the credential is bound to the real site's domain), app-based one-time codes (TOTP), then SMS (weakest: SIM swapping, interception). Offer recovery codes, and treat the recovery path as part of the attack surface.

Mistakes that keep appearing

  1. Fast hashes, unsalted hashes, or reversible encryption of passwords.
  2. Session ids that are predictable or are not rotated at login.
  3. Tokens in URLs (they leak through logs and Referer).
  4. Verifying a JWT's signature but not its audience, issuer or expiry.
  5. Different error messages for "no such user" and "wrong password".
  6. No rate limit on login, reset and code-verification endpoints.
  7. Trusting client-supplied identity (a userId in the request body) instead of the session.
  8. Long-lived, never-rotated API keys in code repositories.

Sources

Practise this topic: Security

More guides