diff --git a/homeassistant/components/abrp/config_flow.py b/homeassistant/components/abrp/config_flow.py index 3fda7ffaf241..c1845ca7f3d6 100644 --- a/homeassistant/components/abrp/config_flow.py +++ b/homeassistant/components/abrp/config_flow.py @@ -1,6 +1,7 @@ """Config flow for the A Better Routeplanner integration.""" from collections.abc import Mapping +from contextlib import suppress import logging from typing import Any, override @@ -37,7 +38,14 @@ class AbrpFlowHandler( @override def extra_authorize_data(self) -> dict[str, Any]: """Extra data that needs to be appended to the authorize url.""" - return {"scope": " ".join(OAUTH2_SCOPES)} + data = {"scope": " ".join(OAUTH2_SCOPES)} + if self.source == SOURCE_REAUTH: + id_token = self._get_reauth_entry().data["token"].get("id_token") + with suppress(AbrpAuthError): + # Only prefills the login page; the account is still checked on return. + if email := parse_unverified_identity(id_token).email: + data["login_hint"] = email + return data @override async def async_step_user( diff --git a/tests/components/abrp/conftest.py b/tests/components/abrp/conftest.py index 94373a5ed195..ef74f9620658 100644 --- a/tests/components/abrp/conftest.py +++ b/tests/components/abrp/conftest.py @@ -121,15 +121,21 @@ def mock_expires_at() -> float: return time.time() + 86400 +@pytest.fixture +def id_token() -> str: + """Fixture for the stored OIDC id_token.""" + return build_id_token() + + @pytest.fixture(name="token_entry") -def mock_token_entry(expires_at: float) -> dict[str, Any]: +def mock_token_entry(expires_at: float, id_token: str) -> dict[str, Any]: """Fixture for OAuth ``token`` data for a ConfigEntry.""" return { "access_token": "mock-access-token", "refresh_token": "mock-refresh-token", "token_type": "Bearer", "expires_at": expires_at, - "id_token": build_id_token(), + "id_token": id_token, } diff --git a/tests/components/abrp/test_config_flow.py b/tests/components/abrp/test_config_flow.py index a03a80e30a74..220a9f2144c8 100644 --- a/tests/components/abrp/test_config_flow.py +++ b/tests/components/abrp/test_config_flow.py @@ -10,6 +10,7 @@ from pytest_homeassistant.common import MockConfigEntry from pytest_homeassistant.test_util.aiohttp import AiohttpClientMocker from pytest_homeassistant.typing import ClientSessionGenerator from syrupy.assertion import SnapshotAssertion +from yarl import URL from homeassistant.components.abrp.const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN from homeassistant.config_entries import SOURCE_USER @@ -365,3 +366,30 @@ async def test_reauth( mock_abrp_client.assert_not_called() await hass.async_block_till_done() assert len(mock_setup_entry.mock_calls) == expected_setups + + +@pytest.mark.parametrize( + ("id_token", "expected_login_hint"), + [ + pytest.param( + build_id_token(email="sofie@example.com", name="Sofie"), + "sofie@example.com", + id="email", + ), + pytest.param(build_id_token(name="Sofie"), None, id="no_email"), + pytest.param("header.not-base64!.sig", None, id="unparsable_id_token"), + ], +) +@pytest.mark.usefixtures("current_request_with_host", "use_cloud") +async def test_reauth_login_hint( + hass: HomeAssistant, + config_entry: MockConfigEntry, + expected_login_hint: str | None, +) -> None: + """Reauth prefills the stored account's email on the login page when known.""" + config_entry.add_to_hass(hass) + + result = await config_entry.start_reauth_flow(hass) + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + + assert URL(result["url"]).query.get("login_hint") == expected_login_hint