# Copyright (C) 2026 Mattia Zatelli # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """ Custom middleware for the BIM Query """ import re import time import uuid import json import logging from typing import Callable from fastapi import Request, Response, HTTPException, status from fastapi.responses import JSONResponse from starlette.middleware.base import BaseHTTPMiddleware from starlette.types import ASGIApp from app.core.config import get_settings from app.core.redis import RedisCache, RedisRateLimit from app.services.auth import AuthService from app.core.exceptions import BIMAPIException logger = logging.getLogger(__name__) settings = get_settings() # Regex for validating client-supplied request IDs (alphanumeric, hyphens, max 64 chars) _VALID_REQUEST_ID = re.compile(r'^[a-zA-Z0-9\-]{1,64}$') class RequestIDMiddleware(BaseHTTPMiddleware): """Add unique request ID to each request""" async def dispatch(self, request: Request, call_next: Callable) -> Response: # Get or generate request ID (validate client-supplied IDs to prevent log injection) client_id = request.headers.get("X-Request-Id") if client_id and _VALID_REQUEST_ID.match(client_id): request_id = client_id else: request_id = str(uuid.uuid4()) # Add to request state request.state.request_id = request_id # Process request response = await call_next(request) # Add security and tracking headers response.headers["X-Request-Id"] = request_id response.headers["X-Content-Type-Options"] = "nosniff" response.headers["X-Frame-Options"] = "DENY" response.headers["Cache-Control"] = "no-store" if settings.APP_ENV == "prod": response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains" return response class LoggingMiddleware(BaseHTTPMiddleware): """Log requests and responses""" async def dispatch(self, request: Request, call_next: Callable) -> Response: start_time = time.time() # Log request request_id = getattr(request.state, "request_id", "unknown") logger.info( f"Request {request_id}: {request.method} {request.url.path}", extra={ "request_id": request_id, "method": request.method, "path": request.url.path, "query_params": dict(request.query_params), "client_ip": request.client.host if request.client else None, "user_agent": request.headers.get("User-Agent"), } ) # Process request response = await call_next(request) # Calculate duration duration = time.time() - start_time # Log response logger.info( f"Response {request_id}: {response.status_code} ({duration:.3f}s)", extra={ "request_id": request_id, "status_code": response.status_code, "duration": duration, "response_size": response.headers.get("content-length"), } ) return response class AuthenticationMiddleware(BaseHTTPMiddleware): """Middleware for request authentication via API key Validates the X-Api-Key header against the configured API_KEY, injects tenant info into request state, and skips public endpoints. """ def __init__(self, app: ASGIApp): super().__init__(app) self.auth_service = AuthService() # Public endpoints that do not require authentication self.public_paths = { "/health", "/", "/docs", "/redoc", "/openapi.json", } async def dispatch(self, request: Request, call_next: Callable) -> Response: # === SKIP AUTHENTICATION FOR PUBLIC PATHS === if request.url.path in self.public_paths: return await call_next(request) # Skip authentication for health endpoint in API path if request.url.path.endswith("/health"): return await call_next(request) # Prefer Bearer JWT if present, otherwise fall back to the shared API key. auth_header = request.headers.get("Authorization", "") bearer_token = ( auth_header.split(" ", 1)[1].strip() if auth_header.lower().startswith("bearer ") else "" ) api_key = request.headers.get("X-Api-Key") if not bearer_token and not api_key: return JSONResponse( status_code=status.HTTP_401_UNAUTHORIZED, content={"error": {"type": "authentication_error", "code": "missing_credentials", "message": "Authorization: Bearer or X-Api-Key header required"}} ) try: if bearer_token: auth_info = await self.auth_service.introspect_jwt(bearer_token) else: auth_info = await self.auth_service.introspect_api_key(api_key) if not auth_info.get("valid"): return JSONResponse( status_code=status.HTTP_401_UNAUTHORIZED, content={"error": {"type": "authentication_error", "code": "invalid_credentials", "message": "Invalid or expired credentials"}} ) # Add auth info to request state request.state.auth = auth_info # Use X-Tenant-Id from nginx/auth-service if present (per-user tenant isolation), # otherwise fall back to the default tenant_id from API key config forwarded_tenant = request.headers.get("X-Tenant-Id") if forwarded_tenant and re.match(r'^[a-zA-Z0-9_\-]{1,64}$', forwarded_tenant): request.state.tenant_id = forwarded_tenant else: request.state.tenant_id = auth_info.get("tenant_id") request.state.api_key_hash = auth_info.get("api_key_hash") except Exception as e: logger.error(f"Authentication error: {e}") return JSONResponse( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, content={"error": {"type": "authentication_error", "code": "auth_service_error", "message": "Authentication service unavailable"}} ) return await call_next(request) class RateLimitMiddleware(BaseHTTPMiddleware): """Middleware for API key-based rate limiting **Token Bucket Algorithm:** - Each API key has a bucket with configurable capacity - Tokens are consumed per request - Automatic refill at a constant rate - Burst allowed up to maximum capacity **Tier Configurations:** - FREE: 100 req/min, burst 10 - BASIC: 1000 req/min, burst 50 - PRO: 10000 req/min, burst 200 - ENTERPRISE: Unlimited or custom **Redis Storage:** - Key: rate_limit:{api_key_hash} - Automatic TTL for cleanup - Atomic operations for accuracy **Response Headers:** - X-RateLimit-Limit: Configured limit - X-RateLimit-Remaining: Remaining tokens - X-RateLimit-Reset: Reset timestamp - Retry-After: Seconds to wait (if 429) """ def __init__(self, app: ASGIApp): super().__init__(app) # Default rate limits (can be overridden by introspection) self.default_rate_limit = settings.RATE_LIMIT_DEFAULT_RPS self.default_burst = settings.RATE_LIMIT_DEFAULT_BURST # Paths to skip rate limiting self.skip_paths = {"/api/v1/health", "/metrics", "/docs", "/openapi.json"} async def dispatch(self, request: Request, call_next: Callable) -> Response: # Skip rate limiting for certain paths if request.url.path in self.skip_paths: return await call_next(request) # Get tenant/auth info tenant_id = getattr(request.state, "tenant_id", None) if not tenant_id: # Use IP-based rate limiting for unauthenticated requests tenant_id = request.client.host if request.client else "unknown" # Get rate limits from auth info or use defaults auth_info = getattr(request.state, "auth", {}) rate_limit_config = auth_info.get("rate_limit", {}) rps = rate_limit_config.get("rps", settings.RATE_LIMIT_DEFAULT_RPS) burst = rate_limit_config.get("burst", settings.RATE_LIMIT_DEFAULT_BURST) # Rate limiting is enforced in all environments except dev if settings.APP_ENV != "dev": rate_limit_key = f"rate_limit:{tenant_id}" allowed, info = await RedisRateLimit.check_rate_limit( key=rate_limit_key, limit=rps, window=60, # 1 minute window burst=burst ) if not allowed: response = JSONResponse( status_code=status.HTTP_429_TOO_MANY_REQUESTS, content={ "error": { "type": "rate_limit_error", "code": "rate_limit_exceeded", "message": "Rate limit exceeded. Please retry later.", "request_id": getattr(request.state, "request_id", "unknown") } } ) response.headers["X-RateLimit-Limit"] = str(rps) response.headers["X-RateLimit-Remaining"] = str(info["remaining"]) response.headers["X-RateLimit-Reset"] = str(info["reset"]) response.headers["Retry-After"] = str(info.get("window", 60)) return response # Process request response = await call_next(request) # Add rate limit headers response.headers["X-RateLimit-Limit"] = str(rps) response.headers["X-RateLimit-Remaining"] = str(info["remaining"]) response.headers["X-RateLimit-Reset"] = str(info["reset"]) return response # Dev environment: no rate limiting enforcement response = await call_next(request) response.headers["X-RateLimit-Limit"] = str(rps) return response class IdempotencyMiddleware(BaseHTTPMiddleware): """Middleware for idempotent request handling **Purpose:** - Prevent duplicate processing for critical operations - Guarantee exactly-once semantics for uploads - Cache responses for identical requests - Protection from network retries/client bugs **Supported Methods:** - POST: File uploads, query creation, calculations - PUT: Updates that must be idempotent - PATCH: Partial modifications **X-Idempotency-Key Header:** - Client-generated UUID v4 - Unique per specific operation - 24h TTL to prevent accumulation - Cached response for immediate replay **Conflict Handling:** - Same key + different body = 400 Bad Request - Reused key = original cached response - Timeout during processing = 202 Accepted **Redis Storage:** - Key: idempotency:{key_hash} - Value: complete serialized response - TTL: 24 hours (configurable) """ def __init__(self, app: ASGIApp): super().__init__(app) # HTTP methods that support idempotency self.idempotent_methods = {"POST", "PUT", "PATCH"} # Skip paths for WebSocket and health endpoints self.skip_paths = {"/api/v1/health", "/metrics", "/docs", "/openapi.json", "/ws"} # Default TTL for idempotency keys (24 hours) self.ttl_seconds = 24 * 3600 async def dispatch(self, request: Request, call_next: Callable) -> Response: # Only handle idempotent methods if request.method not in self.idempotent_methods: return await call_next(request) # Skip WebSocket and health endpoints if any(skip_path in request.url.path for skip_path in self.skip_paths): return await call_next(request) # Get idempotency key idempotency_key = request.headers.get("Idempotency-Key") if not idempotency_key: # Not idempotent, process normally return await call_next(request) # Generate cache key tenant_id = getattr(request.state, "tenant_id", "unknown") cache_key = f"idempotency:{tenant_id}:{idempotency_key}" # Check if request was already processed cached_response = await RedisCache.get(cache_key) if cached_response: logger.info(f"Returning cached response for idempotency key: {idempotency_key}") return JSONResponse( status_code=cached_response["status_code"], content=cached_response["content"], headers=cached_response.get("headers", {}) ) # Process request response = await call_next(request) # Cache successful responses (2xx status codes) if 200 <= response.status_code < 300: # Read response content body = b"" async for chunk in response.body_iterator: body += chunk # Parse content try: content = json.loads(body.decode()) except (json.JSONDecodeError, UnicodeDecodeError): content = {"message": "Response cached"} # Cache response for 1 hour cached_data = { "status_code": response.status_code, "content": content, "headers": dict(response.headers) } await RedisCache.set(cache_key, cached_data, ttl=3600) # Recreate response response = JSONResponse( status_code=response.status_code, content=content, headers=response.headers ) return response