Skip to main content

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.yaml

OAuth 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
API keys are read at startup and hot-reloaded when config.yaml changes.

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 by cmd/server/main.go:

Implementation

The Gemini authenticator (sdk/auth/gemini.go) implements the device flow:

Token Storage

OAuth tokens are persisted in the auth directory:
Token file format:
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

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

Refresh happens in the background without blocking requests. If a token expires during a request, the request fails and triggers immediate refresh.

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:
Automatic commit and push on credential changes.

Object Store

S3-compatible storage for cloud deployments:
All storage backends implement the same Store interface, making them interchangeable.

Security Considerations

Credential Protection

  1. File Permissions: Auth files use 0600 (owner read/write only)
  2. Path Validation: Prevents path traversal attacks
  3. Error Sanitization: Strips credentials from error messages
  4. Memory Clearing: Overwrites sensitive data after use

Best Practices

Use Environment Variables

Store sensitive values in .env files, not committed to version control

Rotate 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