4. Session model, TTL, logout & 401

13 cards

Review this deck
All cards in this deck

Server-side session vs client-side JWT

Session: easy to revoke, tokens not exposed, needs Redis; client-side JWT: stateless but hard to revoke, easily stolen by XSS, and the token usually reaches the browser.

SessionID1 (`login:{id}`)

Temporary session for the login sequence, cookie `login_sid` (HttpOnly, Lax): stores state, nonce, codeVerifier, successUrl, failureUrl, idProvider, channel; TTL `SESSION_LOGIN_TTL`.

SessionID2 (`sess:{id}`)

The logged-in session: stores accountNo, sub, xsrfToken, channel, usedPasskey; TTL `SESSION_TTL` (e.g. 3h).

Why split SessionID1 and SessionID2

Temporary flow data (state/nonce/verifier) does not live alongside the logged-in session; SessionID1 is deleted as soon as the callback finishes, whether it succeeds or fails.

Session fixation

The attacker plants a session id on the victim in advance; prevent it by creating a brand-new SessionID2 after successful authentication, never reusing SessionID1.

CSPRNG for session ids

Use `crypto.randomBytes(32)` encoded as base64url (43 characters), not `Math.random()` because its output can be predicted from previous outputs.

Absolute vs sliding TTL

Absolute expires at a fixed time from creation; sliding extends on each use; the spec must state which one and which Redis command (`SET EX` vs re-running `EXPIRE`).

Redis TTL vs cookie TTL

Redis decides whether the session is alive; the cookie is just a pointer and may have no Expires; cookie still present but Redis expired → 401.

`stepup:{id}`

Child session proving re-authentication, fixed TTL of 600s because it is only proof of re-authentication and stores no tokens.

`stepup-mfa:{id}`

Child session holding a real access_token to call `/mfa/*`, TTL = the token's `expires_in` because once the token expires the session is meaningless.

What logout deletes in the BFF

Deletes SessionID2 in Redis and invalidates every cookie issued by the BFF (`sid`, `xsrf`, `uid`, `sid_stepup`).

How the UI handles 401

Clears the BFF-issued cookies and sessionStorage, goes to the login screen; does NOT delete `APP_SSO_TOKEN` because the native app injected it.

RP-initiated logout

The BFF redirects to `/v2/logout` (Auth0, parameter `returnTo`) or `end_session` (Keycloak, parameter `post_logout_redirect_uri`) to end the session at the IdP too, then the IdP redirects back to logout-callback.

4. Session model, TTL, logout & 401 – Flashcards – Login Flow | Học Dev