37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
|
|
# ================================================================
|
||
|
|
# services/data/app/core/auth.py
|
||
|
|
# ================================================================
|
||
|
|
"""Authentication utilities for data service"""
|
||
|
|
|
||
|
|
from fastapi import HTTPException, Depends, status
|
||
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||
|
|
import httpx
|
||
|
|
import structlog
|
||
|
|
|
||
|
|
from app.core.config import settings
|
||
|
|
|
||
|
|
logger = structlog.get_logger()
|
||
|
|
security = HTTPBearer()
|
||
|
|
|
||
|
|
async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)) -> dict:
|
||
|
|
"""Verify JWT token with auth service"""
|
||
|
|
try:
|
||
|
|
async with httpx.AsyncClient() as client:
|
||
|
|
response = await client.post(
|
||
|
|
f"{settings.AUTH_SERVICE_URL}/api/v1/auth/verify",
|
||
|
|
headers={"Authorization": f"Bearer {credentials.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:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||
|
|
detail="Authentication service unavailable"
|
||
|
|
)
|