Authentication API#
The authentication module provides a unified framework for authenticating with multiple cloud environments.
Configuration#
AuthConfig#
- class wxdi.common.auth.auth_config.AuthConfig(environment_type: EnvironmentType, url: str | None = None, api_key: str | None = None, username: str | None = None, password: str | None = None, account_id: str | None = None, disable_ssl_verification: bool = True)#
Bases:
objectConfiguration for cloud environment authentication.
This class holds the configuration details needed to authenticate with different cloud environments. It provides comprehensive validation and automatic configuration based on environment type.
- environment_type#
The type of environment (IBM_CLOUD, AWS_CLOUD, etc.)
- url#
The authentication or API endpoint URL (optional, uses default if not provided)
- api_key#
API key for authentication (optional)
- username#
Username for authentication (optional)
- password#
Password for authentication (optional)
- account_id#
Account ID for AWS environment (optional, required for AWS_CLOUD)
- disable_ssl_verification#
Whether to disable SSL verification (default: True)
- Raises:
TypeError – If environment_type is not an EnvironmentType instance
ValueError – If required fields are missing for the specified environment type
- Parameters:
EnvironmentType#
Authentication Provider#
AuthProvider#
- class wxdi.common.auth.auth_provider.AuthProvider(config: AuthConfig)#
Bases:
objectAuthentication provider for different cloud environments.
This class implements a factory pattern to create and manage authenticators for various cloud environments. It provides a unified interface for token generation across different authentication mechanisms.
- Authenticator mapping:
IBM_CLOUD: IAMAuthenticator (IBM Cloud IAM)
AWS_CLOUD: MCSPV2Authenticator (Multi-Cloud Service Platform V2)
ON_PREM: CloudPakForDataAuthenticator (Cloud Pak for Data)
GOV_CLOUD: GovCloudAuthenticator (Government Cloud custom implementation)
- config#
The authentication configuration
- authenticator#
The created authenticator instance
Example
>>> config = AuthConfig( ... environment_type=EnvironmentType.IBM_CLOUD, ... api_key="your-api-key" ... ) >>> provider = AuthProvider(config) >>> token = provider.get_token()
Initialize the AuthProvider with an environment configuration.
- Parameters:
config (
AuthConfig) – AuthConfig instance with authentication details- Raises:
ValueError – If authenticator creation fails due to invalid configuration
- get_token()#
Generate and return an authentication token.
This method validates the authenticator and retrieves a token from the token manager. The token is automatically refreshed if expired.
- Returns:
The authentication token
- Return type:
- Raises:
ValueError – If the authenticator doesn’t have a token_manager
Exception – If token generation fails
Government Cloud Authenticator#
GovCloudAuthenticator#
- class wxdi.common.auth.gov_cloud_authenticator.GovCloudAuthenticator(apikey: str, url: str, *, disable_ssl_verification: bool = False, headers: dict[str, str] | None = None, proxies: dict[str, str] | None = None)#
Bases:
IAMRequestBasedAuthenticatorGovernment Cloud Authenticator.
This authenticator extends IAMRequestBasedAuthenticator and uses a custom token manager to handle Government Cloud specific authentication flows.
The authenticator manages the complete token lifecycle: - Token acquisition from Government Cloud endpoints - Automatic token refresh when expired - Token expiration handling - SSL verification control
- token_manager#
The Government Cloud token manager instance
- apikey#
The API key for authentication
- url#
The token endpoint URL
Note
headers, proxies, and disable_ssl_verification are managed by the token_manager and can be accessed via token_manager.headers, token_manager.proxies, and token_manager.disable_ssl_verification respectively.
Example
>>> authenticator = GovCloudAuthenticator( ... apikey="your-api-key", ... url="https://dai.ibmforusgov.com/api/rest/mcsp/apikeys/token" ... ) >>> authenticator.validate() >>> token = authenticator.token_manager.get_token()
Initialize the Government Cloud authenticator.
- Parameters:
apikey (
str) – Your API key for Government Cloudurl (
str) – The token endpoint URLdisable_ssl_verification (
bool, default:False) – Whether to disable SSL verification (default: False)headers (
dict[str,str] |None, default:None) – Optional headers to include in token requestsproxies (
dict[str,str] |None, default:None) – Optional proxy configuration
- Raises:
ValueError – If apikey or url is not provided
- validate()#
Validate the authenticator configuration.
This method ensures that all required fields are properly set before attempting authentication.
- Raises:
ValueError – If apikey or url is not specified
- Return type:
GovCloudTokenManager#
- class wxdi.common.auth.gov_cloud_token_manager.GovCloudTokenManager(apikey: str, url: str, *, disable_ssl_verification: bool = False, headers: dict[str, str] | None = None, proxies: dict[str, str] | None = None)#
Bases:
JWTTokenManagerGovernment Cloud Token Manager.
This token manager extends JWTTokenManager to handle custom token request formats for Government Cloud authentication. It manages the complete token lifecycle including acquisition, storage, and expiration.
- apikey#
The API key for authentication
- headers#
Optional headers to include in requests
- proxies#
Optional proxy configuration
Example
>>> manager = GovCloudTokenManager( ... apikey="your-api-key", ... url="https://dai.ibmforusgov.com/api/rest/mcsp/apikeys/token" ... ) >>> token = manager.get_token()
Initialize the Government Cloud token manager.
- Parameters:
apikey (
str) – The API key for authenticationurl (
str) – The token endpoint URLdisable_ssl_verification (
bool, default:False) – Whether to disable SSL verificationheaders (
dict[str,str] |None, default:None) – Optional headers to include in requestsproxies (
dict[str,str] |None, default:None) – Optional proxy configuration
- request_token()#
Request a new token from the Government Cloud endpoint.
This method makes a POST request to the token endpoint with the API key and returns the token response. Uses form-urlencoded content type for compatibility with Government Cloud endpoints.
- Returns:
The token response from the endpoint containing token and expiration info
- Return type:
- Raises:
Exception – If the token request fails
Note
Follows the IAMTokenManager pattern by returning dict for _save_token_info().
Usage Examples#
See Authentication Guide for detailed usage examples.