pywry.auth.providers¶
OAuth2 provider abstractions and concrete implementations.
OAuthProvider (ABC)¶
pywry.auth.providers.OAuthProvider
¶
OAuthProvider(client_id: str, client_secret: str = '', scopes: list[str] | None = None, authorize_url: str = '', token_url: str = '', userinfo_url: str = '', revocation_url: str = '')
Bases: ABC
Abstract base class for OAuth2 providers.
| PARAMETER | DESCRIPTION |
|---|---|
client_id
|
The OAuth2 client ID.
TYPE:
|
client_secret
|
The OAuth2 client secret (empty string for public clients).
TYPE:
|
scopes
|
Requested OAuth2 scopes.
TYPE:
|
authorize_url
|
The provider's authorization endpoint.
TYPE:
|
token_url
|
The provider's token exchange endpoint.
TYPE:
|
userinfo_url
|
The provider's userinfo endpoint (optional for non-OIDC).
TYPE:
|
revocation_url
|
The provider's token revocation endpoint (RFC 7009).
TYPE:
|
Initialize OAuth provider.
Functions¶
build_authorize_url
¶
build_authorize_url(redirect_uri: str, state: str, pkce: PKCEChallenge | None = None, extra_params: dict[str, str] | None = None) -> str
Build the full authorization URL.
| PARAMETER | DESCRIPTION |
|---|---|
redirect_uri
|
The callback URL to redirect to after authorization.
TYPE:
|
state
|
CSRF protection nonce.
TYPE:
|
pkce
|
PKCE challenge for public clients.
TYPE:
|
extra_params
|
Additional query parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The full authorization URL. |
exchange_code
abstractmethod
async
¶
exchange_code(code: str, redirect_uri: str, pkce_verifier: str | None = None, nonce: str | None = None) -> OAuthTokenSet
Exchange authorization code for tokens.
| PARAMETER | DESCRIPTION |
|---|---|
code
|
The authorization code from the callback.
TYPE:
|
redirect_uri
|
The redirect URI used in the authorization request.
TYPE:
|
pkce_verifier
|
The PKCE code verifier if PKCE was used.
TYPE:
|
nonce
|
The nonce sent in the authorize request (for ID token validation).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OAuthTokenSet
|
The token set from the provider. |
| RAISES | DESCRIPTION |
|---|---|
TokenError
|
If the code exchange fails. |
refresh_tokens
abstractmethod
async
¶
Refresh an expired access token.
| PARAMETER | DESCRIPTION |
|---|---|
refresh_token
|
The refresh token.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OAuthTokenSet
|
A new token set with a fresh access token. |
| RAISES | DESCRIPTION |
|---|---|
TokenRefreshError
|
If the refresh fails. |
get_userinfo
async
¶
Fetch user profile information from the provider.
| PARAMETER | DESCRIPTION |
|---|---|
access_token
|
A valid access token.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
User profile data from the provider. |
revoke_token
async
¶
Revoke a token at the provider (RFC 7009).
Posts to the revocation_url if one is configured.
Subclasses with non-standard revocation APIs should override.
| PARAMETER | DESCRIPTION |
|---|---|
token
|
The token to revoke (access or refresh).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if revocation succeeded, False if no endpoint is configured or the request failed. |
GenericOIDCProvider¶
pywry.auth.providers.GenericOIDCProvider
¶
GenericOIDCProvider(client_id: str, client_secret: str = '', issuer_url: str = '', scopes: list[str] | None = None, authorize_url: str = '', token_url: str = '', userinfo_url: str = '', revocation_url: str = '', *, require_id_token_validation: bool = True)
Bases: OAuthProvider
Generic OpenID Connect provider.
Supports auto-discovery from /.well-known/openid-configuration
if an issuer_url is provided.
| PARAMETER | DESCRIPTION |
|---|---|
client_id
|
The OAuth2 client ID.
TYPE:
|
client_secret
|
The OAuth2 client secret.
TYPE:
|
issuer_url
|
The OIDC issuer URL (used for auto-discovery).
TYPE:
|
scopes
|
Requested scopes (defaults to
TYPE:
|
Initialize OIDC provider.
Functions¶
validate_id_token
async
¶
Validate an OIDC ID token.
Checks signature (via JWKS), issuer, audience, expiry, and nonce.
| PARAMETER | DESCRIPTION |
|---|---|
id_token
|
The raw ID token JWT string.
TYPE:
|
nonce
|
Expected nonce value (if one was sent in the authorize request).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The validated claims from the ID token. |
| RAISES | DESCRIPTION |
|---|---|
TokenError
|
If validation fails for any reason. |
exchange_code
async
¶
exchange_code(code: str, redirect_uri: str, pkce_verifier: str | None = None, nonce: str | None = None) -> OAuthTokenSet
Exchange authorization code for tokens via OIDC token endpoint.
| PARAMETER | DESCRIPTION |
|---|---|
code
|
The authorization code from the callback.
TYPE:
|
redirect_uri
|
The redirect URI used in the authorization request.
TYPE:
|
pkce_verifier
|
The PKCE code verifier if PKCE was used.
TYPE:
|
nonce
|
The nonce sent in the authorize request (for ID token validation).
TYPE:
|
refresh_tokens
async
¶
Refresh tokens via OIDC token endpoint.
revoke_token
async
¶
Revoke a token via the OIDC revocation endpoint.
Triggers endpoint auto-discovery before delegating to the base implementation.
build_authorize_url
¶
build_authorize_url(redirect_uri: str, state: str, pkce: PKCEChallenge | None = None, extra_params: dict[str, str] | None = None) -> str
Build the full authorization URL.
| PARAMETER | DESCRIPTION |
|---|---|
redirect_uri
|
The callback URL to redirect to after authorization.
TYPE:
|
state
|
CSRF protection nonce.
TYPE:
|
pkce
|
PKCE challenge for public clients.
TYPE:
|
extra_params
|
Additional query parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The full authorization URL. |
get_userinfo
async
¶
Fetch user profile information from the provider.
| PARAMETER | DESCRIPTION |
|---|---|
access_token
|
A valid access token.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
User profile data from the provider. |
GoogleProvider¶
pywry.auth.providers.GoogleProvider
¶
Bases: GenericOIDCProvider
Google OAuth2 provider with preset endpoints.
| PARAMETER | DESCRIPTION |
|---|---|
client_id
|
Google OAuth2 client ID.
TYPE:
|
client_secret
|
Google OAuth2 client secret.
TYPE:
|
scopes
|
Requested scopes (defaults to openid, email, profile).
TYPE:
|
Initialize Google provider.
Functions¶
build_authorize_url
¶
build_authorize_url(redirect_uri: str, state: str, pkce: PKCEChallenge | None = None, extra_params: dict[str, str] | None = None) -> str
Build Google authorization URL with access_type=offline.
exchange_code
async
¶
exchange_code(code: str, redirect_uri: str, pkce_verifier: str | None = None, nonce: str | None = None) -> OAuthTokenSet
Exchange authorization code for tokens via OIDC token endpoint.
| PARAMETER | DESCRIPTION |
|---|---|
code
|
The authorization code from the callback.
TYPE:
|
redirect_uri
|
The redirect URI used in the authorization request.
TYPE:
|
pkce_verifier
|
The PKCE code verifier if PKCE was used.
TYPE:
|
nonce
|
The nonce sent in the authorize request (for ID token validation).
TYPE:
|
refresh_tokens
async
¶
Refresh tokens via OIDC token endpoint.
get_userinfo
async
¶
Fetch user profile information from the provider.
| PARAMETER | DESCRIPTION |
|---|---|
access_token
|
A valid access token.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
User profile data from the provider. |
revoke_token
async
¶
Revoke a token via the OIDC revocation endpoint.
Triggers endpoint auto-discovery before delegating to the base implementation.
validate_id_token
async
¶
Validate an OIDC ID token.
Checks signature (via JWKS), issuer, audience, expiry, and nonce.
| PARAMETER | DESCRIPTION |
|---|---|
id_token
|
The raw ID token JWT string.
TYPE:
|
nonce
|
Expected nonce value (if one was sent in the authorize request).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The validated claims from the ID token. |
| RAISES | DESCRIPTION |
|---|---|
TokenError
|
If validation fails for any reason. |
GitHubProvider¶
pywry.auth.providers.GitHubProvider
¶
Bases: OAuthProvider
GitHub OAuth2 provider.
GitHub uses a non-standard token exchange (no OIDC) and a separate API endpoint for user info.
| PARAMETER | DESCRIPTION |
|---|---|
client_id
|
GitHub OAuth2 client ID.
TYPE:
|
client_secret
|
GitHub OAuth2 client secret.
TYPE:
|
scopes
|
Requested scopes (defaults to
TYPE:
|
Initialize GitHub provider.
Functions¶
exchange_code
async
¶
exchange_code(code: str, redirect_uri: str, pkce_verifier: str | None = None, nonce: str | None = None) -> OAuthTokenSet
Exchange authorization code for a GitHub access token.
refresh_tokens
async
¶
Refresh GitHub tokens (GitHub uses token rotation).
revoke_token
async
¶
Revoke a GitHub token via the Applications API.
Uses DELETE /applications/{client_id}/token with HTTP
Basic authentication (client_id / client_secret).
| PARAMETER | DESCRIPTION |
|---|---|
token
|
The access token to revoke.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if GitHub accepted the revocation. |
build_authorize_url
¶
build_authorize_url(redirect_uri: str, state: str, pkce: PKCEChallenge | None = None, extra_params: dict[str, str] | None = None) -> str
Build the full authorization URL.
| PARAMETER | DESCRIPTION |
|---|---|
redirect_uri
|
The callback URL to redirect to after authorization.
TYPE:
|
state
|
CSRF protection nonce.
TYPE:
|
pkce
|
PKCE challenge for public clients.
TYPE:
|
extra_params
|
Additional query parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The full authorization URL. |
get_userinfo
async
¶
Fetch user profile information from the provider.
| PARAMETER | DESCRIPTION |
|---|---|
access_token
|
A valid access token.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
User profile data from the provider. |
MicrosoftProvider¶
pywry.auth.providers.MicrosoftProvider
¶
MicrosoftProvider(client_id: str, client_secret: str = '', tenant_id: str = 'common', scopes: list[str] | None = None)
Bases: GenericOIDCProvider
Microsoft / Azure AD OAuth2 provider.
| PARAMETER | DESCRIPTION |
|---|---|
client_id
|
Azure AD application (client) ID.
TYPE:
|
client_secret
|
Azure AD client secret.
TYPE:
|
tenant_id
|
Azure AD tenant ID (default "common" for multi-tenant).
TYPE:
|
scopes
|
Requested scopes.
TYPE:
|
Initialize Microsoft provider.
Functions¶
build_authorize_url
¶
build_authorize_url(redirect_uri: str, state: str, pkce: PKCEChallenge | None = None, extra_params: dict[str, str] | None = None) -> str
Build the full authorization URL.
| PARAMETER | DESCRIPTION |
|---|---|
redirect_uri
|
The callback URL to redirect to after authorization.
TYPE:
|
state
|
CSRF protection nonce.
TYPE:
|
pkce
|
PKCE challenge for public clients.
TYPE:
|
extra_params
|
Additional query parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The full authorization URL. |
exchange_code
async
¶
exchange_code(code: str, redirect_uri: str, pkce_verifier: str | None = None, nonce: str | None = None) -> OAuthTokenSet
Exchange authorization code for tokens via OIDC token endpoint.
| PARAMETER | DESCRIPTION |
|---|---|
code
|
The authorization code from the callback.
TYPE:
|
redirect_uri
|
The redirect URI used in the authorization request.
TYPE:
|
pkce_verifier
|
The PKCE code verifier if PKCE was used.
TYPE:
|
nonce
|
The nonce sent in the authorize request (for ID token validation).
TYPE:
|
refresh_tokens
async
¶
Refresh tokens via OIDC token endpoint.
get_userinfo
async
¶
Fetch user profile information from the provider.
| PARAMETER | DESCRIPTION |
|---|---|
access_token
|
A valid access token.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
User profile data from the provider. |
revoke_token
async
¶
Revoke a token via the OIDC revocation endpoint.
Triggers endpoint auto-discovery before delegating to the base implementation.
validate_id_token
async
¶
Validate an OIDC ID token.
Checks signature (via JWKS), issuer, audience, expiry, and nonce.
| PARAMETER | DESCRIPTION |
|---|---|
id_token
|
The raw ID token JWT string.
TYPE:
|
nonce
|
Expected nonce value (if one was sent in the authorize request).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The validated claims from the ID token. |
| RAISES | DESCRIPTION |
|---|---|
TokenError
|
If validation fails for any reason. |
Factory¶
pywry.auth.providers.create_provider_from_settings
¶
create_provider_from_settings(settings: Any) -> OAuthProvider
Create an OAuthProvider instance from OAuth2Settings.
| PARAMETER | DESCRIPTION |
|---|---|
settings
|
The OAuth2 configuration settings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OAuthProvider
|
A configured provider instance. |
| RAISES | DESCRIPTION |
|---|---|
AuthenticationError
|
If the provider type is unknown or settings are invalid. |