forked from mtusi2026/mtusi2026lab2
Carry over the lab 3 solution
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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b0f463ac5c
commit
bf8e4e3cd1
@@ -0,0 +1,17 @@
|
||||
from messenger.shared.domain.errors import DomainError
|
||||
|
||||
|
||||
class IdentityError(DomainError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidUserData(IdentityError):
|
||||
pass
|
||||
|
||||
|
||||
class UsernameAlreadyTaken(IdentityError):
|
||||
pass
|
||||
|
||||
|
||||
class ActorNotIdentified(IdentityError):
|
||||
pass
|
||||
@@ -0,0 +1,25 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class UserRegistered(DomainEvent):
|
||||
user_id: UUID
|
||||
registered_at: datetime
|
||||
name: str = field(init=False, default="user.registered")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SessionOpened(DomainEvent):
|
||||
user_id: UUID
|
||||
expires_at: datetime
|
||||
name: str = field(init=False, default="session.opened")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SessionClosed(DomainEvent):
|
||||
user_id: UUID
|
||||
name: str = field(init=False, default="session.closed")
|
||||
@@ -0,0 +1,62 @@
|
||||
import secrets
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timedelta
|
||||
from hashlib import sha256
|
||||
from uuid import UUID
|
||||
|
||||
from messenger.identity.domain.events import UserRegistered
|
||||
from messenger.identity.domain.values import DisplayName, Username
|
||||
from messenger.shared.domain.events import DomainEvent
|
||||
|
||||
TOKEN_BYTES = 32
|
||||
|
||||
|
||||
def token_digest(token: str) -> bytes:
|
||||
return sha256(token.encode()).digest()
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class User:
|
||||
id: UUID
|
||||
username: Username
|
||||
display_name: DisplayName
|
||||
created_at: datetime
|
||||
password_hash: str = field(repr=False)
|
||||
|
||||
@classmethod
|
||||
def register(
|
||||
cls,
|
||||
*,
|
||||
user_id: UUID,
|
||||
username: Username,
|
||||
display_name: DisplayName,
|
||||
password_hash: str,
|
||||
registered_at: datetime,
|
||||
) -> tuple[User, DomainEvent]:
|
||||
user = cls(
|
||||
id=user_id,
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
created_at=registered_at,
|
||||
password_hash=password_hash,
|
||||
)
|
||||
return user, UserRegistered(user_id=user.id, registered_at=registered_at)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Session:
|
||||
token_hash: bytes = field(repr=False)
|
||||
user_id: UUID
|
||||
created_at: datetime
|
||||
expires_at: datetime
|
||||
|
||||
@classmethod
|
||||
def issue(cls, *, user_id: UUID, issued_at: datetime, ttl: timedelta) -> tuple[str, Session]:
|
||||
token = secrets.token_urlsafe(TOKEN_BYTES)
|
||||
session = cls(
|
||||
token_hash=token_digest(token),
|
||||
user_id=user_id,
|
||||
created_at=issued_at,
|
||||
expires_at=issued_at + ttl,
|
||||
)
|
||||
return token, session
|
||||
@@ -0,0 +1,54 @@
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from messenger.identity.domain.errors import InvalidUserData
|
||||
|
||||
USERNAME_PATTERN = re.compile(r"\A[a-z0-9_]{3,32}\Z")
|
||||
DISPLAY_NAME_MAX = 100
|
||||
PASSWORD_MIN = 12
|
||||
PASSWORD_MAX = 128
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Username:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidUserData("поле username должно быть строкой")
|
||||
if USERNAME_PATTERN.match(self.value) is None:
|
||||
raise InvalidUserData("поле username должно соответствовать [a-z0-9_]{3,32}")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class DisplayName:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidUserData("поле display_name должно быть строкой")
|
||||
if not self.value.strip():
|
||||
raise InvalidUserData("поле display_name не может быть пустым или состоять из пробелов")
|
||||
if len(self.value) > DISPLAY_NAME_MAX:
|
||||
raise InvalidUserData(f"поле display_name длиннее {DISPLAY_NAME_MAX} символов Unicode")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Password:
|
||||
value: str = field(repr=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.value, str):
|
||||
raise InvalidUserData("поле password должно быть строкой")
|
||||
if not self.value.strip():
|
||||
raise InvalidUserData("поле password не может состоять из пробелов")
|
||||
if not PASSWORD_MIN <= len(self.value) <= PASSWORD_MAX:
|
||||
raise InvalidUserData(
|
||||
f"поле password: от {PASSWORD_MIN} до {PASSWORD_MAX} символов Unicode"
|
||||
)
|
||||
Reference in New Issue
Block a user