Skip to main content
App basics: HTTP, auth, dataLesson 5 of 11

How Logging In Works (Sessions, JWT, OIDC)

You type a password, or click Log in with Google, and the site knows who you are on every page after that. Follow how: password check → session cookie or JWT → OAuth 2.0 and OpenID Connect → tokens → 401 and 403.

WhoUnknown
CredentialPassword
Response—
BrowserYouWeb appshop.example.comSession storeSession ID → userUser DBPassword hashesIdentity providere.g. GoogleAPIResource serverPOST /login
RequestResponse / reportAppears in a later step

What you will learn

  • Authentication (who you are) versus authorization (what you may do)
  • Why passwords are stored as slow, salted hashes
  • Session cookies versus stateless JWTs, and revoking each
  • The OAuth 2.0 authorization code flow with PKCE, and what OIDC adds
  • ID, access and refresh tokens, and 401 versus 403
Session cookieJWT (self-contained token)
What the client holdsA random ID with no meaning by itselfSigned claims (sub, exp, aud, …) anyone can read
How the server checks itLooks the ID up in the session storeVerifies the signature with a key, no lookup
Revoking itDelete the session record, effective at onceWait for exp, or keep a denylist
Typical useClassic server-rendered web appsAPIs, services that share one issuer, OIDC ID tokens
Main riskSession store becomes a dependencyLong lifetimes, secrets put in the readable payload

Three tokens from OIDC​

TokenMeant forFormatLifetimeSend it to
ID tokenThe app (client)Always a JWTShortNobody; the app checks it and reads who logged in
Access tokenThe API (resource server)JWT or opaque stringShort (minutes to an hour)The API, as Authorization: Bearer
Refresh tokenThe identity providerUsually opaqueLongOnly the token endpoint

Try it yourself​

curl -s https://accounts.google.com/.well-known/openid-configuration | jq '{issuer, authorization_endpoint, token_endpoint, jwks_uri}' # OIDC discovery
curl -s https://www.googleapis.com/oauth2/v3/certs | jq '.keys[].kid' # Public keys (JWKS) that sign ID tokens

TOKEN='<paste a JWT>'
echo "$TOKEN" | cut -d. -f1 | tr '_-' '/+' | base64 -d; echo # Header: alg, kid (may warn about missing = padding)
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d; echo # Payload: iss, sub, aud, exp. Readable by anyone, so not secret

curl -i -H "Authorization: Bearer $TOKEN" https://api.example.com/me # Watch for 200, 401 (+ WWW-Authenticate) or 403
date -r 1767225600 # Turn an exp value into a time (macOS; GNU: date -d @1767225600)

Check yourself

  1. alice has a valid, fresh access token, but calling an admin endpoint returns 403. What should she do?
  2. A user's JWT is stolen. Why is it harder to cut off than a stolen session cookie?
  3. In the authorization code flow, an attacker steals the one-time code from the redirect. Why can't they get tokens with it?

Further reading: OAuth 2.0 (RFC 6749) · PKCE (RFC 7636) · OAuth 2.0 Security Best Current Practice (RFC 9700) · JWT (RFC 7519) · OpenID Connect Core · OWASP Password Storage Cheat Sheet · OWASP Session Management Cheat Sheet · Kubernetes OIDC authentication · Permission Checks on One API Request (IAM) · Who Can Do What (RBAC)