Skip to content

Commit b878a49

Browse files
committed
feat(authentication): Revanp to new authentication mechanism
1 parent ed2147a commit b878a49

27 files changed

+1410
-661
lines changed

ibm_cloud_sdk_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
from .detailed_response import DetailedResponse
1818
from .iam_token_manager import IAMTokenManager
1919
from .jwt_token_manager import JWTTokenManager
20-
from .icp4d_token_manager import ICP4DTokenManager
20+
from .cp4d_token_manager import CP4DTokenManager
2121
from .api_exception import ApiException
2222
from .utils import datetime_to_string, string_to_datetime
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# coding: utf-8
2+
3+
# Copyright 2019 IBM All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from .authenticator import Authenticator
18+
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
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# coding: utf-8
2+
3+
# Copyright 2019 IBM All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from abc import ABC, abstractmethod
18+
19+
class Authenticator(ABC):
20+
@abstractmethod
21+
def authenticate(self):
22+
"""
23+
Returns the (username, password) or bearer <token>
24+
"""
25+
pass
26+
27+
@abstractmethod
28+
def validate(self):
29+
"""
30+
Checks if all the inputs needed are present
31+
"""
32+
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
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# coding: utf-8
2+
3+
# Copyright 2019 IBM All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from .authenticator import Authenticator
18+
from ..utils import has_bad_first_or_last_char
19+
20+
21+
class BasicAuthenticator(Authenticator):
22+
authentication_type = 'basic'
23+
24+
def __init__(self, username, password):
25+
"""
26+
:attr str username: The username
27+
:attr str password: The password
28+
"""
29+
self.username = username
30+
self.password = password
31+
self.validate()
32+
33+
def validate(self):
34+
"""
35+
Performs validation on input params
36+
"""
37+
if self.username is None or self.password is None:
38+
raise ValueError('The username and password shouldn\'t be None.')
39+
40+
if has_bad_first_or_last_char(
41+
self.username) or has_bad_first_or_last_char(self.password):
42+
raise ValueError(
43+
'The username and password shouldn\'t start or end with curly brackets or quotes. '
44+
'Please remove any surrounding {, }, or \" characters.')
45+
46+
def authenticate(self):
47+
"""
48+
Returns the username and password tuple
49+
"""
50+
return (self.username, self.password)
51+
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
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# coding: utf-8
2+
3+
# Copyright 2019 IBM All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from .authenticator import Authenticator
18+
from ..utils import has_bad_first_or_last_char
19+
20+
21+
class BearerAuthenticator(Authenticator):
22+
authentication_type = 'bearerToken'
23+
24+
def __init__(self, bearer_token):
25+
"""
26+
:attr str bearer_token: User managed bearer token
27+
"""
28+
self.bearer_token = bearer_token
29+
self.validate()
30+
31+
def validate(self):
32+
"""
33+
Performs validation on input params
34+
"""
35+
if self.bearer_token is None:
36+
raise ValueError('The bearer token shouldn\'t be None.')
37+
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):
44+
"""
45+
Returns the bearer token
46+
"""
47+
return 'Bearer {0}'.format(self.bearer_token)
48+
49+
def set_bearer_token(self, bearer_token):
50+
"""
51+
Sets the bearer token
52+
"""
53+
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
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# coding: utf-8
2+
3+
# Copyright 2019 IBM All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from .authenticator import Authenticator
18+
from ..cp4d_token_manager import CP4DTokenManager
19+
from ..utils import has_bad_first_or_last_char
20+
21+
22+
class CP4DAuthenticator(Authenticator):
23+
authentication_type = 'cp4d'
24+
25+
def __init__(self,
26+
username,
27+
password,
28+
url,
29+
disable_ssl_verification=False,
30+
headers=None,
31+
proxies=None):
32+
"""
33+
:attr str username: The username
34+
:attr str password: The password
35+
:attr str url: The url for authentication
36+
:attr bool disable_ssl_verification: enables/ disabled ssl verification
37+
:attr dict headers: user-defined headers
38+
:attr dict proxies: user-defined proxies
39+
"""
40+
self.token_manager = CP4DTokenManager(
41+
username, password, url, disable_ssl_verification, headers, proxies)
42+
self.validate()
43+
44+
def validate(self):
45+
"""
46+
Performs validation on input params
47+
"""
48+
if self.token_manager.username is None or self.token_manager.password is None:
49+
raise ValueError('The username and password shouldn\'t be None.')
50+
51+
if has_bad_first_or_last_char(
52+
self.token_manager.username) or has_bad_first_or_last_char(self.token_manager.password):
53+
raise ValueError(
54+
'The username and password shouldn\'t start or end with curly brackets or quotes. '
55+
'Please remove any surrounding {, }, or \" characters.')
56+
57+
if has_bad_first_or_last_char(self.token_manager.url):
58+
raise ValueError(
59+
'The url shouldn\'t start or end with curly brackets or quotes. '
60+
'Please remove any surrounding {, }, or \" characters.')
61+
62+
def authenticate(self):
63+
"""
64+
Returns the bearer token
65+
"""
66+
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()
95+
96+
def set_disable_ssl_verification(self, status=False):
97+
"""
98+
Sets the ssl verification to enabled or disabled
99+
"""
100+
self.token_manager.set_disable_ssl_verification(status)
101+
102+
def set_headers(self, headers):
103+
"""
104+
Sets user-defined headers
105+
"""
106+
self.token_manager.set_headers(headers)
107+
108+
def set_proxies(self, proxies):
109+
"""
110+
Sets the proxies
111+
"""
112+
self.token_manager.set_proxies(proxies)

0 commit comments

Comments
 (0)