diff --git a/backend/app/routes/auth.py b/backend/app/routes/auth.py index ec4fafd..55a90de 100644 --- a/backend/app/routes/auth.py +++ b/backend/app/routes/auth.py @@ -41,7 +41,7 @@ def callback( code: str = Query(..., description="Authorization code from IdP"), state: str = Query(..., description="CSRF state parameter"), ) -> Response: - target_url = "/birthday/" + target_url = "/birthday/admin" response = RedirectResponse(url=target_url, status_code=status.HTTP_302_FOUND) adapter.handle_callback( code=code, @@ -62,7 +62,11 @@ def callback( summary="Logout user session (API)", description="Revokes the session on server/storage, clears browser cookie (Max-Age=0), and returns JSON confirmation.", ) -def logout(request: Request, adapter: AuthAdapterDep) -> Response: +def logout( + request: Request, + adapter: AuthAdapterDep, + redirect_uri: Optional[str] = Query(None, description="Optional post-logout redirect URI"), +) -> Response: accept_header = request.headers.get("accept", "") if request.method == "POST" or "application/json" in accept_header: json_resp = JSONResponse( @@ -71,7 +75,9 @@ def logout(request: Request, adapter: AuthAdapterDep) -> Response: ) return adapter.logout(request, response=json_resp) - return adapter.logout(request) + target = redirect_uri or "/birthday/admin?logged_out=1" + redirect_resp = RedirectResponse(url=target, status_code=status.HTTP_302_FOUND) + return adapter.logout(request, response=redirect_resp) @router.get( diff --git a/backend/tests/test_browser_sso.py b/backend/tests/test_browser_sso.py new file mode 100644 index 0000000..c54b6a0 --- /dev/null +++ b/backend/tests/test_browser_sso.py @@ -0,0 +1,156 @@ +"""Automated Browser Acceptance Tests for Single Sign-On (SSO) Flow via Playwright. + +Verifies Checkpoint 7 (CP7) requirements: +1. Browser navigates to /birthday/admin -> redirected to OIDC login form. +2. Form submitted with admin@example.com / password123. +3. Browser redirects back through callback -> receives HttpOnly cookie -> arrives on Admin Dashboard. +4. Admin Dashboard renders RSVPs table and authenticated user information. +5. Clicking Logout button revokes session -> returns to unauthenticated state. +""" + +import json +import threading +import time +from typing import Generator + +import httpx +import pytest +import uvicorn +from playwright.sync_api import BrowserContext, Page, sync_playwright + +from app.config import settings +from app.database import Base, SessionLocal, engine +from app.models import RSVP +from app.services.config import seed_default_config + + +@pytest.fixture(scope="module") +def live_server() -> Generator[str, None, None]: + """Start uvicorn server running FastAPI application in background daemon thread.""" + # Ensure fresh DB tables and seed data + Base.metadata.create_all(bind=engine) + with SessionLocal() as db: + seed_default_config(db) + # Seed an RSVP item to guarantee the table renders records + test_rsvp = db.query(RSVP).filter_by(phone="3009998877").first() + if not test_rsvp: + test_rsvp = RSVP( + name="Invitado de Prueba", + phone="3009998877", + email="invitado@test.com", + ) + db.add(test_rsvp) + db.commit() + + config = uvicorn.Config( + "app.main:app", + host="127.0.0.1", + port=settings.APP_PORT, + log_level="warning", + ) + server = uvicorn.Server(config) + thread = threading.Thread(target=server.run, daemon=True) + thread.start() + + base_url = f"http://localhost:{settings.APP_PORT}" + + # Poll until server is responding + ready = False + for _ in range(50): + try: + with httpx.Client() as client: + res = client.get(f"{base_url}/birthday/api/config", timeout=1.0) + if res.status_code == 200: + ready = True + break + except Exception: + time.sleep(0.1) + + assert ready, f"Live server failed to start on {base_url}" + + yield base_url + + server.should_exit = True + thread.join(timeout=3.0) + + +@pytest.fixture(scope="module") +def browser_context(): + """Launch headless Chromium browser and provide an isolated context.""" + with sync_playwright() as p: + browser = p.chromium.launch(headless=True) + context = browser.new_context() + yield context + context.close() + browser.close() + + +def test_browser_sso_complete_flow(live_server: str, browser_context: BrowserContext): + """Execute complete end-to-end interactive browser authentication flow.""" + page: Page = browser_context.new_page() + + # 1. Unauthenticated navigation to /birthday/admin initiates login redirect + page.goto(f"{live_server}/birthday/admin") + + # 2. Browser is redirected to OIDC provider login page + page.wait_for_url(lambda u: "8088" in u and "/authorize" in u, timeout=10000) + assert "8088" in page.url + + # 3. Submit credentials on Mock OAuth2 Server login form + page.wait_for_selector("input[name='username']", timeout=5000) + page.fill("input[name='username']", "admin@example.com") + + if page.locator("input[type='password']").count() > 0: + page.fill("input[type='password']", "password123") + + if page.locator("textarea[name='claims']").count() > 0: + page.fill( + "textarea[name='claims']", + json.dumps({ + "sub": "admin-001", + "email": "admin@example.com", + "name": "Admin User", + "groups": ["bdinvite_admins"], + }), + ) + + page.click("input[type='submit']") + + # 4. Redirects through callback -> receives HttpOnly cookie -> arrives on Admin Dashboard + page.wait_for_url(lambda u: "/birthday/admin" in u and "8088" not in u, timeout=10000) + assert "/birthday/admin" in page.url + + # Verify HttpOnly session cookie + cookies = browser_context.cookies("http://localhost:8000") + session_cookies = [c for c in cookies if c.get("name") == settings.SESSION_COOKIE_NAME] + assert len(session_cookies) >= 1, f"Missing {settings.SESSION_COOKIE_NAME} cookie" + session_cookie = session_cookies[0] + assert session_cookie.get("httpOnly") is True, "Session cookie must be HttpOnly" + same_site = session_cookie.get("sameSite") + assert same_site is not None and same_site.lower() in ["lax", "strict"] + + # 5. Admin navigation header displays authenticated user info and working Logout button + page.wait_for_selector("[data-testid='user-info']", timeout=5000) + user_info_text = page.locator("[data-testid='user-info']").inner_text() + assert "admin@example.com" in user_info_text or "Admin User" in user_info_text + + # 6. Admin Dashboard renders RSVPs table with live data + page.wait_for_selector("table", timeout=5000) + assert page.locator("table").is_visible() + # Confirm seeded attendee appears in the table + page.wait_for_selector("text=Invitado de Prueba", timeout=5000) + assert page.locator("text=Invitado de Prueba").is_visible() + + # 7. Clicking "Logout" button revokes session and returns UI to unauthenticated state + logout_btn = page.locator("[data-testid='logout-btn']") + assert logout_btn.is_visible() + logout_btn.click() + + # UI displays unauthenticated state + page.wait_for_selector("[data-testid='unauthenticated-state']", timeout=5000) + assert page.locator("[data-testid='unauthenticated-state']").is_visible() + + # RSVPs table is no longer displayed + assert page.locator("table").count() == 0 + + page.close() diff --git a/frontend/package.json b/frontend/package.json index 08fb858..401b64b 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "dev": "vite", - "build": "tsc -b && vite build", + "build": "tsc -b && vite build && cp -r dist/* ../backend/static/", "preview": "vite preview", "lint": "biome check .", "lint:fix": "biome check --write .", diff --git a/frontend/src/admin/AdminLayout.tsx b/frontend/src/admin/AdminLayout.tsx index 5243210..9c1e299 100644 --- a/frontend/src/admin/AdminLayout.tsx +++ b/frontend/src/admin/AdminLayout.tsx @@ -1,6 +1,161 @@ +import { useCallback, useEffect, useState } from "react"; import { Link, NavLink, Outlet } from "react-router-dom"; +import { type AuthUser, fetchSession, logoutUser } from "../api/client"; export function AdminLayout() { + const [loading, setLoading] = useState(true); + const [identity, setIdentity] = useState(null); + const [loggedOut, setLoggedOut] = useState(false); + + useEffect(() => { + const params = new URLSearchParams(window.location.search); + if (params.get("logged_out") === "1") { + setLoading(false); + setLoggedOut(true); + return; + } + + let isMounted = true; + fetchSession() + .then((session) => { + if (!isMounted) return; + if (session.authenticated && session.identity) { + setIdentity(session.identity); + setLoading(false); + } else { + // Unauthenticated user navigating to /birthday/admin initiates login flow + window.location.href = "/birthday/api/auth/login"; + } + }) + .catch(() => { + if (!isMounted) return; + window.location.href = "/birthday/api/auth/login"; + }); + + return () => { + isMounted = false; + }; + }, []); + + const handleLogout = useCallback(async () => { + try { + await logoutUser(); + } catch (err) { + console.error("Logout error:", err); + } + setIdentity(null); + setLoggedOut(true); + window.history.replaceState(null, "", "/birthday/admin?logged_out=1"); + }, []); + + if (loading) { + return ( +
+ + Cargando panel de control... + +
+ ); + } + + if (loggedOut || !identity) { + return ( +
+
+
+ 🎂 +

+ Panel de Control · Invitación +

+
+ + Iniciar Sesión + +
+ +
+
🔒
+

+ Sesión Finalizada +

+

+ Has cerrado sesión correctamente. No hay sesión autenticada activa. +

+ + Iniciar Sesión + +
+
+ ); + } + return (
-