Files
iptv-manager-service/app/main.py
Stefano 30ccf86c86
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m13s
Added cognito authentication - Fix 8
2025-05-15 16:52:54 -05:00

38 lines
1.2 KiB
Python

from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi.responses import RedirectResponse
from app.cabletv.utils.auth import get_current_user, exchange_code_for_token
app = FastAPI()
@app.get("/")
async def root():
return {"message": "IPTV Updater API"}
@app.get("/protected")
async def protected_route(request: Request, user = Depends(get_current_user)):
return {"message": "Protected content", "user": user['Username']}
@app.get("/auth/callback")
async def auth_callback(request: Request, code: str):
try:
redirect_uri = str(request.base_url)
tokens = exchange_code_for_token(code, redirect_uri)
# Create redirect response to protected route
response = RedirectResponse(url="/protected", status_code=302)
# Set token cookie
response.set_cookie(
key="token",
value=tokens["id_token"],
httponly=True,
secure=True,
samesite="lax"
)
return response
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Authentication failed: {str(e)}"
)