Fix websocket authentication

This commit is contained in:
mcarlton00 2026-03-27 17:47:24 -04:00
commit 7a38aad0cc

View file

@ -47,20 +47,34 @@ class WSClient(threading.Thread):
def run(self):
monitor = xbmc.Monitor()
token = self.client.config.data["auth.token"]
device_id = self.client.config.data["app.device_id"]
server = self.client.config.data["auth.server"]
server = (
server.replace("https://", "wss://")
if server.startswith("https")
else server.replace("http://", "ws://")
)
wsc_url = "%s/socket?ApiKey=%s&device_id=%s" % (server, token, device_id)
wsc_url = "%s/socket" % server
# Build authorization header for websocket connection
auth_params = {}
if "app.device_name" in self.client.config.data:
auth_params.update(
{
"Client": self.client.config.data["app.name"],
"Device": self.client.config.data["app.device_name"],
"DeviceId": self.client.config.data["app.device_id"],
"Version": self.client.config.data["app.version"],
}
)
if "auth.token" in self.client.config.data:
auth_params["Token"] = self.client.config.data["auth.token"]
auth_line = ", ".join(f'{k}="{v}"' for k, v in auth_params.items())
ws_headers = {"Authorization": f"MediaBrowser {auth_line}"}
LOG.info("Websocket url: %s", wsc_url)
self.wsc = websocket.WebSocketApp(
wsc_url,
header=ws_headers,
on_open=lambda ws: self.on_open(ws),
on_message=lambda ws, message: self.on_message(ws, message),
on_error=lambda ws, error: self.on_error(ws, error),