Skip to content

Commit bf7da48

Browse files
committed
feat(authenticator): Refactor autethnticators
1 parent 7c969c8 commit bf7da48

15 files changed

+83
-235
lines changed

ibm_cloud_sdk_core/authenticators/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from .authenticator import Authenticator
1818
from .basic_authenticator import BasicAuthenticator
19-
from .bearer_authenticator import BearerAuthenticator
20-
from .cp4d_authenticator import CP4DAuthenticator
21-
from .iam_authenticator import IAMAuthenticator
22-
from .no_auth_authenticator import NoAuthAuthenticator
19+
from .bearer_token_authenticator import BearerTokenAuthenticator
20+
from .cp4d_authenticator import CloudPakForDataAuthenticator
21+
from .iam_authenticator import IamAuthenticator
22+
from .no_auth_authenticator import NoauthAuthenticator

ibm_cloud_sdk_core/authenticators/authenticator.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
class Authenticator(ABC):
2020
@abstractmethod
21-
def authenticate(self):
21+
def authenticate(self, req):
2222
"""
23-
Returns the (username, password) or bearer <token>
23+
Adds the Authorization header, if applicable
2424
"""
2525
pass
2626

@@ -30,17 +30,3 @@ def validate(self):
3030
Checks if all the inputs needed are present
3131
"""
3232
pass
33-
34-
@abstractmethod
35-
def _is_basic_authentication(self):
36-
"""
37-
Return true if basic authentication
38-
"""
39-
pass
40-
41-
@abstractmethod
42-
def _is_bearer_authentication(self):
43-
"""
44-
Return true if bearer authentication
45-
"""
46-
pass

ibm_cloud_sdk_core/authenticators/basic_authenticator.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from .authenticator import Authenticator
1818
from ..utils import has_bad_first_or_last_char
19+
import base64
1920

2021

2122
class BasicAuthenticator(Authenticator):
@@ -43,36 +44,12 @@ def validate(self):
4344
'The username and password shouldn\'t start or end with curly brackets or quotes. '
4445
'Please remove any surrounding {, }, or \" characters.')
4546

46-
def authenticate(self):
47+
def authenticate(self, req):
4748
"""
48-
Returns the username and password tuple
49+
Adds the Authorization header, if applicable
4950
"""
50-
return (self.username, self.password)
51+
authstring = "{0}:{1}".format(self.username, self.password)
52+
base64_authorization = base64.b64encode(authstring.encode('utf-8')).decode('utf-8')
5153

52-
def set_username(self, username):
53-
"""
54-
Sets the username
55-
"""
56-
self.username = username
57-
self.validate()
58-
59-
def set_password(self, password):
60-
"""
61-
Sets the password
62-
"""
63-
self.password = password
64-
self.validate()
65-
66-
def set_username_and_password(self, username, password):
67-
"""
68-
Sets the username and password
69-
"""
70-
self.username = username
71-
self.password = password
72-
self.validate()
73-
74-
def _is_basic_authentication(self):
75-
return True
76-
77-
def _is_bearer_authentication(self):
78-
return False
54+
headers = req.get('headers')
55+
headers['Authorization'] = 'Basic {0}'.format(base64_authorization)

ibm_cloud_sdk_core/authenticators/bearer_authenticator.py renamed to ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from ..utils import has_bad_first_or_last_char
1919

2020

21-
class BearerAuthenticator(Authenticator):
21+
class BearerTokenAuthenticator(Authenticator):
2222
authentication_type = 'bearerToken'
2323

2424
def __init__(self, bearer_token):
@@ -35,25 +35,15 @@ def validate(self):
3535
if self.bearer_token is None:
3636
raise ValueError('The bearer token shouldn\'t be None.')
3737

38-
if has_bad_first_or_last_char(self.bearer_token):
39-
raise ValueError(
40-
'The bearer token shouldn\'t start or end with curly brackets or quotes. '
41-
'Please remove any surrounding {, }, or \" characters.')
42-
43-
def authenticate(self):
38+
def authenticate(self, req):
4439
"""
45-
Returns the bearer token
40+
Adds the Authorization header, if applicable
4641
"""
47-
return 'Bearer {0}'.format(self.bearer_token)
42+
headers = req.get('headers')
43+
headers['Authorization'] = 'Bearer {0}'.format(self.bearer_token)
4844

4945
def set_bearer_token(self, bearer_token):
5046
"""
5147
Sets the bearer token
5248
"""
5349
self.bearer_token = bearer_token
54-
55-
def _is_basic_authentication(self):
56-
return False
57-
58-
def _is_bearer_authentication(self):
59-
return True

ibm_cloud_sdk_core/authenticators/cp4d_authenticator.py

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from ..utils import has_bad_first_or_last_char
2020

2121

22-
class CP4DAuthenticator(Authenticator):
22+
class CloudPakForDataAuthenticator(Authenticator):
2323
authentication_type = 'cp4d'
2424

2525
def __init__(self,
@@ -48,6 +48,9 @@ def validate(self):
4848
if self.token_manager.username is None or self.token_manager.password is None:
4949
raise ValueError('The username and password shouldn\'t be None.')
5050

51+
if self.token_manager.url is None:
52+
raise ValueError('The url shouldn\'t be None.')
53+
5154
if has_bad_first_or_last_char(
5255
self.token_manager.username) or has_bad_first_or_last_char(self.token_manager.password):
5356
raise ValueError(
@@ -59,39 +62,13 @@ def validate(self):
5962
'The url shouldn\'t start or end with curly brackets or quotes. '
6063
'Please remove any surrounding {, }, or \" characters.')
6164

62-
def authenticate(self):
65+
def authenticate(self, req):
6366
"""
64-
Returns the bearer token
67+
Adds the Authorization header, if applicable
6568
"""
69+
headers = req.get('headers')
6670
bearer_token = self.token_manager.get_token()
67-
return 'Bearer {0}'.format(bearer_token)
68-
69-
def _is_basic_authentication(self):
70-
return False
71-
72-
def _is_bearer_authentication(self):
73-
return True
74-
75-
def set_username(self, username):
76-
"""
77-
Sets the username
78-
"""
79-
self.token_manager.set_username(username)
80-
self.validate()
81-
82-
def set_password(self, password):
83-
"""
84-
Sets the password
85-
"""
86-
self.token_manager.set_password(password)
87-
self.validate()
88-
89-
def set_url(self, url):
90-
"""
91-
Sets the url
92-
"""
93-
self.token_manager.set_url(url)
94-
self.validate()
71+
headers['Authorization'] = 'Bearer {0}'.format(bearer_token)
9572

9673
def set_disable_ssl_verification(self, status=False):
9774
"""

