From 2dc34d6d0e67c106789a0981e682f5fcde7513b6 Mon Sep 17 00:00:00 2001 From: dettlaff Date: Fri, 15 Nov 2024 18:30:51 +0400 Subject: [PATCH] refactor: mv hash_password to repository --- selfprivacy_api/actions/users.py | 16 +++------------- .../users/abstract_user_repository.py | 5 ++--- .../repositories/users/json_user_repository.py | 17 +++++++++++++++-- .../users/kanidm_user_repository.py | 4 ++-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/selfprivacy_api/actions/users.py b/selfprivacy_api/actions/users.py index 53e3b0a..404e239 100644 --- a/selfprivacy_api/actions/users.py +++ b/selfprivacy_api/actions/users.py @@ -5,11 +5,10 @@ from typing import Optional from selfprivacy_api.models.user import UserDataUser -from selfprivacy_api.utils import hash_password, is_username_forbidden +from selfprivacy_api.utils import is_username_forbidden from selfprivacy_api.repositories.users import ACTIVE_USERS_PROVIDER from selfprivacy_api.repositories.users.exceptions import ( - PasswordIsEmpty, UsernameForbidden, UsernameNotAlphanumeric, UsernameTooLong, @@ -45,14 +44,9 @@ def create_user( if len(username) >= 32: raise UsernameTooLong("Username must be less than 32 characters") - if password == "": - raise PasswordIsEmpty("Password is empty") - - hashed_password = hash_password(password) if password else None - return ACTIVE_USERS_PROVIDER.create_user( username=username, - hashed_password=hashed_password, + password=password, displayname=displayname, email=email, directmemberof=directmemberof, @@ -72,14 +66,10 @@ def update_user( directmemberof: Optional[list[str]] = None, memberof: Optional[list[str]] = None, ) -> None: - if password == "": - raise PasswordIsEmpty("Password is empty") - - hashed_password = hash_password(password) if password else None return ACTIVE_USERS_PROVIDER.update_user( username=username, - hashed_password=hashed_password, + password=password, displayname=displayname, email=email, directmemberof=directmemberof, diff --git a/selfprivacy_api/repositories/users/abstract_user_repository.py b/selfprivacy_api/repositories/users/abstract_user_repository.py index 2941286..7c458b2 100644 --- a/selfprivacy_api/repositories/users/abstract_user_repository.py +++ b/selfprivacy_api/repositories/users/abstract_user_repository.py @@ -5,7 +5,6 @@ from selfprivacy_api.models.user import UserDataUser class AbstractUserRepository(ABC): - @staticmethod @abstractmethod def get_users( @@ -16,7 +15,7 @@ class AbstractUserRepository(ABC): @staticmethod @abstractmethod - def create_user(username: str, hashed_password: str) -> None: + def create_user(username: str, password: str) -> None: """Creates a new user""" @staticmethod @@ -26,7 +25,7 @@ class AbstractUserRepository(ABC): @staticmethod @abstractmethod - def update_user(username: str, hashed_password: str) -> None: + def update_user(username: str, password: str) -> None: """Updates the password of an existing user""" @staticmethod diff --git a/selfprivacy_api/repositories/users/json_user_repository.py b/selfprivacy_api/repositories/users/json_user_repository.py index 4208844..1c999de 100644 --- a/selfprivacy_api/repositories/users/json_user_repository.py +++ b/selfprivacy_api/repositories/users/json_user_repository.py @@ -6,6 +6,7 @@ from selfprivacy_api.utils import ( ReadUserData, WriteUserData, ensure_ssh_and_users_fields_exist, + hash_password, ) from selfprivacy_api.repositories.users.abstract_user_repository import ( AbstractUserRepository, @@ -15,10 +16,18 @@ from selfprivacy_api.repositories.users.exceptions import ( UserAlreadyExists, UserIsProtected, UserNotFound, + PasswordIsEmpty, ) class JsonUserRepository(AbstractUserRepository): + @staticmethod + def _check_and_hash_password(password: str): + if password == "": + raise PasswordIsEmpty("Password is empty") + + return hash_password(password) + @staticmethod def get_users( exclude_primary: bool = False, @@ -55,7 +64,9 @@ class JsonUserRepository(AbstractUserRepository): return users @staticmethod - def create_user(username: str, hashed_password: str) -> None: + def create_user(username: str, password: str) -> None: + hashed_password = JsonUserRepository._check_and_hash_password(password) + with ReadUserData() as user_data: ensure_ssh_and_users_fields_exist(user_data) if "username" not in user_data.keys(): @@ -89,7 +100,9 @@ class JsonUserRepository(AbstractUserRepository): raise UserNotFound("User did not exist") @staticmethod - def update_user(username: str, hashed_password: str) -> None: + def update_user(username: str, password: str) -> None: + hashed_password = JsonUserRepository._check_and_hash_password(password) + with WriteUserData() as data: ensure_ssh_and_users_fields_exist(data) diff --git a/selfprivacy_api/repositories/users/kanidm_user_repository.py b/selfprivacy_api/repositories/users/kanidm_user_repository.py index cbec5f5..eae15d1 100644 --- a/selfprivacy_api/repositories/users/kanidm_user_repository.py +++ b/selfprivacy_api/repositories/users/kanidm_user_repository.py @@ -46,7 +46,7 @@ class KanidmUserRepository(AbstractUserRepository): @staticmethod def create_user( username: str, - hashed_password: Optional[str] = None, # TODO legacy? + password: Optional[str] = None, # TODO legacy? displayname: Optional[str] = None, email: Optional[str] = None, directmemberof: Optional[list[str]] = None, @@ -101,7 +101,7 @@ class KanidmUserRepository(AbstractUserRepository): def update_user( username: str, - hashed_password: Optional[str] = None, # TODO legacy? + password: Optional[str] = None, # TODO legacy? displayname: Optional[str] = None, email: Optional[str] = None, directmemberof: Optional[list[str]] = None,