Skip to content

Commit

Permalink
feat(authentication): Revanp to new authentication mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdsouza committed Aug 16, 2019
1 parent ed2147a commit b878a49
Show file tree
Hide file tree
Showing 27 changed files with 1,410 additions and 661 deletions.
2 changes: 1 addition & 1 deletion ibm_cloud_sdk_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
from .detailed_response import DetailedResponse
from .iam_token_manager import IAMTokenManager
from .jwt_token_manager import JWTTokenManager
from .icp4d_token_manager import ICP4DTokenManager
from .cp4d_token_manager import CP4DTokenManager
from .api_exception import ApiException
from .utils import datetime_to_string, string_to_datetime
22 changes: 22 additions & 0 deletions ibm_cloud_sdk_core/authenticators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator
from .basic_authenticator import BasicAuthenticator
from .bearer_authenticator import BearerAuthenticator
from .cp4d_authenticator import CP4DAuthenticator
from .iam_authenticator import IAMAuthenticator
from .no_auth_authenticator import NoAuthAuthenticator
46 changes: 46 additions & 0 deletions ibm_cloud_sdk_core/authenticators/authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABC, abstractmethod

class Authenticator(ABC):
@abstractmethod
def authenticate(self):
"""
Returns the (username, password) or bearer <token>
"""
pass

@abstractmethod
def validate(self):
"""
Checks if all the inputs needed are present
"""
pass

@abstractmethod
def _is_basic_authentication(self):
"""
Return true if basic authentication
"""
pass

@abstractmethod
def _is_bearer_authentication(self):
"""
Return true if bearer authentication
"""
pass
78 changes: 78 additions & 0 deletions ibm_cloud_sdk_core/authenticators/basic_authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator
from ..utils import has_bad_first_or_last_char


class BasicAuthenticator(Authenticator):
authentication_type = 'basic'

def __init__(self, username, password):
"""
:attr str username: The username
:attr str password: The password
"""
self.username = username
self.password = password
self.validate()

def validate(self):
"""
Performs validation on input params
"""
if self.username is None or self.password is None:
raise ValueError('The username and password shouldn\'t be None.')

if has_bad_first_or_last_char(
self.username) or has_bad_first_or_last_char(self.password):
raise ValueError(
'The username and password shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')

def authenticate(self):
"""
Returns the username and password tuple
"""
return (self.username, self.password)

def set_username(self, username):
"""
Sets the username
"""
self.username = username
self.validate()

def set_password(self, password):
"""
Sets the password
"""
self.password = password
self.validate()

def set_username_and_password(self, username, password):
"""
Sets the username and password
"""
self.username = username
self.password = password
self.validate()

def _is_basic_authentication(self):
return True

def _is_bearer_authentication(self):
return False
59 changes: 59 additions & 0 deletions ibm_cloud_sdk_core/authenticators/bearer_authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator
from ..utils import has_bad_first_or_last_char


class BearerAuthenticator(Authenticator):
authentication_type = 'bearerToken'

def __init__(self, bearer_token):
"""
:attr str bearer_token: User managed bearer token
"""
self.bearer_token = bearer_token
self.validate()

def validate(self):
"""
Performs validation on input params
"""
if self.bearer_token is None:
raise ValueError('The bearer token shouldn\'t be None.')

if has_bad_first_or_last_char(self.bearer_token):
raise ValueError(
'The bearer token shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')

def authenticate(self):
"""
Returns the bearer token
"""
return 'Bearer {0}'.format(self.bearer_token)

def set_bearer_token(self, bearer_token):
"""
Sets the bearer token
"""
self.bearer_token = bearer_token

def _is_basic_authentication(self):
return False

def _is_bearer_authentication(self):
return True
112 changes: 112 additions & 0 deletions ibm_cloud_sdk_core/authenticators/cp4d_authenticator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# coding: utf-8

# Copyright 2019 IBM All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .authenticator import Authenticator
from ..cp4d_token_manager import CP4DTokenManager
from ..utils import has_bad_first_or_last_char


class CP4DAuthenticator(Authenticator):
authentication_type = 'cp4d'

def __init__(self,
username,
password,
url,
disable_ssl_verification=False,
headers=None,
proxies=None):
"""
:attr str username: The username
:attr str password: The password
:attr str url: The url for authentication
:attr bool disable_ssl_verification: enables/ disabled ssl verification
:attr dict headers: user-defined headers
:attr dict proxies: user-defined proxies
"""
self.token_manager = CP4DTokenManager(
username, password, url, disable_ssl_verification, headers, proxies)
self.validate()

def validate(self):
"""
Performs validation on input params
"""
if self.token_manager.username is None or self.token_manager.password is None:
raise ValueError('The username and password shouldn\'t be None.')

if has_bad_first_or_last_char(
self.token_manager.username) or has_bad_first_or_last_char(self.token_manager.password):
raise ValueError(
'The username and password shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')

if has_bad_first_or_last_char(self.token_manager.url):
raise ValueError(
'The url shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')

def authenticate(self):
"""
Returns the bearer token
"""
bearer_token = self.token_manager.get_token()
return 'Bearer {0}'.format(bearer_token)

def _is_basic_authentication(self):
return False

def _is_bearer_authentication(self):
return True

def set_username(self, username):
"""
Sets the username
"""
self.token_manager.set_username(username)
self.validate()

def set_password(self, password):
"""
Sets the password
"""
self.token_manager.set_password(password)
self.validate()

def set_url(self, url):
"""
Sets the url
"""
self.token_manager.set_url(url)
self.validate()

def set_disable_ssl_verification(self, status=False):
"""
Sets the ssl verification to enabled or disabled
"""
self.token_manager.set_disable_ssl_verification(status)

def set_headers(self, headers):
"""
Sets user-defined headers
"""
self.token_manager.set_headers(headers)

def set_proxies(self, proxies):
"""
Sets the proxies
"""
self.token_manager.set_proxies(proxies)
Loading

0 comments on commit b878a49

Please sign in to comment.