diff --git a/app/api/v1/users/auth/token.py b/app/api/v1/users/auth/token.py index bad350b..804ac03 100644 --- a/app/api/v1/users/auth/token.py +++ b/app/api/v1/users/auth/token.py @@ -12,7 +12,7 @@ async def token(data: UserCreate, logic: deps.Logic): """ Retrieve new access token """ - return await logic.users.generate_token(**data.model_dump()) + return await logic.users.auth.generate_token(**data.model_dump()) __all__ = ["router"] diff --git a/app/logic/users/__init__.py b/app/logic/users/__init__.py new file mode 100644 index 0000000..b591560 --- /dev/null +++ b/app/logic/users/__init__.py @@ -0,0 +1,3 @@ +from .users import Users + +__all__ = ["Users"] diff --git a/app/logic/users/auth/__init__.py b/app/logic/users/auth/__init__.py new file mode 100644 index 0000000..188cd9d --- /dev/null +++ b/app/logic/users/auth/__init__.py @@ -0,0 +1,3 @@ +from .auth import Auth + +__all__ = ["Auth"] diff --git a/app/logic/users/auth/auth.py b/app/logic/users/auth/auth.py new file mode 100644 index 0000000..ac46a02 --- /dev/null +++ b/app/logic/users/auth/auth.py @@ -0,0 +1,21 @@ +from typing import TYPE_CHECKING + +from app.core import exps +from app.models.token import AccessToken + + +if TYPE_CHECKING: + from app.logic import Logic + + +class Auth: + def __init__(self, logic: "Logic"): + self.logic = logic + + async def generate_token(self, email: str, password: str) -> AccessToken | None: + if user := await self.logic.db.user.retrieve_by_email(email): + if not self.logic.security.pwd.checkpwd(password, user.password): + raise exps.UserIsCorrectException() + access_token = self.logic.security.jwt.encode_token({"id": user.id}, 1440) + return AccessToken(token=access_token) + raise exps.UserNotFoundException() diff --git a/app/logic/users.py b/app/logic/users/users.py similarity index 65% rename from app/logic/users.py rename to app/logic/users/users.py index 24a3e9c..476ecb9 100644 --- a/app/logic/users.py +++ b/app/logic/users/users.py @@ -1,9 +1,9 @@ from typing import TYPE_CHECKING from app.core import exps -from app.models.token import AccessToken from app.models.user import User +from .auth import Auth if TYPE_CHECKING: from app.logic import Logic @@ -12,6 +12,7 @@ class Users: def __init__(self, logic: "Logic"): self.logic = logic + self.auth = Auth(self.logic) async def create(self, email: str, password: str) -> User | None: if await self.logic.db.user.retrieve_by_email(email): @@ -22,14 +23,6 @@ async def create(self, email: str, password: str) -> User | None: user = await self.logic.db.user.create(model) return user - async def generate_token(self, email: str, password: str) -> AccessToken | None: - if user := await self.logic.db.user.retrieve_by_email(email): - if not self.logic.security.pwd.checkpwd(password, user.password): - raise exps.UserIsCorrectException() - access_token = self.logic.security.jwt.encode_token({"id": user.id}, 1440) - return AccessToken(token=access_token) - raise exps.UserNotFoundException() - async def retrieve_by_token(self, token: str) -> User | None: if payload := self.logic.security.jwt.decode_token(token): if not (