Ushka

ushka.auth.providers

OAuth 2.0 / OpenID Connect providers: Google and GitHub, on the standard library.

A Provider knows its endpoints, how to build the authorization URL, how to trade the code for a token and how to turn the token into an Identity. Endpoints are class attributes: a test points them at a fake server by subclassing or monkeypatching; they are never read from the user's configuration, so a setting can not send the client secret somewhere else.

The HTTP transport is urllib run in a thread: TLS verification on, a timeout, a cap on the size of the answer, and redirects are never followed.

In this module

class OAuthErrorA login that failed. code is a short, fixed word safe to show in a URL.
class IdentityWho the provider says the user is (already verified).
def new_token()
def pkce_challenge()The S256 code challenge of verifier (RFC 7636).
class ProviderBase of the providers. Subclass it and set the endpoints to add another one.
class GoogleGoogle, as OpenID Connect: the userinfo endpoint with the access token from the token endpoint.
class GitHubGitHub: the user, and the primary verified e-mail address.

class OAuthError (Exception)

A login that failed. code is a short, fixed word safe to show in a URL.

The message never holds text written by the provider or any secret.

class Identity

Who the provider says the user is (already verified).

profile

def profile() -> dict[str, Any]

The minimal profile kept in the session.

email_verified is always true: a login with an unverified address is refused.

def new_token

def new_token(nbytes: int = 32) -> str

def pkce_challenge

def pkce_challenge(verifier: str) -> str

The S256 code challenge of verifier (RFC 7636).

class Provider

Base of the providers. Subclass it and set the endpoints to add another one.

Methods

installapp.use(Google()): the provider is on and its login routes are served.
authorization_url
extra_params
exchangeTrades the authorization code for a token response (client secret: server to server).
identity

install

def install(app: Any) -> None

app.use(Google()): the provider is on and its login routes are served.

The id and the secret come from USHKA_OAUTH_<NAME>_CLIENT_ID and ..._CLIENT_SECRET unless you give the id here. The secret is never accepted in code. allowed_domains is [oauth] allowed_email_domains; **options are any other keys of [oauth].

authorization_url

def authorization_url(
    redirect_uri: str,
    state: str,
    verifier: str,
    nonce: str,
    domains: list[str],
) -> str

extra_params

def extra_params(domains: list[str]) -> dict[str, str]

exchange

async def exchange(code: str, verifier: str, redirect_uri: str) -> dict

Trades the authorization code for a token response (client secret: server to server).

identity

async def identity(token: dict, nonce: str) -> Identity

class Google (Provider)

Google, as OpenID Connect: the userinfo endpoint with the access token from the token endpoint.

extra_params

def extra_params(domains: list[str]) -> dict[str, str]

identity

async def identity(token: dict, nonce: str) -> Identity

class GitHub (Provider)

GitHub: the user, and the primary verified e-mail address.

identity

async def identity(token: dict, nonce: str) -> Identity