Overview
The API uses JSON Web Tokens (JWT) for stateless authentication. Tokens are issued after successful authentication and must be included in theAuthorization header for protected endpoints.
Token Structure
JWT Claims
Access tokens contain the following claims:Unique token identifier (UUID v4) used for revocation
User UUID identifier
User role name (e.g., “Basic User”, “Admin”)
Authentication provider:
local, github, google, or microsoftToken version number for bulk revocation (incremented on password change)
Expiration timestamp (Unix epoch)
Issued at timestamp (Unix epoch)
Example Token Payload
Token Generation
Tokens are generated after successful authentication:- Local Login -
/auth/loginendpoint (router.py:66) - OAuth Callback -
/auth/oauth/{provider}/callbackendpoint (router.py:120)
Expiration
Token expiration is configurable viaJWT_EXPIRY_DAYS setting (jwt.py:9). Default expiration is calculated from the current time plus the configured number of days.
Signing Algorithm
Tokens are signed using the algorithm specified inJWT_ALGORITHM configuration (jwt.py:8). The signing key is JWT_SECRET_KEY (jwt.py:7).
Token Validation
Tokens are validated on protected endpoints using theget_current_user dependency.
Validation Steps
- Token Extraction - Extract token from
Authorization: Bearer <token>header - Signature Verification - Verify token signature using secret key
- Expiration Check - Ensure token has not expired
- Required Claims - Verify
expandiatclaims are present - Revocation Check - Check if token JTI is in revoked tokens table
- Version Check - Verify token version matches user’s current token version
Validation Errors
401 Unauthorized - Invalid signature or expired tokenToken Usage
Making Authenticated Requests
Include the access token in theAuthorization header:
Token Revocation
Tokens can be revoked in two ways:Individual Revocation
Revoking a specific token via logout. Endpoint:POST /auth/logout
- Extract
jtiandexpfrom token claims - Add token to
revoked_tokenstable with expiration timestamp - Future requests with this token will be rejected
Bulk Revocation
Revoking all tokens for a user (password change). Endpoint:POST /auth/password
New password (must meet password requirements if validation is enabled)
- Update password hash in
auth_identitiestable - Increment
token_versionon user record (service.py:30) - All previous tokens become invalid due to version mismatch
Revocation Implementation
Revoked tokens are stored in therevoked_tokens database table with:
jti- Token identifierexpires_at- Token expiration timestamp (for cleanup)
OAuth State Tokens
In addition to access tokens, the API uses temporary state tokens for OAuth flows.State Token Claims
Subject (always “oauth_state”)
OAuth provider name:
github, google, or microsoftToken purpose (always “login”)
Optional user ID for linking flows
Expiration timestamp (15 minutes from issuance)
Issued at timestamp
State Token Generation
State tokens are created when initiating OAuth flows (jwt.py:38):State Token Validation
State tokens are validated during OAuth callbacks (jwt.py:53):- Signature Verification - Verify token signature
- Expiration Check - Ensure token hasn’t expired (15 minute TTL)
- Subject Check - Verify
subclaim is “oauth_state” - Provider Match - Confirm provider matches URL parameter
- Purpose Check - Validate purpose is “login”
Security Best Practices
Token Storage
- Never store tokens in localStorage (vulnerable to XSS)
- Use httpOnly cookies or secure session storage
- Clear tokens on logout
Token Transmission
- Always use HTTPS in production
- Never include tokens in URL parameters
- Use
Authorizationheader for token transmission
Token Lifecycle
- Implement token refresh if needed for long-lived sessions
- Revoke tokens on password change
- Revoke tokens on logout
- Clean up expired revoked tokens periodically
Secret Key Management
- Use strong, randomly generated secret keys
- Rotate secret keys periodically (requires re-authentication)
- Store secrets in environment variables, never in code
- Use different secrets for development and production
Implementation Reference
Key source files:- Token Creation -
src/api/core/jwt.py:13(create_access_token) - Token Validation -
src/api/core/jwt.py:65(decode_token) - OAuth State -
src/api/core/jwt.py:38(create_oauth_state) - Revocation -
src/api/auth/service.py:15(revoke_token) - Bulk Revocation -
src/api/auth/service.py:23(revoke_all_user_tokens)
Next Steps
Authentication Overview
Learn about authentication concepts and flows
OAuth Authentication
Explore OAuth flows for GitHub, Google, and Microsoft