| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Telefonica S.A. |
| 4 | # Copyright 2018 ALTRAN Innovación S.L. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | # |
| 18 | # For those usages not covered by the Apache License, Version 2.0 please |
| 19 | # contact: esousa@whitestack.com or glavado@whitestack.com |
| 20 | ## |
| 21 | |
| 22 | """ |
| 23 | AuthconnInternal implements implements the connector for |
| 24 | OSM Internal Authentication Backend and leverages the RBAC model |
| 25 | """ |
| 26 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 27 | __author__ = ( |
| 28 | "Pedro de la Cruz Ramos <pdelacruzramos@altran.com>, " |
| 29 | "Alfonso Tierno <alfonso.tiernosepulveda@telefoncia.com" |
| 30 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 31 | __date__ = "$06-jun-2019 11:16:08$" |
| 32 | |
| tierno | 5ec768a | 2020-03-31 09:46:44 +0000 | [diff] [blame] | 33 | import logging |
| 34 | import re |
| 35 | |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 36 | from osm_nbi.authconn import ( |
| 37 | Authconn, |
| 38 | AuthException, |
| 39 | AuthconnConflictException, |
| 40 | ) # , AuthconnOperationException |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 41 | from osm_common.dbbase import DbException |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 42 | from osm_nbi.base_topic import BaseTopic |
| elumalai | 7802ff8 | 2023-04-24 20:38:32 +0530 | [diff] [blame] | 43 | from osm_nbi.utils import cef_event, cef_event_builder |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 44 | from osm_nbi.password_utils import ( |
| 45 | hash_password, |
| 46 | verify_password, |
| 47 | verify_password_sha256, |
| 48 | ) |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 49 | from osm_nbi.validation import is_valid_uuid, email_schema |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 50 | from time import time, sleep |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 51 | from http import HTTPStatus |
| 52 | from uuid import uuid4 |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 53 | from copy import deepcopy |
| 54 | from random import choice as random_choice |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 55 | import smtplib |
| 56 | from email.message import EmailMessage |
| 57 | from email.mime.text import MIMEText |
| 58 | from email.mime.multipart import MIMEMultipart |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class AuthconnInternal(Authconn): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 62 | token_time_window = 2 # seconds |
| 63 | token_delay = 1 # seconds to wait upon second request within time window |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 64 | |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 65 | users_collection = "users" |
| 66 | roles_collection = "roles" |
| 67 | projects_collection = "projects" |
| 68 | tokens_collection = "tokens" |
| 69 | |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 70 | def __init__(self, config, db, role_permissions): |
| 71 | Authconn.__init__(self, config, db, role_permissions) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 72 | self.logger = logging.getLogger("nbi.authenticator.internal") |
| 73 | |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 74 | self.db = db |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 75 | # self.msg = msg |
| 76 | # self.token_cache = token_cache |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 77 | |
| 78 | # To be Confirmed |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 79 | self.sess = None |
| elumalai | 7802ff8 | 2023-04-24 20:38:32 +0530 | [diff] [blame] | 80 | self.cef_logger = cef_event_builder(config) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 81 | |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 82 | def validate_token(self, token): |
| 83 | """ |
| 84 | Check if the token is valid. |
| 85 | |
| 86 | :param token: token to validate |
| 87 | :return: dictionary with information associated with the token: |
| 88 | "_id": token id |
| 89 | "project_id": project id |
| 90 | "project_name": project name |
| 91 | "user_id": user id |
| 92 | "username": user name |
| 93 | "roles": list with dict containing {name, id} |
| 94 | "expires": expiration date |
| 95 | If the token is not valid an exception is raised. |
| 96 | """ |
| 97 | |
| 98 | try: |
| 99 | if not token: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 100 | raise AuthException( |
| 101 | "Needed a token or Authorization HTTP header", |
| 102 | http_code=HTTPStatus.UNAUTHORIZED, |
| 103 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 104 | |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 105 | now = time() |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 106 | |
| 107 | # get from database if not in cache |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 108 | # if not token_info: |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 109 | token_info = self.db.get_one(self.tokens_collection, {"_id": token}) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 110 | if token_info["expires"] < now: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 111 | raise AuthException( |
| 112 | "Expired Token or Authorization HTTP header", |
| 113 | http_code=HTTPStatus.UNAUTHORIZED, |
| 114 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 115 | |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 116 | return token_info |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 117 | |
| 118 | except DbException as e: |
| 119 | if e.http_code == HTTPStatus.NOT_FOUND: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 120 | raise AuthException( |
| 121 | "Invalid Token or Authorization HTTP header", |
| 122 | http_code=HTTPStatus.UNAUTHORIZED, |
| 123 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 124 | else: |
| 125 | raise |
| 126 | except AuthException: |
| tierno | e1eb3b2 | 2019-08-26 15:59:24 +0000 | [diff] [blame] | 127 | raise |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 128 | except Exception: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 129 | self.logger.exception( |
| 130 | "Error during token validation using internal backend" |
| 131 | ) |
| 132 | raise AuthException( |
| 133 | "Error during token validation using internal backend", |
| 134 | http_code=HTTPStatus.UNAUTHORIZED, |
| 135 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 136 | |
| 137 | def revoke_token(self, token): |
| 138 | """ |
| 139 | Invalidate a token. |
| 140 | |
| 141 | :param token: token to be revoked |
| 142 | """ |
| 143 | try: |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 144 | # self.token_cache.pop(token, None) |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 145 | self.db.del_one(self.tokens_collection, {"_id": token}) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 146 | return True |
| 147 | except DbException as e: |
| 148 | if e.http_code == HTTPStatus.NOT_FOUND: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 149 | raise AuthException( |
| 150 | "Token '{}' not found".format(token), http_code=HTTPStatus.NOT_FOUND |
| 151 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 152 | else: |
| 153 | # raise |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 154 | exmsg = "Error during token revocation using internal backend" |
| 155 | self.logger.exception(exmsg) |
| 156 | raise AuthException(exmsg, http_code=HTTPStatus.UNAUTHORIZED) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 157 | |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 158 | def validate_user(self, user, password, otp=None): |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 159 | """ |
| 160 | Validate username and password via appropriate backend. |
| 161 | :param user: username of the user. |
| 162 | :param password: password to be validated. |
| 163 | """ |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 164 | user_rows = self.db.get_list( |
| 165 | self.users_collection, {BaseTopic.id_field("users", user): user} |
| 166 | ) |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 167 | now = time() |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 168 | user_content = None |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 169 | if user: |
| 170 | user_rows = self.db.get_list( |
| 171 | self.users_collection, |
| 172 | {BaseTopic.id_field(self.users_collection, user): user}, |
| 173 | ) |
| 174 | if user_rows: |
| 175 | user_content = user_rows[0] |
| 176 | # Updating user_status for every system_admin id role login |
| 177 | mapped_roles = user_content.get("project_role_mappings") |
| 178 | for role in mapped_roles: |
| 179 | role_id = role.get("role") |
| 180 | role_assigned = self.db.get_one( |
| 181 | self.roles_collection, |
| 182 | {BaseTopic.id_field(self.roles_collection, role_id): role_id}, |
| 183 | ) |
| 184 | |
| 185 | if role_assigned.get("permissions")["admin"]: |
| 186 | if role_assigned.get("permissions")["default"]: |
| 187 | if self.config.get("user_management"): |
| 188 | filt = {} |
| 189 | users = self.db.get_list(self.users_collection, filt) |
| 190 | for user_info in users: |
| 191 | if not user_info.get("username") == "admin": |
| 192 | if not user_info.get("_admin").get( |
| 193 | "account_expire_time" |
| 194 | ): |
| 195 | expire = now + 86400 * self.config.get( |
| 196 | "account_expire_days" |
| 197 | ) |
| 198 | self.db.set_one( |
| 199 | self.users_collection, |
| 200 | {"_id": user_info["_id"]}, |
| 201 | {"_admin.account_expire_time": expire}, |
| 202 | ) |
| 203 | else: |
| 204 | if now > user_info.get("_admin").get( |
| 205 | "account_expire_time" |
| 206 | ): |
| 207 | self.db.set_one( |
| 208 | self.users_collection, |
| 209 | {"_id": user_info["_id"]}, |
| 210 | {"_admin.user_status": "expired"}, |
| 211 | ) |
| 212 | break |
| 213 | |
| 214 | # To add "admin" user_status key while upgrading osm setup with feature enabled |
| 215 | if user_content.get("username") == "admin": |
| 216 | if self.config.get("user_management"): |
| 217 | self.db.set_one( |
| 218 | self.users_collection, |
| 219 | {"_id": user_content["_id"]}, |
| 220 | {"_admin.user_status": "always-active"}, |
| 221 | ) |
| 222 | |
| 223 | if not user_content.get("username") == "admin": |
| 224 | if self.config.get("user_management"): |
| 225 | if not user_content.get("_admin").get("account_expire_time"): |
| 226 | account_expire_time = now + 86400 * self.config.get( |
| 227 | "account_expire_days" |
| 228 | ) |
| 229 | self.db.set_one( |
| 230 | self.users_collection, |
| 231 | {"_id": user_content["_id"]}, |
| 232 | {"_admin.account_expire_time": account_expire_time}, |
| 233 | ) |
| 234 | else: |
| 235 | account_expire_time = user_content.get("_admin").get( |
| 236 | "account_expire_time" |
| 237 | ) |
| 238 | |
| 239 | if now > account_expire_time: |
| 240 | self.db.set_one( |
| 241 | self.users_collection, |
| 242 | {"_id": user_content["_id"]}, |
| 243 | {"_admin.user_status": "expired"}, |
| 244 | ) |
| 245 | raise AuthException( |
| 246 | "Account expired", http_code=HTTPStatus.UNAUTHORIZED |
| 247 | ) |
| 248 | |
| 249 | if user_content.get("_admin").get("user_status") == "locked": |
| 250 | raise AuthException( |
| 251 | "Failed to login as the account is locked due to MANY FAILED ATTEMPTS" |
| 252 | ) |
| 253 | elif user_content.get("_admin").get("user_status") == "expired": |
| 254 | raise AuthException( |
| 255 | "Failed to login as the account is expired" |
| 256 | ) |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 257 | if otp: |
| 258 | return user_content |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 259 | correct_pwd = False |
| 260 | if user_content.get("hashing_function") == "bcrypt": |
| 261 | correct_pwd = verify_password( |
| 262 | password=password, hashed_password_hex=user_content["password"] |
| 263 | ) |
| 264 | else: |
| 265 | correct_pwd = verify_password_sha256( |
| 266 | password=password, |
| 267 | hashed_password_hex=user_content["password"], |
| 268 | salt=user_content["_admin"]["salt"], |
| 269 | ) |
| 270 | if not correct_pwd: |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 271 | count = 1 |
| 272 | if user_content.get("_admin").get("retry_count") >= 0: |
| 273 | count += user_content.get("_admin").get("retry_count") |
| 274 | self.db.set_one( |
| 275 | self.users_collection, |
| 276 | {"_id": user_content["_id"]}, |
| 277 | {"_admin.retry_count": count}, |
| 278 | ) |
| 279 | self.logger.debug( |
| 280 | "Failed Authentications count: {}".format(count) |
| 281 | ) |
| 282 | |
| 283 | if user_content.get("username") == "admin": |
| 284 | user_content = None |
| 285 | else: |
| 286 | if not self.config.get("user_management"): |
| 287 | user_content = None |
| 288 | else: |
| 289 | if ( |
| 290 | user_content.get("_admin").get("retry_count") |
| 291 | >= self.config["max_pwd_attempt"] - 1 |
| 292 | ): |
| 293 | self.db.set_one( |
| 294 | self.users_collection, |
| 295 | {"_id": user_content["_id"]}, |
| 296 | {"_admin.user_status": "locked"}, |
| 297 | ) |
| 298 | raise AuthException( |
| 299 | "Failed to login as the account is locked due to MANY FAILED ATTEMPTS" |
| 300 | ) |
| 301 | else: |
| 302 | user_content = None |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 303 | elif correct_pwd and user_content.get("hashing_function") != "bcrypt": |
| 304 | # Update the database using a more secure hashing function to store the password |
| 305 | user_content["password"] = hash_password( |
| 306 | password=password, |
| 307 | rounds=self.config.get("password_rounds", 12), |
| 308 | ) |
| 309 | user_content["hashing_function"] = "bcrypt" |
| 310 | user_content["_admin"]["password_history_sha256"] = user_content[ |
| 311 | "_admin" |
| 312 | ]["password_history"] |
| 313 | user_content["_admin"]["password_history"] = [ |
| 314 | user_content["password"] |
| 315 | ] |
| 316 | del user_content["_admin"]["salt"] |
| 317 | |
| 318 | uid = user_content["_id"] |
| 319 | idf = BaseTopic.id_field("users", uid) |
| 320 | self.db.set_one(self.users_collection, {idf: uid}, user_content) |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 321 | return user_content |
| 322 | |
| tierno | 6486f74 | 2020-02-13 16:30:14 +0000 | [diff] [blame] | 323 | def authenticate(self, credentials, token_info=None): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 324 | """ |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 325 | Authenticate a user using username/password or previous token_info plus project; its creates a new token |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 326 | |
| tierno | 6486f74 | 2020-02-13 16:30:14 +0000 | [diff] [blame] | 327 | :param credentials: dictionary that contains: |
| 328 | username: name, id or None |
| 329 | password: password or None |
| 330 | project_id: name, id, or None. If None first found project will be used to get an scope token |
| 331 | other items are allowed and ignored |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 332 | :param token_info: previous token_info to obtain authorization |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 333 | :return: the scoped token info or raises an exception. The token is a dictionary with: |
| 334 | _id: token string id, |
| 335 | username: username, |
| 336 | project_id: scoped_token project_id, |
| 337 | project_name: scoped_token project_name, |
| 338 | expires: epoch time when it expires, |
| 339 | """ |
| 340 | |
| 341 | now = time() |
| 342 | user_content = None |
| tierno | 6486f74 | 2020-02-13 16:30:14 +0000 | [diff] [blame] | 343 | user = credentials.get("username") |
| 344 | password = credentials.get("password") |
| 345 | project = credentials.get("project_id") |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 346 | otp_validation = credentials.get("otp") |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 347 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 348 | # Try using username/password |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 349 | if otp_validation: |
| 350 | user_content = self.validate_user(user, password=None, otp=otp_validation) |
| 351 | elif user: |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 352 | user_content = self.validate_user(user, password) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 353 | if not user_content: |
| elumalai | 7802ff8 | 2023-04-24 20:38:32 +0530 | [diff] [blame] | 354 | cef_event( |
| 355 | self.cef_logger, |
| 356 | { |
| 357 | "name": "User login", |
| 358 | "sourceUserName": user, |
| 359 | "message": "Invalid username/password Project={} Outcome=Failure".format( |
| 360 | project |
| 361 | ), |
| 362 | "severity": "3", |
| 363 | }, |
| 364 | ) |
| 365 | self.logger.exception("{}".format(self.cef_logger)) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 366 | raise AuthException( |
| 367 | "Invalid username/password", http_code=HTTPStatus.UNAUTHORIZED |
| 368 | ) |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 369 | if not user_content.get("_admin", None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 370 | raise AuthException( |
| 371 | "No default project for this user.", |
| 372 | http_code=HTTPStatus.UNAUTHORIZED, |
| 373 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 374 | elif token_info: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 375 | user_rows = self.db.get_list( |
| 376 | self.users_collection, {"username": token_info["username"]} |
| 377 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 378 | if user_rows: |
| 379 | user_content = user_rows[0] |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 380 | else: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 381 | raise AuthException("Invalid token", http_code=HTTPStatus.UNAUTHORIZED) |
| 382 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 383 | raise AuthException( |
| 384 | "Provide credentials: username/password or Authorization Bearer token", |
| 385 | http_code=HTTPStatus.UNAUTHORIZED, |
| 386 | ) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 387 | # Delay upon second request within time window |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 388 | if ( |
| 389 | now - user_content["_admin"].get("last_token_time", 0) |
| 390 | < self.token_time_window |
| 391 | ): |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 392 | sleep(self.token_delay) |
| 393 | # user_content["_admin"]["last_token_time"] = now |
| 394 | # self.db.replace("users", user_content["_id"], user_content) # might cause race conditions |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 395 | user_data = { |
| 396 | "_admin.last_token_time": now, |
| 397 | "_admin.retry_count": 0, |
| 398 | } |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 399 | self.db.set_one( |
| 400 | self.users_collection, |
| 401 | {"_id": user_content["_id"]}, |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 402 | user_data, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 403 | ) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 404 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 405 | token_id = "".join( |
| 406 | random_choice( |
| 407 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 408 | ) |
| 409 | for _ in range(0, 32) |
| 410 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 411 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 412 | # projects = user_content.get("projects", []) |
| 413 | prm_list = user_content.get("project_role_mappings", []) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 414 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 415 | if not project: |
| 416 | project = prm_list[0]["project"] if prm_list else None |
| 417 | if not project: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 418 | raise AuthException( |
| 419 | "can't find a default project for this user", |
| 420 | http_code=HTTPStatus.UNAUTHORIZED, |
| 421 | ) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 422 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 423 | projects = [prm["project"] for prm in prm_list] |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 424 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 425 | proj = self.db.get_one( |
| 426 | self.projects_collection, {BaseTopic.id_field("projects", project): project} |
| 427 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 428 | project_name = proj["name"] |
| 429 | project_id = proj["_id"] |
| 430 | if project_name not in projects and project_id not in projects: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 431 | raise AuthException( |
| 432 | "project {} not allowed for this user".format(project), |
| 433 | http_code=HTTPStatus.UNAUTHORIZED, |
| 434 | ) |
| tierno | 701018c | 2019-06-25 11:13:14 +0000 | [diff] [blame] | 435 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 436 | # TODO remove admin, this vill be used by roles RBAC |
| 437 | if project_name == "admin": |
| 438 | token_admin = True |
| 439 | else: |
| 440 | token_admin = proj.get("admin", False) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 441 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 442 | # add token roles |
| 443 | roles = [] |
| 444 | roles_list = [] |
| 445 | for prm in prm_list: |
| 446 | if prm["project"] in [project_id, project_name]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 447 | role = self.db.get_one( |
| 448 | self.roles_collection, |
| 449 | {BaseTopic.id_field("roles", prm["role"]): prm["role"]}, |
| 450 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 451 | rid = role["_id"] |
| 452 | if rid not in roles: |
| 453 | rnm = role["name"] |
| 454 | roles.append(rid) |
| 455 | roles_list.append({"name": rnm, "id": rid}) |
| 456 | if not roles_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 457 | rid = self.db.get_one(self.roles_collection, {"name": "project_admin"})[ |
| 458 | "_id" |
| 459 | ] |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 460 | roles_list = [{"name": "project_admin", "id": rid}] |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 461 | |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 462 | login_count = user_content.get("_admin").get("retry_count") |
| 463 | last_token_time = user_content.get("_admin").get("last_token_time") |
| 464 | |
| 465 | admin_show = False |
| 466 | user_show = False |
| 467 | if self.config.get("user_management"): |
| 468 | for role in roles_list: |
| 469 | role_id = role.get("id") |
| 470 | permission = self.db.get_one( |
| 471 | self.roles_collection, |
| 472 | {BaseTopic.id_field(self.roles_collection, role_id): role_id}, |
| 473 | ) |
| 474 | if permission.get("permissions")["admin"]: |
| 475 | if permission.get("permissions")["default"]: |
| 476 | admin_show = True |
| 477 | break |
| 478 | else: |
| 479 | user_show = True |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 480 | new_token = { |
| 481 | "issued_at": now, |
| 482 | "expires": now + 3600, |
| 483 | "_id": token_id, |
| 484 | "id": token_id, |
| 485 | "project_id": proj["_id"], |
| 486 | "project_name": proj["name"], |
| 487 | "username": user_content["username"], |
| 488 | "user_id": user_content["_id"], |
| 489 | "admin": token_admin, |
| 490 | "roles": roles_list, |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 491 | "login_count": login_count, |
| 492 | "last_login": last_token_time, |
| 493 | "admin_show": admin_show, |
| 494 | "user_show": user_show, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 495 | } |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 496 | |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 497 | self.db.create(self.tokens_collection, new_token) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 498 | return deepcopy(new_token) |
| 499 | |
| 500 | def get_role_list(self, filter_q={}): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 501 | """ |
| 502 | Get role list. |
| 503 | |
| 504 | :return: returns the list of roles. |
| 505 | """ |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 506 | return self.db.get_list(self.roles_collection, filter_q) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 507 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 508 | def create_role(self, role_info): |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 509 | """ |
| 510 | Create a role. |
| 511 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 512 | :param role_info: full role info. |
| 513 | :return: returns the role id. |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 514 | :raises AuthconnOperationException: if role creation failed. |
| 515 | """ |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 516 | # TODO: Check that role name does not exist ? |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 517 | rid = str(uuid4()) |
| 518 | role_info["_id"] = rid |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 519 | rid = self.db.create(self.roles_collection, role_info) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 520 | return rid |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 521 | |
| 522 | def delete_role(self, role_id): |
| 523 | """ |
| 524 | Delete a role. |
| 525 | |
| 526 | :param role_id: role identifier. |
| 527 | :raises AuthconnOperationException: if role deletion failed. |
| 528 | """ |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 529 | rc = self.db.del_one(self.roles_collection, {"_id": role_id}) |
| 530 | self.db.del_list(self.tokens_collection, {"roles.id": role_id}) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 531 | return rc |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 532 | |
| 533 | def update_role(self, role_info): |
| 534 | """ |
| 535 | Update a role. |
| 536 | |
| 537 | :param role_info: full role info. |
| 538 | :return: returns the role name and id. |
| 539 | :raises AuthconnOperationException: if user creation failed. |
| 540 | """ |
| 541 | rid = role_info["_id"] |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 542 | self.db.set_one(self.roles_collection, {"_id": rid}, role_info) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 543 | return {"_id": rid, "name": role_info["name"]} |
| 544 | |
| 545 | def create_user(self, user_info): |
| 546 | """ |
| 547 | Create a user. |
| 548 | |
| 549 | :param user_info: full user info. |
| 550 | :return: returns the username and id of the user. |
| 551 | """ |
| 552 | BaseTopic.format_on_new(user_info, make_public=False) |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 553 | user_info["_admin"]["user_status"] = "active" |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 554 | present = time() |
| 555 | if not user_info["username"] == "admin": |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 556 | if self.config.get("user_management"): |
| 557 | user_info["_admin"]["modified"] = present |
| 558 | user_info["_admin"]["password_expire_time"] = present |
| 559 | account_expire_time = present + 86400 * self.config.get( |
| 560 | "account_expire_days" |
| 561 | ) |
| 562 | user_info["_admin"]["account_expire_time"] = account_expire_time |
| 563 | |
| 564 | user_info["_admin"]["retry_count"] = 0 |
| 565 | user_info["_admin"]["last_token_time"] = present |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 566 | if "password" in user_info: |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 567 | user_info["password"] = hash_password( |
| 568 | password=user_info["password"], |
| 569 | rounds=self.config.get("password_rounds", 12), |
| 570 | ) |
| 571 | user_info["hashing_function"] = "bcrypt" |
| 572 | user_info["_admin"]["password_history"] = [user_info["password"]] |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 573 | # "projects" are not stored any more |
| 574 | if "projects" in user_info: |
| 575 | del user_info["projects"] |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 576 | self.db.create(self.users_collection, user_info) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 577 | return {"username": user_info["username"], "_id": user_info["_id"]} |
| 578 | |
| 579 | def update_user(self, user_info): |
| 580 | """ |
| 581 | Change the user name and/or password. |
| 582 | |
| 583 | :param user_info: user info modifications |
| 584 | """ |
| 585 | uid = user_info["_id"] |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 586 | old_pwd = user_info.get("old_password") |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 587 | unlock = user_info.get("unlock") |
| 588 | renew = user_info.get("renew") |
| 589 | permission_id = user_info.get("system_admin_id") |
| Adurti | 0c9b010 | 2023-11-08 11:16:32 +0000 | [diff] [blame] | 590 | now = time() |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 591 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 592 | user_data = self.db.get_one( |
| 593 | self.users_collection, {BaseTopic.id_field("users", uid): uid} |
| 594 | ) |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 595 | if old_pwd: |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 596 | correct_pwd = False |
| 597 | if user_data.get("hashing_function") == "bcrypt": |
| 598 | correct_pwd = verify_password( |
| 599 | password=old_pwd, hashed_password_hex=user_data["password"] |
| 600 | ) |
| 601 | else: |
| 602 | correct_pwd = verify_password_sha256( |
| 603 | password=old_pwd, |
| 604 | hashed_password_hex=user_data["password"], |
| 605 | salt=user_data["salt"], |
| 606 | ) |
| 607 | if not correct_pwd: |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 608 | raise AuthconnConflictException( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 609 | "Incorrect password", http_code=HTTPStatus.CONFLICT |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 610 | ) |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 611 | # Unlocking the user |
| 612 | if unlock: |
| 613 | system_user = None |
| 614 | unlock_state = False |
| 615 | if not permission_id: |
| 616 | raise AuthconnConflictException( |
| 617 | "system_admin_id is the required field to unlock the user", |
| 618 | http_code=HTTPStatus.CONFLICT, |
| 619 | ) |
| 620 | else: |
| 621 | system_user = self.db.get_one( |
| 622 | self.users_collection, |
| 623 | { |
| 624 | BaseTopic.id_field( |
| 625 | self.users_collection, permission_id |
| 626 | ): permission_id |
| 627 | }, |
| 628 | ) |
| 629 | mapped_roles = system_user.get("project_role_mappings") |
| 630 | for role in mapped_roles: |
| 631 | role_id = role.get("role") |
| 632 | role_assigned = self.db.get_one( |
| 633 | self.roles_collection, |
| 634 | {BaseTopic.id_field(self.roles_collection, role_id): role_id}, |
| 635 | ) |
| 636 | if role_assigned.get("permissions")["admin"]: |
| 637 | if role_assigned.get("permissions")["default"]: |
| 638 | user_data["_admin"]["retry_count"] = 0 |
| Adurti | 0c9b010 | 2023-11-08 11:16:32 +0000 | [diff] [blame] | 639 | if now > user_data["_admin"]["account_expire_time"]: |
| 640 | user_data["_admin"]["user_status"] = "expired" |
| 641 | else: |
| 642 | user_data["_admin"]["user_status"] = "active" |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 643 | unlock_state = True |
| 644 | break |
| 645 | if not unlock_state: |
| 646 | raise AuthconnConflictException( |
| 647 | "User '{}' does not have the privilege to unlock the user".format( |
| 648 | permission_id |
| 649 | ), |
| 650 | http_code=HTTPStatus.CONFLICT, |
| 651 | ) |
| 652 | # Renewing the user |
| 653 | if renew: |
| 654 | system_user = None |
| 655 | renew_state = False |
| 656 | if not permission_id: |
| 657 | raise AuthconnConflictException( |
| 658 | "system_admin_id is the required field to renew the user", |
| 659 | http_code=HTTPStatus.CONFLICT, |
| 660 | ) |
| 661 | else: |
| 662 | system_user = self.db.get_one( |
| 663 | self.users_collection, |
| 664 | { |
| 665 | BaseTopic.id_field( |
| 666 | self.users_collection, permission_id |
| 667 | ): permission_id |
| 668 | }, |
| 669 | ) |
| 670 | mapped_roles = system_user.get("project_role_mappings") |
| 671 | for role in mapped_roles: |
| 672 | role_id = role.get("role") |
| 673 | role_assigned = self.db.get_one( |
| 674 | self.roles_collection, |
| 675 | {BaseTopic.id_field(self.roles_collection, role_id): role_id}, |
| 676 | ) |
| 677 | if role_assigned.get("permissions")["admin"]: |
| 678 | if role_assigned.get("permissions")["default"]: |
| 679 | present = time() |
| 680 | account_expire = ( |
| 681 | present + 86400 * self.config["account_expire_days"] |
| 682 | ) |
| 683 | user_data["_admin"]["modified"] = present |
| 684 | user_data["_admin"]["account_expire_time"] = account_expire |
| Adurti | 0c9b010 | 2023-11-08 11:16:32 +0000 | [diff] [blame] | 685 | if ( |
| 686 | user_data["_admin"]["retry_count"] |
| 687 | >= self.config["max_pwd_attempt"] |
| 688 | ): |
| 689 | user_data["_admin"]["user_status"] = "locked" |
| 690 | else: |
| 691 | user_data["_admin"]["user_status"] = "active" |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 692 | renew_state = True |
| 693 | break |
| 694 | if not renew_state: |
| 695 | raise AuthconnConflictException( |
| 696 | "User '{}' does not have the privilege to renew the user".format( |
| 697 | permission_id |
| 698 | ), |
| 699 | http_code=HTTPStatus.CONFLICT, |
| 700 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 701 | BaseTopic.format_on_edit(user_data, user_info) |
| 702 | # User Name |
| 703 | usnm = user_info.get("username") |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 704 | email_id = user_info.get("email_id") |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 705 | if usnm: |
| 706 | user_data["username"] = usnm |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 707 | if email_id: |
| 708 | user_data["email_id"] = email_id |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 709 | # If password is given and is not already encripted |
| 710 | pswd = user_info.get("password") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 711 | if pswd and ( |
| 712 | len(pswd) != 64 or not re.match("[a-fA-F0-9]*", pswd) |
| 713 | ): # TODO: Improve check? |
| elumalai | 7802ff8 | 2023-04-24 20:38:32 +0530 | [diff] [blame] | 714 | cef_event( |
| 715 | self.cef_logger, |
| 716 | { |
| 717 | "name": "Change Password", |
| 718 | "sourceUserName": user_data["username"], |
| garciadeblas | f53612b | 2024-07-12 14:44:37 +0200 | [diff] [blame] | 719 | "message": "User {} changing Password for user {}, Outcome=Success".format( |
| 720 | user_info.get("session_user"), user_data["username"] |
| 721 | ), |
| elumalai | 7802ff8 | 2023-04-24 20:38:32 +0530 | [diff] [blame] | 722 | "severity": "2", |
| 723 | }, |
| 724 | ) |
| 725 | self.logger.info("{}".format(self.cef_logger)) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 726 | if "_admin" not in user_data: |
| 727 | user_data["_admin"] = {} |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 728 | if user_data.get("_admin").get("password_history"): |
| 729 | old_pwds = user_data.get("_admin").get("password_history") |
| 730 | else: |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 731 | old_pwds = [] |
| 732 | for v in old_pwds: |
| 733 | if verify_password(password=pswd, hashed_password_hex=v): |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 734 | raise AuthconnConflictException( |
| 735 | "Password is used before", http_code=HTTPStatus.CONFLICT |
| 736 | ) |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 737 | |
| 738 | # Backwards compatibility for SHA256 hashed passwords |
| 739 | if user_data.get("_admin").get("password_history_sha256"): |
| 740 | old_pwds_sha256 = user_data.get("_admin").get("password_history_sha256") |
| 741 | else: |
| 742 | old_pwds_sha256 = {} |
| 743 | for k, v in old_pwds_sha256.items(): |
| 744 | if verify_password_sha256(password=pswd, hashed_password_hex=v, salt=k): |
| 745 | raise AuthconnConflictException( |
| 746 | "Password is used before", http_code=HTTPStatus.CONFLICT |
| 747 | ) |
| 748 | |
| 749 | # Finally, hash the password to be updated |
| 750 | user_data["password"] = hash_password( |
| 751 | password=pswd, rounds=self.config.get("password_rounds", 12) |
| 752 | ) |
| 753 | user_data["hashing_function"] = "bcrypt" |
| 754 | |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 755 | if len(old_pwds) >= 3: |
| 756 | old_pwds.pop(list(old_pwds.keys())[0]) |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 757 | old_pwds.append([user_data["password"]]) |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 758 | user_data["_admin"]["password_history"] = old_pwds |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 759 | if not user_data["username"] == "admin": |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 760 | if self.config.get("user_management"): |
| selvi.j | a9a1fc8 | 2022-04-04 06:54:30 +0000 | [diff] [blame] | 761 | present = time() |
| garciadeblas | 6d83f8f | 2023-06-19 22:34:49 +0200 | [diff] [blame] | 762 | if self.config.get("pwd_expire_days"): |
| 763 | expire = present + 86400 * self.config.get("pwd_expire_days") |
| 764 | user_data["_admin"]["modified"] = present |
| 765 | user_data["_admin"]["password_expire_time"] = expire |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 766 | # Project-Role Mappings |
| 767 | # TODO: Check that user_info NEVER includes "project_role_mappings" |
| 768 | if "project_role_mappings" not in user_data: |
| 769 | user_data["project_role_mappings"] = [] |
| 770 | for prm in user_info.get("add_project_role_mappings", []): |
| 771 | user_data["project_role_mappings"].append(prm) |
| 772 | for prm in user_info.get("remove_project_role_mappings", []): |
| 773 | for pidf in ["project", "project_name"]: |
| 774 | for ridf in ["role", "role_name"]: |
| 775 | try: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 776 | user_data["project_role_mappings"].remove( |
| 777 | {"role": prm[ridf], "project": prm[pidf]} |
| 778 | ) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 779 | except KeyError: |
| 780 | pass |
| 781 | except ValueError: |
| 782 | pass |
| delacruzramo | 3d6881c | 2019-12-04 13:42:26 +0100 | [diff] [blame] | 783 | idf = BaseTopic.id_field("users", uid) |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 784 | self.db.set_one(self.users_collection, {idf: uid}, user_data) |
| delacruzramo | 3d6881c | 2019-12-04 13:42:26 +0100 | [diff] [blame] | 785 | if user_info.get("remove_project_role_mappings"): |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 786 | idf = "user_id" if idf == "_id" else idf |
| Adurti | 9f2ac99 | 2024-03-25 10:58:29 +0000 | [diff] [blame] | 787 | if not user_data.get("project_role_mappings") or user_info.get( |
| 788 | "remove_session_project" |
| 789 | ): |
| 790 | self.db.del_list(self.tokens_collection, {idf: uid}) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 791 | |
| 792 | def delete_user(self, user_id): |
| 793 | """ |
| 794 | Delete user. |
| 795 | |
| 796 | :param user_id: user identifier. |
| 797 | :raises AuthconnOperationException: if user deletion failed. |
| 798 | """ |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 799 | self.db.del_one(self.users_collection, {"_id": user_id}) |
| 800 | self.db.del_list(self.tokens_collection, {"user_id": user_id}) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 801 | return True |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 802 | |
| 803 | def get_user_list(self, filter_q=None): |
| 804 | """ |
| 805 | Get user list. |
| 806 | |
| tierno | 5ec768a | 2020-03-31 09:46:44 +0000 | [diff] [blame] | 807 | :param filter_q: dictionary to filter user list by: |
| 808 | name (username is also admitted). If a user id is equal to the filter name, it is also provided |
| 809 | other |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 810 | :return: returns a list of users. |
| 811 | """ |
| 812 | filt = filter_q or {} |
| tierno | 5ec768a | 2020-03-31 09:46:44 +0000 | [diff] [blame] | 813 | if "name" in filt: # backward compatibility |
| 814 | filt["username"] = filt.pop("name") |
| 815 | if filt.get("username") and is_valid_uuid(filt["username"]): |
| 816 | # username cannot be a uuid. If this is the case, change from username to _id |
| 817 | filt["_id"] = filt.pop("username") |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 818 | users = self.db.get_list(self.users_collection, filt) |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 819 | project_id_name = {} |
| 820 | role_id_name = {} |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 821 | for user in users: |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 822 | prms = user.get("project_role_mappings") |
| 823 | projects = user.get("projects") |
| 824 | if prms: |
| 825 | projects = [] |
| 826 | # add project_name and role_name. Generate projects for backward compatibility |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 827 | for prm in prms: |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 828 | project_id = prm["project"] |
| 829 | if project_id not in project_id_name: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 830 | pr = self.db.get_one( |
| 831 | self.projects_collection, |
| 832 | {BaseTopic.id_field("projects", project_id): project_id}, |
| 833 | fail_on_empty=False, |
| 834 | ) |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 835 | project_id_name[project_id] = pr["name"] if pr else None |
| 836 | prm["project_name"] = project_id_name[project_id] |
| 837 | if prm["project_name"] not in projects: |
| 838 | projects.append(prm["project_name"]) |
| 839 | |
| 840 | role_id = prm["role"] |
| 841 | if role_id not in role_id_name: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 842 | role = self.db.get_one( |
| 843 | self.roles_collection, |
| 844 | {BaseTopic.id_field("roles", role_id): role_id}, |
| 845 | fail_on_empty=False, |
| 846 | ) |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 847 | role_id_name[role_id] = role["name"] if role else None |
| 848 | prm["role_name"] = role_id_name[role_id] |
| 849 | user["projects"] = projects # for backward compatibility |
| 850 | elif projects: |
| 851 | # user created with an old version. Create a project_role mapping with role project_admin |
| 852 | user["project_role_mappings"] = [] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 853 | role = self.db.get_one( |
| 854 | self.roles_collection, |
| 855 | {BaseTopic.id_field("roles", "project_admin"): "project_admin"}, |
| 856 | ) |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 857 | for p_id_name in projects: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 858 | pr = self.db.get_one( |
| 859 | self.projects_collection, |
| 860 | {BaseTopic.id_field("projects", p_id_name): p_id_name}, |
| 861 | ) |
| 862 | prm = { |
| 863 | "project": pr["_id"], |
| 864 | "project_name": pr["name"], |
| 865 | "role_name": "project_admin", |
| 866 | "role": role["_id"], |
| 867 | } |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 868 | user["project_role_mappings"].append(prm) |
| 869 | else: |
| 870 | user["projects"] = [] |
| 871 | user["project_role_mappings"] = [] |
| 872 | |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 873 | return users |
| 874 | |
| 875 | def get_project_list(self, filter_q={}): |
| 876 | """ |
| 877 | Get role list. |
| 878 | |
| 879 | :return: returns the list of projects. |
| 880 | """ |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 881 | return self.db.get_list(self.projects_collection, filter_q) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 882 | |
| 883 | def create_project(self, project_info): |
| 884 | """ |
| 885 | Create a project. |
| 886 | |
| 887 | :param project: full project info. |
| 888 | :return: the internal id of the created project |
| 889 | :raises AuthconnOperationException: if project creation failed. |
| 890 | """ |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 891 | pid = self.db.create(self.projects_collection, project_info) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 892 | return pid |
| 893 | |
| 894 | def delete_project(self, project_id): |
| 895 | """ |
| 896 | Delete a project. |
| 897 | |
| 898 | :param project_id: project identifier. |
| 899 | :raises AuthconnOperationException: if project deletion failed. |
| 900 | """ |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 901 | idf = BaseTopic.id_field("projects", project_id) |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 902 | r = self.db.del_one(self.projects_collection, {idf: project_id}) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 903 | idf = "project_id" if idf == "_id" else "project_name" |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 904 | self.db.del_list(self.tokens_collection, {idf: project_id}) |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 905 | return r |
| 906 | |
| 907 | def update_project(self, project_id, project_info): |
| 908 | """ |
| 909 | Change the name of a project |
| 910 | |
| 911 | :param project_id: project to be changed |
| 912 | :param project_info: full project info |
| 913 | :return: None |
| 914 | :raises AuthconnOperationException: if project update failed. |
| 915 | """ |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 916 | self.db.set_one( |
| 917 | self.projects_collection, |
| 918 | {BaseTopic.id_field("projects", project_id): project_id}, |
| 919 | project_info, |
| 920 | ) |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 921 | |
| 922 | def generate_otp(self): |
| 923 | otp = "".join(random_choice("0123456789") for i in range(0, 4)) |
| 924 | return otp |
| 925 | |
| 926 | def send_email(self, indata): |
| 927 | user = indata.get("username") |
| 928 | user_rows = self.db.get_list(self.users_collection, {"username": user}) |
| 929 | sender_password = None |
| 930 | otp_expiry_time = self.config.get("otp_expiry_time", 300) |
| 931 | if not re.match(email_schema["pattern"], indata.get("email_id")): |
| 932 | raise AuthException( |
| 933 | "Invalid email-id", |
| 934 | http_code=HTTPStatus.BAD_REQUEST, |
| 935 | ) |
| 936 | if self.config.get("sender_email"): |
| 937 | sender_email = self.config["sender_email"] |
| 938 | else: |
| 939 | raise AuthException( |
| 940 | "sender_email not found", |
| 941 | http_code=HTTPStatus.NOT_FOUND, |
| 942 | ) |
| 943 | if self.config.get("smtp_server"): |
| 944 | smtp_server = self.config["smtp_server"] |
| 945 | else: |
| 946 | raise AuthException( |
| 947 | "smtp server not found", |
| 948 | http_code=HTTPStatus.NOT_FOUND, |
| 949 | ) |
| 950 | if self.config.get("smtp_port"): |
| 951 | smtp_port = self.config["smtp_port"] |
| 952 | else: |
| 953 | raise AuthException( |
| 954 | "smtp port not found", |
| 955 | http_code=HTTPStatus.NOT_FOUND, |
| 956 | ) |
| 957 | sender_password = self.config.get("sender_password") or None |
| 958 | if user_rows: |
| 959 | user_data = user_rows[0] |
| 960 | user_status = user_data["_admin"]["user_status"] |
| 961 | if not user_data.get("project_role_mappings", None): |
| 962 | raise AuthException( |
| 963 | "can't find a default project for this user", |
| 964 | http_code=HTTPStatus.UNAUTHORIZED, |
| 965 | ) |
| 966 | if user_status != "active" and user_status != "always-active": |
| 967 | raise AuthException( |
| 968 | f"User account is {user_status}.Please contact the system administrator.", |
| 969 | http_code=HTTPStatus.UNAUTHORIZED, |
| 970 | ) |
| 971 | if user_data.get("email_id"): |
| 972 | if user_data["email_id"] == indata.get("email_id"): |
| 973 | otp = self.generate_otp() |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 974 | encode_otp = hash_password( |
| 975 | password=otp, rounds=self.config.get("password_rounds", 12) |
| 976 | ) |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 977 | otp_field = {encode_otp: time() + otp_expiry_time, "retries": 0} |
| 978 | user_data["OTP"] = otp_field |
| 979 | uid = user_data["_id"] |
| 980 | idf = BaseTopic.id_field("users", uid) |
| 981 | reciever_email = user_data["email_id"] |
| 982 | email_template_path = self.config.get("email_template") |
| 983 | with open(email_template_path, "r") as et: |
| 984 | email_template = et.read() |
| 985 | msg = EmailMessage() |
| 986 | msg = MIMEMultipart("alternative") |
| 987 | html_content = email_template.format( |
| 988 | username=user_data["username"], |
| 989 | otp=otp, |
| 990 | validity=otp_expiry_time // 60, |
| 991 | ) |
| 992 | html = MIMEText(html_content, "html") |
| 993 | msg["Subject"] = "OSM password reset request" |
| 994 | msg.attach(html) |
| 995 | with smtplib.SMTP(smtp_server, smtp_port) as smtp: |
| 996 | smtp.starttls() |
| 997 | if sender_password: |
| 998 | smtp.login(sender_email, sender_password) |
| 999 | smtp.sendmail(sender_email, reciever_email, msg.as_string()) |
| 1000 | self.db.set_one(self.users_collection, {idf: uid}, user_data) |
| 1001 | return {"email": "sent"} |
| 1002 | else: |
| 1003 | raise AuthException( |
| 1004 | "No email id is registered for this user.Please contact the system administrator.", |
| 1005 | http_code=HTTPStatus.NOT_FOUND, |
| 1006 | ) |
| 1007 | else: |
| 1008 | raise AuthException( |
| 1009 | "user not found", |
| 1010 | http_code=HTTPStatus.NOT_FOUND, |
| 1011 | ) |
| 1012 | |
| 1013 | def validate_otp(self, indata): |
| 1014 | otp = indata.get("otp") |
| 1015 | user = indata.get("username") |
| 1016 | user_rows = self.db.get_list(self.users_collection, {"username": user}) |
| 1017 | user_data = user_rows[0] |
| 1018 | uid = user_data["_id"] |
| 1019 | idf = BaseTopic.id_field("users", uid) |
| 1020 | retry_count = self.config.get("retry_count", 3) |
| 1021 | if user_data: |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 1022 | if not user_data.get("OTP"): |
| 1023 | otp_field = {"retries": 1} |
| 1024 | user_data["OTP"] = otp_field |
| 1025 | self.db.set_one(self.users_collection, {idf: uid}, user_data) |
| 1026 | return {"retries": user_data["OTP"]["retries"]} |
| 1027 | for key, value in user_data["OTP"].items(): |
| 1028 | curr_time = time() |
| escaleira | 9e39382 | 2025-04-03 18:53:24 +0100 | [diff] [blame] | 1029 | if ( |
| 1030 | verify_password(password=otp, hashed_password_hex=key) |
| 1031 | and curr_time < value |
| 1032 | ): |
| jegan | be1a3df | 2024-06-04 12:05:19 +0000 | [diff] [blame] | 1033 | user_data["OTP"] = {} |
| 1034 | self.db.set_one(self.users_collection, {idf: uid}, user_data) |
| 1035 | return {"valid": "True", "password_change": "True"} |
| 1036 | else: |
| 1037 | user_data["OTP"]["retries"] += 1 |
| 1038 | self.db.set_one(self.users_collection, {idf: uid}, user_data) |
| 1039 | if user_data["OTP"].get("retries") >= retry_count: |
| 1040 | raise AuthException( |
| 1041 | "Invalid OTP. Maximum retries exceeded", |
| 1042 | http_code=HTTPStatus.TOO_MANY_REQUESTS, |
| 1043 | ) |
| 1044 | return {"retry_count": user_data["OTP"]["retries"]} |