forked from mtusi2026/mtusi2026lab7
wip: apply auth-hardening (interrupted by session end)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+14
-1
@@ -19,6 +19,7 @@ from src.core import exceptions
|
||||
from src.utils.constants import (
|
||||
AUTH_REALM,
|
||||
INSTANCE_ID_HEADER,
|
||||
PASSWORD_HASHING_RETRY_AFTER_SECONDS,
|
||||
REQUEST_ID_HEADER,
|
||||
UPLOAD_BACKLOG_RETRY_AFTER_SECONDS,
|
||||
UPLOAD_RETRY_AFTER_SECONDS,
|
||||
@@ -48,6 +49,10 @@ DOMAIN_ERRORS: dict[type[exceptions.DomainError], tuple[int, ErrorCode]] = {
|
||||
# решение 1): ограничено количество работы, а не частота — вопрос
|
||||
# преподавателю, другой ответ меняет эту строку
|
||||
exceptions.UploadBacklogFull: (429, ErrorCode.RATE_LIMITED),
|
||||
# предел неудачных входов (Retry-After — из исключения) и занятый бюджет
|
||||
# хэшей пароля (design change auth-hardening, решения 3, 5)
|
||||
exceptions.LoginRateLimited: (429, ErrorCode.RATE_LIMITED),
|
||||
exceptions.PasswordHashingBusy: (429, ErrorCode.RATE_LIMITED),
|
||||
exceptions.UnsupportedMediaType: (
|
||||
415,
|
||||
ErrorCode.UNSUPPORTED_MEDIA_TYPE,
|
||||
@@ -71,8 +76,16 @@ CHALLENGES: dict[type[exceptions.DomainError], str] = {
|
||||
RETRY_AFTER: dict[type[exceptions.DomainError], int] = {
|
||||
exceptions.UploadsBusy: UPLOAD_RETRY_AFTER_SECONDS,
|
||||
exceptions.UploadBacklogFull: UPLOAD_BACKLOG_RETRY_AFTER_SECONDS,
|
||||
exceptions.PasswordHashingBusy: PASSWORD_HASHING_RETRY_AFTER_SECONDS,
|
||||
}
|
||||
|
||||
|
||||
def _retry_after(exc: exceptions.DomainError) -> int | None:
|
||||
"""Retry-After у 429: у предела входа — остаток окна из исключения."""
|
||||
if isinstance(exc, exceptions.LoginRateLimited):
|
||||
return exc.retry_after
|
||||
return RETRY_AFTER.get(type(exc))
|
||||
|
||||
HTTP_ERRORS: dict[int, tuple[ErrorCode, str]] = {
|
||||
404: (ErrorCode.NOT_FOUND, "Not found"),
|
||||
405: (ErrorCode.INVALID_REQUEST, "Method not allowed"),
|
||||
@@ -112,7 +125,7 @@ async def handle_domain_error(
|
||||
headers: dict[str, str] = {}
|
||||
if challenge := CHALLENGES.get(type(exc)):
|
||||
headers["WWW-Authenticate"] = challenge
|
||||
if (retry_after := RETRY_AFTER.get(type(exc))) is not None:
|
||||
if (retry_after := _retry_after(exc)) is not None:
|
||||
headers["Retry-After"] = str(retry_after)
|
||||
return error_response(
|
||||
request_id=_request_id(request),
|
||||
|
||||
Reference in New Issue
Block a user