2.8 KiB
2.8 KiB
Simplified Auth Flow Specification
Requirements
Functional Requirements
- OAuth2 Login: Users authenticate via Authentik using standard OAuth2 flow
- Session Management: Authenticated users have a signed session cookie
- User Sync: User data (email, name, groups) synced from Authentik on login
- Protected Routes: API endpoints can require authentication
- Logout: Users can logout, clearing their session
Non-Functional Requirements
- Simplicity: Auth flow should be understandable in 5 minutes
- Security: Session cookies must be signed and httpOnly
- Stateless: No server-side session state (cookie contains all needed info)
- Performance: No token refresh overhead
API Specification
GET /auth/login
Initiates OAuth2 login flow.
Response: 307 Redirect to Authentik authorize URL
GET /auth/callback
Handles OAuth2 callback from Authentik.
Query Parameters:
code: Authorization codestate: State parameter for CSRF protection
Response:
- Success: 307 Redirect to frontend with session cookie set
- Error: 400 Bad Request (invalid state or code)
GET /auth/me
Returns current authenticated user.
Headers: Requires session cookie
Response:
{
"id": "uuid",
"email": "user@example.com",
"name": "User Name",
"avatar_url": "..."
}
POST /auth/logout
Logs out current user.
Response: 200 OK with session cookie cleared
Data Model
User Model (existing, kept)
class User:
id: UUID
email: str
name: str
authentik_id: str
avatar_url: Optional[str]
created_at: datetime
updated_at: datetime
Session Cookie Format
session={signed_payload}; HttpOnly; Secure; SameSite=Lax
Where signed_payload is:
{
"user_id": "uuid",
"exp": 1234567890
}
Signed with HMAC-SHA256 using SESSION_SECRET.
Security Considerations
- CSRF Protection: State parameter in OAuth flow
- Session Security: Signed cookies prevent tampering
- Cookie Attributes: httpOnly, Secure, SameSite=Lax
- Session Expiry: Configurable TTL with automatic cleanup
- Token Handling: Authentik access token not exposed to client
Error Handling
Authentication Errors
- Missing session: 401 Unauthorized
- Invalid session signature: 401 Unauthorized
- Expired session: 401 Unauthorized (redirect to login)
- Invalid OAuth state: 400 Bad Request
- OAuth code exchange failure: 400 Bad Request
Future Considerations
Teams/Groups
- Authentik groups available via userinfo endpoint
- Can store group membership in user model
- Team management can be built on top
Session Persistence
- Currently using signed cookies (stateless)
- Can add Redis session store later if needed
- No database changes required for upgrade