forked from mtusi2026/mtusi2026lab3
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:
co-authored by
Claude Opus 5.5
parent
0ec2f9dae3
commit
416ce4f9db
@@ -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
|
||||
Reference in New Issue
Block a user