-
Notifications
You must be signed in to change notification settings - Fork 29
New Authentication mechanism #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b878a49
feat(authentication): Revanp to new authentication mechanism
ehdsouza 7c969c8
chore(travis): remove older versions of python
ehdsouza bf7da48
feat(authenticator): Refactor autethnticators
ehdsouza 8a95b89
fix(base service): Separate out request into prepare_request and send
ehdsouza 52d6108
chore(token test): Handle tokens for all python versions
ehdsouza c50d2bb
feat(config): get_authenticator_from_environment for loading from env
ehdsouza b12274e
doc(readme): Update readme supporting auth methods
ehdsouza c7fcb3e
chore(auth): Changes as per Dustin's comments
ehdsouza e67e35a
chore(imports): remove unused imports
ehdsouza 866f279
feat(env): Load from env variables
ehdsouza 795c630
fix(value error): Throw error if authenticator is not set
ehdsouza e89e1a1
doc(typos): Correct typos by silly me
ehdsouza 9054fa9
chore(rename): rename noath and iam authenticators to camel case
ehdsouza c488ace
chore(noAuth): changes as per new naming
ehdsouza cec3609
chore(noAuth): Consistent naming of noAuth value
ehdsouza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
language: python | ||
matrix: | ||
include: | ||
- python: 2.7 | ||
- python: 3.4 | ||
- python: 3.5 | ||
- python: 3.6 | ||
- python: 3.7 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_token_authenticator import BearerTokenAuthenticator | ||
from .cp4d_authenticator import CloudPakForDataAuthenticator | ||
from .iam_authenticator import IamAuthenticator | ||
from .no_auth_authenticator import NoauthAuthenticator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 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, req): | ||
""" | ||
Adds the Authorization header, if applicable | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def validate(self): | ||
""" | ||
Checks if all the inputs needed are present | ||
""" | ||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 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 | ||
import base64 | ||
|
||
|
||
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, req): | ||
""" | ||
Adds the Authorization header, if applicable | ||
""" | ||
authstring = "{0}:{1}".format(self.username, self.password) | ||
base64_authorization = base64.b64encode(authstring.encode('utf-8')).decode('utf-8') | ||
|
||
headers = req.get('headers') | ||
headers['Authorization'] = 'Basic {0}'.format(base64_authorization) |
48 changes: 48 additions & 0 deletions
48
ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 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 | ||
|
||
|
||
class BearerTokenAuthenticator(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.') | ||
|
||
def authenticate(self, req): | ||
""" | ||
Adds the Authorization header, if applicable | ||
""" | ||
headers = req.get('headers') | ||
headers['Authorization'] = 'Bearer {0}'.format(self.bearer_token) | ||
|
||
def set_bearer_token(self, bearer_token): | ||
""" | ||
Sets the bearer token | ||
""" | ||
self.bearer_token = bearer_token |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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 CloudPakForDataAuthenticator(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.') | ||
dpopp07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if self.token_manager.url is None: | ||
raise ValueError('The url 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, req): | ||
""" | ||
Adds the Authorization header, if applicable | ||
""" | ||
headers = req.get('headers') | ||
bearer_token = self.token_manager.get_token() | ||
headers['Authorization'] = 'Bearer {0}'.format(bearer_token) | ||
|
||
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.