From e4d421c782a181f421e2987f75b4610845e3ef55 Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Wed, 24 Jun 2026 22:47:13 +0900 Subject: Harden web UI security --- src/ollama_web/routes/auth.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/ollama_web/routes/auth.py (limited to 'src/ollama_web/routes/auth.py') diff --git a/src/ollama_web/routes/auth.py b/src/ollama_web/routes/auth.py new file mode 100644 index 0000000..808c2ec --- /dev/null +++ b/src/ollama_web/routes/auth.py @@ -0,0 +1,32 @@ +"""Authentication routes.""" + +from __future__ import annotations + +from starlette.requests import Request +from starlette.responses import JSONResponse + +from ..auth import clear_auth_cookies, is_authenticated, set_auth_cookies + + +async def login(request: Request) -> JSONResponse: + try: + body = await request.json() + except Exception: # noqa: BLE001 + body = {} + pin = str(body.get("pin", "")) + if pin != request.app.state.settings.pin: + return JSONResponse({"authenticated": False, "error": "Invalid PIN"}, status_code=401) + response = JSONResponse({"authenticated": True}) + csrf = set_auth_cookies(response, request.app.state.settings.secret_key) + response.headers["X-CSRF-Token"] = csrf + return response + + +async def logout(request: Request) -> JSONResponse: + response = JSONResponse({"authenticated": False}) + clear_auth_cookies(response) + return response + + +async def status(request: Request) -> JSONResponse: + return JSONResponse({"authenticated": is_authenticated(request)}) -- cgit v1.2.3