mud_server.api.auth
Session management and authentication.
This module handles session-based authentication for the MUD server. It provides: 1. Database-backed session storage (source of truth) 2. Session validation with expiration enforcement 3. Permission checks for admin endpoints
Session Lifecycle: 1. Login: New session ID created and stored in the database 2. Requests: Each API call validates session, enforces expiry, updates activity 3. Logout: Session removed from the database 4. Server Restart: Sessions survive until they expire or are revoked
Security Considerations: - Sessions use opaque IDs (UUIDs by default) - Sessions expire using a TTL and (optionally) sliding expiration - Database is the source of truth (supports restart persistence) - Session validation updates activity timestamp to track last action
Future Improvements: - Implement session refresh tokens (“remember me”) - Add device/session management UI - Add optional IP/User-Agent tracking for session audits
Functions
Clear all sessions from the database. |
|
|
Remove a specific session from the database. |
Get the count of active (non-expired) sessions. |
|
|
Get username from session ID. |
|
Get both username and role from session ID. |
|
Validate session and return user information. |
|
Validate session and ensure a character is selected for gameplay. |
|
Validate session and check if user has required permission. |
Module Contents
- mud_server.api.auth.clear_all_sessions()[source]
Clear all sessions from the database.
- This should be called:
When performing emergency session resets
In test fixtures to ensure clean state
- Returns:
Number of sessions removed from the database.
- Return type:
- Side Effects:
Deletes all rows from sessions table (committed to database)
- mud_server.api.auth.remove_session(session_id)[source]
Remove a specific session from the database.
- This function handles targeted session removal, typically used when:
A user explicitly logs out (via logout endpoint)
An admin force-disconnects a user
A session is detected as invalid and needs cleanup
- mud_server.api.auth.get_active_session_count()[source]
Get the count of active (non-expired) sessions.
- This is useful for:
Health check endpoints (reporting active_players)
Admin dashboards showing current load
Rate limiting decisions based on server load
Logging and monitoring
- mud_server.api.auth.get_username_from_session(session_id)[source]
Get username from session ID.
Returns None if the session is not found or has expired.
- mud_server.api.auth.get_username_and_role_from_session(session_id)[source]
Get both username and role from session ID.
Returns None if the session is not found or has expired.
- mud_server.api.auth.validate_session(session_id)[source]
Validate session and return user information.