Skip to content

GitHub

reauth.factors.oauth2.github

GitHubOAuth2Extra

Bases: TypedDict

Extra parameters for GitHub OAuth2 authorization URL.

References
  • GitHub: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps

login instance-attribute

login: str

allow_signup instance-attribute

allow_signup: Literal['true', 'false']

prompt instance-attribute

prompt: Literal['select_account']

GitHubEmail

Bases: TypedDict

GitHub email address object from the emails endpoint.

email instance-attribute

email: str

primary instance-attribute

primary: bool

verified instance-attribute

verified: bool

visibility instance-attribute

visibility: Literal['public', 'private']

GitHubOAuth2GetEmailsException

GitHubOAuth2GetEmailsException(message: str | None = None)

Bases: OAuth2Exception

Raised when fetching the email addresses from GitHub fails.

GitHubOAuth2Factor

GitHubOAuth2Factor(
    *,
    identifier: str = "github",
    client_id: str,
    client_secret: str,
    state_service: OAuth2StateService,
    step: int = 0,
    advance_by: int = 1,
)

Bases: OAuth2Factor[GitHubOAuth2Extra], ABC

GitHub OAuth2 factor implementation, using the standard OAuth2 protocol.

Depending on the user settings, emails may not be included in the profile response. To get all the emails associated with the account, use the get_emails method with the access token. The scope user:email is required to access the emails endpoint.

References
  • GitHub: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps

AUTHORIZATION_ENDPOINT class-attribute instance-attribute

AUTHORIZATION_ENDPOINT = (
    "https://github.com/login/oauth/authorize"
)

TOKEN_ENDPOINT class-attribute instance-attribute

TOKEN_ENDPOINT = (
    "https://github.com/login/oauth/access_token"
)

PROFILE_ENDPOINT class-attribute instance-attribute

PROFILE_ENDPOINT = 'https://api.github.com/user'

EMAILS_ENDPOINT class-attribute instance-attribute

EMAILS_ENDPOINT = 'https://api.github.com/user/emails'

get_request_authentication async

get_request_authentication(
    *, token_endpoint: str
) -> tuple[dict[str, str], dict[str, str]]

get_authorization_url async

get_authorization_url(
    *,
    redirect_uri: str,
    scope: list[str] | None = None,
    state: str,
    code_challenge: str | None = None,
    code_challenge_method: CodeChallengeMethod
    | None = None,
    nonce: str | None = None,
    extra: GitHubOAuth2Extra | None = None,
) -> str

exchange_code async

exchange_code(
    *,
    code: str,
    redirect_uri: str,
    code_verifier: str | None = None,
    nonce: str | None = None,
    state: OAuth2State,
) -> TokenResponse

get_profile async

get_profile(access_token: str) -> dict[str, typing.Any]

get_emails async

get_emails(access_token: str) -> list[GitHubEmail]

Fetch the list of email addresses associated with the GitHub account.

This requires the user:email scope to be granted by the user during authorization.

Parameters:

Name Type Description Default
access_token str

The OAuth2 access token for making authenticated requests.

required

Returns:

Type Description
list[GitHubEmail]

A list of GitHubEmail objects representing the email addresses associated with the account.

Raises:

Type Description
GitHubOAuth2GetEmailsException

If the request to fetch emails fails.

get_primary_email

get_primary_email(emails: list[GitHubEmail]) -> str

Get the primary email address from a list of GitHubEmail objects.

Parameters:

Name Type Description Default
emails list[GitHubEmail]

A list of GitHubEmail objects representing the email addresses associated with the account.

required

Returns:

Type Description
str

The primary email address.

Raises:

Type Description
ValueError

If no primary email is found in the list.