Skip to main content

h2o-authn

licence pypi

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_url or token_endpoint_url needs to be provided
    • issuer_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 the H2O_CLOUD_TOKEN_ENDPOINT_URL environment 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")

Examples

Example: Use with H2O.ai MLOps Python CLient

import h2o_authn
import h2o_mlops_client as mlops

provider = h2o_authn.TokenProvider(...)
mlops_client = mlops.Client(
    gateway_url="https://mlops-api.cloud.h2o.ai", token_provider=provider
)
...

Example: Use with H2O.ai Drive Python Client within the Wave App

import h2o_authn
import h2o_drive
from h2o_wave import Q, app, ui
from h2o_wave import main


@app("/")
async def serve(q: Q):
    provider = h2o_authn.AsyncTokenProvider(
        refresh_token=q.auth.refresh_token,
        issuer_url=os.getenv("H2O_WAVE_OIDC_PROVIDER_URL"),
        client_id=os.getenv("H2O_WAVE_OIDC_CLIENT_ID"),
        client_secret=os.getenv("H2O_WAVE_OIDC_CLIENT_SECRET"),
    )
    my_home = await h2o_drive.MyHome(token=provider)

    ...

Example: Use with H2O.ai Enterprise Steam Python Client

import h2o_authn
import h2osteam
import h2osteam.clients

provider = h2o_authn.TokenProvider(...)

h2osteam.login(url="https://steam.cloud-dev.h2o.ai", access_token=provider())
client = h2osteam.clients.DriverlessClient()

...

H2O Cloud Discovery support

If you use the token provider to access H2O.ai services running in your H2O AI Cloud environment, you can simplify the configuration by using h2o-authn[discovery] extension.

For more info regarding H2O Cloud Discovery, please see H2O Cloud Discovery Client

pip install h2o-authn[discovery]

Module h2o_authn.discovery provides new and new_async functions which accepts following arguments:

  • discovery: The Discovery object to use for configuration.
  • client: The name of the client to use for configuration. Defaults to "platform".
  • scope: The scope to use for the token requests.
  • 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.

Example: Use of the H2O Cloud Discovery with H2O.ai MLOps Python CLient

import h2o_authn.discovery
import h2o_discovery
import h2o_mlops_client as mlops


discovery = h2o_discovery.discover()

provider = h2o_authn.discovery.create(discovery)

mlops_client = mlops.Client(
    gateway_url=discovery.services["mlops-api"].uri, token_provider=provider
)
...

Development

See RELEASE.md for instructions on how to build and release.

Metadata

Release files for h2o-authn 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for h2o-authn 4.0.0
File Size Uploaded Provenance Actions
h2o_authn-4.0.0.tar.gz 18.1 kB Without provenance Details for h2o_authn-4.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for h2o-authn 4.0.0
File Interpreter ABI Platform Actions
h2o_authn-4.0.0-py3-none-any.whl Python 3 none any Details for h2o_authn-4.0.0-py3-none-any.whl

Total release size: 31.0 kB

Release files / h2o_authn-4.0.0.tar.gz

Download URL h2o_authn-4.0.0.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
fe90adcfbc86d0c955df887719754ce15c801a3b6508425aa2c82c39d85985b0
BLAKE2b-256 checksum
How to use checksums
4476afa6ca4dd38afa4a08c33801573ebbba277db46ba695da00fea76fd8ede6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / h2o_authn-4.0.0-py3-none-any.whl

Download URL h2o_authn-4.0.0-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6568497a3060f8cb2e9409070f70760ea705f4bb1f5fe9e3348244e42875c7b8
BLAKE2b-256 checksum
How to use checksums
31fab5e898a4d8a1bdca81fa5ce1d4aff1fd2b592052ae7f2d651d6f63acb11f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Security for h2o-authn 4.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page