Skip to content

CopilotAuthBackend

Base classes

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

⋔ Inheritance diagram

graph TD
  500858304["github_auth.CopilotAuthBackend"]
  1157112198["base.ProviderAuthBackend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  1157112198 --> 500858304
  925645236 --> 1157112198
  2751680898 --> 925645236

🛈 DocStrings

Bases: ProviderAuthBackend

GitHub Copilot device-code auth backend.

Source code in src/llmling_models/auth/github_auth.py
class CopilotAuthBackend(ProviderAuthBackend):
    """GitHub Copilot device-code auth backend."""

    def __init__(self) -> None:
        self._pending_device_codes: dict[str, str] = {}

    @property
    def provider_id(self) -> str:
        return "copilot"

    def methods(self) -> list[ProviderAuthMethod]:
        return [ProviderAuthMethod(type="oauth", label="Connect GitHub Copilot")]

    async def authorize(self, method: int = 0) -> ProviderAuthAuthorization:
        async with httpx2.AsyncClient(timeout=30.0) as client:
            resp = await client.post(
                "https://github.com/login/device/code",
                headers=COPILOT_HEADERS,
                json={"client_id": COPILOT_CLIENT_ID, "scope": "read:user"},
            )
            resp.raise_for_status()
            data: dict[str, Any] = resp.json()

        device_code = data["device_code"]
        user_code = data["user_code"]
        verification_uri = data["verification_uri"]
        self._pending_device_codes[device_code] = device_code

        return ProviderAuthAuthorization(
            url=verification_uri,
            instructions=f"Enter code: {user_code}",
            method="auto",
        )

    async def callback(
        self,
        *,
        code: str | None = None,
        device_code: str | None = None,
        verifier: str | None = None,
    ) -> bool:
        if not device_code:
            msg = "Missing device_code for Copilot OAuth"
            raise ValueError(msg)
        async with httpx2.AsyncClient(timeout=30.0) as client:
            resp = await client.post(
                "https://github.com/login/oauth/access_token",
                headers=COPILOT_HEADERS,
                data={
                    "client_id": COPILOT_CLIENT_ID,
                    "device_code": device_code,
                    "grant_type": "urn:ietf:params:oauth:grant-type:device_code",
                },
            )
            data: dict[str, Any] = resp.json()

        if "error" in data:
            detail = data.get("error_description", data["error"])
            raise ValueError(detail)

        github_token = data.get("access_token")
        if not github_token:
            raise ValueError("No token received")

        self._pending_device_codes.pop(device_code, None)

        # Exchange GitHub token for Copilot API token and store
        copilot_data = refresh_copilot_token(github_token)
        store = CopilotTokenStore()
        store.save({
            "github_token": github_token,
            "copilot_token": copilot_data["copilot_token"],
            "base_url": copilot_data["base_url"],
            "expires_at": copilot_data["expires_at"],
            "enterprise_domain": None,
        })
        return True

:fa-brands-github: Show source on GitHub