Pattern: Sessions, tokens and devices
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. Numbers are example values.
- Core ideas: every token maps to a server-side session record that is checked on every request, can be revoked one by one, and dies when the password changes.
- Related: passwords and login, account recovery, admin permissions.
1. Problem
After login, something must prove “this is the same person”. If that proof cannot be revoked, a stolen phone or token stays dangerous. If it carries personal data, a leak exposes it.
2. Options
| Method | Upside | Downside | Fits when |
|---|---|---|---|
| Server-side session with a cookie | Revocation is trivial; small cookie | Needs a shared session store when you run several servers | Web apps |
| Self-contained signed token (JWT) only | No lookup per request | Cannot be revoked before expiry without extra state | Short-lived access with a revocation list |
| Signed token plus a session record (recommended for mobile and API) | Revocable per device; token stays small | One lookup per request | Mobile apps, public APIs |
RFC 8725 (JWT Best Current Practices) and the OWASP Session Management Cheat Sheet are the main references.
3. Recommended design
Token contents and checks
- If you use signed tokens: pin the signing algorithm and issuer in the verifier; use separate issuers for end users and admins; check audience and expiry (RFC 8725).
- Put only the account id and the session id inside. No email, no name (they leak in logs and screenshots).
- Short life (example: 15 minutes to 1 hour) plus silent renewal. Reason: a stolen token expires soon even if revocation lags.
- Every token refers to a row in a session table, and the server checks that row on each request. That makes single-session revocation possible.
Lifecycle
- Tokens issued before a password change are invalid after it.
- Limit open sessions per account (example: 10); opening one more closes the oldest.
- Idle and absolute timeouts: OWASP gives common idle ranges of 2–5 minutes for high-value apps and 15–30 minutes for low-risk apps; pick by risk. Renew the session id after any privilege change.
- A “Devices and sessions” screen: list sessions (device name, last use, approximate place), close one remotely (ask for the password), and “sign out everywhere”.
- The signing secret is mandatory in production and must be strong. If it is missing or weak, the server refuses to start.
Web cookies
- Session cookies:
Secure,HttpOnly, and an explicitSameSitevalue (MDN cookies guide and RFC 6265 define the attributes). Pair with CSRF protection where cookies authenticate state-changing requests.
Mobile apps
- Store the token in the platform secure store (Android Keystore-backed storage, iOS Keychain). Prefer settings that keep it on this device only.
- On a 401 response the app signs out by itself.
- On sign-out delete cached data, drafts and temporary files, not just the token.
- OWASP MASVS lists storage and authentication requirements to verify against.
4. Scenarios and edge cases
- Lost phone: the owner opens another device, closes that session; its token fails the next server check.
- Password changed: every other session ends at once; the current device gets a fresh one.
- Clock skew on devices: allow a small tolerance for token times.
- Two tabs renew at once: accept the old token briefly for the same session, or serialise renewals.
- Server restart: sessions live in a shared store, so users stay signed in.
5. Common mistakes
- A token that cannot be revoked and lives for weeks.
- Personal data inside tokens.
- Accepting any signing algorithm the token header names.
- User and admin tokens signed and checked the same way.
- Keeping the token in unprotected app storage or web local storage on pages with injected scripts.
- Signing out only on the screen, without server-side revocation.
6. How to audit
- What: a revoked session stops working.
- How to check: sign in on two devices, close one from the other, call the API with the closed token.
- Evidence: the 401 response.
- What: password change ends older sessions.
- How to check: change the password, retry an old token.
- Evidence: the 401 response.
- What: token content and verification.
- How to check: decode a token; read the verifier configuration.
- Evidence: list of claims; pinned algorithm and issuer in code.
- What: secure storage on mobile.
- How to check: inspect where the token is saved; sign out and inspect caches.
- Evidence: storage API name; empty cache listing.
Sources
- Session Management Cheat Sheet — OWASP Cheat Sheet Series
- RFC 7519: JSON Web Token (JWT) — IETF / RFC Editor
- RFC 8725: JSON Web Token Best Current Practices — IETF / RFC Editor
- RFC 6265: HTTP State Management Mechanism — IETF / RFC Editor
- Using HTTP cookies — MDN Web Docs
- Android Keystore system — Android Developers
- Keychain services — Apple Developer
- OWASP MASVS (Mobile Application Security Verification Standard) — OWASP MAS
- Application Security Verification Standard (ASVS) — OWASP