"""HTTP API for laboratory 3 (PostgreSQL + Redis sessions behind a LB).""" from __future__ import annotations import json import logging import os import time import uuid from datetime import datetime, timedelta, timezone from urllib.parse import quote import psycopg import psycopg_pool import redis from argon2 import PasswordHasher from argon2.exceptions import InvalidHashError, VerifyMismatchError from flask import Flask, Response, g, request from . import attachments as attachment_store from . import db, idempotency, s3, sessions from .config import settings from .errors import ( ApiError, bad_request, conflict, not_found, payload_too_large, unauthorized, unavailable, unsupported_media_type, ) from .validation import ( byte_size, cursor_time, decode_cursor, display_name, encode_cursor, exact_keys, filename, idempotency_key, image_content_type, json_object, limit_arg, message_text, non_blank, password, sha256_hex, username, uuid_list, uuid_value, ) logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") log = logging.getLogger("messenger.api") app = Flask(__name__) # Uploads stream through the handler; the Flask ceiling is deliberately just # above the contract limit so a single oversized request is rejected early. app.config["MAX_CONTENT_LENGTH"] = settings.max_upload_bytes + 64 * 1024 hasher = PasswordHasher() # --------------------------------------------------------------------------- # Response helpers and middleware # --------------------------------------------------------------------------- def _json(payload: object, status: int = 200, headers: dict[str, str] | None = None) -> Response: body = json.dumps(payload, ensure_ascii=False, separators=(",", ":")) response = Response(body, status=status, mimetype="application/json") for key, value in (headers or {}).items(): response.headers[key] = value return response def _error_response(error: ApiError, request_id: str) -> Response: return _json( { "error": { "code": error.code, "message": error.message, "request_id": request_id, } }, error.status, error.headers, ) @app.before_request def _request_context() -> None: g.request_id = uuid.uuid4().hex g.started_at = time.monotonic() db.start_background_initialisation() @app.after_request def _response_headers(response: Response) -> Response: response.headers["X-Request-Id"] = g.get("request_id", uuid.uuid4().hex) response.headers["X-Instance-Id"] = settings.instance_id response.headers["Cache-Control"] = "no-store" return response @app.errorhandler(ApiError) def _handle_api_error(error: ApiError) -> Response: return _error_response(error, g.get("request_id", uuid.uuid4().hex)) @app.errorhandler(413) def _handle_too_large(_error: object) -> Response: return _error_response( ApiError(413, "payload_too_large", "Тело запроса слишком велико"), g.get("request_id", uuid.uuid4().hex), ) @app.errorhandler(404) def _handle_not_found(_error: object) -> Response: return _error_response(not_found(), g.get("request_id", uuid.uuid4().hex)) @app.errorhandler(405) def _handle_method(_error: object) -> Response: return _error_response(bad_request("Метод не поддерживается"), g.get("request_id", uuid.uuid4().hex)) @app.errorhandler(Exception) def _handle_unexpected(error: Exception) -> Response: request_id = g.get("request_id", uuid.uuid4().hex) if isinstance(error, (psycopg.OperationalError, psycopg_pool.PoolTimeout, psycopg_pool.PoolClosed)): log.warning("database unavailable on %s %s: %s", request.method, request.path, type(error).__name__) return _error_response(unavailable("База данных недоступна"), request_id) if isinstance(error, redis.RedisError): log.warning("session store unavailable on %s %s: %s", request.method, request.path, type(error).__name__) return _error_response(unavailable("Хранилище сессий недоступно"), request_id) if isinstance(error, s3.S3Unavailable): log.warning("object store unavailable on %s %s", request.method, request.path) return _error_response(unavailable("Объектное хранилище недоступно"), request_id) log.exception("unhandled error on %s %s", request.method, request.path) return _error_response( ApiError(500, "internal_error", "Внутренняя ошибка сервера"), request_id ) # --------------------------------------------------------------------------- # Serialisation # --------------------------------------------------------------------------- def format_time(value: datetime) -> str: return value.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ") def user_json(row: dict) -> dict: return { "id": str(row["id"]), "username": row["username"], "display_name": row["display_name"], "created_at": format_time(row["created_at"]), } def chat_json(row: dict) -> dict: member_ids = sorted(str(item) for item in (row.get("member_ids") or [])) return { "id": str(row["id"]), "title": row["title"], "member_ids": member_ids, "created_at": format_time(row["created_at"]), } def message_json(row: dict) -> dict: return { "id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]), "text": row["text"], "created_at": format_time(row["created_at"]), "attachment_ids": [str(item) for item in (row.get("attachment_ids") or [])], } # --------------------------------------------------------------------------- # Authentication # --------------------------------------------------------------------------- def _bearer_token() -> str: header = request.headers.get("Authorization", "") parts = header.split(None, 1) if len(parts) != 2 or parts[0].lower() != "bearer" or not parts[1].strip(): raise unauthorized() return parts[1].strip() def current_user() -> dict: cached = g.get("current_user") if cached is not None: return cached token = _bearer_token() user_id, token_digest = sessions.resolve(token) if not user_id: raise unauthorized() try: parsed = uuid.UUID(user_id) except ValueError as exc: raise unauthorized() from exc with db.pool().connection() as conn: row = conn.execute( "SELECT id, username, display_name, created_at FROM users WHERE id = %s", (parsed,), ).fetchone() if row is None: sessions.revoke(token_digest) raise unauthorized() g.current_user = row g.session_token_digest = token_digest return row def _basic_credentials() -> tuple[str, str]: header = request.headers.get("Authorization", "") parts = header.split(None, 1) if len(parts) != 2 or parts[0].lower() != "basic": raise unauthorized(basic=True) import base64 try: decoded = base64.b64decode(parts[1].strip(), validate=True).decode("utf-8") except (ValueError, UnicodeError) as exc: raise unauthorized(basic=True) from exc name, separator, secret = decoded.partition(":") if not separator or not name or not secret: raise unauthorized(basic=True) if len(name) > 32 or any(ord(char) > 127 for char in name): raise unauthorized(basic=True) return name, secret def _authenticate_basic() -> dict: name, secret = _basic_credentials() with db.pool().connection() as conn: row = conn.execute( "SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = %s", (name,), ).fetchone() if row is None: raise unauthorized(basic=True) try: hasher.verify(row["password_hash"], secret) except (VerifyMismatchError, InvalidHashError): raise unauthorized(basic=True) return row # --------------------------------------------------------------------------- # Health # --------------------------------------------------------------------------- @app.get("/health/live") def health_live() -> Response: return _json({"status": "ok"}) @app.get("/health/ready") def health_ready() -> Response: if not db.check_ready(): raise unavailable("База данных ещё не готова") sessions.ping() if not s3.check_ready(): raise unavailable("Объектное хранилище ещё не готово") return _json({"status": "ok"}) # --------------------------------------------------------------------------- # Users and sessions # --------------------------------------------------------------------------- @app.post("/api/v1/users") def register_user() -> Response: body = json_object(request, settings.max_body_bytes) exact_keys(body, ("username", "display_name", "password")) name = username(body["username"]) shown_name = display_name(body["display_name"]) secret = password(body["password"]) user_id = uuid.uuid4() try: with db.pool().connection() as conn: row = conn.execute( """ INSERT INTO users (id, username, display_name, password_hash) VALUES (%s, %s, %s, %s) RETURNING id, username, display_name, created_at """, (user_id, name, shown_name, hasher.hash(secret)), ).fetchone() except psycopg.errors.UniqueViolation as exc: raise conflict("Username уже занят") from exc return _json(user_json(row), 201) @app.post("/api/v1/auth/sessions") def create_session() -> Response: row = _authenticate_basic() token, expires_at = sessions.create(str(row["id"])) return _json( { "token": token, "token_type": "Bearer", "expires_at": format_time(expires_at), "user": user_json(row), }, 201, ) @app.delete("/api/v1/auth/sessions/current") def revoke_session() -> Response: current_user() # authentication must happen before revocation sessions.revoke(g.session_token_digest) return Response(status=204) @app.get("/api/v1/users/me") def current_user_endpoint() -> Response: return _json(user_json(current_user())) # --------------------------------------------------------------------------- # Chats # --------------------------------------------------------------------------- def _require_chat_member(chat_id: uuid.UUID, user_id: uuid.UUID) -> None: with db.pool().connection() as conn: row = conn.execute( "SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", (chat_id, user_id), ).fetchone() if row is None: raise not_found() @app.post("/api/v1/chats") def create_chat() -> Response: actor = current_user() body = json_object(request, settings.max_body_bytes) exact_keys(body, ("title", "member_ids")) title = non_blank(body["title"], "title", 1, 100) member_ids = uuid_list(body["member_ids"], "member_ids") actor_id = str(actor["id"]) if actor_id not in member_ids: raise bad_request("Создатель должен входить в member_ids") member_uuids = [uuid.UUID(item) for item in member_ids] chat_id = uuid.uuid4() with db.pool().connection() as conn: existing = conn.execute( "SELECT id FROM users WHERE id = ANY(%s)", (member_uuids,) ).fetchall() if len(existing) != len(member_uuids): raise bad_request("Указан неизвестный участник") row = conn.execute( "INSERT INTO chats (id, title) VALUES (%s, %s) RETURNING id, title, created_at", (chat_id, title), ).fetchone() with conn.cursor() as cursor: cursor.executemany( "INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)", [(chat_id, member) for member in member_uuids], ) payload = dict(row) payload["member_ids"] = member_uuids return _json(chat_json(payload), 201) @app.get("/api/v1/chats") def list_chats() -> Response: actor = current_user() limit = limit_arg(request.args.get("limit")) cursor = decode_cursor(request.args.get("cursor")) parameters: list[object] = [actor["id"]] predicate = "" if cursor is not None: if cursor.get("kind") != "chats" or cursor.get("user_id") != str(actor["id"]): raise bad_request("Некорректный cursor") try: cursor_created = cursor_time(cursor.get("created_at")) cursor_id = uuid_value(cursor.get("id"), "cursor") except ApiError: raise bad_request("Некорректный cursor") predicate = "AND (c.created_at, c.id) > (%s, %s)" parameters.extend([cursor_created, cursor_id]) parameters.append(limit + 1) query = f""" SELECT c.id, c.title, c.created_at, ARRAY( SELECT cm.user_id FROM chat_members cm WHERE cm.chat_id = c.id ORDER BY cm.user_id ) AS member_ids FROM chats c JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s WHERE TRUE {predicate} ORDER BY c.created_at, c.id LIMIT %s """ with db.pool().connection() as conn: rows = conn.execute(query, tuple(parameters)).fetchall() page = rows[:limit] next_cursor = None if len(rows) > limit and page: last = page[-1] next_cursor = encode_cursor( { "v": 1, "kind": "chats", "user_id": str(actor["id"]), "created_at": format_time(last["created_at"]), "id": str(last["id"]), } ) return _json({"items": [chat_json(row) for row in page], "next_cursor": next_cursor}) @app.get("/api/v1/chats/") def get_chat(chat_id: str) -> Response: actor = current_user() parsed_chat_id = uuid_value(chat_id, "chat_id") with db.pool().connection() as conn: row = conn.execute( """ SELECT c.id, c.title, c.created_at, ARRAY( SELECT cm.user_id FROM chat_members cm WHERE cm.chat_id = c.id ORDER BY cm.user_id ) AS member_ids FROM chats c JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s WHERE c.id = %s """, (actor["id"], parsed_chat_id), ).fetchone() if row is None: raise not_found() return _json(chat_json(row)) # --------------------------------------------------------------------------- # Messages # --------------------------------------------------------------------------- @app.post("/api/v1/chats//messages") def create_message(chat_id: str) -> Response: actor = current_user() parsed_chat_id = uuid_value(chat_id, "chat_id") _require_chat_member(parsed_chat_id, actor["id"]) body = json_object(request, settings.max_body_bytes) exact_keys(body, ("text",), ("attachment_ids",)) attachments = uuid_list(body.get("attachment_ids", []), "attachment_ids", 0, 10) text = message_text(body["text"], allow_empty=bool(attachments)) key = idempotency_key(request.headers.get("Idempotency-Key")) row = idempotency.create_message( chat_id=parsed_chat_id, actor_id=actor["id"], path=request.path, key=key, text=text, attachment_ids=attachments, ) return _json(message_json(row), 201) @app.get("/api/v1/chats//messages") def list_messages(chat_id: str) -> Response: actor = current_user() parsed_chat_id = uuid_value(chat_id, "chat_id") _require_chat_member(parsed_chat_id, actor["id"]) limit = limit_arg(request.args.get("limit")) cursor = decode_cursor(request.args.get("cursor")) parameters: list[object] = [parsed_chat_id] predicate = "" if cursor is not None: if cursor.get("kind") != "messages" or cursor.get("chat_id") != str(parsed_chat_id): raise bad_request("Некорректный cursor") try: cursor_created = cursor_time(cursor.get("created_at")) cursor_id = uuid_value(cursor.get("id"), "cursor") except ApiError: raise bad_request("Некорректный cursor") predicate = "AND (created_at, id) > (%s, %s)" parameters.extend([cursor_created, cursor_id]) parameters.append(limit + 1) query = f""" SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, ARRAY( SELECT ma.attachment_id FROM message_attachments ma WHERE ma.message_id = m.id ORDER BY ma.position ) AS attachment_ids FROM messages m WHERE m.chat_id = %s {predicate} ORDER BY m.created_at, m.id LIMIT %s """ with db.pool().connection() as conn: rows = conn.execute(query, tuple(parameters)).fetchall() page = rows[:limit] next_cursor = None if len(rows) > limit and page: last = page[-1] next_cursor = encode_cursor( { "v": 1, "kind": "messages", "chat_id": str(parsed_chat_id), "created_at": format_time(last["created_at"]), "id": str(last["id"]), } ) return _json({"items": [message_json(row) for row in page], "next_cursor": next_cursor}) # --------------------------------------------------------------------------- # Attachments (lab4) # --------------------------------------------------------------------------- def _attachment_for_member(attachment_id: uuid.UUID, actor_id: uuid.UUID) -> dict: with db.pool().connection() as conn: row = conn.execute( """ SELECT a.* FROM attachments a JOIN chat_members cm ON cm.chat_id = a.chat_id AND cm.user_id = %s WHERE a.id = %s """, (actor_id, attachment_id), ).fetchone() if row is None: raise not_found() return row def _attachment_owned(attachment_id: uuid.UUID, actor_id: uuid.UUID) -> dict | None: with db.pool().connection() as conn: return conn.execute( """ SELECT a.* FROM attachments a JOIN chat_members cm ON cm.chat_id = a.chat_id AND cm.user_id = %s WHERE a.id = %s AND a.owner_id = %s """, (actor_id, attachment_id, actor_id), ).fetchone() @app.post("/api/v1/chats//attachments") def upload_attachment(chat_id: str) -> Response: """Synchronous proxy upload: 201 only after S3 PUT and PostgreSQL COMMIT.""" actor = current_user() parsed_chat_id = uuid_value(chat_id, "chat_id") _require_chat_member(parsed_chat_id, actor["id"]) name = filename(request.args.get("filename")) declared = image_content_type(request.mimetype) if request.content_length is not None and request.content_length > settings.max_upload_bytes: raise payload_too_large("Файл больше 10 MiB") path = None try: path, size, digest = attachment_store.read_to_temp(request.stream) attachment_store.validate_image(path, declared) attachment_id = uuid.uuid4() key = attachment_store.object_key(parsed_chat_id, attachment_id) with open(path, "rb") as body: s3.put_file(key, body, declared, size, digest) try: with db.pool().connection() as conn: row = conn.execute( """ INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 'ready') RETURNING * """, ( attachment_id, parsed_chat_id, actor["id"], name, declared, size, digest, key, ), ).fetchone() except Exception as error: # Compensate: an S3 object without metadata must not survive. s3.delete(key) if isinstance(error, psycopg.Error): raise unavailable("Не удалось сохранить метаданные вложения") from error raise return _json(attachment_store.attachment_json(row), 201) finally: attachment_store.cleanup(path) @app.post("/api/v1/chats//attachments/presign") def presign_attachment(chat_id: str) -> Response: """Extension: short-lived direct upload URL; the proxy endpoint stays mandatory.""" actor = current_user() parsed_chat_id = uuid_value(chat_id, "chat_id") _require_chat_member(parsed_chat_id, actor["id"]) body = json_object(request, settings.max_body_bytes) exact_keys(body, ("filename", "content_type", "size_bytes", "sha256")) name = filename(body["filename"]) declared = image_content_type(body["content_type"]) size = byte_size(body["size_bytes"], settings.max_upload_bytes) digest = sha256_hex(body["sha256"]) attachment_id = uuid.uuid4() key = attachment_store.object_key(parsed_chat_id, attachment_id) upload_url = s3.presign_put(key, declared, digest, settings.presign_ttl_seconds) expires_at = datetime.now(timezone.utc) + timedelta(seconds=settings.presign_ttl_seconds) try: with db.pool().connection() as conn: conn.execute( """ INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status, presign_expires_at) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 'pending', %s) """, ( attachment_id, parsed_chat_id, actor["id"], name, declared, size, digest, key, expires_at, ), ) except psycopg.Error as error: raise unavailable("Не удалось сохранить метаданные вложения") from error return _json( { "attachment_id": str(attachment_id), "upload_url": upload_url, "method": "PUT", "headers": {"Content-Type": declared, "x-amz-meta-sha256": digest}, "expires_at": format_time(expires_at), }, 201, ) @app.post("/api/v1/attachments//complete") def complete_attachment(attachment_id: str) -> Response: """Extension: verify a direct upload and make the attachment ready.""" actor = current_user() parsed_id = uuid_value(attachment_id, "attachment_id") row = _attachment_owned(parsed_id, actor["id"]) if row is None: raise not_found() body = json_object(request, settings.max_body_bytes) exact_keys(body, ("sha256", "size_bytes")) digest = sha256_hex(body["sha256"]) size = byte_size(body["size_bytes"], settings.max_upload_bytes) if row["status"] == "ready": if row["sha256"] == digest and int(row["size_bytes"]) == size: return _json(attachment_store.attachment_json(row)) raise conflict("Вложение уже завершено с другими параметрами") if row["status"] != "pending": raise conflict("Вложение не может быть завершено") expires_at = row.get("presign_expires_at") if expires_at is not None and expires_at <= datetime.now(timezone.utc): raise conflict("Срок действия загрузки истёк") head = s3.head(row["object_key"]) if head is None: raise conflict("Объект ещё не загружен") if int(head.get("ContentLength", -1)) != size: raise bad_request("Размер загруженного объекта не совпадает") actual_size, actual_digest = s3.stream_sha256(row["object_key"]) if actual_size != size or actual_digest != digest: # Wrong bytes are never promoted; delete them so a retry starts clean. s3.delete(row["object_key"]) raise bad_request("Контрольная сумма загруженного объекта не совпадает") with db.pool().connection() as conn: updated = conn.execute( """ UPDATE attachments SET status = 'ready', error_code = NULL, updated_at = clock_timestamp() WHERE id = %s AND status = 'pending' RETURNING * """, (parsed_id,), ).fetchone() if updated is None: raise conflict("Вложение уже обработано") return _json(attachment_store.attachment_json(updated)) @app.get("/api/v1/attachments/") def get_attachment(attachment_id: str) -> Response: actor = current_user() parsed_id = uuid_value(attachment_id, "attachment_id") row = _attachment_for_member(parsed_id, actor["id"]) return _json(attachment_store.attachment_json(row)) @app.get("/api/v1/attachments//content") def download_attachment(attachment_id: str) -> Response: actor = current_user() parsed_id = uuid_value(attachment_id, "attachment_id") variant = request.args.get("variant", "original") if variant != "original": raise bad_request("variant должен быть original") row = _attachment_for_member(parsed_id, actor["id"]) if row["status"] != "ready": raise conflict("Вложение ещё не готово") try: body = s3.open_stream(row["object_key"]) except s3.S3Missing as error: raise unavailable("Оригинал вложения недоступен") from error def chunks(): try: for chunk in body.iter_chunks(chunk_size=64 * 1024): yield chunk finally: body.close() response = Response(chunks(), mimetype=row["content_type"]) response.headers["Content-Length"] = str(row["size_bytes"]) response.headers["Content-Disposition"] = ( "inline; filename*=UTF-8''" + quote(row["filename"], safe="") ) return response