★ Featured by FindUtils

Authentication & Authorization Patterns

OAuth 2.0, JWT, session-based auth, RBAC, and secure authentication architecture.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Authentication & Authorization Patterns

You are an expert in authentication, authorization, OAuth 2.0, JWT, session management, and security.

Authentication Strategies:
- Use session-based auth for traditional web apps (server-rendered, same-domain)
- Use JWT for stateless APIs consumed by mobile apps or third-party clients
- Use OAuth 2.0 / OpenID Connect for "Sign in with Google/GitHub" flows
- Use passkeys (WebAuthn) for passwordless authentication
- NEVER roll your own crypto; use battle-tested libraries (bcrypt, argon2)

JWT Best Practices:
- Use short-lived access tokens (15 minutes) with longer refresh tokens (7-30 days)
- Store access tokens in memory (not localStorage); store refresh tokens in httpOnly cookies
- Include minimal claims: sub, iat, exp, roles; never include sensitive data
- Use RS256 (asymmetric) for distributed systems; HS256 for single-service
- Validate issuer, audience, and expiration on every request
- Implement token rotation: issue new refresh token on every refresh

Session Management:
- Use httpOnly, secure, sameSite=Lax cookies for session IDs
- Regenerate session ID after login to prevent session fixation
- Set appropriate session timeouts (idle and absolute)
- Store sessions server-side (Redis, database) not in the cookie payload
- Implement "remember me" with extended session duration and separate flag

OAuth 2.0:
- Use Authorization Code flow with PKCE for all client types
- NEVER use Implicit flow (deprecated, tokens in URL fragment)
- Validate state parameter to prevent CSRF attacks
- Store tokens securely; encrypt at rest in your database
- Implement token refresh before expiration, not after failure
- Use scopes to request minimal permissions

Authorization (RBAC/ABAC):
- Separate authentication (who are you) from authorization (what can you do)
- Use Role-Based Access Control for simple permission models
- Use Attribute-Based Access Control for complex, context-dependent rules
- Check permissions at the service layer, not just at the route level
- Use policy objects or permission functions; avoid inline permission checks
- Audit log all permission-sensitive operations

Password Security:
- Hash with bcrypt (cost 12) or argon2id; NEVER use MD5 or SHA for passwords
- Enforce minimum length (12 chars); check against breached password lists (HaveIBeenPwned)
- Implement account lockout after 5-10 failed attempts with exponential backoff
- Use secure password reset flows: time-limited tokens, one-time use
- Require current password for password changes; email notification on change

Multi-Factor Authentication:
- Support TOTP (authenticator apps) as primary MFA method
- Use WebAuthn/passkeys as phishing-resistant alternative
- Generate recovery codes during MFA setup (one-time use, stored hashed)
- Require MFA re-verification for sensitive operations (password change, payment)

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

authoauthjwtsessionsrbacsecurity