Files
iptv-manager-service/app/main.py
Stefano 795a25961f
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m11s
Added cognito authentication - Fix 11
2025-05-15 17:23:52 -05:00

41 lines
1.3 KiB
Python

from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi.responses import JSONResponse, RedirectResponse
from app.cabletv.utils.auth import exchange_code_for_token, get_current_user, DOMAIN, CLIENT_ID
app = FastAPI()
@app.get("/")
async def root():
return {"message": "IPTV Updater API"}
@app.get("/protected")
async def protected_route(user = Depends(get_current_user)):
if isinstance(user, RedirectResponse):
return 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) + "auth/callback"
tokens = exchange_code_for_token(code, redirect_uri)
response = JSONResponse(content={
"message": "Authentication successful",
"id_token": tokens["id_token"] # Changed from access_token
})
response.set_cookie(
key="token",
value=tokens["id_token"], # Changed from access_token
httponly=True,
secure=True,
samesite="lax"
)
return response
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Authentication failed: {str(e)}"
)