Overview
Claude authentication enables access to Anthropic’s Claude AI models through OAuth 2.0. The CLI Proxy API handles the complete OAuth flow, including PKCE (Proof Key for Code Exchange) for enhanced security.Prerequisites
Before authenticating with Claude, ensure you have:- A Claude account (personal or professional)
- CLIProxyAPI server installed and configured
- A web browser for OAuth authentication (or use
-no-browserflag) - Network access to Anthropic’s authentication servers
Authentication
Start the login process
Run the following command to initiate Claude OAuth:This will:
- Start a local OAuth callback server
- Generate PKCE challenge and verifier
- Open your default web browser automatically
- Navigate to Claude’s OAuth authorization page
Complete OAuth authorization
In the browser window:
- Sign in to your Claude account
- Review the requested permissions
- Click “Authorize” to grant access to the CLI Proxy API
- Wait for the redirect back to the local callback server
Advanced Options
Manual Browser Authentication
If you’re on a headless server or the browser doesn’t open automatically:Custom OAuth Callback Port
To use a specific port for the OAuth callback server:The default callback port is Claude-specific. Use this flag if the default port is already in use or blocked by your firewall.
Combined Options
You can combine flags for specific scenarios:Configuration
Token Storage Location
Authentication tokens are stored in the configured auth directory:- Default location: Configured via
-auth-dirflag or in config file - Filename format:
claude-<identifier>-<timestamp>.json - Example:
claude-user-1234567890.json
Token Contents
The stored token file contains:- OAuth 2.0 access token
- Refresh token (if provided)
- Token type and expiration timestamp
- Account identifier
- PKCE parameters (stored securely)
Multiple Accounts
You can authenticate with multiple Claude accounts:- Run
-claude-loginfor each account - Each account creates a separate token file
- The server automatically manages credential rotation and load balancing
Security Features
PKCE (Proof Key for Code Exchange)
The Claude OAuth implementation uses PKCE to prevent authorization code interception:- A cryptographically random code verifier is generated
- The SHA-256 hash is sent as the code challenge
- The original verifier is used during token exchange
- This prevents man-in-the-middle attacks
Secure Token Storage
Tokens are stored with appropriate file permissions:- Files are created with
0600permissions (owner read/write only) - Tokens are never logged or displayed in plaintext
- Refresh tokens enable automatic token renewal
Verification
To verify your authentication is working:Start the server
- Automatically discover Claude token files
- Load credentials into the credential pool
- Log successful credential registration
Troubleshooting
Port already in use error
Cause: The OAuth callback port is already occupied by another service. Solution: Use a custom callback port:Browser doesn’t open automatically
Solution: Use the-no-browser flag:
OAuth callback timeout
Cause: The authorization wasn’t completed within the timeout period. Solution:- Complete the OAuth flow more quickly
- Check if your firewall blocks the callback port
- Ensure localhost/127.0.0.1 is accessible
- Try a different callback port with
-oauth-callback-port
”Authentication failed” error
Possible causes:- Invalid authorization code
- PKCE verification failed
- Network connectivity issues
- Claude service outage
- Check your network connection
- Verify Claude services are operational
- Try authenticating again
- Check the server logs for detailed error messages
Token not saving
Cause: Permission issues with the auth directory. Solution:- Verify the auth directory exists:
mkdir -p /path/to/auth-dir - Check directory permissions:
chmod 755 /path/to/auth-dir - Ensure sufficient disk space
- Review error logs for specific failure reasons
Invalid redirect URI error
Cause: The callback port in the OAuth request doesn’t match the actual server port. Solution:- Don’t manually modify the OAuth URL
- Ensure no network proxies are modifying requests
- Use the
-oauth-callback-portflag if needed
Token Refresh
Claude tokens are automatically refreshed:- The server monitors token expiration
- Refresh tokens are used to obtain new access tokens
- Refreshed tokens are saved back to the auth directory
- No manual intervention required
Re-authentication
To re-authenticate with a Claude account:-
Delete the existing token file:
-
Run the login command again:
Security Best Practices
- Never share token files - They contain credentials with full account access
- Use restrictive permissions - Keep auth directory at
755and token files at600 - Rotate credentials regularly - Re-authenticate periodically for security
- Monitor token usage - Check logs for unauthorized access attempts
- Secure your auth directory - Back up tokens securely, encrypt if storing remotely