JWT Token Structure
The application uses JSON Web Tokens (JWT) for session management. Tokens are stored in httpOnly cookies for security.Token Payload
Subject - User ID as a string
Expiration time - Unix timestamp when the token expires
Issued at - Unix timestamp when the token was created
Token Generation
Tokens are created using thecreate_access_token function in app/auth/jwt_utils.py:6.
Configuration
Secret key used for signing JWT tokens. Must be kept secure.
Algorithm used for JWT encoding/decoding
Token expiration time in minutes
Token Validation
Tokens are validated using thedecode_access_token function in app/auth/jwt_utils.py:17.
Validation Rules
- Signature Verification - Token must be signed with the correct SECRET_KEY
- Expiration Check - Token must not be expired
- Subject Presence - Token must contain a “sub” claim with the user ID
- Algorithm Match - Token must use the configured algorithm
Return Values
The user ID extracted from the token
Returned when:
- Token signature is invalid
- Token is expired
- Token is malformed
- “sub” claim is missing
Authentication Dependency
Theget_current_user dependency function validates the JWT cookie and retrieves the authenticated user from the database.
Implementation
Defined inapp/auth/dependencies.py:9:
Behavior
- Extracts the
access_tokenfrom the httpOnly cookie - Decodes and validates the JWT token
- Queries the database for the user by ID
- Returns the User object if found
- Raises HTTPException 401 for any authentication failure
Error Responses
Not authenticated - Cookie is missing
Invalid or expired token - JWT validation failed
User not found - User ID from token doesn’t exist in database
Logout
Response
Confirmation message: “Logged out”
Behavior
- Deletes the
access_tokenhttpOnly cookie - Cookie deletion respects the same security settings (secure, samesite) as when it was set
- No authentication required (can be called even if not logged in)