Take auth/ whole from boilerplate 09e144d: the lockout claimed before the scrypt, APP_PASSWORD recovery closing every session, Starlette's cookie parser, the rightmost forwarded hop in the log, revoke_all on a password change, a 421 for a non-loopback Host on an open app, API tokens in auth_tokens behind Authorization: Bearer, and sockets closed with their credential

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-10-27 11:51:21 +01:00
1 parent 83e965dfeb
commit 244e57ac29
19 files changed
+2288 -214

No files matched your search

+44 -5
View File
@@ -5,7 +5,8 @@ one, so its tests are not allowed to reach for `app/` or `pipeline/` either --
`tests/test_coupling.py` reads the imports of every file under this folder and
says so. What they need is a FastAPI app with a page, an API route, a socket,
and a couple of neighbours to prove the open list is matched the way it claims.
That is this file, and it is nine routes.
That is this file, and it is nine routes and a stand-in for the broadcaster
the socket subscribes to.
A module rather than a conftest fixture because two tests build the same
database twice, to see what a restart does, and a fixture cannot be asked for
@@ -15,11 +16,32 @@ twice in one test.
from __future__ import annotations
import os
from collections.abc import Iterable
from fastapi import FastAPI, WebSocket
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.responses import PlainTextResponse
from auth import AuthConfig, AuthStore, connect
from auth import AuthConfig, AuthStore, confirm_current, connect
class Bus:
"""What `auth.close_ended_sockets` reads off `app.state.bus`: the open
sockets and a way to close some. A stand-in for `pipeline.web.broadcaster.
Broadcaster`, which this folder may not import, with its two members."""
def __init__(self) -> None:
self.held: set[WebSocket] = set()
@property
def sockets(self) -> tuple[WebSocket, ...]:
return tuple(self.held)
async def close(self, sockets: Iterable[WebSocket], code: int, reason: str = "") -> int:
gone = [ws for ws in sockets if ws in self.held]
for ws in gone:
self.held.discard(ws)
await ws.close(code, reason)
return len(gone)
def build(db: str | os.PathLike[str], *, host: str = "0.0.0.0",
@@ -28,6 +50,7 @@ def build(db: str | os.PathLike[str], *, host: str = "0.0.0.0",
app = FastAPI()
connect(app, AuthStore(db), AuthConfig(**settings), host=host,
open=("/static/",), stylesheet="/static/app.css")
app.state.bus = bus = Bus()
@app.get("/", response_class=PlainTextResponse)
def front() -> str:
@@ -41,6 +64,13 @@ def build(db: str | os.PathLike[str], *, host: str = "0.0.0.0",
def thing() -> dict[str, bool]:
return {"ok": True}
@app.post("/thing/replace")
def replace(request: Request) -> dict[str, bool]:
"""A route that swaps the database as a file would: the current
password first, in its header, before any body."""
confirm_current(request)
return {"ok": True}
@app.get("/health")
def health() -> dict[str, bool]:
return {"ok": True}
@@ -61,8 +91,17 @@ def build(db: str | os.PathLike[str], *, host: str = "0.0.0.0",
@app.websocket("/ws/jobs")
async def socket(ws: WebSocket) -> None:
"""Held open until the client leaves or the bus closes it, as the
app's `/ws/jobs` is."""
await ws.accept()
await ws.send_json({"type": "jobs"})
await ws.close()
bus.held.add(ws)
try:
await ws.send_json({"type": "jobs"})
while True:
await ws.receive_text()
except WebSocketDisconnect:
pass
finally:
bus.held.discard(ws)
return app