ibm_cloud_sdk_core/authenticators/iam_authenticator.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from ..utils import has_bad_first_or_last_char
2020

2121

22-
class IAMAuthenticator(Authenticator):
22+
class IamAuthenticator(Authenticator):
2323
authentication_type = 'iam'
2424

2525
def __init__(self,
@@ -63,33 +63,15 @@ def validate(self):
6363
raise ValueError(
6464
'Both client id and client secret should be initialized.')
6565

66-
def authenticate(self):
66+
def authenticate(self, req):
6767
"""
68-
Returns the bearer token
68+
Adds the Authorization header, if applicable
6969
"""
70+
headers = req.get('headers')
7071
bearer_token = self.token_manager.get_token()
71-
return 'Bearer {0}'.format(bearer_token)
72+
headers['Authorization'] = 'Bearer {0}'.format(bearer_token)
7273

73-
def _is_basic_authentication(self):
74-
return False
75-
76-
def _is_bearer_authentication(self):
77-
return True
78-
79-
def set_apikey(self, apikey):
80-
"""
81-
Set the IAM api key
82-
"""
83-
self.token_manager.set_apikey(apikey)
84-
self.validate()
85-
86-
def set_url(self, url):
87-
"""
88-
Set the IAM url
89-
"""
90-
self.token_manager.set_url(url)
91-
92-
def set_authorization_info(self, client_id, client_secret):
74+
def set_client_id_and_secret(self, client_id, client_secret):
9375
"""
9476
Set the IAM authorization information.
9577
This consists of the client_id and secret.
@@ -98,7 +80,7 @@ def set_authorization_info(self, client_id, client_secret):
9880
If these values are not supplied, then a default Authorization header
9981
is used.
10082
"""
101-
self.token_manager.set_authorization_info(client_id, client_secret)
83+
self.token_manager.set_client_id_and_secret(client_id, client_secret)
10284
self.validate()
10385

10486
def set_disable_ssl_verification(self, status=False):

ibm_cloud_sdk_core/authenticators/no_auth_authenticator.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@
1616

1717
from .authenticator import Authenticator
1818

19-
class NoAuthAuthenticator(Authenticator):
19+
class NoauthAuthenticator(Authenticator):
2020
authentication_type = 'noauth'
2121

2222
def validate(self):
2323
pass
2424

25-
def _is_basic_authentication(self):
26-
return False
27-
28-
def _is_bearer_authentication(self):
29-
return False
30-
31-
def authenticate(self):
25+
def authenticate(self, req):
3226
pass

ibm_cloud_sdk_core/cp4d_token_manager.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self,
3737
"""
3838
self.username = username
3939
self.password = password
40-
url = url + '/v1/preauth/validateAuth'
40+
url = url + '/v1/preauth/validateAuth' if url else None
4141
self.headers = headers
4242
self.proxies = proxies
4343
super(CP4DTokenManager, self).__init__(url, disable_ssl_verification,
@@ -57,24 +57,6 @@ def request_token(self):
5757
proxies=self.proxies)
5858
return response
5959

60-
def set_username(self, username):
61-
"""
62-
Sets the username
63-
"""
64-
self.username = username
65-
66-
def set_password(self, password):
67-
"""
68-
Sets the password
69-
"""
70-
self.password = password
71-
72-
def set_url(self, url):
73-
"""
74-
Sets the url
75-
"""
76-
self.url = url
77-
7860
def set_headers(self, headers):
7961
"""
8062
Sets user-defined headers

ibm_cloud_sdk_core/iam_token_manager.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,7 @@ def request_token(self):
8383
proxies=self.proxies)
8484
return response
8585

