PotionUI Docs v0.3.0

WebSocket authentication

authenticate_websocket(websocket) authenticates a plugin WebSocket by hand, before accept(). It reads a single-use ?ticket= (minted by POST /api/auth/ws-ticket, the browser path), an Authorization: Bearer header (a session token or an API key), and, for plugins written against v0.1.0, a session token in ?token=. The order is ticket, header, then ?token=. A key in ?token= is always refused. Declare what an API key needs on the route with key_scope.

python
@ws_router.websocket("/ws/plugins/my-plugin", dependencies=[key_scope(ApiScope.READ)])
async def endpoint(websocket: WebSocket):
    user, error = await authenticate_websocket(websocket)
    if user is None:
        await websocket.accept()
        await websocket.close(code=4001, reason=error or "Authentication failed")
        return
    await websocket.accept()

authenticate_websocket_token(token) is the v0.1.0 helper and keeps its signature and behaviour: it takes the bare session token you read from ?token= and returns (user, error) synchronously. It is deprecated; it never accepts a ticket, a header or an API key. A ?token= connection logs one deprecation line per process with the token redacted.