Skip to content

OAuth2TokenProvider

OAuth2TokenProvider

OAuth2TokenProvider(
    refresh_token: str,
    client_id: str,
    token_endpoint_url: str,
    token_introspection_url: str,
    access_token: Optional[str] = None,
    client_secret: Optional[str] = None,
    *,
    refresh_expiry_threshold_band: timedelta = datetime.timedelta(seconds=5)
)

Provide an OAuth 2.0 token.

Parameters:

  • refresh_token (str) –

    The refresh token (from the Driverless AI server web UI) used to obtain new access tokens.

  • client_id (str) –

    The client identifier as assigned by the authorization server.

  • token_endpoint_url (str) –

    The URL of the authorization server's token endpoint.

  • token_introspection_url (str) –

    The URL of the authorization server's token introspection endpoint.

  • access_token (Optional[str], default: None ) –

    The initial access token, if available.

  • client_secret (Optional[str], default: None ) –

    The client secret, if required for the client credentials.

  • refresh_expiry_threshold_band (timedelta, default: timedelta(seconds=5) ) –

    The time duration before actual expiry when the access token should be refreshed.

Example
# Create a token provider with a refresh token from the Driverless AI web UI
token_provider = driverlessai.token_providers.OAuth2TokenProvider(
    refresh_token="eyJhbGciOiJIUzI1N...",
    client_id="python_client",
    token_endpoint_url="https://keycloak-server/auth/realms/driverlessai/protocol/openid-connect/token",
    token_introspection_url="https://keycloak-server/auth/realms/driverlessai/protocol/openid-connect/token/introspect"
)

# Use the token provider to get authorization to connect to the server
client = driverlessai.Client(
    address="https://localhost:12345",
    token_provider=token_provider.ensure_fresh_token
)

do_refresh

do_refresh() -> None

Refreshes the current access token.

ensure_fresh_token

ensure_fresh_token() -> str

Returns an access token ensuring that it is fresh and valid.

Returns:

  • str

    The current access token.

refresh_possible

refresh_possible() -> bool

Returns whether it is possible to refresh the access token.

Returns:

  • bool

    True if possible to refresh the access token, otherwise False.

refresh_required

refresh_required() -> bool

Returns whether the current access token needs to be refreshed.

Returns:

  • bool

    True if the current access token needs to be refreshed, otherwise False.