Init project structure
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Business logic services."""
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Authentication workflows."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.security import JWTService, PasswordHasher
|
||||
from app.models.token import TokenResponse
|
||||
from app.models.user import User
|
||||
from app.repositories.user_repo import UserRepository
|
||||
|
||||
|
||||
class InvalidCredentialsError(Exception):
|
||||
"""Raised when user authentication fails."""
|
||||
|
||||
|
||||
class AuthService:
|
||||
"""Handles authentication flows and token issuance."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
user_repository: UserRepository,
|
||||
password_hasher: PasswordHasher,
|
||||
jwt_service: JWTService,
|
||||
) -> None:
|
||||
self._user_repository = user_repository
|
||||
self._password_hasher = password_hasher
|
||||
self._jwt_service = jwt_service
|
||||
|
||||
async def authenticate(self, email: str, password: str) -> User:
|
||||
user = await self._user_repository.get_by_email(email)
|
||||
if user is None or not self._password_hasher.verify(password, user.hashed_password):
|
||||
raise InvalidCredentialsError("Invalid email or password")
|
||||
return user
|
||||
|
||||
def create_access_token(self, user: User) -> TokenResponse:
|
||||
expires_delta = timedelta(minutes=settings.access_token_expire_minutes)
|
||||
token = self._jwt_service.create_access_token(
|
||||
subject=str(user.id),
|
||||
expires_delta=expires_delta,
|
||||
claims={"email": user.email},
|
||||
)
|
||||
return TokenResponse(access_token=token, expires_in=int(expires_delta.total_seconds()))
|
||||
@@ -0,0 +1,48 @@
|
||||
"""User-related business logic."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
from app.core.security import PasswordHasher
|
||||
from app.models.user import User, UserCreate
|
||||
from app.repositories.user_repo import UserRepository
|
||||
|
||||
|
||||
class UserServiceError(Exception):
|
||||
"""Base class for user service errors."""
|
||||
|
||||
|
||||
class UserAlreadyExistsError(UserServiceError):
|
||||
"""Raised when attempting to create a user with duplicate email."""
|
||||
|
||||
|
||||
class UserNotFoundError(UserServiceError):
|
||||
"""Raised when user record cannot be located."""
|
||||
|
||||
|
||||
class UserService:
|
||||
"""Encapsulates user-related workflows."""
|
||||
|
||||
def __init__(self, user_repository: UserRepository, password_hasher: PasswordHasher) -> None:
|
||||
self._repository = user_repository
|
||||
self._password_hasher = password_hasher
|
||||
|
||||
async def list_users(self) -> Sequence[User]:
|
||||
return await self._repository.list()
|
||||
|
||||
async def get_user(self, user_id: int) -> User:
|
||||
user = await self._repository.get_by_id(user_id)
|
||||
if user is None:
|
||||
raise UserNotFoundError(f"User {user_id} not found")
|
||||
return user
|
||||
|
||||
async def create_user(self, data: UserCreate) -> User:
|
||||
existing = await self._repository.get_by_email(data.email)
|
||||
if existing is not None:
|
||||
raise UserAlreadyExistsError(f"User {data.email} already exists")
|
||||
|
||||
hashed_password = self._password_hasher.hash(data.password)
|
||||
user = await self._repository.create(data=data, hashed_password=hashed_password)
|
||||
await self._repository.session.commit()
|
||||
await self._repository.session.refresh(user)
|
||||
return user
|
||||
Reference in New Issue
Block a user