feat: add password login and bearer sessions

- Argon2id password hashes, KDF in a thread with its own limit
- POST /auth/sessions with Basic, DELETE /auth/sessions/current
- Bearer sessions in PostgreSQL, X-User-Id ignored
- migration drops lab 1 data without passwords
- student-tests on tokens, sessions spec and synced specs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 18:50:42 +03:00
co-authored by Claude Opus 5.5
parent ee2643d21e
commit ae4154eaf5
68 changed files with 2946 additions and 314 deletions
+18 -1
View File
@@ -23,6 +23,10 @@ UUID_RE = re.compile(
# RFC 3339 в UTC: шесть знаков долей секунды и суффикс Z
CREATED_AT_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$")
ID_KEYS = frozenset({"id", "chat_id", "sender_id"})
TIME_KEYS = frozenset({"created_at", "expires_at"})
# WWW-Authenticate у 401 (contracts/README.md): вход и остальные запросы
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
BEARER_CHALLENGE = 'Bearer realm="messenger"'
# признаки внутренностей сервиса в error.message
LEAK_RE = re.compile(r"Traceback|File \"|\.py\b|/app/|/src/|Error\(")
@@ -82,7 +86,7 @@ def check_formats(value: Any, path: str = "") -> None:
elif key == "member_ids":
for number, member in enumerate(item):
assert_uuid(member, f"{where}/{number}")
elif key == "created_at":
elif key in TIME_KEYS:
assert_created_at(item, where)
else:
check_formats(item, where)
@@ -135,6 +139,19 @@ def assert_error(resp: httpx.Response, status: int) -> dict[str, Any]:
return error
def assert_unauthorized(
resp: httpx.Response,
challenge: str,
) -> dict[str, Any]:
"""401 в формате контракта с заданным `WWW-Authenticate`."""
error = assert_error(resp, 401)
assert resp.headers.get("WWW-Authenticate") == challenge, describe(
resp,
f"expected WWW-Authenticate {challenge}",
)
return error
def json_body(resp: httpx.Response) -> Any:
"""JSON-тело в UTF-8 и непустой `X-Request-Id`."""
request_id = resp.headers.get("X-Request-Id", "")