Files
bakery-ia/services/procurement/app/core/dependencies.py

48 lines
1.3 KiB
Python
Raw Normal View History

2025-10-30 21:08:07 +01:00
"""
FastAPI Dependencies for Procurement Service
2026-01-12 22:15:11 +01:00
Uses shared authentication infrastructure with UUID validation
2025-10-30 21:08:07 +01:00
"""
2026-01-12 22:15:11 +01:00
from fastapi import Depends, HTTPException, status
2025-10-30 21:08:07 +01:00
from uuid import UUID
from typing import Optional
from sqlalchemy.ext.asyncio import AsyncSession
from .database import get_db
2026-01-12 22:15:11 +01:00
from shared.auth.decorators import get_current_tenant_id_dep
2025-10-30 21:08:07 +01:00
async def get_current_tenant_id(
2026-01-12 22:15:11 +01:00
tenant_id: Optional[str] = Depends(get_current_tenant_id_dep)
2025-10-30 21:08:07 +01:00
) -> UUID:
"""
2026-01-12 22:15:11 +01:00
Extract and validate tenant ID from request using shared infrastructure.
Adds UUID validation to ensure tenant ID format is correct.
2025-10-30 21:08:07 +01:00
Args:
2026-01-12 22:15:11 +01:00
tenant_id: Tenant ID from shared dependency
2025-10-30 21:08:07 +01:00
Returns:
UUID: Validated tenant ID
Raises:
2026-01-12 22:15:11 +01:00
HTTPException: If tenant ID is missing or invalid UUID format
2025-10-30 21:08:07 +01:00
"""
2026-01-12 22:15:11 +01:00
if not tenant_id:
2025-10-30 21:08:07 +01:00
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
2026-01-12 22:15:11 +01:00
detail="x-tenant-id header is required"
2025-10-30 21:08:07 +01:00
)
try:
2026-01-12 22:15:11 +01:00
return UUID(tenant_id)
2025-10-30 21:08:07 +01:00
except (ValueError, AttributeError):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
2026-01-12 22:15:11 +01:00
detail=f"Invalid tenant ID format: {tenant_id}"
2025-10-30 21:08:07 +01:00
)
# Re-export get_db for convenience
__all__ = ["get_db", "get_current_tenant_id"]