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