feat: перевести API на password/Basic/Bearer и PostgreSQL с Redis-кэшем
Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
+13
-13
@@ -8,10 +8,6 @@ from .errors import ApiError
|
||||
|
||||
USERNAME_RE = re.compile(r"\A[a-z0-9_]{3,32}\Z")
|
||||
|
||||
# Уникальный идентификатор жизни процесса: cursors, выданные до рестарта,
|
||||
# становятся недействительными.
|
||||
CURSOR_EPOCH = uuid.uuid4().hex
|
||||
|
||||
|
||||
def _is_blank(value: str) -> bool:
|
||||
return value.strip() == ""
|
||||
@@ -28,16 +24,19 @@ def reject_extra(body: dict[str, Any], allowed: set[str]) -> None:
|
||||
raise ApiError(400, "invalid_request", "Неизвестные поля в теле запроса")
|
||||
|
||||
|
||||
def validate_create_user(body: Any) -> tuple[str, str]:
|
||||
def validate_create_user(body: Any) -> tuple[str, str, str]:
|
||||
body = require_object(body)
|
||||
reject_extra(body, {"username", "display_name"})
|
||||
reject_extra(body, {"username", "display_name", "password"})
|
||||
username = body.get("username")
|
||||
display_name = body.get("display_name")
|
||||
password = body.get("password")
|
||||
if not isinstance(username, str) or not USERNAME_RE.match(username):
|
||||
raise ApiError(400, "invalid_request", "Некорректный username")
|
||||
if not isinstance(display_name, str) or not (1 <= len(display_name) <= 100) or _is_blank(display_name):
|
||||
raise ApiError(400, "invalid_request", "Некорректный display_name")
|
||||
return username, display_name
|
||||
if not isinstance(password, str) or not (12 <= len(password) <= 128):
|
||||
raise ApiError(400, "invalid_request", "Пароль должен быть 12..128 символов")
|
||||
return username, display_name, password
|
||||
|
||||
|
||||
def validate_create_chat(body: Any, caller_id: str) -> tuple[str, list[str]]:
|
||||
@@ -96,9 +95,10 @@ def parse_limit(raw: str | None, default: int = 50) -> int:
|
||||
|
||||
|
||||
def parse_cursor(raw: str | None, scope: str) -> tuple[str, str] | None:
|
||||
"""Cursor — непрозрачная строка base64url от 'epoch|scope|created_at|id'.
|
||||
"""Cursor — непрозрачная строка base64url от 'scope|created_at|id'.
|
||||
|
||||
Cursor привязан к ресурсу (scope) и к жизни процесса (epoch).
|
||||
Cursor привязан к ресурсу (scope). Значения created_at/id — из БД,
|
||||
поэтому cursor переживает рестарт процесса.
|
||||
"""
|
||||
if raw is None:
|
||||
return None
|
||||
@@ -107,10 +107,10 @@ def parse_cursor(raw: str | None, scope: str) -> tuple[str, str] | None:
|
||||
except Exception:
|
||||
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
||||
parts = decoded.split("|")
|
||||
if len(parts) != 4:
|
||||
if len(parts) != 3:
|
||||
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
||||
epoch, cursor_scope, created_at, item_id = parts
|
||||
if epoch != CURSOR_EPOCH or cursor_scope != scope:
|
||||
cursor_scope, created_at, item_id = parts
|
||||
if cursor_scope != scope:
|
||||
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
||||
if not created_at or not item_id:
|
||||
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
||||
@@ -118,5 +118,5 @@ def parse_cursor(raw: str | None, scope: str) -> tuple[str, str] | None:
|
||||
|
||||
|
||||
def encode_cursor(created_at: str, item_id: str, scope: str) -> str:
|
||||
raw = "|".join([CURSOR_EPOCH, scope, created_at, item_id]).encode("utf-8")
|
||||
raw = "|".join([scope, created_at, item_id]).encode("utf-8")
|
||||
return base64.urlsafe_b64encode(raw).decode("ascii")
|
||||
|
||||
Reference in New Issue
Block a user