38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
"""
|
|
Authentication utilities for training service
|
|
"""
|
|
|
|
import httpx
|
|
from fastapi import HTTPException, status, Depends
|
|
from fastapi.security import HTTPBearer
|
|
import structlog
|
|
|
|
from app.core.config import settings
|
|
|
|
logger = structlog.get_logger()
|
|
|
|
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"
|
|
) |