Utility module for accessing frontend in backend code.

This commit is contained in:
Leo Vasanko
2025-09-02 16:06:10 -06:00
parent dd20e7e7f8
commit 3cd6a59b26
5 changed files with 75 additions and 58 deletions
+7 -19
View File
@@ -1,18 +1,15 @@
import logging
import os
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from passkey.util import passphrase
from passkey.util import frontend, passphrase
from . import admin, api, ws
STATIC_DIR = Path(__file__).parent.parent / "frontend-build"
@asynccontextmanager
async def lifespan(app: FastAPI): # pragma: no cover - startup path
@@ -52,27 +49,18 @@ app = FastAPI(lifespan=lifespan)
app.mount("/auth/admin", admin.app)
app.mount("/auth/api", api.app)
app.mount("/auth/ws", ws.app)
app.mount("/auth/assets", StaticFiles(directory=STATIC_DIR / "assets"), name="assets")
app.mount("/auth/assets", StaticFiles(directory=frontend.file("assets")), name="assets")
@app.get("/auth/")
async def frontend():
async def frontapp():
"""Serve the main authentication app."""
return FileResponse(STATIC_DIR / "index.html")
return FileResponse(frontend.file("index.html"))
@app.get("/auth/{reset}")
async def reset_authentication(request: Request, reset: str):
"""Validate reset token and redirect with it as query parameter (no cookies).
After validation we 303 redirect to /auth/?reset=<token>. The frontend will:
- Read the token from location.search
- Use it via Authorization header or websocket query param
- history.replaceState to remove it from the address bar/history
"""
async def reset_link(request: Request, reset: str):
"""Pretty URL for reset links."""
if not passphrase.is_well_formed(reset):
raise HTTPException(status_code=404)
return RedirectResponse(request.url_for("frontend", reset=reset), status_code=303)
## forward-auth endpoint moved to /auth/api/forward in api.py
return RedirectResponse(request.url_for("frontapp", reset=reset), status_code=303)