86-
def set_apikey(self, apikey):
87-
"""
88-
Set the apikey
89-
"""
90-
self.apikey = apikey
91-
92-
def set_url(self, url):
93-
"""
94-
Set the IAM url
95-
"""
96-
self.url = url
97-
98-
def set_authorization_info(self, client_id, client_secret):
86+
def set_client_id_and_secret(self, client_id, client_secret):
9987
"""
10088
Set the IAM authorization information.
10189
This consists of the client_id and secret.

test/test_basic_authenticator.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,9 @@ def test_basic_authenticator():
77
assert authenticator.username == 'my_username'
88
assert authenticator.password == 'my_password'
99

10-
authenticator.set_username('bogus')
11-
authenticator.set_password('bogus')
12-
13-
(username, password) = authenticator.authenticate()
14-
assert username == 'bogus'
15-
assert password == 'bogus'
16-
17-
authenticator.set_username_and_password('wonder', 'woman')
18-
assert authenticator.username == 'wonder'
19-
assert authenticator.password == 'woman'
20-
21-
assert authenticator._is_basic_authentication() is True
22-
assert authenticator._is_bearer_authentication() is False
10+
request = {'headers': {}}
11+
authenticator.authenticate(request)
12+
assert request['headers']['Authorization'] == 'Basic bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ='
2313

2414
def test_basic_authenticator_validate_failed():
2515
with pytest.raises(ValueError) as err:

test/test_bearer_authenticator.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@
33
import time
44
import jwt
55
import json
6-
from ibm_cloud_sdk_core.authenticators import BearerAuthenticator
6+
from ibm_cloud_sdk_core.authenticators import BearerTokenAuthenticator
77

88
def test_bearer_authenticator():
9-
authenticator = BearerAuthenticator('my_bearer_token')
9+
authenticator = BearerTokenAuthenticator('my_bearer_token')
1010
assert authenticator is not None
1111
assert authenticator.bearer_token == 'my_bearer_token'
1212

1313
authenticator.set_bearer_token('james bond')
1414
assert authenticator.bearer_token == 'james bond'
1515

16-
assert authenticator._is_basic_authentication() is False
17-
assert authenticator._is_bearer_authentication() is True
18-
19-
assert authenticator.authenticate() == 'Bearer james bond'
16+
request = {'headers': {}}
17+
authenticator.authenticate(request)
18+
assert request['headers']['Authorization'] == 'Bearer james bond'
2019

2120
def test_bearer_validate_failed():
2221
with pytest.raises(ValueError) as err:
23-
BearerAuthenticator(None)
22+
BearerTokenAuthenticator(None)
2423
assert str(err.value) == 'The bearer token shouldn\'t be None.'
25-
26-
with pytest.raises(ValueError) as err:
27-
BearerAuthenticator('{my_bearer_token}')
28-
assert str(err.value) == 'The bearer token shouldn\'t start or end with curly brackets or quotes. Please remove any surrounding {, }, or \" characters.'

0 commit comments

Comments
 (0)