24
24
from .version import __version__
25
25
from .utils import has_bad_first_or_last_char , remove_null_values , cleanup_values
26
26
from .iam_token_manager import IAMTokenManager
27
+ from .icp_token_manager import ICPTokenManager
27
28
from .detailed_response import DetailedResponse
28
29
from .api_exception import ApiException
29
30
@@ -55,9 +56,9 @@ class BaseService(object):
55
56
SDK_NAME = 'ibm-python-sdk-core'
56
57
57
58
def __init__ (self , vcap_services_name , url , username = None , password = None ,
58
- use_vcap_services = True , api_key = None ,
59
- iam_apikey = None , iam_access_token = None , iam_url = None , iam_client_id = None , iam_client_secret = None ,
60
- display_name = None ):
59
+ use_vcap_services = True , api_key = None , iam_apikey = None , iam_url = None ,
60
+ iam_access_token = None , iam_client_id = None , iam_client_secret = None ,
61
+ display_name = None , icp_access_token = None , authentication_type = None ):
61
62
"""
62
63
It loads credentials with the following preference:
63
64
1) Credentials explicitly set in the request
@@ -66,41 +67,50 @@ def __init__(self, vcap_services_name, url, username=None, password=None,
66
67
"""
67
68
self .url = url
68
69
self .http_config = {}
70
+ self .authentication_type = authentication_type .lower () if authentication_type else None
69
71
self .jar = None
70
- self .api_key = None
71
- self .username = None
72
- self .password = None
73
- self .default_headers = None
74
- self .iam_apikey = None
75
- self .iam_access_token = None
76
- self .iam_url = None
77
- self .iam_client_id = None
78
- self .iam_client_secret = None
72
+ self .api_key = api_key
73
+ self .username = username
74
+ self .password = password
75
+ self .iam_apikey = iam_apikey
76
+ self .iam_access_token = iam_access_token
77
+ self .iam_url = iam_url
78
+ self .iam_client_id = iam_client_id
79
+ self .iam_client_secret = iam_client_secret
80
+ self .icp_access_token = icp_access_token
79
81
self .token_manager = None
82
+ self .default_headers = None
80
83
self .verify = None # Indicates whether to ignore verifying the SSL certification
81
84
82
- if has_bad_first_or_last_char (self .url ):
83
- raise ValueError ('The URL shouldn\' t start or end with curly brackets or quotes. '
84
- 'Be sure to remove any {} and \" characters surrounding your URL' )
85
+ self ._check_credentials ()
85
86
86
87
self .set_user_agent_header (self .build_user_agent ())
87
88
88
89
# 1. Credentials are passed in constructor
89
- if api_key is not None :
90
- if api_key .startswith (self .ICP_PREFIX ):
91
- self .set_username_and_password (self .APIKEY , api_key )
92
- else :
93
- self .set_token_manager (api_key , iam_access_token , iam_url , iam_client_id , iam_client_secret )
94
- elif username is not None and password is not None :
95
- if username is self .APIKEY and not password .startswith (self .ICP_PREFIX ):
96
- self .set_token_manager (password , iam_access_token , iam_url , iam_client_id , iam_client_secret )
97
- else :
98
- self .set_username_and_password (username , password )
99
- elif iam_access_token is not None or iam_apikey is not None :
100
- if iam_apikey and iam_apikey .startswith (self .ICP_PREFIX ):
101
- self .set_username_and_password (self .APIKEY , iam_apikey )
102
- else :
103
- self .set_token_manager (iam_apikey , iam_access_token , iam_url , iam_client_id , iam_client_secret )
90
+ if self .authentication_type == 'iam' or self ._has_iam_credentials (self .iam_apikey , self .iam_access_token ) or self ._has_iam_credentials (self .api_key , self .iam_access_token ):
91
+ self .token_manager = IAMTokenManager (self .iam_apikey or self .api_key or self .password ,
92
+ self .iam_access_token ,
93
+ self .iam_url ,
94
+ self .iam_client_id ,
95
+ self .iam_client_secret )
96
+ self .iam_apikey = self .iam_apikey or self .api_key or self .password
97
+ elif self ._uses_basic_for_iam (self .username , self .password ):
98
+ self .token_manager = IAMTokenManager (self .password ,
99
+ self .iam_access_token ,
100
+ self .iam_url ,
101
+ self .iam_client_id ,
102
+ self .iam_client_secret )
103
+ self .iam_apikey = self .password
104
+ self .username = None
105
+ self .password = None
106
+ elif self ._is_for_icp4d (self .authentication_type , self .icp_access_token ):
107
+ self .token_manager = ICPTokenManager (self .url ,
108
+ self .username ,
109
+ self .password ,
110
+ self .icp_access_token )
111
+ elif self ._is_for_icp (self .api_key ) or self ._is_for_icp (self .iam_apikey ):
112
+ self .username = self .APIKEY
113
+ self .password = self .api_key or self .iam_apikey
104
114
105
115
# 2. Credentials from credential file
106
116
if display_name and not self .username and not self .token_manager :
@@ -183,6 +193,41 @@ def _load_from_vcap_services(self, service_name):
183
193
else :
184
194
return None
185
195
196
+ def _is_for_icp (self , credential = None ):
197
+ return credential and credential .startswith (self .ICP_PREFIX )
198
+
199
+ def _is_for_icp4d (self , authentication_type , icp_access_token = None ):
200
+ return authentication_type == 'icp4d' or icp_access_token
201
+
202
+ def _has_basic_credentials (self , username , password ):
203
+ return username and password and not self ._uses_basic_for_iam (username , password )
204
+
205
+ def _has_iam_credentials (self , iam_apikey , iam_access_token ):
206
+ return (iam_apikey or iam_access_token ) and not self ._is_for_icp (iam_apikey )
207
+
208
+ def _uses_basic_for_iam (self , username , password ):
209
+ """
210
+ Returns true if the user provides basic auth creds with the intention
211
+ of using IAM auth
212
+ """
213
+ return username and password and username == self .APIKEY and not self ._is_for_icp (password )
214
+
215
+ def _has_bad_first_or_last_char (self , str ):
216
+ return str is not None and (str .startswith ('{' ) or str .startswith ('"' ) or str .endswith ('}' ) or str .endswith ('"' ))
217
+
218
+ def _check_credentials (self ):
219
+ credentials_to_check = {
220
+ 'URL' : self .url ,
221
+ 'username' : self .username ,
222
+ 'password' : self .password ,
223
+ 'credentials' : self .iam_apikey
224
+ }
225
+
226
+ for key in credentials_to_check :
227
+ if self ._has_bad_first_or_last_char (credentials_to_check .get (key )):
228
+ raise ValueError ('The ' + key + ' shouldn\' t start or end with curly brackets or quotes. '
229
+ 'Be sure to remove any {} and \" characters surrounding your ' + key )
230
+
186
231
def disable_SSL_verification (self ):
187
232
self .verify = False
188
233
0 commit comments