feat: api-tokens pro gerät (backend)
Tokens werden einmal angezeigt und nur gehasht gespeichert. Anmeldung per Authorization: Bearer für Clips, Dateien, Projektliste/-anlage und den Live-Sync (WebSocket ohne Origin). Konto, Admin, Sessions, Tokens und Projektverwaltung bleiben der Browser-Session vorbehalten. Widerruf schließt die Verbindungen des Tokens sofort. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4ae459cf53
commit
d8377c8a04
14 files changed
+520
-32
No files matched your search
+32
-11
@@ -4,10 +4,11 @@ import time
|
||||
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
|
||||
from app.api.deps import SESSION_COOKIE
|
||||
from app.api.deps import SESSION_COOKIE, bearer_token
|
||||
from app.config import get_settings
|
||||
from app.db import _session_factory
|
||||
from app.services.sessions import is_session_valid, resolve_session
|
||||
from app.services.tokens import is_token_valid, resolve_token
|
||||
from app.ws.hub import CLOSE_UNAUTHORIZED, Connection, hub
|
||||
|
||||
router = APIRouter()
|
||||
@@ -18,15 +19,23 @@ REVALIDATE_INTERVAL_SECONDS = 300
|
||||
CLOSE_TOO_MANY = 4429
|
||||
|
||||
|
||||
def _resolve(token: str | None) -> tuple[int, int] | None:
|
||||
def _resolve_session(token: str | None) -> tuple[int, int] | None:
|
||||
with _session_factory()() as db:
|
||||
session = resolve_session(db, token)
|
||||
return (session.id, session.user_id) if session else None
|
||||
|
||||
|
||||
def _still_valid(session_id: int) -> bool:
|
||||
def _resolve_token(plain: str, ip: str | None) -> tuple[int, int] | None:
|
||||
with _session_factory()() as db:
|
||||
return is_session_valid(db, session_id)
|
||||
token = resolve_token(db, plain, ip)
|
||||
return (token.id, token.user_id) if token else None
|
||||
|
||||
|
||||
def _still_valid(conn: Connection) -> bool:
|
||||
with _session_factory()() as db:
|
||||
if conn.token_id is not None:
|
||||
return is_token_valid(db, conn.token_id)
|
||||
return conn.session_id is not None and is_session_valid(db, conn.session_id)
|
||||
|
||||
|
||||
async def _heartbeat(conn: Connection) -> None:
|
||||
@@ -37,7 +46,7 @@ async def _heartbeat(conn: Connection) -> None:
|
||||
await conn.websocket.send_json({"type": "ping"})
|
||||
if time.monotonic() - last_check >= REVALIDATE_INTERVAL_SECONDS:
|
||||
last_check = time.monotonic()
|
||||
if not await run_in_threadpool(_still_valid, conn.session_id):
|
||||
if not await run_in_threadpool(_still_valid, conn):
|
||||
await conn.websocket.close(code=CLOSE_UNAUTHORIZED)
|
||||
return
|
||||
|
||||
@@ -50,24 +59,36 @@ async def _drain(conn: Connection) -> None:
|
||||
|
||||
@router.websocket("/ws")
|
||||
async def websocket_endpoint(websocket: WebSocket) -> None:
|
||||
# Origin check protects the cookie-authenticated socket against cross-site hijacking
|
||||
origin = (websocket.headers.get("origin") or "").rstrip("/")
|
||||
if origin not in get_settings().allowed_origin_list:
|
||||
plain = bearer_token(websocket.headers.get("authorization"))
|
||||
origin = websocket.headers.get("origin")
|
||||
# Origin check protects the cookie-authenticated socket against cross-site hijacking.
|
||||
# The desktop agent sends a token and no Origin; browsers always send Origin and
|
||||
# cannot set an Authorization header on WebSockets.
|
||||
origin_ok = (origin or "").rstrip("/") in get_settings().allowed_origin_list
|
||||
if not origin_ok and not (plain is not None and origin is None):
|
||||
await websocket.close(code=4403) # before accept: handshake is rejected
|
||||
return
|
||||
|
||||
await websocket.accept()
|
||||
# Accept first so the client sees the close code and can redirect to login
|
||||
ids = await run_in_threadpool(_resolve, websocket.cookies.get(SESSION_COOKIE))
|
||||
ip = websocket.client.host if websocket.client else None
|
||||
if plain is not None:
|
||||
ids = await run_in_threadpool(_resolve_token, plain, ip)
|
||||
else:
|
||||
ids = await run_in_threadpool(_resolve_session, websocket.cookies.get(SESSION_COOKIE))
|
||||
if ids is None:
|
||||
await websocket.close(code=CLOSE_UNAUTHORIZED)
|
||||
return
|
||||
session_id, user_id = ids
|
||||
credential_id, user_id = ids
|
||||
if hub.count(user_id) >= MAX_CONNECTIONS_PER_USER:
|
||||
await websocket.close(code=CLOSE_TOO_MANY)
|
||||
return
|
||||
|
||||
conn = Connection(websocket=websocket, user_id=user_id, session_id=session_id)
|
||||
conn = (
|
||||
Connection(websocket=websocket, user_id=user_id, token_id=credential_id)
|
||||
if plain is not None
|
||||
else Connection(websocket=websocket, user_id=user_id, session_id=credential_id)
|
||||
)
|
||||
hub.add(conn)
|
||||
try:
|
||||
await websocket.send_json({"type": "hello"})
|
||||
|
||||
Reference in new issue
Block a user