1
+ import base64
2
+ import logging
1
3
from importlib .metadata import version
2
4
from typing import Optional
3
5
4
- import base64
5
6
import httpx
6
- import logging
7
7
from kiota_abstractions .authentication import AuthenticationProvider , ApiKeyAuthenticationProvider , KeyLocation
8
8
from kiota_abstractions .authentication .access_token_provider import AccessTokenProvider
9
9
from kiota_abstractions .authentication .anonymous_authentication_provider import AnonymousAuthenticationProvider
21
21
22
22
logger = logging .getLogger (__name__ )
23
23
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 :
25
27
# Use not authenticated client to fetch the auth mechanism
26
28
auth_provider = AnonymousAuthenticationProvider ()
27
- req_adapter = HttpxRequestAdapter (auth_provider )
29
+ req_adapter = HttpxRequestAdapter (authentication_provider = auth_provider , http_client = http_client )
28
30
req_adapter .base_url = base_url
29
31
auth_client = HorreumRawClient (req_adapter )
30
32
31
33
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 )
34
35
35
36
36
37
class HorreumClient :
@@ -49,6 +50,7 @@ def __init__(self, base_url: str, credentials: Optional[HorreumCredentials],
49
50
self .__base_url = base_url
50
51
self .__credentials = credentials
51
52
self .__client_config = client_config
53
+ self .__auth_verify = client_config .auth_verify if client_config is not None else True
52
54
53
55
if client_config and client_config .http_client and client_config .use_default_middlewares :
54
56
self .__http_client = KiotaClientFactory .create_with_default_middleware (client = client_config .http_client ,
@@ -62,20 +64,25 @@ async def setup(self):
62
64
"""
63
65
64
66
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 ):
66
69
# 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" )
68
72
logger .info ('Using API Key authentication' )
69
73
70
74
elif self .__credentials .username is not None :
71
75
if self .__client_config is None or self .__client_config .auth_method == AuthMethod .BEARER :
72
76
# 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 )
74
80
self .auth_provider = BaseBearerTokenAuthenticationProvider (access_provider )
75
81
logger .info ('Using OIDC bearer token authentication' )
76
82
elif self .__client_config .auth_method == AuthMethod .BASIC :
77
83
# 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 ()
79
86
self .auth_provider = ApiKeyAuthenticationProvider (KeyLocation .Header , basic , "Authentication" )
80
87
logger .info ('Using Basic HTTP authentication' )
81
88
elif self .__credentials .password is not None :
0 commit comments