|
| 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