Send go2rtc credentials as an Authorization header (#179838)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Paulus Schoutsen
2026-08-22 10:32:27 -05:00
committed by GitHub
co-authored by Claude
parent d7fba1b54e
commit cdc96c07da
2 changed files with 21 additions and 27 deletions
+5 -6
View File
@@ -7,7 +7,7 @@ import shutil
from tempfile import mkdtemp
from typing import override
from aiohttp import BasicAuth, ClientSession, UnixConnector
from aiohttp import ClientSession, UnixConnector, encode_basic_auth
from aiohttp.client_exceptions import ClientConnectionError, ServerConnectionError
from awesomeversion import AwesomeVersion
from go2rtc_client import Go2RtcRestClient
@@ -153,14 +153,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
password = token_hex()
_LOGGER.debug("Generated random credentials for go2rtc server")
auth = BasicAuth(username, password)
# HA will manage the binary
temp_dir = mkdtemp(prefix="go2rtc-")
# Manually created session (not using the helper) needs to be closed manually
# See on_stop listener below
session = ClientSession(
connector=UnixConnector(path=get_go2rtc_unix_socket_path(temp_dir)),
auth=auth,
headers={"Authorization": encode_basic_auth(username, password)},
)
server = Server(
hass,
@@ -186,9 +185,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
url = HA_MANAGED_URL
elif username and password:
# Create session with BasicAuth if credentials are provided
auth = BasicAuth(username, password)
session = async_create_clientsession(hass, auth=auth)
session = async_create_clientsession(
hass, headers={"Authorization": encode_basic_auth(username, password)}
)
else:
session = async_get_clientsession(hass)
+16 -21
View File
@@ -7,7 +7,7 @@ from pathlib import Path
from typing import NamedTuple
from unittest.mock import ANY, AsyncMock, Mock, patch
from aiohttp import BasicAuth, UnixConnector
from aiohttp import UnixConnector, encode_basic_auth
from aiohttp.client_exceptions import ClientConnectionError, ServerConnectionError
from awesomeversion import AwesomeVersion
from go2rtc_client import Stream
@@ -1267,12 +1267,11 @@ async def test_unix_socket_connection(hass: HomeAssistant, server_dir: Path) ->
assert isinstance(connector, UnixConnector)
assert connector.path == get_go2rtc_unix_socket_path(server_dir)
# Auth should be auto-generated when credentials are not explicitly configured
assert "auth" in call_kwargs
auth = call_kwargs["auth"]
assert isinstance(auth, BasicAuth)
# Verify auto-generated credentials match our mocked values
assert auth.login == "mock_username_token"
assert auth.password == "mock_password_token"
assert call_kwargs["headers"] == {
"Authorization": encode_basic_auth(
"mock_username_token", "mock_password_token"
)
}
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
@@ -1300,7 +1299,7 @@ async def test_unix_socket_not_used_for_custom_server(hass: HomeAssistant) -> No
@pytest.mark.usefixtures("rest_client", "server")
async def test_basic_auth_with_custom_url(hass: HomeAssistant) -> None:
"""Test BasicAuth session is created with username/password and URL."""
"""Test an auth header session is created with username/password and URL."""
config = {
DOMAIN: {
CONF_URL: "http://localhost:1984/",
@@ -1318,19 +1317,17 @@ async def test_basic_auth_with_custom_url(hass: HomeAssistant) -> None:
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done(wait_background_tasks=True)
# Verify async_create_clientsession was called with BasicAuth
# Verify async_create_clientsession was called with an auth header
mock_create_session.assert_called_once()
call_kwargs = mock_create_session.call_args[1]
assert "auth" in call_kwargs
auth = call_kwargs["auth"]
assert isinstance(auth, BasicAuth)
assert auth.login == "test_user"
assert auth.password == "test_pass"
assert call_kwargs["headers"] == {
"Authorization": encode_basic_auth("test_user", "test_pass")
}
@pytest.mark.usefixtures("rest_client")
async def test_basic_auth_with_debug_ui(hass: HomeAssistant, server_dir: Path) -> None:
"""Test BasicAuth session created with username/password and debug_ui."""
"""Test an auth header session is created with username/password and debug_ui."""
config = {
DOMAIN: {
CONF_DEBUG_UI: True,
@@ -1361,18 +1358,16 @@ async def test_basic_auth_with_debug_ui(hass: HomeAssistant, server_dir: Path) -
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done(wait_background_tasks=True)
# Verify ClientSession was created with BasicAuth and UnixConnector
# Verify ClientSession was created with an auth header and UnixConnector
mock_session_cls.assert_called_once()
call_kwargs = mock_session_cls.call_args[1]
assert "connector" in call_kwargs
connector = call_kwargs["connector"]
assert isinstance(connector, UnixConnector)
assert connector.path == get_go2rtc_unix_socket_path(server_dir)
assert "auth" in call_kwargs
auth = call_kwargs["auth"]
assert isinstance(auth, BasicAuth)
assert auth.login == "test_user"
assert auth.password == "test_pass"
assert call_kwargs["headers"] == {
"Authorization": encode_basic_auth("test_user", "test_pass")
}
# Verify Server was called with username and password
mock_server_cls.assert_called_once()