Skip to content

Commit 0b159bd

Browse files
allenporterCopilot
authored andcommitted
Update authorization server to prefer absolute urls (home-assistant#152313)
Co-authored-by: Copilot <[email protected]>
1 parent 8728312 commit 0b159bd

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

homeassistant/components/auth/login_flow.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@
9292
from homeassistant.components.http.data_validator import RequestDataValidator
9393
from homeassistant.components.http.view import HomeAssistantView
9494
from homeassistant.core import HomeAssistant, callback
95-
from homeassistant.helpers.network import is_cloud_connection
95+
from homeassistant.helpers.network import (
96+
NoURLAvailableError,
97+
get_url,
98+
is_cloud_connection,
99+
)
96100
from homeassistant.util.network import is_local
97101

98102
from . import indieauth
@@ -125,11 +129,18 @@ class WellKnownOAuthInfoView(HomeAssistantView):
125129

126130
async def get(self, request: web.Request) -> web.Response:
127131
"""Return the well known OAuth2 authorization info."""
132+
hass = request.app[KEY_HASS]
133+
# Some applications require absolute urls, so we prefer using the
134+
# current requests url if possible, with fallback to a relative url.
135+
try:
136+
url_prefix = get_url(hass, require_current_request=True)
137+
except NoURLAvailableError:
138+
url_prefix = ""
128139
return self.json(
129140
{
130-
"authorization_endpoint": "/auth/authorize",
131-
"token_endpoint": "/auth/token",
132-
"revocation_endpoint": "/auth/revoke",
141+
"authorization_endpoint": f"{url_prefix}/auth/authorize",
142+
"token_endpoint": f"{url_prefix}/auth/token",
143+
"revocation_endpoint": f"{url_prefix}/auth/revoke",
133144
"response_types_supported": ["code"],
134145
"service_documentation": (
135146
"https://developers.home-assistant.io/docs/auth_api"

tests/components/auth/test_login_flow.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
from homeassistant.core import HomeAssistant
10+
from homeassistant.core_config import async_process_ha_core_config
1011

1112
from . import BASE_CONFIG, async_setup_auth
1213

@@ -371,19 +372,54 @@ async def test_login_exist_user_ip_changes(
371372
assert response == {"message": "IP address changed"}
372373

373374

375+
@pytest.mark.usefixtures("current_request_with_host") # Has example.com host
376+
@pytest.mark.parametrize(
377+
("config", "expected_url_prefix"),
378+
[
379+
(
380+
{
381+
"internal_url": "http://192.168.1.100:8123",
382+
# Current request matches external url
383+
"external_url": "https://example.com",
384+
},
385+
"https://example.com",
386+
),
387+
(
388+
{
389+
# Current request matches internal url
390+
"internal_url": "https://example.com",
391+
"external_url": "https://other.com",
392+
},
393+
"https://example.com",
394+
),
395+
(
396+
{
397+
# Current request does not match either url
398+
"internal_url": "https://other.com",
399+
"external_url": "https://again.com",
400+
},
401+
"",
402+
),
403+
],
404+
ids=["external_url", "internal_url", "no_match"],
405+
)
374406
async def test_well_known_auth_info(
375-
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator
407+
hass: HomeAssistant,
408+
aiohttp_client: ClientSessionGenerator,
409+
config: dict[str, str],
410+
expected_url_prefix: str,
376411
) -> None:
377-
"""Test logging in and the ip address changes results in an rejection."""
412+
"""Test the well-known OAuth authorization server endpoint with different URL configurations."""
413+
await async_process_ha_core_config(hass, config)
378414
client = await async_setup_auth(hass, aiohttp_client, setup_api=True)
379415
resp = await client.get(
380416
"/.well-known/oauth-authorization-server",
381417
)
382418
assert resp.status == 200
383419
assert await resp.json() == {
384-
"authorization_endpoint": "/auth/authorize",
385-
"token_endpoint": "/auth/token",
386-
"revocation_endpoint": "/auth/revoke",
420+
"authorization_endpoint": f"{expected_url_prefix}/auth/authorize",
421+
"token_endpoint": f"{expected_url_prefix}/auth/token",
422+
"revocation_endpoint": f"{expected_url_prefix}/auth/revoke",
387423
"response_types_supported": ["code"],
388424
"service_documentation": "https://developers.home-assistant.io/docs/auth_api",
389425
}

0 commit comments

Comments
 (0)