"""Routes, authentication and request validation for lab 2.""" from __future__ import annotations import base64 import binascii import hashlib import json import re import uuid from dataclasses import dataclass from typing import Annotated, Any, cast from fastapi import APIRouter, Depends, Request, Response from psycopg_pool import AsyncConnectionPool from pydantic import ValidationError from starlette.concurrency import run_in_threadpool from . import sessions as session_service from . import store from .config import DEFAULT_PAGE_SIZE, MAX_JSON_BYTES, MAX_PAGE_SIZE, IDEMPOTENCY_KEY_PATTERN from .db import ping from .errors import BASIC_CHALLENGE, BEARER_CHALLENGE, ApiProblem from .models import CreateChat, CreateMessage, CreateUser, StrictModel from .pagination import build_page, decode_cursor, format_timestamp from .security import hash_password, verify_password, verify_password_unknown_user router = APIRouter() USERNAME_PATTERN = re.compile(r"^[a-z0-9_]{3,32}$") @dataclass(frozen=True) class Principal: """The authenticated identity behind a Bearer token.""" user_id: uuid.UUID user: dict[str, Any] def get_pool(request: Request) -> AsyncConnectionPool: return cast(AsyncConnectionPool, request.app.state.pool) def get_cache(request: Request) -> session_service.SessionCache: return cast(session_service.SessionCache, request.app.state.cache) async def drain_body(request: Request) -> None: """Consume an unwanted request body without retaining it in memory.""" async for _chunk in request.stream(): pass def declared_body_size(request: Request) -> int | None: value = request.headers.get("Content-Length") if value is None: return None try: size = int(value) except ValueError: raise ApiProblem(400, "invalid_request", "Content-Length is invalid") from None if size < 0: raise ApiProblem(400, "invalid_request", "Content-Length is invalid") return size async def read_limited_body(request: Request) -> bytes: chunks: list[bytes] = [] size = 0 too_large = False async for chunk in request.stream(): size += len(chunk) if size <= MAX_JSON_BYTES: chunks.append(chunk) else: too_large = True if too_large: raise ApiProblem(413, "payload_too_large", "Request body is too large") return b"".join(chunks) async def parse_body(request: Request, model: type[StrictModel]) -> Any: content_length = declared_body_size(request) if content_length is not None and content_length > MAX_JSON_BYTES: await drain_body(request) raise ApiProblem(413, "payload_too_large", "Request body is too large") content_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip() if content_type.lower() != "application/json": await drain_body(request) raise ApiProblem( 415, "unsupported_media_type", "Content-Type must be application/json" ) body = await read_limited_body(request) try: decoded = json.loads(body.decode("utf-8")) except (json.JSONDecodeError, UnicodeDecodeError): raise ApiProblem( 400, "invalid_request", "Request body is not valid JSON" ) from None try: return model.model_validate(decoded) except ValidationError: raise ApiProblem(400, "invalid_request", "Request fields are invalid") from None def ensure_no_query(request: Request) -> None: if request.url.query: raise ApiProblem(400, "invalid_request", "Query parameters are not supported") def parse_pagination_request(request: Request) -> tuple[int, str | None]: """Read limit/cursor only; unknown or repeated parameters are a 400.""" parameters = request.query_params keys = [key for key, _ in parameters.multi_items()] if set(keys) - {"limit", "cursor"} or any( keys.count(key) != 1 for key in set(keys) ): raise ApiProblem(400, "invalid_request", "Query parameters are invalid") limit_value = parameters.get("limit", str(DEFAULT_PAGE_SIZE)) try: limit = int(limit_value) except (TypeError, ValueError): raise ApiProblem(400, "invalid_request", "limit is invalid") from None if not 1 <= limit <= MAX_PAGE_SIZE: raise ApiProblem(400, "invalid_request", "limit is invalid") cursor = parameters.get("cursor") if cursor == "": raise ApiProblem(400, "invalid_request", "cursor is invalid") return limit, cursor def require_chat_id(chat_id: str) -> uuid.UUID: try: return uuid.UUID(chat_id) except (ValueError, AttributeError): raise ApiProblem(400, "invalid_request", "chat_id must be a UUID") from None def unauthorized(message: str, challenge: str) -> ApiProblem: return ApiProblem(401, "unauthorized", message, {"WWW-Authenticate": challenge}) def bearer_token(request: Request) -> str: """Only Bearer is accepted; Basic here is a 401, not a fallback login.""" header = request.headers.get("Authorization") if header is None: raise unauthorized("Bearer session token is required", BEARER_CHALLENGE) scheme, _, value = header.partition(" ") if scheme.strip().lower() != "bearer" or not value.strip(): raise unauthorized("Bearer session token is required", BEARER_CHALLENGE) return value.strip() def basic_credentials(request: Request) -> tuple[str, str]: """Decode Basic credentials; it is an encoding, never a trusted identity.""" header = request.headers.get("Authorization") if header is None: raise unauthorized("Basic credentials are required", BASIC_CHALLENGE) scheme, _, value = header.partition(" ") if scheme.strip().lower() != "basic" or not value.strip(): raise unauthorized("Basic credentials are required", BASIC_CHALLENGE) try: raw = base64.b64decode(value.strip(), validate=True).decode("utf-8") except (binascii.Error, UnicodeDecodeError, ValueError): raise unauthorized("Basic credentials are invalid", BASIC_CHALLENGE) from None username, separator, password = raw.partition(":") if not separator or not username or not password: raise unauthorized("Basic credentials are invalid", BASIC_CHALLENGE) if USERNAME_PATTERN.fullmatch(username) is None: # Unknown or malformed usernames must not be distinguished from a bad password. raise unauthorized("Invalid username or password", BASIC_CHALLENGE) return username, password async def current_principal(request: Request) -> Principal: token = bearer_token(request) user_id, user = await session_service.resolve_session( get_pool(request), get_cache(request), token ) return Principal(user_id=user_id, user=user) Actor = Annotated[Principal, Depends(current_principal)] # --------------------------------------------------------------------------- # # Health # --------------------------------------------------------------------------- # @router.get("/health/live") async def liveness(request: Request) -> dict[str, str]: ensure_no_query(request) return {"status": "ok"} @router.get("/health/ready") async def readiness(request: Request) -> dict[str, str]: ensure_no_query(request) try: await ping(get_pool(request)) except Exception as exc: # noqa: BLE001 - any DB failure means not ready request.app.state.logger.warning( "readiness_database_failed error=%s", type(exc).__name__ ) raise ApiProblem(503, "unavailable", "Database is unavailable") from None return {"status": "ok"} # --------------------------------------------------------------------------- # # Users and sessions # --------------------------------------------------------------------------- # @router.post("/api/v1/users", status_code=201) async def register_user(request: Request) -> dict[str, Any]: ensure_no_query(request) body = await parse_body(request, CreateUser) assert isinstance(body, CreateUser) password_hash = await run_in_threadpool(hash_password, body.password) async with get_pool(request).connection() as connection: async with connection.cursor() as cursor: return await store.create_user( cursor, username=body.username, display_name=body.display_name, password_hash=password_hash, ) @router.post("/api/v1/auth/sessions", status_code=201) async def create_session(request: Request) -> dict[str, Any]: ensure_no_query(request) await drain_body(request) username, password = basic_credentials(request) pool = get_pool(request) async with pool.connection() as connection: async with connection.cursor() as cursor: row = await store.user_credentials(cursor, username) if row is None: await run_in_threadpool(verify_password_unknown_user, password) raise unauthorized("Invalid username or password", BASIC_CHALLENGE) if not await run_in_threadpool( verify_password, password, row["password_hash"] ): raise unauthorized("Invalid username or password", BASIC_CHALLENGE) token, expires_at = await session_service.create_session( pool, get_cache(request), user_id=row["id"], ttl_seconds=request.app.state.settings.session_ttl_seconds, ) return { "token": token, "token_type": "Bearer", "expires_at": format_timestamp(expires_at), "user": store.user_json(row), } @router.delete("/api/v1/auth/sessions/current", status_code=204) async def delete_current_session(request: Request) -> Response: ensure_no_query(request) token = bearer_token(request) await session_service.revoke_session(get_pool(request), get_cache(request), token) return Response(status_code=204) @router.get("/api/v1/users/me") async def current_user(request: Request, actor: Actor) -> dict[str, Any]: ensure_no_query(request) return actor.user # --------------------------------------------------------------------------- # # Chats and messages # --------------------------------------------------------------------------- # @router.post("/api/v1/chats", status_code=201) async def create_chat(request: Request, actor: Actor) -> dict[str, Any]: ensure_no_query(request) body = await parse_body(request, CreateChat) assert isinstance(body, CreateChat) members = [uuid.UUID(member) for member in body.member_ids] async with get_pool(request).connection() as connection: async with connection.cursor() as cursor: return await store.create_chat( cursor, actor_id=actor.user_id, title=body.title, member_ids=members ) @router.get("/api/v1/chats") async def list_chats(request: Request, actor: Actor) -> dict[str, Any]: limit, cursor = parse_pagination_request(request) resource = "chats:" + str(actor.user_id) after = decode_cursor(cursor, resource) if cursor is not None else None async with get_pool(request).connection() as connection: async with connection.cursor() as cursor_handle: rows = await store.chat_page( cursor_handle, actor_id=actor.user_id, limit=limit, after=after ) return build_page(rows, resource, limit) @router.get("/api/v1/chats/{chat_id}") async def get_chat(request: Request, chat_id: str, actor: Actor) -> dict[str, Any]: ensure_no_query(request) normalized = require_chat_id(chat_id) async with get_pool(request).connection() as connection: async with connection.cursor() as cursor: return await store.accessible_chat( cursor, actor_id=actor.user_id, chat_id=normalized ) @router.post("/api/v1/chats/{chat_id}/messages", status_code=201) async def create_message( request: Request, chat_id: str, actor: Actor ) -> dict[str, Any]: normalized = require_chat_id(chat_id) ensure_no_query(request) body = await parse_body(request, CreateMessage) assert isinstance(body, CreateMessage) pool = get_pool(request) request_sha256 = hashlib.sha256( json.dumps(body.model_dump(), sort_keys=True).encode("utf-8") ).hexdigest() async with pool.connection() as connection: async with connection.cursor() as cursor: # Check membership first so non-members get 404, not 400. await store.require_membership( cursor, actor_id=actor.user_id, chat_id=normalized ) key = request.headers.get("Idempotency-Key", "").strip() if not key or not IDEMPOTENCY_KEY_PATTERN.fullmatch(key): raise ApiProblem( 400, "invalid_request", "Idempotency-Key must match " "^[A-Za-z0-9._:-]{1,128}$" ) existing = await store.get_idempotency_key( cursor, actor_id=actor.user_id, chat_id=normalized, key=key ) if existing is not None: if existing["request_sha256"] != request_sha256: raise ApiProblem(409, "conflict", "Idempotency-Key was already used") # Replay: return the original message. original = await store.message_by_id(cursor, existing["message_id"]) if original is not None: return original inserted = await store.insert_idempotency_key( cursor, actor_id=actor.user_id, key=key, chat_id=normalized, request_sha256=request_sha256, ) if not inserted: # INSERT .. ON CONFLICT keeps this transaction usable. PostgreSQL # waited for the winning transaction, so its message is committed # and can be returned as the exact replay result. existing = await store.get_idempotency_key( cursor, actor_id=actor.user_id, chat_id=normalized, key=key ) if existing is not None: if existing["request_sha256"] != request_sha256: raise ApiProblem(409, "conflict", "Idempotency-Key was already used") original = await store.message_by_id(cursor, existing["message_id"]) if original is not None: return original raise ApiProblem(409, "conflict", "Idempotency-Key is still being stored") return await store.create_message( cursor, actor_id=actor.user_id, chat_id=normalized, text=body.text, request_sha256=request_sha256, key=key, ) @router.get("/api/v1/chats/{chat_id}/messages") async def list_messages( request: Request, chat_id: str, actor: Actor ) -> dict[str, Any]: normalized = require_chat_id(chat_id) limit, cursor = parse_pagination_request(request) resource = "messages:" + str(normalized) after = decode_cursor(cursor, resource) if cursor is not None else None async with get_pool(request).connection() as connection: async with connection.cursor() as cursor_handle: await store.require_membership( cursor_handle, actor_id=actor.user_id, chat_id=normalized ) rows = await store.message_page( cursor_handle, chat_id=normalized, limit=limit, after=after ) return build_page(rows, resource, limit)