| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| Eduardo Sousa | d795f87 | 2019-02-05 16:05:53 +0000 | [diff] [blame] | 3 | # Copyright 2018 Whitestack, LLC |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # 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, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | # |
| 17 | # For those usages not covered by the Apache License, Version 2.0 please |
| 18 | # contact: esousa@whitestack.com or glavado@whitestack.com |
| 19 | ## |
| 20 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 21 | """ |
| 22 | Authconn implements an Abstract class for the Auth backend connector |
| 23 | plugins with the definition of the methods to be implemented. |
| 24 | """ |
| 25 | |
| 26 | __author__ = "Eduardo Sousa <esousa@whitestack.com>" |
| 27 | __date__ = "$27-jul-2018 23:59:59$" |
| 28 | |
| 29 | from http import HTTPStatus |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 30 | from base_topic import BaseTopic |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class AuthException(Exception): |
| 34 | """ |
| tierno | c844536 | 2019-06-14 12:07:15 +0000 | [diff] [blame] | 35 | Authentication error, because token, user password not recognized |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 36 | """ |
| 37 | def __init__(self, message, http_code=HTTPStatus.UNAUTHORIZED): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 38 | super(AuthException, self).__init__(message) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 39 | self.http_code = http_code |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 40 | |
| 41 | |
| tierno | c844536 | 2019-06-14 12:07:15 +0000 | [diff] [blame] | 42 | class AuthExceptionUnauthorized(AuthException): |
| 43 | """ |
| 44 | Authentication error, because not having rights to make this operation |
| 45 | """ |
| 46 | pass |
| 47 | |
| 48 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 49 | class AuthconnException(Exception): |
| 50 | """ |
| 51 | Common and base class Exception for all authconn exceptions. |
| 52 | """ |
| 53 | def __init__(self, message, http_code=HTTPStatus.UNAUTHORIZED): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 54 | super(AuthconnException, self).__init__(message) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 55 | self.http_code = http_code |
| 56 | |
| 57 | |
| 58 | class AuthconnConnectionException(AuthconnException): |
| 59 | """ |
| 60 | Connectivity error with Auth backend. |
| 61 | """ |
| 62 | def __init__(self, message, http_code=HTTPStatus.BAD_GATEWAY): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 63 | super(AuthconnConnectionException, self).__init__(message, http_code) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 64 | |
| 65 | |
| 66 | class AuthconnNotSupportedException(AuthconnException): |
| 67 | """ |
| 68 | The request is not supported by the Auth backend. |
| 69 | """ |
| 70 | def __init__(self, message, http_code=HTTPStatus.NOT_IMPLEMENTED): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 71 | super(AuthconnNotSupportedException, self).__init__(message, http_code) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 72 | |
| 73 | |
| 74 | class AuthconnNotImplementedException(AuthconnException): |
| 75 | """ |
| 76 | The method is not implemented by the Auth backend. |
| 77 | """ |
| 78 | def __init__(self, message, http_code=HTTPStatus.NOT_IMPLEMENTED): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 79 | super(AuthconnNotImplementedException, self).__init__(message, http_code) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 80 | |
| 81 | |
| 82 | class AuthconnOperationException(AuthconnException): |
| 83 | """ |
| 84 | The operation executed failed. |
| 85 | """ |
| 86 | def __init__(self, message, http_code=HTTPStatus.INTERNAL_SERVER_ERROR): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 87 | super(AuthconnOperationException, self).__init__(message, http_code) |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 88 | |
| 89 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 90 | class AuthconnNotFoundException(AuthconnException): |
| 91 | """ |
| 92 | The operation executed failed because element not found. |
| 93 | """ |
| 94 | def __init__(self, message, http_code=HTTPStatus.NOT_FOUND): |
| 95 | super().__init__(message, http_code) |
| 96 | |
| 97 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 98 | class AuthconnConflictException(AuthconnException): |
| 99 | """ |
| 100 | The operation has conflicts. |
| 101 | """ |
| 102 | def __init__(self, message, http_code=HTTPStatus.CONFLICT): |
| 103 | super().__init__(message, http_code) |
| 104 | |
| 105 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 106 | class Authconn: |
| 107 | """ |
| 108 | Abstract base class for all the Auth backend connector plugins. |
| 109 | Each Auth backend connector plugin must be a subclass of |
| 110 | Authconn class. |
| 111 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 112 | def __init__(self, config, db, token_cache): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 113 | """ |
| 114 | Constructor of the Authconn class. |
| 115 | |
| 116 | Note: each subclass |
| 117 | |
| 118 | :param config: configuration dictionary containing all the |
| 119 | necessary configuration parameters. |
| 120 | """ |
| 121 | self.config = config |
| 122 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 123 | def authenticate(self, user, password, project=None, token_info=None): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 124 | """ |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 125 | Authenticate a user using username/password or token_info, plus project |
| tierno | 38dcfeb | 2019-06-10 16:44:00 +0000 | [diff] [blame] | 126 | :param user: user: name, id or None |
| 127 | :param password: password or None |
| 128 | :param project: name, id, or None. If None first found project will be used to get an scope token |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 129 | :param token_info: previous token_info to obtain authorization |
| tierno | 38dcfeb | 2019-06-10 16:44:00 +0000 | [diff] [blame] | 130 | :return: the scoped token info or raises an exception. The token is a dictionary with: |
| 131 | _id: token string id, |
| 132 | username: username, |
| 133 | project_id: scoped_token project_id, |
| 134 | project_name: scoped_token project_name, |
| 135 | expires: epoch time when it expires, |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 136 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 137 | """ |
| 138 | raise AuthconnNotImplementedException("Should have implemented this") |
| 139 | |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 140 | def validate_token(self, token): |
| 141 | """ |
| 142 | Check if the token is valid. |
| 143 | |
| 144 | :param token: token to validate |
| 145 | :return: dictionary with information associated with the token. If the |
| 146 | token is not valid, returns None. |
| 147 | """ |
| 148 | raise AuthconnNotImplementedException("Should have implemented this") |
| 149 | |
| 150 | def revoke_token(self, token): |
| 151 | """ |
| 152 | Invalidate a token. |
| 153 | |
| 154 | :param token: token to be revoked |
| 155 | """ |
| 156 | raise AuthconnNotImplementedException("Should have implemented this") |
| 157 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 158 | def create_user(self, user_info): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 159 | """ |
| 160 | Create a user. |
| 161 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 162 | :param user_info: full user info. |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 163 | :raises AuthconnOperationException: if user creation failed. |
| 164 | """ |
| 165 | raise AuthconnNotImplementedException("Should have implemented this") |
| 166 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 167 | def update_user(self, user_info): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 168 | """ |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 169 | Change the user name and/or password. |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 170 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 171 | :param user_info: user info modifications |
| 172 | :raises AuthconnNotImplementedException: if function not implemented |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 173 | """ |
| 174 | raise AuthconnNotImplementedException("Should have implemented this") |
| 175 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 176 | def delete_user(self, user_id): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 177 | """ |
| 178 | Delete user. |
| 179 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 180 | :param user_id: user identifier. |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 181 | :raises AuthconnOperationException: if user deletion failed. |
| 182 | """ |
| 183 | raise AuthconnNotImplementedException("Should have implemented this") |
| 184 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 185 | def get_user_list(self, filter_q=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 186 | """ |
| 187 | Get user list. |
| 188 | |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 189 | :param filter_q: dictionary to filter user list by name (username is also admited) and/or _id |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 190 | :return: returns a list of users. |
| 191 | """ |
| 192 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 193 | def get_user(self, id, fail=True): |
| 194 | filt = {BaseTopic.id_field("users", id): id} |
| 195 | users = self.get_user_list(filt) |
| 196 | if not users: |
| 197 | if fail: |
| 198 | raise AuthconnNotFoundException("User with {} not found".format(filt), http_code=HTTPStatus.NOT_FOUND) |
| 199 | else: |
| 200 | return None |
| 201 | return users[0] |
| 202 | |
| 203 | def create_role(self, role_info): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 204 | """ |
| 205 | Create a role. |
| 206 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 207 | :param role_info: full role info. |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 208 | :raises AuthconnOperationException: if role creation failed. |
| 209 | """ |
| 210 | raise AuthconnNotImplementedException("Should have implemented this") |
| 211 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 212 | def delete_role(self, role_id): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 213 | """ |
| 214 | Delete a role. |
| 215 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 216 | :param role_id: role identifier. |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 217 | :raises AuthconnOperationException: if user deletion failed. |
| 218 | """ |
| 219 | raise AuthconnNotImplementedException("Should have implemented this") |
| 220 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 221 | def get_role_list(self, filter_q=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 222 | """ |
| 223 | Get all the roles. |
| 224 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 225 | :param filter_q: dictionary to filter role list by _id and/or name. |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 226 | :return: list of roles |
| 227 | """ |
| 228 | raise AuthconnNotImplementedException("Should have implemented this") |
| 229 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 230 | def get_role(self, id, fail=True): |
| 231 | filt = {BaseTopic.id_field("roles", id): id} |
| 232 | roles = self.get_role_list(filt) |
| 233 | if not roles: |
| 234 | if fail: |
| 235 | raise AuthconnNotFoundException("Role with {} not found".format(filt)) |
| 236 | else: |
| 237 | return None |
| 238 | return roles[0] |
| 239 | |
| 240 | def update_role(self, role_info): |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 241 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 242 | Change the information of a role |
| 243 | :param role_info: full role info |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 244 | :return: None |
| 245 | """ |
| 246 | raise AuthconnNotImplementedException("Should have implemented this") |
| 247 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 248 | def create_project(self, project_info): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 249 | """ |
| 250 | Create a project. |
| 251 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 252 | :param project_info: full project info. |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 253 | :return: the internal id of the created project |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 254 | :raises AuthconnOperationException: if project creation failed. |
| 255 | """ |
| 256 | raise AuthconnNotImplementedException("Should have implemented this") |
| 257 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 258 | def delete_project(self, project_id): |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 259 | """ |
| 260 | Delete a project. |
| 261 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 262 | :param project_id: project identifier. |
| Eduardo Sousa | 819d34c | 2018-07-31 01:20:02 +0100 | [diff] [blame] | 263 | :raises AuthconnOperationException: if project deletion failed. |
| 264 | """ |
| 265 | raise AuthconnNotImplementedException("Should have implemented this") |
| 266 | |
| tierno | 38dcfeb | 2019-06-10 16:44:00 +0000 | [diff] [blame] | 267 | def get_project_list(self, filter_q=None): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 268 | """ |
| 269 | Get all the projects. |
| 270 | |
| tierno | 38dcfeb | 2019-06-10 16:44:00 +0000 | [diff] [blame] | 271 | :param filter_q: dictionary to filter project list, by "name" and/or "_id" |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 272 | :return: list of projects |
| 273 | """ |
| 274 | raise AuthconnNotImplementedException("Should have implemented this") |
| 275 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 276 | def get_project(self, id, fail=True): |
| 277 | filt = {BaseTopic.id_field("projects", id): id} |
| 278 | projs = self.get_project_list(filt) |
| 279 | if not projs: |
| 280 | if fail: |
| 281 | raise AuthconnNotFoundException("project with {} not found".format(filt)) |
| 282 | else: |
| 283 | return None |
| 284 | return projs[0] |
| 285 | |
| 286 | def update_project(self, project_id, project_info): |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 287 | """ |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 288 | Change the information of a project |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 289 | :param project_id: project to be changed |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 290 | :param project_info: full project info |
| tierno | 4015b47 | 2019-06-10 13:57:29 +0000 | [diff] [blame] | 291 | :return: None |
| 292 | """ |
| 293 | raise AuthconnNotImplementedException("Should have implemented this") |