Overview
OpenAI Codex authentication provides access to OpenAI’s code completion models and services. The CLI Proxy API supports two authentication methods:- OAuth 2.0 Flow - Browser-based authentication with callback
- Device Code Flow - No browser required, ideal for headless servers
Prerequisites
Before authenticating with Codex, ensure you have:- An OpenAI account with Codex access
- CLIProxyAPI server installed and configured
- For OAuth flow: A web browser
- For device flow: No browser required
Authentication Method 1: OAuth Flow
The OAuth flow opens a browser and handles authentication through a local callback server.Start OAuth login
Run the following command:This will:
- Start a local OAuth callback server
- Generate PKCE challenge and verifier
- Open your default web browser
- Navigate to OpenAI’s OAuth authorization page
Complete authorization
In the browser:
- Sign in to your OpenAI account
- Review the requested permissions
- Click “Authorize” to grant access
- Wait for automatic redirect to callback
OAuth Flow Options
Manual Browser
For headless servers or when the browser doesn’t open:Custom Callback Port
Authentication Method 2: Device Code Flow
The device code flow doesn’t require a browser on the same machine. Perfect for:- Headless servers
- SSH sessions
- Environments without graphical browsers
- Restricted network environments
Enter device code
On any device with a browser:
- Navigate to the displayed URL (usually https://openai.com/activate)
- Sign in to your OpenAI account if prompted
- Enter the device code exactly as shown
- Confirm authorization
The device code typically expires after 15 minutes. Complete the authorization before it expires.
Configuration
Token Storage Location
Authentication tokens are stored in the configured auth directory:- Default location: Set via
-auth-diror in config file - Filename format:
codex-<identifier>-<timestamp>.json - Example:
codex-user-1234567890.json
Token Contents
The stored token file contains:- OAuth 2.0 access token
- Refresh token (when available)
- Token type and expiration
- Authentication method metadata
- PKCE parameters (securely stored)
Multiple Accounts
Authenticate with multiple OpenAI accounts for increased capacity:- Discovers all token files
- Manages credential rotation
- Balances load across accounts
- Handles rate limits per account
Comparison: OAuth vs Device Flow
| Feature | OAuth Flow | Device Code Flow |
|---|---|---|
| Browser required | Yes (local) | No |
| Headless server support | Limited | Excellent |
| Speed | Faster | Slower (manual entry) |
| User experience | Seamless redirect | Manual code entry |
| Security | PKCE + callback | Device code + polling |
| Use case | Local development | Remote/headless servers |
Verification
To verify your authentication is working:Troubleshooting
OAuth Flow Issues
Port already in use
Solution: Use a custom port:Browser doesn’t open
Solution: Use manual browser mode:Callback timeout
Cause: Authorization not completed quickly enough. Solution:- Complete OAuth flow faster
- Check firewall settings for callback port
- Verify localhost is accessible
Device Flow Issues
Device code expired
Cause: The 15-minute device code timeout expired. Solution: Run-codex-device-login again to generate a new code.
”Invalid device code” error
Cause: Code was entered incorrectly. Solution:- Double-check the code (case-sensitive)
- Ensure no extra spaces
- Generate a new code if needed
Polling timeout
Cause: Authorization wasn’t completed within the polling period. Solution:- Complete the authorization on the other device
- Check network connectivity
- Run the command again
Common Issues (Both Methods)
Authentication failed
Possible causes:- Invalid credentials
- Network connectivity issues
- OpenAI service outage
- Insufficient account permissions
- Verify your OpenAI account has Codex access
- Check network connection
- Review error logs for details
- Try again after a few minutes
Token not saving
Cause: Permission issues with auth directory. Solution:PKCE verification failed
Cause: Security parameter mismatch. Solution:- Don’t modify OAuth URLs manually
- Ensure no proxies are interfering
- Clear any cached authentication state
- Try authentication again
Security Features
PKCE (Proof Key for Code Exchange)
Both authentication methods use PKCE:- Prevents authorization code interception
- Protects against CSRF attacks
- Validates code exchange authenticity
Secure Token Storage
- Token files use
0600permissions (owner-only) - Tokens are never logged in plaintext
- Refresh tokens enable automatic renewal
- Files include checksums for integrity
Token Refresh
Tokens are automatically refreshed by the server:- Monitors token expiration timestamps
- Uses refresh tokens to obtain new access tokens
- Saves refreshed tokens back to auth directory
- Handles refresh failures gracefully
Re-authentication
To re-authenticate a Codex account:-
Remove the existing token:
-
Run authentication again:
Best Practices
- Choose the right method - OAuth for local, device flow for remote
- Multiple accounts - Use several accounts to increase rate limits
- Secure storage - Protect your auth directory with appropriate permissions
- Monitor usage - Track Codex usage in server logs
- Rotate regularly - Re-authenticate periodically for security
- Backup tokens - Keep secure backups of token files