auth next part
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
from .auth_request import UserCreateRequest
|
||||
from .auth_request import LoginRequest, UserCreateRequest
|
||||
|
||||
__all__ = ("UserCreateRequest",)
|
||||
__all__ = ("UserCreateRequest", "LoginRequest")
|
||||
|
@@ -6,3 +6,9 @@ class UserCreateRequest:
|
||||
name: str
|
||||
email: str
|
||||
password: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LoginRequest:
|
||||
email: str
|
||||
password: str
|
||||
|
9
api/application/contracts/auth/auth_response.py
Normal file
9
api/application/contracts/auth/auth_response.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AuthenticationResponse:
|
||||
id: UUID
|
||||
name: str
|
||||
email: str
|
7
api/application/protocols/date_time.py
Normal file
7
api/application/protocols/date_time.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from datetime import datetime
|
||||
from typing import Protocol
|
||||
|
||||
|
||||
class DateTimeProvider(Protocol):
|
||||
def get_current_time(self) -> datetime:
|
||||
raise NotImplementedError
|
11
api/application/protocols/jwt.py
Normal file
11
api/application/protocols/jwt.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from typing import Protocol
|
||||
|
||||
from api.domain.user.model import UserId
|
||||
|
||||
|
||||
class JwtTokenProcessor(Protocol):
|
||||
def generate_token(self, user_id: UserId) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
def validate_token(self, token: str) -> UserId | None:
|
||||
raise NotImplementedError
|
30
api/application/usecase/auth/auth_user.py
Normal file
30
api/application/usecase/auth/auth_user.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from api.application.contracts.auth.auth_request import LoginRequest
|
||||
from api.application.contracts.auth.auth_response import AuthenticationResponse
|
||||
from api.application.protocols.password_hasher import PasswordHasher
|
||||
from api.domain.user.error import UserInvalidCredentialsError
|
||||
from api.domain.user.repository import UserRepository
|
||||
|
||||
|
||||
class LoginUser:
|
||||
def __init__(
|
||||
self,
|
||||
user_repository: UserRepository,
|
||||
password_hasher: PasswordHasher,
|
||||
) -> None:
|
||||
self.user_repository = user_repository
|
||||
self.hasher = password_hasher
|
||||
|
||||
async def __call__(self, request: LoginRequest) -> AuthenticationResponse:
|
||||
user = await self.user_repository.get_user(filter={"email": request.email})
|
||||
error = UserInvalidCredentialsError("Email or password is incorrect")
|
||||
if user is None:
|
||||
raise error
|
||||
|
||||
if not self.hasher.verify_password(request.password, user.hashed_password):
|
||||
raise error
|
||||
|
||||
return AuthenticationResponse(
|
||||
id=user.id.value,
|
||||
name=user.name.value,
|
||||
email=user.email.value,
|
||||
)
|
Reference in New Issue
Block a user