Пофиксила замечание про баг с русскоязычными паролями
This commit is contained in:
+16
-12
@@ -11,8 +11,6 @@ from ..storage.postgres import storage
|
||||
|
||||
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
|
||||
|
||||
basic_scheme = HTTPBasic(auto_error=False)
|
||||
|
||||
DEFAULT_TTL_SECONDS = 3600
|
||||
|
||||
|
||||
@@ -23,15 +21,23 @@ def _ttl_seconds() -> int:
|
||||
return DEFAULT_TTL_SECONDS
|
||||
|
||||
|
||||
def _parse_basic(credentials: HTTPBasicCredentials | None) -> tuple[str, str] | None:
|
||||
"""HTTPBasic уже разобрал заголовок; сюда попадают username/password.
|
||||
def _parse_basic_header(request: Request) -> tuple[str, str] | None:
|
||||
"""Ручной разбор Basic: base64 -> UTF-8 -> split по первому ':'.
|
||||
|
||||
Дополнительно валидируем кодировку: username — ASCII, пароль — UTF-8.
|
||||
HTTPBasic из Starlette декодирует latin-1 и ломает кириллицу.
|
||||
Здесь пароль читается как UTF-8 (RFC 7617 разрешает UTF-8 charset).
|
||||
Username остаётся ASCII по контракту.
|
||||
"""
|
||||
if credentials is None:
|
||||
raw = request.headers.get("Authorization", "")
|
||||
if not raw.lower().startswith("basic "):
|
||||
return None
|
||||
username = credentials.username
|
||||
password = credentials.password
|
||||
try:
|
||||
decoded = base64.b64decode(raw[6:].strip(), validate=True).decode("utf-8")
|
||||
except (binascii.Error, UnicodeDecodeError):
|
||||
return None
|
||||
if ":" not in decoded:
|
||||
return None
|
||||
username, password = decoded.split(":", 1)
|
||||
if not username or not password:
|
||||
return None
|
||||
try:
|
||||
@@ -47,10 +53,8 @@ def _parse_basic(credentials: HTTPBasicCredentials | None) -> tuple[str, str] |
|
||||
response_model=Session,
|
||||
status_code=201,
|
||||
)
|
||||
def create_session(
|
||||
credentials: HTTPBasicCredentials | None = Depends(basic_scheme),
|
||||
) -> Session:
|
||||
parsed = _parse_basic(credentials)
|
||||
def create_session(request: Request) -> Session:
|
||||
parsed = _parse_basic_header(request)
|
||||
if parsed is None:
|
||||
raise HTTPException(status_code=401, detail="unauthorized")
|
||||
username, password = parsed
|
||||
|
||||
Reference in New Issue
Block a user