1+ import base64
2+ import logging
13from importlib .metadata import version
24from typing import Optional
35
4- import base64
56import httpx
6- import logging
77from kiota_abstractions .authentication import AuthenticationProvider , ApiKeyAuthenticationProvider , KeyLocation
88from kiota_abstractions .authentication .access_token_provider import AccessTokenProvider
99from kiota_abstractions .authentication .anonymous_authentication_provider import AnonymousAuthenticationProvider
2121
2222logger = logging .getLogger (__name__ )
2323
24- async def setup_auth_provider (base_url : str , username : str , password : str ) -> AccessTokenProvider :
24+
25+ async def setup_auth_provider (base_url : str , username : str , password : str , http_client : httpx .AsyncClient = None ,
26+ verify : bool = True ) -> AccessTokenProvider :
2527 # Use not authenticated client to fetch the auth mechanism
2628 auth_provider = AnonymousAuthenticationProvider ()
27- req_adapter = HttpxRequestAdapter (auth_provider )
29+ req_adapter = HttpxRequestAdapter (authentication_provider = auth_provider , http_client = http_client )
2830 req_adapter .base_url = base_url
2931 auth_client = HorreumRawClient (req_adapter )
3032
3133 auth_config = await auth_client .api .config .keycloak .get ()
32- # TODO: we could generalize using a generic OIDC client
33- return KeycloakAccessProvider (auth_config , username , password )
34+ return KeycloakAccessProvider (auth_config , username , password , verify )
3435
3536
3637class HorreumClient :
@@ -49,6 +50,7 @@ def __init__(self, base_url: str, credentials: Optional[HorreumCredentials],
4950 self .__base_url = base_url
5051 self .__credentials = credentials
5152 self .__client_config = client_config
53+ self .__auth_verify = client_config .auth_verify if client_config is not None else True
5254
5355 if client_config and client_config .http_client and client_config .use_default_middlewares :
5456 self .__http_client = KiotaClientFactory .create_with_default_middleware (client = client_config .http_client ,
@@ -62,20 +64,25 @@ async def setup(self):
6264 """
6365
6466 if self .__credentials :
65- if self .__credentials .apikey is not None and (self .__client_config is None or self .__client_config .auth_method == AuthMethod .API_KEY ):
67+ if self .__credentials .apikey is not None and (
68+ self .__client_config is None or self .__client_config .auth_method == AuthMethod .API_KEY ):
6669 # API key authentication
67- self .auth_provider = ApiKeyAuthenticationProvider (KeyLocation .Header , self .__credentials .apikey , "X-Horreum-API-Key" )
70+ self .auth_provider = ApiKeyAuthenticationProvider (KeyLocation .Header , self .__credentials .apikey ,
71+ "X-Horreum-API-Key" )
6872 logger .info ('Using API Key authentication' )
6973
7074 elif self .__credentials .username is not None :
7175 if self .__client_config is None or self .__client_config .auth_method == AuthMethod .BEARER :
7276 # Bearer token authentication
73- access_provider = await setup_auth_provider (self .__base_url , self .__credentials .username , self .__credentials .password )
77+ access_provider = await setup_auth_provider (self .__base_url , self .__credentials .username ,
78+ self .__credentials .password , self .__http_client ,
79+ self .__auth_verify )
7480 self .auth_provider = BaseBearerTokenAuthenticationProvider (access_provider )
7581 logger .info ('Using OIDC bearer token authentication' )
7682 elif self .__client_config .auth_method == AuthMethod .BASIC :
7783 # Basic authentication
78- basic = "Basic " + base64 .b64encode ((self .__credentials .username + ":" + self .__credentials .password ).encode ()).decode ()
84+ basic = "Basic " + base64 .b64encode (
85+ (self .__credentials .username + ":" + self .__credentials .password ).encode ()).decode ()
7986 self .auth_provider = ApiKeyAuthenticationProvider (KeyLocation .Header , basic , "Authentication" )
8087 logger .info ('Using Basic HTTP authentication' )
8188 elif self .__credentials .password is not None :
0 commit comments