Пофиксила замечание про баг с русскоязычными паролями
This commit is contained in:
+16
-12
@@ -11,8 +11,6 @@ from ..storage.postgres import storage
|
|||||||
|
|
||||||
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
|
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
|
||||||
|
|
||||||
basic_scheme = HTTPBasic(auto_error=False)
|
|
||||||
|
|
||||||
DEFAULT_TTL_SECONDS = 3600
|
DEFAULT_TTL_SECONDS = 3600
|
||||||
|
|
||||||
|
|
||||||
@@ -23,15 +21,23 @@ def _ttl_seconds() -> int:
|
|||||||
return DEFAULT_TTL_SECONDS
|
return DEFAULT_TTL_SECONDS
|
||||||
|
|
||||||
|
|
||||||
def _parse_basic(credentials: HTTPBasicCredentials | None) -> tuple[str, str] | None:
|
def _parse_basic_header(request: Request) -> tuple[str, str] | None:
|
||||||
"""HTTPBasic уже разобрал заголовок; сюда попадают username/password.
|
"""Ручной разбор 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
|
return None
|
||||||
username = credentials.username
|
try:
|
||||||
password = credentials.password
|
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:
|
if not username or not password:
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
@@ -47,10 +53,8 @@ def _parse_basic(credentials: HTTPBasicCredentials | None) -> tuple[str, str] |
|
|||||||
response_model=Session,
|
response_model=Session,
|
||||||
status_code=201,
|
status_code=201,
|
||||||
)
|
)
|
||||||
def create_session(
|
def create_session(request: Request) -> Session:
|
||||||
credentials: HTTPBasicCredentials | None = Depends(basic_scheme),
|
parsed = _parse_basic_header(request)
|
||||||
) -> Session:
|
|
||||||
parsed = _parse_basic(credentials)
|
|
||||||
if parsed is None:
|
if parsed is None:
|
||||||
raise HTTPException(status_code=401, detail="unauthorized")
|
raise HTTPException(status_code=401, detail="unauthorized")
|
||||||
username, password = parsed
|
username, password = parsed
|
||||||
|
|||||||
Reference in New Issue
Block a user