cmem_client.auth_provider.abc¤
Abstract base class and factory for authentication providers.
This module defines the AuthProvider abstract base class that establishes the interface all authentication providers must implement. It also provides a factory method that automatically selects the appropriate authentication provider based on environment variables.
The factory method supports automatic configuration from environment variables, making it easy to switch between different authentication methods without code changes by simply setting the OAUTH_GRANT_TYPE environment variable.
Classes:
- AuthProvider – Abstract base class for authentication providers.
Attributes:
AuthProvider¤
Bases: ABC
Abstract base class for authentication providers.
AuthProvider defines the common interface that all authentication providers must implement to work with the Corporate Memory client. It provides the contract for obtaining access tokens and includes a factory method for creating appropriate provider instances based on environment configuration.
All concrete authentication provider implementations must inherit from this class and implement the get_access_token method. The class also provides automatic provider selection through environment variables.
Functions:
- from_cmempy – Create an authentication provider from a cmempy environment.
- from_context – Create an authentication provider from a cmem-plugin-base context object.
- from_dict – Create an authentication provider from a plain dictionary.
- from_env – Create an authentication provider from environment variables.
- get_access_token – Get the access token for Bearer Authorization header.
Attributes:
- logger (
Logger) – The logger for the auth provider. - preferred_username (
str) – The preferred username for the authentication provider.
from_cmempy¤
Create an authentication provider from a cmempy environment.
from_context¤
Create an authentication provider from a cmem-plugin-base context object.
Wraps the token callable exposed by the context’s UserContext in a
ProvidedToken provider.
Parameters:
- context (
object) – AnExecutionContextorPluginContextinstance fromcmem-plugin-base. Must expose auserattribute (UserContext) with atoken()method that returns a valid bearer token.
Returns:
AuthProvider– AProvidedTokenauthentication provider backed by theAuthProvider– context’sUserContext.token()method.
from_dict¤
Create an authentication provider from a plain dictionary.
Selects and configures the appropriate authentication provider based
on the OAUTH_GRANT_TYPE key in the dictionary, defaulting to
"client_credentials" when not specified.
Parameters:
- config (
Config) – Configuration object containing Corporate Memory connection details and endpoint URLs. - d (
dict[str, str]) – Dictionary of configuration values. TheOAUTH_GRANT_TYPEkey controls which provider is created. Remaining keys are forwarded to the selected provider’sfrom_dictfactory.
Returns:
AuthProvider– A configured AuthProvider instance.
Raises:
ClientEnvConfigError– IfOAUTH_GRANT_TYPEis not a supported value or if required keys for the selected provider are missing.
from_env¤
Create an authentication provider from environment variables.
This factory method automatically selects and configures the appropriate authentication provider based on the OAUTH_GRANT_TYPE environment variable. It supports multiple OAuth 2.0 flows and authentication methods.
Parameters:
- config (
Config) – Configuration object containing Corporate Memory connection details and endpoint URLs.
Returns:
AuthProvider– A configured AuthProvider instance appropriate for the environmentAuthProvider– configuration.
Raises:
ClientEnvConfigError– If the OAUTH_GRANT_TYPE is not supported or if required environment variables for the selected provider are missing.
Environment Variables
OAUTH_GRANT_TYPE (optional): The OAuth flow type. Defaults to “client_credentials”. Supported values: - “client_credentials”: Client Credentials Flow for M2M auth - “password”: Resource Owner Password Flow for trusted apps - “prefetched_token”: Use externally obtained access token
get_access_token¤
Get the access token for Bearer Authorization header.
Also sets the preferred username for the authentication provider via the extracted token.
Returns:
str– A valid access token string.
Raises:
ValueError– If the provider returned no access token.
Note
Implementations should handle token refresh logic internally when tokens expire, ensuring this method always returns a valid token.
logger¤
The logger for the auth provider.
preferred_username¤
The preferred username for the authentication provider.