Skip to content

OpenAICodexAuthBackend

Base classes

Name Children Inherits
ProviderAuthBackend
llmling_models.auth.base
Protocol for a provider-specific auth backend.

⋔ Inheritance diagram

graph TD
  644175574["openai_codex_auth.OpenAICodexAuthBackend"]
  1157112198["base.ProviderAuthBackend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  1157112198 --> 644175574
  925645236 --> 1157112198
  2751680898 --> 925645236

🛈 DocStrings

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

:fa-brands-github: Show source on GitHub