forked from mtusi2026/mtusi2026lab4
Bring the application, own tests, Dockerfile, toolchain, compose stack, HAProxy and k6 configuration, migrations, hooks, evidence and level 4 scripts and the CI job from the lab 3 solution commit 61c0c28, merge the lab 3 Makefile targets into the lab 4 skeleton Makefile without touching its targets, and append the lab 3 solution variables to the skeleton .env.example. The package and the application follow the contract version 1.4.0, and the image and the throwaway test containers are tagged lab4 so they do not collide with a lab 3 stand on the same host. This is the starting point of the lab 4 cycle: lint, own tests and the offline skeleton check pass before any lab 4 change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
113 lines
4.1 KiB
Python
113 lines
4.1 KiB
Python
from collections.abc import AsyncIterator
|
|
from datetime import timedelta
|
|
|
|
from dishka import Provider, Scope, provide
|
|
from redis.asyncio import Redis
|
|
from redis.asyncio.retry import Retry
|
|
from redis.backoff import NoBackoff
|
|
|
|
from messenger.identity.application.bus import IdentityCommandBus
|
|
from messenger.identity.application.commands import CloseSession, OpenSession, RegisterUser
|
|
from messenger.identity.application.handlers import (
|
|
CloseSessionHandler,
|
|
OpenSessionHandler,
|
|
RegisterUserHandler,
|
|
)
|
|
from messenger.identity.application.ports import (
|
|
IdentityUnitOfWork,
|
|
PasswordHasher,
|
|
SessionDirectory,
|
|
UserDirectory,
|
|
)
|
|
from messenger.identity.application.queries import Authenticate, GetCurrentUser, VerifyCredentials
|
|
from messenger.identity.config import IdentitySettings
|
|
from messenger.identity.infrastructure.argon2 import Argon2PasswordHasher
|
|
from messenger.identity.infrastructure.postgres import (
|
|
PostgresIdentityUnitOfWork,
|
|
PostgresSessionDirectory,
|
|
PostgresUserDirectory,
|
|
)
|
|
from messenger.identity.infrastructure.valkey import CachedSessionDirectory
|
|
from messenger.shared.application.commands import Command
|
|
from messenger.shared.application.messagebus import CommandHandler
|
|
from messenger.shared.application.ports import Clock, EventPublisher, IdGenerator, Logger
|
|
from messenger.shared.infrastructure.postgres import Database
|
|
|
|
|
|
class IdentityProvider(Provider):
|
|
def __init__(self, settings: IdentitySettings) -> None:
|
|
super().__init__()
|
|
self._settings = settings
|
|
|
|
@provide(scope=Scope.APP)
|
|
def settings(self) -> IdentitySettings:
|
|
return self._settings
|
|
|
|
@provide(scope=Scope.APP)
|
|
def hasher(self, settings: IdentitySettings) -> PasswordHasher:
|
|
return Argon2PasswordHasher(settings.password_hash_concurrency)
|
|
|
|
@provide(scope=Scope.APP)
|
|
def users(self, database: Database) -> UserDirectory:
|
|
return PostgresUserDirectory(database)
|
|
|
|
@provide(scope=Scope.APP)
|
|
async def sessions(
|
|
self, database: Database, settings: IdentitySettings, logger: Logger
|
|
) -> AsyncIterator[SessionDirectory]:
|
|
origin = PostgresSessionDirectory(database)
|
|
if settings.cache_url is None:
|
|
yield origin
|
|
return
|
|
client = Redis.from_url(
|
|
settings.cache_url.get_secret_value(),
|
|
socket_connect_timeout=settings.cache_timeout_seconds,
|
|
socket_timeout=settings.cache_timeout_seconds,
|
|
retry=Retry(NoBackoff(), 1),
|
|
)
|
|
try:
|
|
yield CachedSessionDirectory(
|
|
origin,
|
|
client,
|
|
logger,
|
|
limit=timedelta(seconds=settings.session_cache_ttl_seconds),
|
|
cooldown=settings.cache_cooldown_seconds,
|
|
)
|
|
finally:
|
|
await client.aclose()
|
|
|
|
@provide(scope=Scope.APP)
|
|
def verify(self, users: UserDirectory, hasher: PasswordHasher) -> VerifyCredentials:
|
|
return VerifyCredentials(users, hasher)
|
|
|
|
@provide(scope=Scope.APP)
|
|
def authenticate(self, sessions: SessionDirectory, clock: Clock) -> Authenticate:
|
|
return Authenticate(sessions, clock)
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def unit_of_work(self, database: Database) -> IdentityUnitOfWork:
|
|
return PostgresIdentityUnitOfWork(database)
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def current_user(self, users: UserDirectory) -> GetCurrentUser:
|
|
return GetCurrentUser(users)
|
|
|
|
@provide(scope=Scope.REQUEST)
|
|
def command_bus(
|
|
self,
|
|
uow: IdentityUnitOfWork,
|
|
publisher: EventPublisher,
|
|
clock: Clock,
|
|
ids: IdGenerator,
|
|
hasher: PasswordHasher,
|
|
sessions: SessionDirectory,
|
|
settings: IdentitySettings,
|
|
) -> IdentityCommandBus:
|
|
ttl = timedelta(seconds=settings.session_ttl_seconds)
|
|
handlers: dict[type[Command], CommandHandler] = {
|
|
RegisterUser: RegisterUserHandler(uow, clock, ids, hasher),
|
|
OpenSession: OpenSessionHandler(uow, clock, ttl),
|
|
CloseSession: CloseSessionHandler(uow, sessions),
|
|
}
|
|
return IdentityCommandBus(handlers, uow, publisher)
|