Skip to main content

H2O Python Clients Authentication Helpers

Project description

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 used for the access token exchange.
  • client_id: OAuth 2.0 client id that will be used or 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 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.
  • client_secret: Optional OAuth 2.0 client secret for the confidential clients. Used only when provided.
  • scope: Optionally sets the the scope for which the access token should be requested.
  • expiry_threshold: How long before token expiration should token be refreshed when needed. This does not mean that the token will be refreshed before it expires, 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 token response does not contains expires_in field.
  • minimal_refresh_period: Optionally minimal period between the earliest token refresh exchanges.

Both classes has identical interface in sync or async variant.

provider = h2o_authn.TokenProvider(...)
aprovider = h2o_authn.AsyncTokenProvider(...)


# Calling the providers directly makes sure that 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 used as 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 server provided it.


# Sync/Async variants can be converted from one to another.
provider = aprovider.as_sync()
aprovider = provider.as_async()


# When access token with different scope is needed new instance can cloned from
# the current with 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.as_async())

    ...

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()

...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

h2o-authn-0.1.0a2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

h2o_authn-0.1.0a2-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file h2o-authn-0.1.0a2.tar.gz.

File metadata

  • Download URL: h2o-authn-0.1.0a2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for h2o-authn-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 f3bc6ae4f039438f2c7967e222c9a767623ccb80e577dfe2c7376227c0424f27
MD5 bbce3eb766306b12bc2232bf1b4e5b93
BLAKE2b-256 aebbd0380f86ebff2930a3a9b73d2bffacea1392bd94ce425893cd1689dac712

See more details on using hashes here.

File details

Details for the file h2o_authn-0.1.0a2-py3-none-any.whl.

File metadata

  • Download URL: h2o_authn-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for h2o_authn-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 32bf9eafb35d2aed7d1a4b341df9ef0c08b6f3bd946cc02e09a4007341b07a79
MD5 00e782425f08e7207fb8bbcb10c62788
BLAKE2b-256 e848a9ee06507907aee7dbe350730e6767bc0cb26e019c5d8596fa7a624c37a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page