Carry over the lab 2 solution

Bring the application, own tests, Dockerfile, toolchain, compose
stack, migrations, secret generator, restart hook, evidence scripts and
CI job from the lab 2 solution commit 8cddb09 unchanged, merge the lab
2 Makefile targets into the lab 3 skeleton Makefile without touching
its targets, and append the lab 2 solution variables to the skeleton
.env.example. The package version follows the contract version 1.3.0.

This is the starting point of the lab 3 cycle: lint, own tests and the
offline skeleton check pass before any lab 3 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-28 11:26:15 +03:00
co-authored by Claude Opus 5.5
parent 0ec2f9dae3
commit 416ce4f9db
111 changed files with 7066 additions and 1 deletions
@@ -0,0 +1,5 @@
from messenger.shared.application.messagebus import CommandBus
class IdentityCommandBus(CommandBus):
__slots__ = ()
@@ -0,0 +1,22 @@
from dataclasses import dataclass, field
from uuid import UUID
from messenger.identity.domain.models import Session
from messenger.shared.application.commands import Command
@dataclass(frozen=True, slots=True)
class RegisterUser(Command):
username: str
display_name: str
password: str = field(repr=False)
@dataclass(frozen=True, slots=True)
class OpenSession(Command):
user_id: UUID
@dataclass(frozen=True, slots=True)
class CloseSession(Command):
session: Session
@@ -0,0 +1,76 @@
from datetime import timedelta
from messenger.identity.application.commands import CloseSession, OpenSession, RegisterUser
from messenger.identity.application.ports import (
IdentityUnitOfWork,
PasswordHasher,
SessionDirectory,
)
from messenger.identity.domain.events import SessionClosed, SessionOpened
from messenger.identity.domain.models import Session, User
from messenger.identity.domain.values import DisplayName, Password, Username
from messenger.shared.application.ports import Clock, IdGenerator
class RegisterUserHandler:
__slots__ = ("_clock", "_hasher", "_ids", "_uow")
def __init__(
self, uow: IdentityUnitOfWork, clock: Clock, ids: IdGenerator, hasher: PasswordHasher
) -> None:
self._uow = uow
self._clock = clock
self._ids = ids
self._hasher = hasher
async def __call__(self, command: RegisterUser) -> User:
username = Username(command.username)
display_name = DisplayName(command.display_name)
password = Password(command.password)
password_hash = await self._hasher.hash(password.value)
user, event = User.register(
user_id=self._ids.new_id(),
username=username,
display_name=display_name,
password_hash=password_hash,
registered_at=self._clock.now(),
)
async with self._uow.transaction():
await self._uow.users.add(user)
self._uow.record(event)
await self._uow.commit()
return user
class OpenSessionHandler:
__slots__ = ("_clock", "_ttl", "_uow")
def __init__(self, uow: IdentityUnitOfWork, clock: Clock, ttl: timedelta) -> None:
self._uow = uow
self._clock = clock
self._ttl = ttl
async def __call__(self, command: OpenSession) -> tuple[str, Session]:
token, session = Session.issue(
user_id=command.user_id, issued_at=self._clock.now(), ttl=self._ttl
)
async with self._uow.transaction():
await self._uow.sessions.add(session)
self._uow.record(SessionOpened(user_id=session.user_id, expires_at=session.expires_at))
await self._uow.commit()
return token, session
class CloseSessionHandler:
__slots__ = ("_sessions", "_uow")
def __init__(self, uow: IdentityUnitOfWork, sessions: SessionDirectory) -> None:
self._uow = uow
self._sessions = sessions
async def __call__(self, command: CloseSession) -> None:
await self._sessions.invalidate(command.session)
async with self._uow.transaction():
await self._uow.sessions.remove(command.session.token_hash)
self._uow.record(SessionClosed(user_id=command.session.user_id))
await self._uow.commit()
@@ -0,0 +1,48 @@
from datetime import datetime
from typing import Protocol, runtime_checkable
from uuid import UUID
from messenger.identity.domain.models import Session, User
from messenger.shared.application.ports import UnitOfWork
@runtime_checkable
class UserRepository(Protocol):
async def add(self, user: User) -> None: ...
@runtime_checkable
class SessionRepository(Protocol):
async def add(self, session: Session) -> None: ...
async def remove(self, token_hash: bytes) -> None: ...
@runtime_checkable
class IdentityUnitOfWork(UnitOfWork, Protocol):
@property
def users(self) -> UserRepository: ...
@property
def sessions(self) -> SessionRepository: ...
@runtime_checkable
class UserDirectory(Protocol):
async def find(self, user_id: UUID) -> User | None: ...
async def find_by_username(self, username: str) -> User | None: ...
@runtime_checkable
class SessionDirectory(Protocol):
async def find(self, token_hash: bytes, now: datetime) -> Session | None: ...
async def invalidate(self, session: Session) -> None: ...
@runtime_checkable
class PasswordHasher(Protocol):
async def hash(self, password: str) -> str: ...
async def verify(self, password_hash: str | None, password: str) -> bool: ...
@@ -0,0 +1,43 @@
from uuid import UUID
from messenger.identity.application.ports import PasswordHasher, SessionDirectory, UserDirectory
from messenger.identity.domain.errors import ActorNotIdentified
from messenger.identity.domain.models import Session, User, token_digest
from messenger.shared.application.ports import Clock
class GetCurrentUser:
__slots__ = ("_directory",)
def __init__(self, directory: UserDirectory) -> None:
self._directory = directory
async def __call__(self, user_id: UUID) -> User:
user = await self._directory.find(user_id)
if user is None:
raise ActorNotIdentified("идентификация не подтверждена")
return user
class VerifyCredentials:
__slots__ = ("_directory", "_hasher")
def __init__(self, directory: UserDirectory, hasher: PasswordHasher) -> None:
self._directory = directory
self._hasher = hasher
async def __call__(self, username: str, password: str) -> User | None:
user = await self._directory.find_by_username(username)
valid = await self._hasher.verify(user.password_hash if user else None, password)
return user if valid else None
class Authenticate:
__slots__ = ("_clock", "_sessions")
def __init__(self, sessions: SessionDirectory, clock: Clock) -> None:
self._sessions = sessions
self._clock = clock
async def __call__(self, token: str) -> Session | None:
return await self._sessions.find(token_digest(token), self._clock.now())