Authentication System Design
Design secure and scalable authentication systems following industry best practices and security standards.
Quick Reference
Authentication Method Selection
- Session-based: Traditional web apps, server-side control
- JWT Token: SPA/mobile/microservices, stateless
- OAuth 2.0: Third-party integration, standard protocols
- OpenID Connect: Identity + authentication
JWT Claims Structure
- Standard: iss, sub, aud, exp, nbf, iat, jti
- Custom: userId, roles, permissions
Decision Workflow
1. Choose Authentication Method
| Method |
Best For |
Key Considerations |
| Session-based |
Traditional web apps |
Server state required |
| JWT Token |
SPA, mobile, microservices |
Token revocation challenges |
| OAuth 2.0 |
Third-party integration |
Complex setup |
| OpenID Connect |
Identity verification |
More complex than OAuth |
2. Design Authentication Flows
- Sign Up: Validate → Create → Verify → Login
- Login: Validate → Generate tokens → Redirect
- Logout: Invalidate → Clear → Redirect
- Refresh: Check expiry → Use refresh token → Retry
3. JWT Structure & OAuth Selection
- Use RS256 algorithm, short expiry (15-60 min)
- Authorization Code flow for web apps, PKCE for public clients
4. Security Validation
- Password hashing (bcrypt/Argon2)
- Rate limiting, HTTPS, token expiration
- Input validation, secure headers
Essential Patterns
Secure Password Handling
import bcrypt
def hash_password(password: str) -> str:
salt = bcrypt.gensalt(rounds=12)
return bcrypt.hashpw(password.encode(), salt).decode()
def verify_password(plain: str, hashed: str) -> bool:
return bcrypt.checkpw(plain.encode(), hashed.encode())
JWT Token Operations
import jwt
from datetime import datetime, timedelta
def create_token(user_id: str, roles: list) -> str:
payload = {
"user_id": user_id,
"roles": roles,
"exp": (datetime.utcnow() + timedelta(minutes=15)).timestamp(),
"iss": "https://your-app.com"
}
return jwt.encode(payload, key="secret", algorithm="RS256")
Resources
| File |
Purpose |
| auth-methods.md |
Authentication method comparison |
| auth-flows.md |
Flow diagrams and implementation |
| jwt-structure.md |
JWT guidelines and examples |
| oauth-flows.md |
OAuth 2.0 patterns |
| multi-service-auth.md |
Multi-service strategies |
| password-reset.md |
Secure reset implementation |
| rbac-system.md |
Role-based access control |
| security-checklist.md |
Security validation |
| integration-guide.md |
Frontend/backend integration |
| jwt-template.yaml |
JWT schema template |
1---
2name: auth-system-design
3description: |
4 Authentication system design and implementation guidance with Python examples using strict typing. Use when: (1) Designing authentication flows (signup, login, logout, refresh), (2) Selecting between session vs token-based auth, (3) Designing JWT structure and claims, (4) Implementing OAuth 2.0 flows, (5) Setting up multi-service authentication patterns, (6) Creating password reset and email verification flows, (7) Implementing role-based access control (RBAC), (8) Creating security checklists for auth systems, (9) Planning frontend/backend auth integration. All examples follow Python typing standards and security best practices.
5metadata:
6 source: https://skillmd.com/skills/aibot88/auth-system-design
7---
8
9# Authentication System Design
10
11Design secure and scalable authentication systems following industry best practices and security standards.
12
13## Quick Reference
14
15### Authentication Method Selection
16- **Session-based**: Traditional web apps, server-side control
17- **JWT Token**: SPA/mobile/microservices, stateless
18- **OAuth 2.0**: Third-party integration, standard protocols
19- **OpenID Connect**: Identity + authentication
20
21### JWT Claims Structure
22- **Standard**: iss, sub, aud, exp, nbf, iat, jti
23- **Custom**: userId, roles, permissions
24
25## Decision Workflow
26
27### 1. Choose Authentication Method
28| Method | Best For | Key Considerations |
29|--------|----------|-------------------|
30| Session-based | Traditional web apps | Server state required |
31| JWT Token | SPA, mobile, microservices | Token revocation challenges |
32| OAuth 2.0 | Third-party integration | Complex setup |
33| OpenID Connect | Identity verification | More complex than OAuth |
34
35### 2. Design Authentication Flows
36- **Sign Up**: Validate → Create → Verify → Login
37- **Login**: Validate → Generate tokens → Redirect
38- **Logout**: Invalidate → Clear → Redirect
39- **Refresh**: Check expiry → Use refresh token → Retry
40
41### 3. JWT Structure & OAuth Selection
42- Use RS256 algorithm, short expiry (15-60 min)
43- Authorization Code flow for web apps, PKCE for public clients
44
45### 4. Security Validation
46- Password hashing (bcrypt/Argon2)
47- Rate limiting, HTTPS, token expiration
48- Input validation, secure headers
49
50## Essential Patterns
51
52### Secure Password Handling
53```python
54import bcrypt
55def hash_password(password: str) -> str:
56 salt = bcrypt.gensalt(rounds=12)
57 return bcrypt.hashpw(password.encode(), salt).decode()
58
59def verify_password(plain: str, hashed: str) -> bool:
60 return bcrypt.checkpw(plain.encode(), hashed.encode())
61```
62
63### JWT Token Operations
64```python
65import jwt
66from datetime import datetime, timedelta
67
68def create_token(user_id: str, roles: list) -> str:
69 payload = {
70 "user_id": user_id,
71 "roles": roles,
72 "exp": (datetime.utcnow() + timedelta(minutes=15)).timestamp(),
73 "iss": "https://your-app.com"
74 }
75 return jwt.encode(payload, key="secret", algorithm="RS256")
76```
77
78## Resources
79
80| File | Purpose |
81|------|---------|
82| [auth-methods.md](references/auth-methods.md) | Authentication method comparison |
83| [auth-flows.md](references/auth-flows.md) | Flow diagrams and implementation |
84| [jwt-structure.md](references/jwt-structure.md) | JWT guidelines and examples |
85| [oauth-flows.md](references/oauth-flows.md) | OAuth 2.0 patterns |
86| [multi-service-auth.md](references/multi-service-auth.md) | Multi-service strategies |
87| [password-reset.md](references/password-reset.md) | Secure reset implementation |
88| [rbac-system.md](references/rbac-system.md) | Role-based access control |
89| [security-checklist.md](references/security-checklist.md) | Security validation |
90| [integration-guide.md](references/integration-guide.md) | Frontend/backend integration |
91| [jwt-template.yaml](assets/jwt-template.yaml) | JWT schema template |