H2O Python Clients Authentication Helpers
Maintained. Last release 3 days ago, commits in 9 of the last 26 weeks. Weaker on release cadence and contributor spread.
pip install h2o-authnh2o-authn
H2O Python Clients Authentication Helpers.
Installation
pip install h2o-authn
Usage
Package provides two top level classes h2o_authn.TokenProvider and h2o_authn.AsyncTokenProvider with identical constructors accepting following arguments:
refresh_token: Refresh token which will be used for the access token exchange.client_id: OAuth 2.0 client id that will be used for the access token exchange.issuer_urlortoken_endpoint_urlneeds to be providedissuer_url: Base URL of the issuer. This URL will be used for the discovery to obtain the token endpoint. Mutually exclusive with the token_endpoint_url argument.token_endpoint_url: URL of the token endpoint that should be used for the access token exchange. Mutually exclusive with the issuer_url argument. If not provided explicitly, the value of theH2O_CLOUD_TOKEN_ENDPOINT_URLenvironment variable is used if set.
client_secret: Optional OAuth 2.0 client secret for the confidential clients. Used only when provided.scope: Optionally sets the scope for which the access token should be requested.expiry_threshold: How long before token expiration should the token be refreshed when needed. This does not mean that the token will be refreshed before it expires, but only indicates the earliest moment before the expiration when refresh would occur. (default: 5s)expires_in_fallback: Fallback value for the expires_in value. Will be used when the token response does not contain the expires_in field.minimal_refresh_period: Optionally, the minimal period between the earliest token refresh exchanges.
Both classes have an identical interface in sync and async variants.
provider = h2o_authn.TokenProvider(...)
aprovider = h2o_authn.AsyncTokenProvider(...)
# Calling the providers directly makes sure that a fresh access token is available
# and returns it.
access_token = provider()
access_token = await aprovider()
# Calling the token() returns h2o_authn.token.Token instance.
token = provider.token()
token = await aprovider.token()
# It can be used as a str.
assert token == access_token
# And contains additional attributes when available.
token.exp # Is expiration of the token as datetime.datetime
token.scope # Is scope of the token if the server provided it.
# Sync/Async variants can be converted from one to another.
provider = aprovider.as_sync()
aprovider = provider.as_async()
# When an access token with a different scope is needed, a new instance can be cloned from
# the current with a different scope.
provider = provider.with_scope("new scopes")
aprovider = aprovider.with_scope("new scopes")