Files
bakery-ia/services/forecasting/app/core/auth.py

49 lines
1.6 KiB
Python
Raw Normal View History

2025-07-21 19:48:56 +02:00
# ================================================================
# services/forecasting/app/core/auth.py
# ================================================================
2025-07-20 02:16:51 +02:00
"""
2025-07-21 19:48:56 +02:00
Authentication utilities for forecasting service
2025-07-20 02:16:51 +02:00
"""
2025-07-21 19:48:56 +02:00
import structlog
from fastapi import HTTPException, status, Request
from typing import Dict, Any, Optional
2025-07-20 02:16:51 +02:00
2025-07-21 19:48:56 +02:00
logger = structlog.get_logger()
2025-07-20 02:16:51 +02:00
2025-07-21 19:48:56 +02:00
async def get_current_user_from_headers(request: Request) -> Dict[str, Any]:
"""
Get current user from gateway headers
Gateway middleware adds user context to headers after JWT verification
"""
try:
# Extract user information from headers set by API Gateway
user_id = request.headers.get("X-User-ID")
user_email = request.headers.get("X-User-Email")
tenant_id = request.headers.get("X-Tenant-ID")
user_roles = request.headers.get("X-User-Roles", "").split(",")
if not user_id or not tenant_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authentication required"
)
return {
"user_id": user_id,
"email": user_email,
"tenant_id": tenant_id,
"roles": [role.strip() for role in user_roles if role.strip()]
}
except HTTPException:
raise
except Exception as e:
logger.error("Error extracting user from headers", error=str(e))
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication"
)
2025-07-20 02:16:51 +02:00