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.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 import ACTIVE_USERS_PROVIDER
|
||||||
from selfprivacy_api.repositories.users.exceptions import (
|
from selfprivacy_api.repositories.users.exceptions import (
|
||||||
PasswordIsEmpty,
|
|
||||||
UsernameForbidden,
|
UsernameForbidden,
|
||||||
UsernameNotAlphanumeric,
|
UsernameNotAlphanumeric,
|
||||||
UsernameTooLong,
|
UsernameTooLong,
|
||||||
|
@ -45,14 +44,9 @@ def create_user(
|
||||||
if len(username) >= 32:
|
if len(username) >= 32:
|
||||||
raise UsernameTooLong("Username must be less than 32 characters")
|
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(
|
return ACTIVE_USERS_PROVIDER.create_user(
|
||||||
username=username,
|
username=username,
|
||||||
hashed_password=hashed_password,
|
password=password,
|
||||||
displayname=displayname,
|
displayname=displayname,
|
||||||
email=email,
|
email=email,
|
||||||
directmemberof=directmemberof,
|
directmemberof=directmemberof,
|
||||||
|
@ -72,14 +66,10 @@ def update_user(
|
||||||
directmemberof: Optional[list[str]] = None,
|
directmemberof: Optional[list[str]] = None,
|
||||||
memberof: Optional[list[str]] = None,
|
memberof: Optional[list[str]] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if password == "":
|
|
||||||
raise PasswordIsEmpty("Password is empty")
|
|
||||||
|
|
||||||
hashed_password = hash_password(password) if password else None
|
|
||||||
|
|
||||||
return ACTIVE_USERS_PROVIDER.update_user(
|
return ACTIVE_USERS_PROVIDER.update_user(
|
||||||
username=username,
|
username=username,
|
||||||
hashed_password=hashed_password,
|
password=password,
|
||||||
displayname=displayname,
|
displayname=displayname,
|
||||||
email=email,
|
email=email,
|
||||||
directmemberof=directmemberof,
|
directmemberof=directmemberof,
|
||||||
|
|
|
@ -5,7 +5,6 @@ from selfprivacy_api.models.user import UserDataUser
|
||||||
|
|
||||||
|
|
||||||
class AbstractUserRepository(ABC):
|
class AbstractUserRepository(ABC):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_users(
|
def get_users(
|
||||||
|
@ -16,7 +15,7 @@ class AbstractUserRepository(ABC):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def create_user(username: str, hashed_password: str) -> None:
|
def create_user(username: str, password: str) -> None:
|
||||||
"""Creates a new user"""
|
"""Creates a new user"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -26,7 +25,7 @@ class AbstractUserRepository(ABC):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@abstractmethod
|
@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"""
|
"""Updates the password of an existing user"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -6,6 +6,7 @@ from selfprivacy_api.utils import (
|
||||||
ReadUserData,
|
ReadUserData,
|
||||||
WriteUserData,
|
WriteUserData,
|
||||||
ensure_ssh_and_users_fields_exist,
|
ensure_ssh_and_users_fields_exist,
|
||||||
|
hash_password,
|
||||||
)
|
)
|
||||||
from selfprivacy_api.repositories.users.abstract_user_repository import (
|
from selfprivacy_api.repositories.users.abstract_user_repository import (
|
||||||
AbstractUserRepository,
|
AbstractUserRepository,
|
||||||
|
@ -15,10 +16,18 @@ from selfprivacy_api.repositories.users.exceptions import (
|
||||||
UserAlreadyExists,
|
UserAlreadyExists,
|
||||||
UserIsProtected,
|
UserIsProtected,
|
||||||
UserNotFound,
|
UserNotFound,
|
||||||
|
PasswordIsEmpty,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class JsonUserRepository(AbstractUserRepository):
|
class JsonUserRepository(AbstractUserRepository):
|
||||||
|
@staticmethod
|
||||||
|
def _check_and_hash_password(password: str):
|
||||||
|
if password == "":
|
||||||
|
raise PasswordIsEmpty("Password is empty")
|
||||||
|
|
||||||
|
return hash_password(password)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_users(
|
def get_users(
|
||||||
exclude_primary: bool = False,
|
exclude_primary: bool = False,
|
||||||
|
@ -55,7 +64,9 @@ class JsonUserRepository(AbstractUserRepository):
|
||||||
return users
|
return users
|
||||||
|
|
||||||
@staticmethod
|
@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:
|
with ReadUserData() as user_data:
|
||||||
ensure_ssh_and_users_fields_exist(user_data)
|
ensure_ssh_and_users_fields_exist(user_data)
|
||||||
if "username" not in user_data.keys():
|
if "username" not in user_data.keys():
|
||||||
|
@ -89,7 +100,9 @@ class JsonUserRepository(AbstractUserRepository):
|
||||||
raise UserNotFound("User did not exist")
|
raise UserNotFound("User did not exist")
|
||||||
|
|
||||||
@staticmethod
|
@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:
|
with WriteUserData() as data:
|
||||||
ensure_ssh_and_users_fields_exist(data)
|
ensure_ssh_and_users_fields_exist(data)
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ class KanidmUserRepository(AbstractUserRepository):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_user(
|
def create_user(
|
||||||
username: str,
|
username: str,
|
||||||
hashed_password: Optional[str] = None, # TODO legacy?
|
password: Optional[str] = None, # TODO legacy?
|
||||||
displayname: Optional[str] = None,
|
displayname: Optional[str] = None,
|
||||||
email: Optional[str] = None,
|
email: Optional[str] = None,
|
||||||
directmemberof: Optional[list[str]] = None,
|
directmemberof: Optional[list[str]] = None,
|
||||||
|
@ -101,7 +101,7 @@ class KanidmUserRepository(AbstractUserRepository):
|
||||||
|
|
||||||
def update_user(
|
def update_user(
|
||||||
username: str,
|
username: str,
|
||||||
hashed_password: Optional[str] = None, # TODO legacy?
|
password: Optional[str] = None, # TODO legacy?
|
||||||
displayname: Optional[str] = None,
|
displayname: Optional[str] = None,
|
||||||
email: Optional[str] = None,
|
email: Optional[str] = None,
|
||||||
directmemberof: Optional[list[str]] = None,
|
directmemberof: Optional[list[str]] = None,
|
||||||
|
|
Loading…
Reference in a new issue