mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-25 13:31:27 +00:00
refactor: mv hash_password to repository
This commit is contained in:
parent
34b603b76c
commit
2dc34d6d0e
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue