Overview
switchAILocal supports multiple authentication methods across different provider types. The Auth Manager orchestrates credential lifecycle, automatic refresh, and secure storage.Authentication Methods
API Keys
Static credentials configured in
config.yamlOAuth 2.0
Interactive login flows with automatic token refresh
Service Accounts
Long-lived credentials for automation
API Key Authentication
The simplest authentication method using static API keys.Configuration
config.yaml
Storage
API keys from config are stored in-memory only:API keys in
config.yaml are never persisted to the auth directory. Only OAuth tokens are saved.OAuth 2.0 Authentication
Interactive login flows for providers like Gemini CLI, Codex, and Claude.OAuth Providers
Gemini CLI (Google)
OAuth with device flow and token refresh
Codex (OpenAI)
OAuth for ChatGPT Plus accounts
Claude (Anthropic)
OAuth for Claude Pro accounts
Qwen
OAuth for Qwen services
Login Flow
The OAuth flow is managed bycmd/server/main.go:
Implementation
Gemini OAuth Flow
Gemini OAuth Flow
The Gemini authenticator (
sdk/auth/gemini.go) implements the device flow:Token Storage
OAuth tokens are persisted in the auth directory:Tokens are stored with
600 permissions (owner read/write only) for security.Token Refresh
OAuth tokens are automatically refreshed before expiration.Auto-Refresh System
The Auth Manager runs a background refresh loop:Refresh Decision
When to Refresh
When to Refresh
The system decides whether to refresh based on multiple factors:Refresh lead times by provider:
- Gemini: 5 minutes before expiry
- Codex: 10 minutes before expiry
- Claude: 15 minutes before expiry
Refresh Execution
Credential Lifecycle
Registration
Credentials are registered during initialization:State Tracking
Each credential tracks its operational state:Result Marking
Execution results update credential state:Storage Backends
Flexible storage for different deployment scenarios.File Store (Default)
Local filesystem storage:Postgres Store
Database-backed storage for multi-node deployments:Git Store
Version-controlled storage with remote sync:Object Store
S3-compatible storage for cloud deployments:All storage backends implement the same
Store interface, making them interchangeable.Security Considerations
Credential Protection
- File Permissions: Auth files use
0600(owner read/write only) - Path Validation: Prevents path traversal attacks
- Error Sanitization: Strips credentials from error messages
- Memory Clearing: Overwrites sensitive data after use
Security Implementation
Security Implementation
Best Practices
Use Environment Variables
Store sensitive values in
.env files, not committed to version controlRotate Credentials
Periodically rotate API keys and refresh OAuth tokens
Restrict Permissions
Ensure auth directory and config files have proper permissions
Monitor Access
Enable logging to track authentication attempts
Troubleshooting
Token Expired
Invalid API Key
Permission Denied
Multiple Accounts
Next Steps
Configure Providers
Set up authentication for each provider
Security Guide
Secure your switchAILocal deployment
OAuth Setup
Detailed OAuth configuration guide
Storage Options
Choose the right storage backend