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
+13 -2
View File
@@ -15,10 +15,11 @@ from starlette.exceptions import HTTPException as StarletteHTTPException
from src.api.schemas import Error, ErrorBody, ErrorCode
from src.core import exceptions
from src.utils.constants import REQUEST_ID_HEADER
from src.utils.constants import AUTH_REALM, REQUEST_ID_HEADER
DOMAIN_ERRORS: dict[type[exceptions.DomainError], tuple[int, ErrorCode]] = {
exceptions.Unidentified: (401, ErrorCode.UNAUTHORIZED),
exceptions.InvalidCredentials: (401, ErrorCode.UNAUTHORIZED),
exceptions.Unauthenticated: (401, ErrorCode.UNAUTHORIZED),
exceptions.UsernameTaken: (409, ErrorCode.CONFLICT),
exceptions.ChatNotFound: (404, ErrorCode.NOT_FOUND),
exceptions.InvalidChatMembers: (400, ErrorCode.INVALID_REQUEST),
@@ -28,6 +29,14 @@ DOMAIN_ERRORS: dict[type[exceptions.DomainError], tuple[int, ErrorCode]] = {
exceptions.DomainError: (400, ErrorCode.INVALID_REQUEST),
}
# вызов в WWW-Authenticate у 401: какой схемой повторить запрос
CHALLENGES: dict[type[exceptions.DomainError], str] = {
exceptions.InvalidCredentials: (
f'Basic realm="{AUTH_REALM}", charset="UTF-8"'
),
exceptions.Unauthenticated: f'Bearer realm="{AUTH_REALM}"',
}
HTTP_ERRORS: dict[int, tuple[ErrorCode, str]] = {
404: (ErrorCode.NOT_FOUND, "Not found"),
405: (ErrorCode.INVALID_REQUEST, "Method not allowed"),
@@ -64,11 +73,13 @@ async def handle_domain_error(
status_code, code = next(
DOMAIN_ERRORS[cls] for cls in type(exc).__mro__ if cls in DOMAIN_ERRORS
)
challenge = CHALLENGES.get(type(exc))
return error_response(
request_id=_request_id(request),
status_code=status_code,
code=code,
message=str(exc),
headers={"WWW-Authenticate": challenge} if challenge else None,
)