OAuth 2.0 + PKCE Flow
Codex Multi-Auth uses the standard OAuth 2.0 Authorization Code flow with PKCE (Proof Key for Code Exchange) to authenticate with ChatGPT accounts.OAuth Endpoints
Fromlib/auth/auth.ts:8-12:
CLIENT_ID is the same one used by the official OpenAI Codex CLI.
Complete OAuth Flow
PKCE (Proof Key for Code Exchange)
PKCE prevents authorization code interception attacks by requiring the client to prove it initiated the flow.PKCE Generation
Fromlib/auth/auth.ts:220:
- Prevents authorization code interception
- No client secret required (safe for CLI apps)
- Industry standard (RFC 7636)
Token Exchange
Fromlib/auth/auth.ts:97:
Token Refresh
Tokens typically expire after 1 hour. Refresh tokens are used to obtain new access tokens without re-authenticating. Fromlib/auth/auth.ts:161:
Queued Refresh (Race Prevention)
Fromlib/refresh-queue.ts:
- Prevents multiple simultaneous refresh requests
- Reduces API load
- Avoids token rotation conflicts
JWT Decoding
Theid_token is a JWT containing user and organization information.
From lib/auth/auth.ts:139:
Account ID Extraction
Fromlib/accounts.ts:350:
- Organization ID (
org-*): Used for workspace/team accounts - User ID (
user-*): Used for personal accounts
Token Refresh Strategy
1. On-Demand Refresh (Request-Time)
Fromindex.ts:1014:
2. Proactive Refresh (Background)
Fromlib/refresh-guardian.ts:
- Reduces request-time latency (no waiting for refresh)
- Prevents auth failures during high-volume usage
- Automatic background maintenance
OAuth Callback Server
Fromlib/auth/server.ts:
REDIRECT_URI. Conflicts prevented by checking server bind success.
Manual OAuth Flow (Fallback)
If the local server fails to start, users can manually paste the callback URL. Fromindex.ts:390:
lib/auth/auth.ts:52):
Security Considerations
- PKCE: Prevents authorization code interception
- State parameter: CSRF protection
- Local server: Binds to
127.0.0.1only (not0.0.0.0) - Token redaction: Tokens never logged in plaintext
- Secure storage: Account pool encrypted at rest (OS keychain integration planned)