Overview
Codex-LB supports multi-layered authentication for the admin dashboard:- Password authentication - bcrypt-hashed password stored in database
- TOTP (Time-based One-Time Password) - Optional 2FA using authenticator apps
- Session management - 12-hour encrypted session cookies
- Rate limiting - Protection against brute-force attacks
/api/* endpoints (except auth endpoints) require valid authentication.
API key authentication (for proxy endpoints) and dashboard authentication (for admin endpoints) are separate systems. See API Keys for proxy authentication.
Password Authentication
Initial Setup
On first launch, the dashboard is open (no authentication required). Set up password protection:- Navigate to Settings → Security
- Click Setup Password
- Enter a strong password (minimum 8 characters)
- Click Save
app/modules/dashboard_auth/service.py:210-213:
Logging In
Once password is configured, the dashboard requires authentication:- Navigate to dashboard URL
- Enter password in login form
- Click Sign In
- Receive 12-hour session cookie
codex_lb_dashboard_session (httponly, secure, samesite=lax, 12h expiry)
Session Management
Sessions are encrypted using Fernet (symmetric encryption):exp: Expiration timestamppw: Password verified (boolean)tv: TOTP verified (boolean)
app/modules/dashboard_auth/service.py:18-19:
Changing Password
Requirements: Valid authenticated sessionapp/modules/dashboard_auth/service.py:222-224:
Removing Password
To return to unauthenticated mode:app/modules/dashboard_auth/service.py:226-228:
TOTP Two-Factor Authentication
Prerequisites
TOTP requires an active password session. You cannot enable TOTP without first configuring password auth.Setting Up TOTP
- Log in with password
- Navigate to Settings → Security → Two-Factor Authentication
- Click Enable TOTP
- Scan QR code with authenticator app (Google Authenticator, Authy, 1Password, etc.)
- Enter 6-digit code from app
- Click Verify
app/modules/dashboard_auth/service.py:246-256:
TOTP uses standard 6-digit codes, 30-second time steps, SHA-1 algorithm, and window=1 (accepts codes from previous/current/next time step for clock skew tolerance).
Logging In with TOTP
Once TOTP is configured andtotp_required_on_login is enabled:
- Enter password → Creates session with
pw=true, tv=false - Dashboard shows TOTP prompt
- Enter 6-digit code from authenticator app
- Upgrades session to
pw=true, tv=true
app/modules/dashboard_auth/service.py:270-287:
totp_last_verified_step field prevents reusing codes. Each time step is a 30-second counter, and the system tracks the most recent verified step.
Disabling TOTP
Requirements: TOTP-verified sessionapp/modules/dashboard_auth/service.py:289-306:
Authentication Guard
Scope
Authentication is enforced on all/api/* routes except:
/api/dashboard-auth/*(auth endpoints themselves)/api/codex/usage(uses separate bearer caller identity validation)
Guard Logic
Fromopenspec/specs/admin-auth/spec.md:84-96:
Authentication required condition: the system SHALL evaluateSession validation steps whenpassword_hashandtotp_required_on_logintogether to determine whether authentication is required. Whenpassword_hashis NULL andtotp_required_on_loginis false, the guard MUST allow all requests (unauthenticated mode). When eitherpassword_hashis set ortotp_required_on_loginis true, the guard MUST require a valid session.
requires_auth is true:
- Valid session cookie must be present (else 401)
- If
password_hashis not NULL, session must havepassword_verified=true - If
totp_required_on_loginis true, session must havetotp_verified=true
app/modules/dashboard_auth/service.py:187-208:
Session State Endpoint
Check current authentication state:Rate Limiting
Both password and TOTP login attempts are rate-limited:app/modules/dashboard_auth/service.py:143-178):
- HTTP Status:
429 Too Many Requests - Header:
Retry-After: {seconds} - Error:
{"error": {"code": "rate_limit_exceeded", ...}}
Settings Cache
To avoid per-request database queries, settings are cached: Fromopenspec/specs/admin-auth/spec.md:179-192:
The system SHALL cache DashboardSettings in memory with a TTL of 5 seconds to avoid per-request DB queries in the auth guard. The cache MUST be invalidated immediately when settings are modified via the settings API or password/TOTP management endpoints.
Cache behavior:
- TTL: 5 seconds
- Invalidation: Immediate on setting changes
- Benefit: Reduces DB load on high-traffic dashboards
Logout
app/modules/dashboard_auth/service.py:308-309:
Security Best Practices
Strong Passwords
- Minimum 8 characters (enforced)
- Recommended: 12+ characters with mixed case, numbers, symbols
- Use a password manager
Enable TOTP
Always enable TOTP for production deployments:- Setup password
- Configure TOTP
- Enable Settings → Require TOTP on Login
Network Security
- HTTPS: Always run behind HTTPS in production
- Firewall: Restrict dashboard access to trusted IPs
- Reverse proxy: Use nginx/Caddy for TLS termination
Session Security
Sessions use Fernet encryption with a key derived from environment:Troubleshooting
Locked Out (Forgot Password)
If you forget the password: Option 1: Reset via databaseTOTP Code Not Working
Causes:- Clock skew between server and authenticator app
- Wrong secret scanned
- Code already used (replay protection)
- Ensure server and device clocks are synced (NTP)
- Re-scan QR code in authenticator app
- Wait for next 30-second window
Rate Limited
Symptom:429 Too Many Requests
Cause: 8 failed login attempts in 60 seconds
Solution: Wait for the Retry-After duration (shown in error response)
Session Expired
Symptom: Dashboard redirects to login after 12 hours Cause: Normal session TTL behavior Solution: Log in again. Consider increasing_SESSION_TTL_SECONDS in code if needed.
Related Features
- API Keys - Authenticate proxy requests (separate from dashboard auth)
- Account Pooling - Manage pooled accounts
- Usage Tracking - Monitor dashboard activity
Technical Reference
Key source files:app/modules/dashboard_auth/service.py- Auth business logicapp/modules/dashboard_auth/repository.py- Database operationsapp/modules/dashboard_auth/schemas.py- API schemasapp/core/auth/totp.py- TOTP implementationapp/core/crypto.py- Encryption utilitiesopenspec/specs/admin-auth/spec.md- Detailed specification