OpenAI Codex (ChatGPT Plus/Pro) OAuth authentication.
This module implements OAuth 2.0 PKCE authentication for OpenAI Codex,
allowing users with ChatGPT Plus/Pro subscriptions to use their
subscription through the OpenAI API.
Based on the pi-mono implementation by badlogic.
OpenAICodexAuthBackend
Bases: ProviderAuthBackend
OpenAI Codex (ChatGPT Plus/Pro) OAuth backend.
Source code in src/llmling_models/auth/openai_codex_auth.py
| class OpenAICodexAuthBackend(ProviderAuthBackend):
"""OpenAI Codex (ChatGPT Plus/Pro) OAuth backend."""
def __init__(self) -> None:
self._pending: dict[str, tuple[str, str]] = {} # state -> (verifier, state)
@property
def provider_id(self) -> str:
return "openai-codex"
def methods(self) -> list[ProviderAuthMethod]:
return [ProviderAuthMethod(type="oauth", label="Connect ChatGPT Plus/Pro")]
async def authorize(self, method: int = 0) -> ProviderAuthAuthorization:
import secrets as _secrets
verifier, challenge = generate_pkce()
state = _secrets.token_hex(16)
auth_url = build_authorization_url(verifier, challenge, state)
self._pending[state] = (verifier, state)
return ProviderAuthAuthorization(
url=auth_url,
instructions="Sign in with your OpenAI account",
method="auto",
)
async def callback(
self,
*,
code: str | None = None,
device_code: str | None = None,
verifier: str | None = None,
) -> bool:
if not code or not verifier:
msg = "Missing code or verifier for OpenAI Codex OAuth"
raise ValueError(msg)
# verifier here is the PKCE verifier stored during authorize
token = exchange_code_for_token(code, verifier)
store = OpenAICodexTokenStore()
store.save(token)
return True
async def remove_credentials(self) -> bool:
OpenAICodexTokenStore().clear()
return True
|
OpenAICodexToken
dataclass
Stored OAuth token data for OpenAI Codex.
Source code in src/llmling_models/auth/openai_codex_auth.py
| @dataclass
class OpenAICodexToken:
"""Stored OAuth token data for OpenAI Codex."""
access_token: str
refresh_token: str
expires_at: float
account_id: str
def is_expired(self, buffer_seconds: int = 300) -> bool:
"""Check if the token is expired or about to expire."""
return time.time() >= (self.expires_at - buffer_seconds)
def to_dict(self) -> dict[str, str | float]:
return {
"access_token": self.access_token,
"refresh_token": self.refresh_token,
"expires_at": self.expires_at,
"account_id": self.account_id,
}
@classmethod
def from_dict(cls, data: dict[str, str | float]) -> Self:
return cls(
access_token=str(data["access_token"]),
refresh_token=str(data["refresh_token"]),
expires_at=float(data["expires_at"]),
account_id=str(data["account_id"]),
)
|
is_expired
is_expired(buffer_seconds: int = 300) -> bool
Check if the token is expired or about to expire.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def is_expired(self, buffer_seconds: int = 300) -> bool:
"""Check if the token is expired or about to expire."""
return time.time() >= (self.expires_at - buffer_seconds)
|
OpenAICodexTokenStore
dataclass
File-based token storage for OpenAI Codex OAuth.
Source code in src/llmling_models/auth/openai_codex_auth.py
| @dataclass
class OpenAICodexTokenStore:
"""File-based token storage for OpenAI Codex OAuth."""
path: Path = field(default_factory=lambda: DEFAULT_TOKEN_PATH)
_token: OpenAICodexToken | None = field(default=None, init=False, repr=False)
_refresh_lock: asyncio.Lock = field(default_factory=asyncio.Lock, init=False, repr=False)
def __post_init__(self) -> None:
self.path.parent.mkdir(parents=True, exist_ok=True)
def load(self, *, reload: bool = False) -> OpenAICodexToken | None:
"""Return the token, from memory unless `reload` (then from the file)."""
if self._token is not None and not reload:
return self._token
if not self.path.exists():
return None
try:
data = anyenv.load_json(self.path.read_text(), return_type=dict)
self._token = OpenAICodexToken.from_dict(data)
except (anyenv.JsonLoadError, KeyError, TypeError) as e:
logger.warning("Failed to load token from %s: %s", self.path, e)
return None
else:
return self._token
def save(self, token: OpenAICodexToken) -> None:
self._token = token
write_private_json(self.path, token.to_dict())
logger.debug("Saved token to %s", self.path)
def clear(self) -> None:
self._token = None
if self.path.exists():
self.path.unlink()
logger.debug("Removed token from %s", self.path)
def get_valid_token(self) -> OpenAICodexToken | None:
token = self.load()
if token is None:
return None
if token.is_expired():
logger.debug("Token is expired, needs refresh")
return None
return token
|
load
Return the token, from memory unless reload (then from the file).
Source code in src/llmling_models/auth/openai_codex_auth.py
| def load(self, *, reload: bool = False) -> OpenAICodexToken | None:
"""Return the token, from memory unless `reload` (then from the file)."""
if self._token is not None and not reload:
return self._token
if not self.path.exists():
return None
try:
data = anyenv.load_json(self.path.read_text(), return_type=dict)
self._token = OpenAICodexToken.from_dict(data)
except (anyenv.JsonLoadError, KeyError, TypeError) as e:
logger.warning("Failed to load token from %s: %s", self.path, e)
return None
else:
return self._token
|
authenticate_openai_codex
authenticate_openai_codex(verbose: bool = True, open_browser: bool = True) -> OpenAICodexToken
Authenticate with OpenAI Codex using OAuth.
Uses a local callback server to capture the authorization code,
with fallback to manual paste.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def authenticate_openai_codex(
verbose: bool = True,
open_browser: bool = True,
) -> OpenAICodexToken:
"""Authenticate with OpenAI Codex using OAuth.
Uses a local callback server to capture the authorization code,
with fallback to manual paste.
"""
verifier, challenge = generate_pkce()
state = secrets.token_hex(16)
if verbose:
print("Starting local server for OAuth callback...")
server, thread = _start_callback_server()
try:
auth_url = build_authorization_url(verifier, challenge, state)
if verbose:
print("\nTo authenticate with ChatGPT Plus/Pro:")
print(f"\n1. Visit: {auth_url}")
print("\n2. Sign in with your OpenAI account")
print("3. The callback will be captured automatically")
print()
if open_browser:
if verbose:
print("Opening browser...")
webbrowser.open(auth_url)
if verbose:
print("Waiting for OAuth callback...")
thread.join(timeout=300)
if _OAuthCallbackHandler.error:
msg = f"OAuth error: {_OAuthCallbackHandler.error}"
raise RuntimeError(msg)
code = _OAuthCallbackHandler.code
cb_state = _OAuthCallbackHandler.state
# Fall back to manual paste
if not code:
print("\nCallback not received. Paste the authorization code or full redirect URL:")
try:
user_input = input("> ").strip()
except KeyboardInterrupt, EOFError:
print("\nAuthentication cancelled.")
msg = "Authentication cancelled by user"
raise RuntimeError(msg) from None
parsed = _parse_authorization_input(user_input)
code = parsed.get("code")
cb_state = parsed.get("state", state)
if not code:
msg = "No authorization code received"
raise RuntimeError(msg)
# Verify state if we got one back
if cb_state and cb_state != state:
msg = "OAuth state mismatch"
raise RuntimeError(msg)
if verbose:
print("\nExchanging code for token...")
token = exchange_code_for_token(code, verifier)
if verbose:
print("Authentication successful!")
print(f"Account ID: {token.account_id}")
return token
finally:
server.server_close()
|
build_authorization_url
build_authorization_url(verifier: str, challenge: str, state: str) -> str
Build the OAuth authorization URL.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def build_authorization_url(verifier: str, challenge: str, state: str) -> str:
"""Build the OAuth authorization URL."""
params = {
"response_type": "code",
"client_id": CLIENT_ID,
"redirect_uri": OAUTH_REDIRECT_URI,
"scope": OAUTH_SCOPE,
"code_challenge": challenge,
"code_challenge_method": "S256",
"state": state,
"id_token_add_organizations": "true",
"codex_cli_simplified_flow": "true",
"originator": "llmling",
}
return f"{OAUTH_AUTHORIZE_URL}?{urlencode(params)}"
|
exchange_code_for_token
Exchange authorization code for access token.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def exchange_code_for_token(code: str, verifier: str) -> OpenAICodexToken:
"""Exchange authorization code for access token."""
with httpx2.Client(timeout=30.0) as client:
response = client.post(
OAUTH_TOKEN_URL,
data={
"grant_type": "authorization_code",
"client_id": CLIENT_ID,
"code": code,
"code_verifier": verifier,
"redirect_uri": OAUTH_REDIRECT_URI,
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
if not response.is_success:
msg = f"Token exchange failed: {response.status_code} - {response.text}"
raise RuntimeError(msg)
data = response.json()
if not data.get("access_token") or not data.get("refresh_token"):
msg = f"Token response missing fields: {data}"
raise RuntimeError(msg)
expires_in = data.get("expires_in", 3600)
expires_at = time.time() + expires_in
account_id = _get_account_id(data["access_token"])
if not account_id:
msg = "Failed to extract account ID from access token"
raise RuntimeError(msg)
return OpenAICodexToken(
access_token=data["access_token"],
refresh_token=data["refresh_token"],
expires_at=expires_at,
account_id=account_id,
)
|
generate_pkce
generate_pkce() -> tuple[str, str]
Generate PKCE code verifier and challenge.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def generate_pkce() -> tuple[str, str]:
"""Generate PKCE code verifier and challenge."""
verifier = secrets.token_urlsafe(32)
digest = hashlib.sha256(verifier.encode()).digest()
challenge = base64.urlsafe_b64encode(digest).rstrip(b"=").decode()
return verifier, challenge
|
get_or_refresh_token
Get a valid token, refreshing if necessary.
Refresh tokens rotate: one refresh at a time across processes (a file lock), and a
token another process refreshed meanwhile is read from the file and used.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def get_or_refresh_token(
store: OpenAICodexTokenStore | None = None,
) -> OpenAICodexToken:
"""Get a valid token, refreshing if necessary.
Refresh tokens rotate: one refresh at a time across processes (a file lock), and a
token another process refreshed meanwhile is read from the file and used.
"""
if store is None:
store = OpenAICodexTokenStore()
token = store.load()
if token is None:
raise RuntimeError(_NO_TOKEN_MSG)
if not token.is_expired():
return token
with file_lock(store.path):
token = store.load(reload=True)
if token is None:
raise RuntimeError(_NO_TOKEN_MSG)
if token.is_expired():
logger.info("Token expired, refreshing...")
token = refresh_access_token(token.refresh_token)
store.save(token)
return token
|
get_or_refresh_token_async
async
Async version of get_or_refresh_token.
Source code in src/llmling_models/auth/openai_codex_auth.py
| async def get_or_refresh_token_async(
store: OpenAICodexTokenStore | None = None,
) -> OpenAICodexToken:
"""Async version of get_or_refresh_token."""
if store is None:
store = OpenAICodexTokenStore()
token = store.load()
if token is None:
raise RuntimeError(_NO_TOKEN_MSG)
if not token.is_expired():
return token
async with store._refresh_lock: # one refresh per process, the file lock for the rest
lock = file_lock(store.path)
await asyncio.to_thread(lock.__enter__)
try:
token = store.load(reload=True)
if token is None:
raise RuntimeError(_NO_TOKEN_MSG)
if token.is_expired():
logger.info("Token expired, refreshing...")
token = await refresh_access_token_async(token.refresh_token)
store.save(token)
finally:
lock.__exit__(None, None, None)
return token
|
openai_codex_auth_main
openai_codex_auth_main() -> None
Command-line entry point for OpenAI Codex OAuth authentication.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def openai_codex_auth_main() -> None:
"""Command-line entry point for OpenAI Codex OAuth authentication."""
parser = argparse.ArgumentParser(
description="Authenticate with OpenAI Codex (ChatGPT Plus/Pro) using OAuth."
)
parser.add_argument(
"--no-browser",
action="store_true",
help="Don't automatically open the browser",
)
parser.add_argument(
"--token-path",
type=Path,
default=DEFAULT_TOKEN_PATH,
help=f"Path to store token (default: {DEFAULT_TOKEN_PATH})",
)
parser.add_argument(
"--logout",
action="store_true",
help="Remove stored token and log out",
)
parser.add_argument(
"--status",
action="store_true",
help="Show current authentication status",
)
args = parser.parse_args()
store = OpenAICodexTokenStore(path=args.token_path)
if args.logout:
store.clear()
print("Logged out. Token removed.")
return
if args.status:
token = store.load()
if token is None:
print("Not authenticated.")
print(f"Token path: {args.token_path}")
sys.exit(1)
elif token.is_expired():
print("Token expired. Run without --status to refresh.")
sys.exit(1)
else:
remaining = token.expires_at - time.time()
hours = int(remaining // 3600)
minutes = int((remaining % 3600) // 60)
print(f"Authenticated. Token expires in {hours}h {minutes}m.")
print(f"Account ID: {token.account_id}")
print(f"Token path: {args.token_path}")
return
try:
token = authenticate_openai_codex(
verbose=True,
open_browser=not args.no_browser,
)
store.save(token)
print(f"\nToken saved to: {args.token_path}")
print("The token can be used by clients of the Codex backend (ChatGPT plan).")
except Exception as e:
logger.exception("Authentication failed")
print(f"\nAuthentication failed: {e}", file=sys.stderr)
sys.exit(1)
|
refresh_access_token
Refresh an expired access token.
Source code in src/llmling_models/auth/openai_codex_auth.py
| def refresh_access_token(refresh_token: str) -> OpenAICodexToken:
"""Refresh an expired access token."""
with httpx2.Client(timeout=30.0) as client:
response = client.post(
OAUTH_TOKEN_URL, data=_refresh_request_data(refresh_token), headers=_REFRESH_HEADERS
)
return _token_from_refresh_response(response)
|
refresh_access_token_async
async
Refresh an expired access token (async).
Source code in src/llmling_models/auth/openai_codex_auth.py
| async def refresh_access_token_async(refresh_token: str) -> OpenAICodexToken:
"""Refresh an expired access token (async)."""
async with httpx2.AsyncClient(timeout=30.0) as client:
response = await client.post(
OAUTH_TOKEN_URL, data=_refresh_request_data(refresh_token), headers=_REFRESH_HEADERS
)
return _token_from_refresh_response(response)
|