Skip to content

Commit 1237010

Browse files
authored
auth: add required issuer to OAuth (home-assistant#152385)
1 parent 26fec2f commit 1237010

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

homeassistant/components/auth/login_flow.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,22 @@ async def get(self, request: web.Request) -> web.Response:
136136
url_prefix = get_url(hass, require_current_request=True)
137137
except NoURLAvailableError:
138138
url_prefix = ""
139-
return self.json(
140-
{
141-
"authorization_endpoint": f"{url_prefix}/auth/authorize",
142-
"token_endpoint": f"{url_prefix}/auth/token",
143-
"revocation_endpoint": f"{url_prefix}/auth/revoke",
144-
"response_types_supported": ["code"],
145-
"service_documentation": (
146-
"https://developers.home-assistant.io/docs/auth_api"
147-
),
148-
}
149-
)
139+
140+
metadata = {
141+
"authorization_endpoint": f"{url_prefix}/auth/authorize",
142+
"token_endpoint": f"{url_prefix}/auth/token",
143+
"revocation_endpoint": f"{url_prefix}/auth/revoke",
144+
"response_types_supported": ["code"],
145+
"service_documentation": (
146+
"https://developers.home-assistant.io/docs/auth_api"
147+
),
148+
}
149+
150+
# Add issuer only when we have a valid base URL (RFC 8414 compliance)
151+
if url_prefix:
152+
metadata["issuer"] = url_prefix
153+
154+
return self.json(metadata)
150155

151156

152157
class AuthProvidersView(HomeAssistantView):

tests/components/auth/test_login_flow.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ async def test_login_exist_user_ip_changes(
374374

375375
@pytest.mark.usefixtures("current_request_with_host") # Has example.com host
376376
@pytest.mark.parametrize(
377-
("config", "expected_url_prefix"),
377+
("config", "expected_url_prefix", "extra_response_data"),
378378
[
379379
(
380380
{
@@ -383,6 +383,7 @@ async def test_login_exist_user_ip_changes(
383383
"external_url": "https://example.com",
384384
},
385385
"https://example.com",
386+
{"issuer": "https://example.com"},
386387
),
387388
(
388389
{
@@ -391,6 +392,7 @@ async def test_login_exist_user_ip_changes(
391392
"external_url": "https://other.com",
392393
},
393394
"https://example.com",
395+
{"issuer": "https://example.com"},
394396
),
395397
(
396398
{
@@ -399,6 +401,7 @@ async def test_login_exist_user_ip_changes(
399401
"external_url": "https://again.com",
400402
},
401403
"",
404+
{},
402405
),
403406
],
404407
ids=["external_url", "internal_url", "no_match"],
@@ -408,6 +411,7 @@ async def test_well_known_auth_info(
408411
aiohttp_client: ClientSessionGenerator,
409412
config: dict[str, str],
410413
expected_url_prefix: str,
414+
extra_response_data: dict[str, str],
411415
) -> None:
412416
"""Test the well-known OAuth authorization server endpoint with different URL configurations."""
413417
await async_process_ha_core_config(hass, config)
@@ -417,6 +421,7 @@ async def test_well_known_auth_info(
417421
)
418422
assert resp.status == 200
419423
assert await resp.json() == {
424+
**extra_response_data,
420425
"authorization_endpoint": f"{expected_url_prefix}/auth/authorize",
421426
"token_endpoint": f"{expected_url_prefix}/auth/token",
422427
"revocation_endpoint": f"{expected_url_prefix}/auth/revoke",

0 commit comments

Comments
 (0)