Added cognito authentication - Fix 7
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m14s

This commit is contained in:
2025-05-15 16:42:42 -05:00
parent 47befceb17
commit ae040fc49e
2 changed files with 60 additions and 46 deletions

View File

@@ -1,17 +1,25 @@
from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi import FastAPI, Depends, HTTPException, Request, Response
from fastapi.responses import RedirectResponse, JSONResponse
from app.cabletv.utils.auth import get_current_user, exchange_code_for_token
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
app = FastAPI()
@app.get("/")
async def root():
return {"message": "IPTV Updater API"}
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add session middleware
app.add_middleware(SessionMiddleware, secret_key="your-secret-key")
@app.get("/protected")
async def protected_route(request: Request, user = Depends(get_current_user)):
if isinstance(user, RedirectResponse):
return user
return {"message": "Protected content", "user": user['Username']}
@app.get("/auth/callback")
@@ -20,11 +28,17 @@ async def auth_callback(request: Request, code: str):
redirect_uri = str(request.base_url)
tokens = exchange_code_for_token(code, redirect_uri)
response = JSONResponse(content={
"message": "Authentication successful",
"id_token": tokens["id_token"]
})
# For browser requests, redirect to protected page
is_browser = "text/html" in request.headers.get("accept", "")
if is_browser:
response = RedirectResponse(url="/protected")
else:
response = JSONResponse(content={
"message": "Authentication successful",
"id_token": tokens["id_token"]
})
# Set the token cookie
response.set_cookie(
key="token",
value=tokens["id_token"],