refactor(connect): one live connection, no token in any address (SSE removed)

Plan step 4.0. Owner (2026-11-17): no phone in use needs to keep working, so no
compatibility path.
- Removed the SSE channel: routers/sse.py (/api/sse/stream, which still accepted a JWT whose
  session had ended — a signed-out phone could keep listening), services/sse_bus.py (every
  backend module now calls services/ws_bus.broadcast_sync directly), waiter useSSE.js.
- Credentials only in the first frame: the waiter's useWebSocket and the manager's
  LiveProvider send {token} there like the terminal; routers/ws.py no longer reads ?token=
  (a token in a URL ends up in proxy and server logs).
- Waiter: SSEContext → LiveContext (LiveProvider, useLiveContext, liveAlive, the
  'live-event' / 'live-reconnected' window events), dead SSE compat code removed.
- Tests: a token in the address is refused, /api/sse/stream is 404; e2e browser (3 delivery
  modes) and manager assert no token in the live connection's address. Android taps now
  wait for their element (the fixed sleeps made the suite flaky, KI-009).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 10:49:21 +02:00
co-authored by Claude Opus 5.5
parent d0c6a10e47
commit f85894a72e
43 changed files with 112 additions and 350 deletions
+10 -10
View File
@@ -2,12 +2,12 @@
WebSocket endpoint — one persistent connection per client (waiter app, manager, terminal).
Protocol:
1. Client connects: ws://host/api/ws/connect (terminal: no query)
or: ws://host/api/ws/connect?token=<jwt> (waiter, manager)
1. Client connects: ws://host/api/ws/connect (no query: a token in a URL ends up in
proxy and server logs; the `?token=` form was removed in phase 4.0)
2. Client immediately sends its first frame:
{ "cursor": <last_seq_id>, "token"?: <jwt>, "device"?: <device token> }
(cursor 0 = first connect / no history). Credentials in the frame stay out of URLs
and proxy logs; the terminal always sends them there.
(cursor 0 = first connect / no history). Every client (waiter, manager, terminal)
sends its credentials here.
3. Server checks who it is (below). Refused → { "type": "refused", "data": {code} }
and close code 4001 (sign in again) or 4003 (device problem).
4. Server replays missed events the socket may see (seq > cursor) as individual frames
@@ -38,7 +38,7 @@ import asyncio
import json
import logging
from fastapi import Depends, APIRouter, WebSocket, WebSocketDisconnect, Query
from fastapi import Depends, APIRouter, WebSocket, WebSocketDisconnect
from starlette.websockets import WebSocketState
from core.errors import XeniaError
@@ -102,21 +102,21 @@ def authenticate(db, token: str | None, device_token: str | None) -> dict:
@router.websocket("/connect", dependencies=[Depends(public())])
async def ws_endpoint(websocket: WebSocket, token: str | None = Query(None)):
async def ws_endpoint(websocket: WebSocket):
await websocket.accept()
# ── First frame: cursor (+ credentials) ───────────────────────────────────
cursor, device_token = 0, None
# ── First frame: cursor + credentials ─────────────────────────────────────
cursor, token, device_token = 0, None, None
try:
msg = json.loads(await asyncio.wait_for(websocket.receive_text(), timeout=FIRST_FRAME_TIMEOUT))
if isinstance(msg, dict):
cursor = int(msg.get("cursor") or 0)
token = msg.get("token") or token
token = msg.get("token") or None
device_token = msg.get("device") or None
except WebSocketDisconnect:
return
except Exception:
pass # no (valid) first frame: fresh connect, credentials from the query only
pass # no (valid) first frame: no credentials → refused below
# ── Auth ──────────────────────────────────────────────────────────────────
try: