Skip to content

Commit faa044c

Browse files
committed
feat(IAM): allow IAM client id/secret to be set via BaseService ctor
1 parent cdb2057 commit faa044c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class BaseService(object):
5656

5757
def __init__(self, vcap_services_name, url, username=None, password=None,
5858
use_vcap_services=True, api_key=None,
59-
iam_apikey=None, iam_access_token=None, iam_url=None,
59+
iam_apikey=None, iam_access_token=None, iam_url=None, iam_client_id=None, iam_client_secret=None,
6060
display_name=None):
6161
"""
6262
It loads credentials with the following preference:
@@ -74,6 +74,8 @@ def __init__(self, vcap_services_name, url, username=None, password=None,
7474
self.iam_apikey = None
7575
self.iam_access_token = None
7676
self.iam_url = None
77+
self.iam_client_id = None
78+
self.iam_client_secret = None
7779
self.token_manager = None
7880
self.verify = None # Indicates whether to ignore verifying the SSL certification
7981

@@ -88,17 +90,17 @@ def __init__(self, vcap_services_name, url, username=None, password=None,
8890
if api_key.startswith(self.ICP_PREFIX):
8991
self.set_username_and_password(self.APIKEY, api_key)
9092
else:
91-
self.set_token_manager(api_key, iam_access_token, iam_url)
93+
self.set_token_manager(api_key, iam_access_token, iam_url, iam_client_id, iam_client_secret)
9294
elif username is not None and password is not None:
9395
if username is self.APIKEY and not password.startswith(self.ICP_PREFIX):
94-
self.set_token_manager(password, iam_access_token, iam_url)
96+
self.set_token_manager(password, iam_access_token, iam_url, iam_client_id, iam_client_secret)
9597
else:
9698
self.set_username_and_password(username, password)
9799
elif iam_access_token is not None or iam_apikey is not None:
98100
if iam_apikey and iam_apikey.startswith(self.ICP_PREFIX):
99101
self.set_username_and_password(self.APIKEY, iam_apikey)
100102
else:
101-
self.set_token_manager(iam_apikey, iam_access_token, iam_url)
103+
self.set_token_manager(iam_apikey, iam_access_token, iam_url, iam_client_id, iam_client_secret)
102104

103105
# 2. Credentials from credential file
104106
if display_name and not self.username and not self.token_manager:
@@ -196,15 +198,18 @@ def set_username_and_password(self, username, password):
196198
self.password = password
197199
self.jar = CookieJar()
198200

199-
def set_token_manager(self, iam_apikey=None, iam_access_token=None, iam_url=None):
201+
def set_token_manager(self, iam_apikey=None, iam_access_token=None, iam_url=None,
202+
iam_client_id=None, iam_client_secret=None):
200203
if has_bad_first_or_last_char(iam_apikey):
201204
raise ValueError('The credentials shouldn\'t start or end with curly brackets or quotes. '
202205
'Be sure to remove any {} and \" characters surrounding your credentials')
203206

204-
self.token_manager = IAMTokenManager(iam_apikey, iam_access_token, iam_url)
207+
self.token_manager = IAMTokenManager(iam_apikey, iam_access_token, iam_url, iam_client_id, iam_client_secret)
205208
self.iam_apikey = iam_apikey
206209
self.iam_access_token = iam_access_token
207210
self.iam_url = iam_url
211+
self.iam_client_id = iam_client_id
212+
self.iam_client_secret = iam_client_secret
208213
self.jar = CookieJar()
209214

210215
def set_iam_access_token(self, iam_access_token):

ibm_cloud_sdk_core/iam_token_manager.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ class IAMTokenManager(object):
2525
REQUEST_TOKEN_RESPONSE_TYPE = 'cloud_iam'
2626
REFRESH_TOKEN_GRANT_TYPE = 'refresh_token'
2727

28-
def __init__(self, iam_apikey=None, iam_access_token=None, iam_url=None, iam_client_id=None, iam_secret=None):
28+
def __init__(self, iam_apikey=None, iam_access_token=None, iam_url=None,
29+
iam_client_id=None, iam_client_secret=None):
2930
self.iam_apikey = iam_apikey
3031
self.user_access_token = iam_access_token
3132
self.iam_url = iam_url if iam_url else self.DEFAULT_IAM_URL
3233
self.iam_client_id = iam_client_id
33-
self.iam_secret = iam_secret
34+
self.iam_client_secret = iam_client_secret
3435
self.token_info = {
3536
'access_token': None,
3637
'refresh_token': None,
@@ -41,8 +42,8 @@ def __init__(self, iam_apikey=None, iam_access_token=None, iam_url=None, iam_cli
4142

4243
def request(self, method, url, headers=None, params=None, data=None, **kwargs):
4344
auth_tuple = ('bx', 'bx')
44-
if self.iam_client_id and self.iam_secret:
45-
auth_tuple = (self.iam_client_id, self.iam_secret)
45+
if self.iam_client_id and self.iam_client_secret:
46+
auth_tuple = (self.iam_client_id, self.iam_client_secret)
4647
response = requests.request(method=method, url=url,
4748
headers=headers, params=params,
4849
data=data, auth=auth_tuple, **kwargs)
@@ -133,7 +134,7 @@ def set_iam_url(self, iam_url):
133134
"""
134135
self.iam_url = iam_url
135136

136-
def set_iam_authorization_info(self, iam_client_id, iam_secret):
137+
def set_iam_authorization_info(self, iam_client_id, iam_client_secret):
137138
"""
138139
Set the IAM authorization information.
139140
This consists of the client_id and secret.
@@ -143,7 +144,7 @@ def set_iam_authorization_info(self, iam_client_id, iam_secret):
143144
is used.
144145
"""
145146
self.iam_client_id = iam_client_id
146-
self.iam_secret = iam_secret
147+
self.iam_client_secret = iam_client_secret
147148

148149
def _is_token_expired(self):
149150
"""

0 commit comments

Comments
 (0)