summaryrefslogtreecommitdiff
path: root/src/ollama_web/routes/auth.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-06-24 22:47:13 +0900
committertanaka takashi <isazaka@gmail.com>2026-06-24 22:47:13 +0900
commite4d421c782a181f421e2987f75b4610845e3ef55 (patch)
treee61b848b32dc046977751346d4346c4debc7c4f8 /src/ollama_web/routes/auth.py
parenta1d744387659f7fe29559017ae03dc76e8532bb1 (diff)
Harden web UI security
Diffstat (limited to 'src/ollama_web/routes/auth.py')
-rw-r--r--src/ollama_web/routes/auth.py32
1 files changed, 32 insertions, 0 deletions
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)})