161 lines
5.4 KiB
Python
161 lines
5.4 KiB
Python
|
|
"""
|
||
|
|
Authentication routes for gateway
|
||
|
|
"""
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Request, HTTPException
|
||
|
|
from fastapi.responses import JSONResponse
|
||
|
|
import httpx
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from app.core.config import settings
|
||
|
|
from app.core.service_discovery import ServiceDiscovery
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
service_discovery = ServiceDiscovery()
|
||
|
|
|
||
|
|
@router.post("/login")
|
||
|
|
async def login(request: Request):
|
||
|
|
"""Proxy login request to auth service"""
|
||
|
|
try:
|
||
|
|
body = await request.body()
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
||
|
|
response = await client.post(
|
||
|
|
f"{settings.AUTH_SERVICE_URL}/login",
|
||
|
|
content=body,
|
||
|
|
headers={"Content-Type": "application/json"}
|
||
|
|
)
|
||
|
|
|
||
|
|
if response.status_code == 200:
|
||
|
|
return response.json()
|
||
|
|
else:
|
||
|
|
return JSONResponse(
|
||
|
|
status_code=response.status_code,
|
||
|
|
content=response.json()
|
||
|
|
)
|
||
|
|
|
||
|
|
except httpx.RequestError as e:
|
||
|
|
logger.error(f"Auth service unavailable: {e}")
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=503,
|
||
|
|
detail="Authentication service unavailable"
|
||
|
|
)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Login error: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
||
|
|
|
||
|
|
@router.post("/register")
|
||
|
|
async def register(request: Request):
|
||
|
|
"""Proxy register request to auth service"""
|
||
|
|
try:
|
||
|
|
body = await request.body()
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
||
|
|
response = await client.post(
|
||
|
|
f"{settings.AUTH_SERVICE_URL}/register",
|
||
|
|
content=body,
|
||
|
|
headers={"Content-Type": "application/json"}
|
||
|
|
)
|
||
|
|
|
||
|
|
return JSONResponse(
|
||
|
|
status_code=response.status_code,
|
||
|
|
content=response.json()
|
||
|
|
)
|
||
|
|
|
||
|
|
except httpx.RequestError as e:
|
||
|
|
logger.error(f"Auth service unavailable: {e}")
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=503,
|
||
|
|
detail="Authentication service unavailable"
|
||
|
|
)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Register error: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
||
|
|
|
||
|
|
@router.post("/refresh")
|
||
|
|
async def refresh_token(request: Request):
|
||
|
|
"""Proxy refresh token request to auth service"""
|
||
|
|
try:
|
||
|
|
body = await request.body()
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
||
|
|
response = await client.post(
|
||
|
|
f"{settings.AUTH_SERVICE_URL}/refresh",
|
||
|
|
content=body,
|
||
|
|
headers={"Content-Type": "application/json"}
|
||
|
|
)
|
||
|
|
|
||
|
|
return JSONResponse(
|
||
|
|
status_code=response.status_code,
|
||
|
|
content=response.json()
|
||
|
|
)
|
||
|
|
|
||
|
|
except httpx.RequestError as e:
|
||
|
|
logger.error(f"Auth service unavailable: {e}")
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=503,
|
||
|
|
detail="Authentication service unavailable"
|
||
|
|
)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Refresh token error: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
||
|
|
|
||
|
|
@router.post("/verify")
|
||
|
|
async def verify_token(request: Request):
|
||
|
|
"""Proxy token verification to auth service"""
|
||
|
|
try:
|
||
|
|
auth_header = request.headers.get("Authorization")
|
||
|
|
if not auth_header:
|
||
|
|
raise HTTPException(status_code=401, detail="Authorization header required")
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=5.0) as client:
|
||
|
|
response = await client.post(
|
||
|
|
f"{settings.AUTH_SERVICE_URL}/verify",
|
||
|
|
headers={"Authorization": auth_header}
|
||
|
|
)
|
||
|
|
|
||
|
|
return JSONResponse(
|
||
|
|
status_code=response.status_code,
|
||
|
|
content=response.json()
|
||
|
|
)
|
||
|
|
|
||
|
|
except httpx.RequestError as e:
|
||
|
|
logger.error(f"Auth service unavailable: {e}")
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=503,
|
||
|
|
detail="Authentication service unavailable"
|
||
|
|
)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Token verification error: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
||
|
|
|
||
|
|
@router.post("/logout")
|
||
|
|
async def logout(request: Request):
|
||
|
|
"""Proxy logout request to auth service"""
|
||
|
|
try:
|
||
|
|
auth_header = request.headers.get("Authorization")
|
||
|
|
if not auth_header:
|
||
|
|
raise HTTPException(status_code=401, detail="Authorization header required")
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=5.0) as client:
|
||
|
|
response = await client.post(
|
||
|
|
f"{settings.AUTH_SERVICE_URL}/logout",
|
||
|
|
headers={"Authorization": auth_header}
|
||
|
|
)
|
||
|
|
|
||
|
|
return JSONResponse(
|
||
|
|
status_code=response.status_code,
|
||
|
|
content=response.json()
|
||
|
|
)
|
||
|
|
|
||
|
|
except httpx.RequestError as e:
|
||
|
|
logger.error(f"Auth service unavailable: {e}")
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=503,
|
||
|
|
detail="Authentication service unavailable"
|
||
|
|
)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Logout error: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|