home-assistant / home-assistant-js-websocket

:aerial_tramway: JavaScript websocket client for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent Behavior with Trailing Slash in URL During Authentication

kirianguiller opened this issue · comments

Description

I've encountered inconsistent behavior in the library when a trailing slash is present in the URL during the authentication process. Specifically, the behavior differs depending on the authentication method used.

Issue Details

In all following explanation, hass_url is the machine hass url and port

  • With Normal Authentication: The library functions correctly both with and without a trailing slash in the URL. For example, http://hass_url and http://hass_url/ both work as expected.
  • With Long Lived Tokens: The library fails to authenticate when the URL includes a trailing slash. For example, http://hass_url/ does not work (further connection returns 404 error code), whereas http://hass_url works correctly.

Steps to Reproduce

  1. Use the normal authentication method with the URL http://hass_url/ with trailing slash – Observe that it succeeds.
  2. Use the normal authentication method with the URL http://hass_url without trailing slash – Observe that it succeeds.
  3. Use authentication with long-lived tokens with the URL http://hass_url/ with trailing slash – Observe that it fails.
  4. Use authentication with long-lived tokens with the URL http://hass_url without trailing slash – Observe that it succeeds.

Code :

// shared variables
const HASS_URL_WITHOUT_TRAILING_SLASH =  "your_hass_url";
const HASS_URL_WITH_TRAILING_SLASH = "yout_hass_url/";
let auth;

// 1 and 2
auth = await getAuth({hassUrl: HASS_URL_WITH_TRAILING_SLASH}); // further connection will works
auth = await getAuth({hassUrl: HASS_URL_WITHOUT_TRAILING_SLASH}); // further connection will works

// 3 and 4
const ACCESS_TOKEN = "your_access_token";
auth = createLongLivedTokenAuth(HASS_URL_WITH_TRAILING_SLASH, ACCESS_TOKEN);  // further connection will NOT works
auth = createLongLivedTokenAuth(HASS_URL_WITHOUT_TRAILING_SLASH, ACCESS_TOKEN);  // further connection will works

// shared further connection
const conn = await createConnection({ auth: auth });

Expected Behavior

The library should consistently handle URLs with or without a trailing slash in both authentication methods.

Actual Behavior

The library only fails with a trailing slash in the URL when using authentication with long-lived tokens (further connection returns 404 error code).

Environment

Library version: 8.2.0
Environment/OS: Ubuntu 23.04

Thanks a lot for your nice lib :)