справление: U+0000 в строках сохраняется (bytea), DataError -> 400

This commit is contained in:
kalibr456
2026-10-09 18:30:42 +03:00
parent 8149a608f8
commit e17d883c15
10 changed files with 102 additions and 101 deletions
+5 -13
View File
@@ -1,16 +1,8 @@
"""Мессенджер, lab2: HTTP API по contracts/openapi.json.
Что изменилось с lab1:
- данные (users, chats, messages) теперь в PostgreSQL, а не в словарях: см. db.py;
- регистрация принимает password, вход — POST /auth/sessions с Basic, выход — DELETE;
- текущий пользователь определяется только по Bearer-токену (сессии в Redis, см. auth.py),
заголовок X-User-Id больше ничего не значит.
Модели, формат ошибок, X-Request-Id и курсоры остались из lab1.
"""
import base64
import binascii
import json
import logging
import re
import time
import uuid
from contextlib import asynccontextmanager
@@ -49,6 +41,8 @@ async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan)
USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}")
class CreateUser(BaseModel):
model_config = ConfigDict(extra="forbid")
@@ -209,7 +203,7 @@ async def request_context(request: Request, call_next):
auth.check_bearer, request.headers.get("Authorization")
)
response = await call_next(request)
except ApiError as exc:
except ApiError as exc:
response = error_response(request, exc.code, exc.message, exc.status_code, exc.headers)
except Exception:
log.exception("Unhandled error, request_id=%s", request_id)
@@ -297,7 +291,6 @@ def page_after(cursor: str | None, *, chat_id: UUID | None = None, user_id: UUID
return created_at, object_id
@app.get("/health/live")
def health_live():
return {"status": "ok"}
@@ -310,7 +303,6 @@ def health_ready():
return {"status": "ok"}
@app.post("/api/v1/users", response_model=User, status_code=201)
def create_user(user_data: CreateUser):
password_hash = auth.hash_password(user_data.password) # сам пароль нигде не сохраняется
@@ -328,7 +320,7 @@ def create_session(
username, password = credentials
found = None
if len(password) <= auth.PASSWORD_MAX_LENGTH:
if USERNAME_RE.fullmatch(username) and len(password) <= auth.PASSWORD_MAX_LENGTH:
found = db.get_user_with_password(username)
if not auth.password_matches(found["password_hash"] if found else None, password):
raise auth.login_failed()