Authenticate sockets by header or one-time ticket instead of ?token=
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013GAeg4Jo6xjZzjwy9QDWMQ
This commit is contained in:
@@ -27,15 +27,15 @@ BACKOFF = (1.0, 5.0, 30.0, 60.0)
|
||||
POLL_S = 1.0
|
||||
|
||||
|
||||
def socket_url(url: str, token: str) -> str:
|
||||
def socket_url(url: str) -> str:
|
||||
"""The websocket beside an engine's HTTP address.
|
||||
|
||||
The token goes in the query string because a handshake carries no headers
|
||||
of its own — the same reason the browser sends it that way.
|
||||
The token is not in it: it goes as a header on the handshake, since a URL
|
||||
ends up in access logs.
|
||||
"""
|
||||
parts = urlsplit(url)
|
||||
scheme = "wss" if parts.scheme == "https" else "ws"
|
||||
return urlunsplit((scheme, parts.netloc, "/api/v1/flows/ws", f"token={token}", ""))
|
||||
return urlunsplit((scheme, parts.netloc, "/api/v1/flows/ws", "", ""))
|
||||
|
||||
|
||||
def subscribe(
|
||||
@@ -61,7 +61,11 @@ def subscribe(
|
||||
attempt = 0
|
||||
while not stop.is_set():
|
||||
try:
|
||||
with connect(socket_url(url, token), open_timeout=POLL_S * 5) as socket:
|
||||
with connect(
|
||||
socket_url(url),
|
||||
additional_headers={"Authorization": f"Bearer {token}"},
|
||||
open_timeout=POLL_S * 5,
|
||||
) as socket:
|
||||
attempt = 0
|
||||
while not stop.is_set():
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user