forked from mtusi2026/mtusi2026lab3
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>
77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
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()
|