Skip to main content

OAuth2Protocol

Protocol for OAuth2/OIDC provider implementations.

Each provider (Google, GitHub, etc.) implements this protocol
to handle the OAuth2 flow.

Methods:
get_authorization_url: Generate URL to redirect user to provider
exchange_code: Exchange authorization code for tokens
get_user_info: Fetch user info from provider using token

Example:
provider = GoogleOAuth2(client_id="...", client_secret="...")
auth_url = await provider.get_authorization_url(state="abc123")
# User visits auth_url and authorizes
token = await provider.exchange_code(code="code-from-callback")
user_info = await provider.get_user_info(token)

Source: oauth.py

Methods

provider_name

def provider_name(self) -> str

Return the provider name (e.g., 'google', 'github').

get_authorization_url

async def get_authorization_url(self,
state: str,
redirect_uri: str,
) -> str

Generate authorization URL to redirect user to provider.

    Args:
state: CSRF protection state parameter
redirect_uri: URL to redirect back after authorization

Returns:
Full authorization URL

exchange_code

async def exchange_code(self,
code: str,
redirect_uri: str,
) -> OAuth2Token

Exchange authorization code for access token.

    Args:
code: Authorization code from callback
redirect_uri: Must match the original redirect_uri

Returns:
OAuth2Token with access_token

Raises:
OAuth2Error: If exchange fails

get_user_info

async def get_user_info(self, token: OAuth2Token) -> OAuth2UserInfo

Fetch user information from provider.

    Args:
token: OAuth2Token from exchange_code

Returns:
OAuth2UserInfo with provider-specific user data

Raises:
OAuth2Error: If fetch fails