|
| 1 | +import uuid |
| 2 | +from typing import Tuple, Union |
| 3 | + |
| 4 | +from src.modules.utils import hash_data |
| 5 | + |
| 6 | +from ssh_manager_backend.app.controllers.secrets import Secrets |
| 7 | +from ssh_manager_backend.app.models import SessionModel, UserModel |
| 8 | + |
| 9 | + |
| 10 | +class User: |
| 11 | + def __init__(self): |
| 12 | + self.secrets: Secrets = Secrets() |
| 13 | + self.username: str = "" |
| 14 | + self.__access_token: str = "" |
| 15 | + self.__password: str = "" |
| 16 | + self.email: str = "" |
| 17 | + self.name: str = "" |
| 18 | + self.admin: str = "" |
| 19 | + self.user: UserModel = UserModel() |
| 20 | + |
| 21 | + def set_attributes( |
| 22 | + self, username: str, password: str, email: str, name: str, access_token: str |
| 23 | + ): |
| 24 | + """ |
| 25 | + Sets the class attribute based on the function arguments. |
| 26 | +
|
| 27 | + :param username: The username of the user |
| 28 | + :param password: The password of the user |
| 29 | + :param email: The email of the user |
| 30 | + :param name: The name of the user |
| 31 | + :param access_token: The access token of the user |
| 32 | + :return: |
| 33 | + """ |
| 34 | + |
| 35 | + self.username = username |
| 36 | + self.__password = password |
| 37 | + self.email = email |
| 38 | + self.name = name |
| 39 | + self.__access_token = access_token |
| 40 | + |
| 41 | + def register(self, username: str, password: str, email: str, name: str) -> bool: |
| 42 | + """ |
| 43 | + Generates the secrets of the user based on the password. Hashes the password using SAH256 and stores the |
| 44 | + generated information in DynamoDB. |
| 45 | +
|
| 46 | + :param username: |
| 47 | + :param password: |
| 48 | + :param email: |
| 49 | + :param name: |
| 50 | + :param table_name: |
| 51 | + :return: True/False for success/failure |
| 52 | + """ |
| 53 | + |
| 54 | + self.set_attributes( |
| 55 | + username=username, |
| 56 | + password=password, |
| 57 | + email=email, |
| 58 | + name=name, |
| 59 | + access_token=uuid.uuid4().hex, |
| 60 | + ) |
| 61 | + |
| 62 | + self.secrets.generate_secrets(password=self.__password) |
| 63 | + self.__password = hash_data(self.__password, self.salt_for_password) |
| 64 | + user_is_admin = True |
| 65 | + |
| 66 | + if self.user.admin_exists(): |
| 67 | + user_is_admin = False |
| 68 | + |
| 69 | + return self.user.create( |
| 70 | + name=name, |
| 71 | + username=self.username, |
| 72 | + password=self.__password, |
| 73 | + admin=user_is_admin, |
| 74 | + encrypted_dek=self.secrets.encrypted_dek, |
| 75 | + iv_for_dek=self.secrets.iv_for_dek, |
| 76 | + salt_for_dek=self.secrets.salt_for_dek, |
| 77 | + iv_for_kek=self.secrets.iv_for_kek, |
| 78 | + salt_for_kek=self.secrets.salt_for_kek, |
| 79 | + salt_for_password=self.secrets.salt_for_password, |
| 80 | + ) |
| 81 | + |
| 82 | + def login(self, username: str, password: str) -> Tuple[Union[bool, str]]: |
| 83 | + """ |
| 84 | + Handles user login. |
| 85 | +
|
| 86 | + :param username: The username of the user |
| 87 | + :param password: The password of the user |
| 88 | + :param table_name: The table in which the data is to be inserted |
| 89 | + :return: access token upon successful login |
| 90 | + """ |
| 91 | + |
| 92 | + if self.user.exists(username): |
| 93 | + if self.user.password_match( |
| 94 | + username=username, password=hash_data(password) |
| 95 | + ): |
| 96 | + access_token: str = uuid.uuid4() |
| 97 | + session: SessionModel = SessionModel() |
| 98 | + if not session.exists(username=username): |
| 99 | + session.create(usernmae=username, access_token=access_token) |
| 100 | + else: |
| 101 | + session.activate_session(username=username) |
| 102 | + |
| 103 | + return (True, access_token) |
| 104 | + else: |
| 105 | + return (False, "Password does not match") |
| 106 | + |
| 107 | + return (False, "User does not exists") |
| 108 | + |
| 109 | + def is_admin(self) -> bool: |
| 110 | + return self.user.get_user(username=self.username).admin |
| 111 | + |
| 112 | + @property |
| 113 | + def password(self) -> str: |
| 114 | + """ |
| 115 | + Returns the password of the user |
| 116 | + """ |
| 117 | + |
| 118 | + return self.__password |
| 119 | + |
| 120 | + @property |
| 121 | + def access_token(self) -> str: |
| 122 | + """ |
| 123 | + Returns the access token of the user |
| 124 | + """ |
| 125 | + |
| 126 | + return self.__access_token |
0 commit comments