Session Management
Last Updated: May 19, 2025
This document provides an overview of session management in the Typus Development Framework.
Overview
Typus uses a token-based session management approach with JWT (JSON Web Tokens) instead of traditional server-side sessions. This stateless approach provides better scalability and is well-suited for modern web applications and APIs.
Session Flow Diagram
Token Types
The session management system uses two types of tokens:
Access Token
- Purpose: Authenticate API requests
- Format: JWT (JSON Web Token)
- Contents: User ID, email, roles, and expiration time
- Storage: Client-side (memory, localStorage, or secure cookie)
- Expiration: Short-lived (typically 1 hour, configurable)
- Validation: Verified on each API request
Refresh Token
- Purpose: Obtain new access tokens without re-authentication
- Format: Cryptographically secure random string
- Storage: Server-side in database with user association
- Expiration: Long-lived (typically 7 days, configurable)
- Usage: Single-use (invalidated after use)
- Security: Protected against token theft with database validation
Session Lifecycle
1. Session Creation
When a user successfully authenticates:
- System generates an access token containing user identity and permissions
- System generates a unique refresh token
- Refresh token is stored in the database with:
- User association
- Expiration timestamp
- Unique identifier
- Both tokens are returned to the client
2. Session Validation
For each protected API request:
- Client includes access token in Authorization header
- AuthMiddleware extracts and verifies the token
- If valid, user information is attached to the request
- If invalid or expired, 401 Unauthorized response is returned
3. Session Renewal
When an access token expires:
- Client uses refresh token to request new tokens
- System validates the refresh token against the database
- If valid, the used refresh token is invalidated
- New access and refresh tokens are generated
- New refresh token is stored in the database
- Both new tokens are returned to the client
4. Session Termination
Sessions can be terminated in several ways:
- User Logout: All refresh tokens for the user are invalidated
- Token Expiration: Access and refresh tokens naturally expire
- Manual Invalidation: Administrators can force-terminate sessions
- Security Events: Password changes or suspicious activity triggers session invalidation
Security Considerations
Token Security
- JWT Secret: Access tokens are signed with a secure secret key
- Token Storage:
- Access tokens should be stored in memory when possible
- Refresh tokens should use secure HTTP-only cookies or secure storage
- HTTPS: All token transmission must occur over HTTPS
Protection Against Common Attacks
- Token Theft: Refresh tokens are single-use and tied to user accounts
- XSS Protection: Proper token storage mitigates cross-site scripting risks
- CSRF Protection: Token-based auth is naturally resistant to CSRF
- Token Replay: JTI (JWT ID) ensures tokens cannot be replayed
Session Management Best Practices
- Short-lived Access Tokens: Minimize the impact of token compromise
- Token Rotation: Refresh tokens are rotated with each use
- Concurrent Sessions: Multiple devices can maintain independent sessions
- Session Monitoring: Failed token attempts are logged for security monitoring
Frontend Session Handling
The frontend application manages sessions through:
- Token Storage: Securely storing tokens (memory for access, secure storage for refresh)
- Automatic Renewal: Transparently refreshing expired tokens
- Auth State Management: Maintaining authentication state across the application
- Logout Handling: Clearing tokens and auth state on logout
Session Configuration
The session management system is configurable through environment variables:
- JWT_SECRET: Secret key for signing JWTs
- JWT_EXPIRY: Access token expiration time (default: 1h)
- REFRESH_TOKEN_EXPIRY: Refresh token expiration time (default: 7d)
Conclusion
The token-based session management system in Typus provides a secure, scalable approach to authentication and authorization. By separating short-lived access tokens from long-lived refresh tokens, the system balances security and user experience while maintaining stateless API architecture.