Authentication and sessions¶
How users prove who they are and how an application keeps them signed in: identity providers, OAuth 2.0 and OpenID Connect, sessions and tokens in browser apps, passwords, multi-factor authentication, passkeys and account recovery. What a signed-in user may do (authorization, SEC-1), secrets and security logging are covered in Security. For Next.js apps, also follow Next.js.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 10 NIST SP 800-63B-4, Authentication and Authenticator Management (2025): the baseline for passwords, multi-factor and phishing-resistant authenticators, syncable authenticators such as passkeys, reauthentication, sessions and account recovery.
- 9 OWASP ASVS 5.0, V6 Authentication, V7 Session Management and V10 OAuth and OIDC: verifiable requirements by level. Use them as the checklist, as Security does with ASVS as a whole.
- 9 RFC 9700, Best Current Practice for OAuth 2.0 Security (IETF, 2025): the current security rules for OAuth clients, authorization servers and resource servers.
- 8 RFC 10017, OAuth 2.0 for Browser-Based Applications (IETF, 2026): the architecture patterns for single-page apps (Backend for Frontend, token-mediating backend, browser-only client), their threats, and the patterns to avoid.
- 8 OWASP Authentication Cheat Sheet and the cheat sheets for Session Management, Password Storage, Multifactor Authentication, Passkey Security and Forgot Password: practical implementation guidance for each part of sign-in.
- 8 OpenID Connect Core 1.0 (OpenID Foundation): the identity layer on top of OAuth 2.0 for sign-in and single sign-on.
- 8 Web Authentication (WebAuthn) Level 3 (W3C Recommendation, 2026): the browser API behind passkeys. passkeys.dev (W3C community group and FIDO Alliance) explains how to adopt them.
Key rules¶
Identity providers and protocols¶
- 9
AUTH-1In browser apps, keep tokens out of reach of JavaScript: use a server-side session in a cookie, or a Backend for Frontend (BFF) that holds the OAuth tokens. Don't keep tokens or session IDs inlocalStorageorsessionStorage, where any XSS can read them; when the frontend and its backend share a domain, a cookie session is enough and OAuth usually isn't needed between them. (RFC 10017, Backend for Frontend, Single-domain applications, OWASP, The localStorage API) - 9
AUTH-2Use the authorization code flow with PKCE for every OAuth client. Don't use the implicit grant, and never use the resource owner password credentials grant. Public clients must use PKCE, and it's recommended for confidential clients too. (RFC 9700, Recommendations, OWASP OAuth 2.0 Cheat Sheet) - 8
AUTH-3Use an established identity provider, or your framework's built-in authentication and session management, instead of writing your own. Keep the library up to date: your code still has to configure it and check what it returns. (OWASP, Built-in session management implementations, OWASP, Passkeys: use a maintained library, OWASP, MFA: consider using a third-party service) - 8
AUTH-4Sign users in through an external identity provider with OpenID Connect, not plain OAuth, and identify each federated user by issuer and subject (issandsub). Don't link an external identity to an existing account just because the email address matches. (OpenID Connect Core, Claim stability and uniqueness, OWASP, Secure federated account linking)
Sessions¶
- 9
AUTH-5Set session cookies withSecure,HttpOnlyandSameSite=StrictorLax, and prefer the__Host-name prefix. (OWASP, Session Management: Cookies, MDN, Set-Cookie: Cookie prefixes) - 9
AUTH-6Issue a new session ID when a user signs in, re-authenticates or changes privilege level, and invalidate the old one. This prevents session fixation. (OWASP, Renew the session ID after any privilege level change, ASVS 5.0, V7 Session Management) - 8
AUTH-7Enforce idle and absolute session timeouts on the server, and invalidate the session on the server when the user logs out. Clearing the cookie isn't enough, and timeouts tracked by the client can be manipulated. (OWASP, Session expiration, OWASP, Logout button) - 8
AUTH-17Protect every state-changing request that browsers send with credentials they attach automatically, such as session cookies or HTTP Basic authentication, against cross-site request forgery (CSRF), and keepGET,HEADandOPTIONSrequests free of side effects. A stateless app that keeps its session in a cookie is exposed too, andSameSitecookies (AUTH-5) are defence in depth, not a replacement for the protection. Turn the protection off only for endpoints that only non-browser clients call. Platform examples:SPRING-7,LARAVEL-9,ASTRO-10andGQL-11. (Spring Security, When to use CSRF protection, CSRF and stateless browser applications, Safe methods must be read-only, SameSite attribute) - 7
AUTH-8Make sessions revocable: end all of a user's sessions when the account is disabled, and offer to end the other sessions after a password or MFA change. Self-contained tokens such as JWTs stay valid until they expire unless you add a revocation mechanism, such as a list of terminated tokens. (ASVS 5.0, V7 Session Management) - 7
AUTH-9Ask for the current credentials again before sensitive changes, such as a new password or email address, and before sensitive transactions. (OWASP, Require re-authentication for sensitive features)
Passwords and multi-factor authentication¶
- 9
AUTH-10Store passwords only as salted hashes from a slow password-hashing function: Argon2id, scrypt, bcrypt or PBKDF2. Fast hashes such as SHA-256 let attackers test guesses quickly. (OWASP Password Storage Cheat Sheet, NIST SP 800-63B-4, Password verifiers) - 9
AUTH-11Follow NIST's password rules: require at least 15 characters for passwords used alone (8 with MFA), allow at least 64, and reject passwords on a blocklist of common and breached ones. Don't impose composition rules, such as a mix of character types, or periodic password changes. (NIST SP 800-63B-4, Passwords, OWASP, Implement proper password strength controls) - 9
AUTH-12Offer multi-factor authentication to all users, and require it for administrators and other privileged accounts. (OWASP, MFA quick recommendations) - 7
AUTH-13Offer a phishing-resistant authenticator, preferably passkeys. When a passkey counts as a second factor, require user verification and check its flag on the server. Passwords and one-time codes can be relayed by a phishing proxy; WebAuthn binds the sign-in to the real origin. (NIST SP 800-63B-4, AAL2 requirements, OWASP, MFA: phishing-resistant control, OWASP, MFA: passkeys) - 7
AUTH-14Give the same response, in about the same time, whether the account doesn't exist, the password is wrong or the account is locked, and limit failed attempts per account. (OWASP, Authentication responses, OWASP, Login throttling, NIST SP 800-63B-4, Rate limiting)
Account recovery¶
- 8
AUTH-15Protect account recovery as strongly as sign-in: use random, single-use, short-lived reset tokens, answer the same whether or not the account exists, and notify the user after every recovery. Don't let email or SMS recovery silently bypass a stronger sign-in, such as passkeys, on high-risk accounts. (OWASP Forgot Password Cheat Sheet, OWASP, Passkeys: account recovery, NIST SP 800-63B-4, Account recovery) - 6
AUTH-16Encourage users to register more than one passkey or authenticator, or to keep single-use recovery codes, so that losing one device doesn't force a weaker recovery. (OWASP, Passkeys: account recovery, OWASP, Resetting MFA)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 6 ZAP baseline scan: runs ZAP's passive scan against a deployed app in CI. The rules Cookie No HttpOnly Flag, Cookie Without Secure Flag and Cookie without SameSite Attribute flag missing cookie attributes. Partly checks
AUTH-5. - 6 CodeQL, Sensitive server cookie exposed to the client (JavaScript) and Failure to use secure cookies (Python, Java): code-scanning queries that flag cookies set without
HttpOnlyorSecure. Partly checksAUTH-5. - 5 CodeQL, Use of password hash with insufficient computational effort (JavaScript) and Use of a broken or weak cryptographic hashing algorithm on sensitive data (Python): code-scanning queries that flag passwords hashed with fast algorithms such as MD5 or SHA-1. Partly checks
AUTH-10.
Tutorials and further reading¶
- 7 Passkeys developer guide (Google for Developers): creating passkeys, signing in with passkey autofill, managing passkeys, and a checklist.
- 6 Bootstrapping an account on the web (passkeys.dev): adding passkeys to an existing sign-in form, with autofill and the step that offers users a passkey after they sign in.