Files
bakery-ia/services/training/app/core/auth.py
2025-07-17 13:09:24 +02:00

38 lines
1.2 KiB
Python

"""
Authentication utilities for training service
"""
import httpx
from fastapi import HTTPException, status, Depends
from fastapi.security import HTTPBearer
import logging
from app.core.config import settings
logger = logging.getLogger(__name__)
security = HTTPBearer()
async def verify_token(token: str = Depends(security)):
"""Verify token with auth service"""
try:
async with httpx.AsyncClient() as client:
response = await client.post(
f"{settings.AUTH_SERVICE_URL}/auth/verify",
headers={"Authorization": f"Bearer {token.credentials}"}
)
if response.status_code == 200:
return response.json()
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials"
)
except httpx.RequestError as e:
logger.error(f"Auth service unavailable: {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Authentication service unavailable"
)