Перенести реализацию мессенджера из ЛР 6

Файлы скопированы без изменений из solution/BPI2301-Vozzhin
репозитория mtusi2026lab6. Доработки ЛР 7 идут отдельными коммитами.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-12-03 19:48:41 +05:00
co-authored by Claude Opus 5.5
parent 20afc2c790
commit 31d441184b
92 changed files with 6153 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
from __future__ import annotations
from datetime import datetime, timezone
from uuid import UUID, uuid4
from ..auth.passwords import hash_password, password_matches
from ..domain.errors import Unauthorized
from ..domain.models import User
from ..repositories import UserRepository
class UserService:
def __init__(self, repository: UserRepository) -> None:
self._repository = repository
async def register(self, username: str, display_name: str, password: str) -> User:
return await self._repository.register(
user_id=uuid4(),
username=username,
display_name=display_name,
password_hash=hash_password(password),
created_at=datetime.now(timezone.utc),
)
async def identify(self, user_id: UUID) -> User:
user = await self._repository.get_by_id(user_id)
if user is None:
raise Unauthorized('требуется действующая сессия')
return user
async def authenticate(self, username: str, password: str) -> User | None:
row = await self._repository.get_credentials(username)
if row is None or not password_matches(row.password_hash, password):
return None
return self._repository.to_domain(row)