Check async_can_start_login when a login flow is started (#187321)

This commit is contained in:
Josef Zweck
2026-10-19 17:32:43 +02:00
committed by GitHub
parent 6f917faaf7
commit a1af789c85
3 changed files with 87 additions and 4 deletions
+12 -3
View File
@@ -18,7 +18,12 @@ from homeassistant.core import (
HomeAssistant,
callback,
)
from homeassistant.data_entry_flow import FlowHandler, FlowManager, FlowResultType
from homeassistant.data_entry_flow import (
FlowHandler,
FlowManager,
FlowResultType,
UnknownHandler,
)
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.util import dt as dt_util
@@ -128,8 +133,12 @@ class AuthManagerFlowManager(
) -> LoginFlow[Any]:
"""Create a login flow."""
auth_provider = self.auth_manager.get_auth_provider(*handler_key)
if not auth_provider:
raise KeyError(f"Unknown auth provider {handler_key}")
# Not offered on the login screen is answered like not configured, so
# it doesn't reveal which providers are set up.
if not auth_provider or not auth_provider.async_can_start_login(
context or AuthFlowContext()
):
raise UnknownHandler
return await auth_provider.async_login_flow(context)
@override
+4 -1
View File
@@ -107,7 +107,10 @@ class AuthProvider:
@callback
def async_can_start_login(self, context: AuthFlowContext) -> bool:
"""Return if the login screen may offer this provider for the context."""
"""Return if a login flow may be started with this provider for the context.
The login screen only offers the providers for which this returns True.
"""
return True
async def async_get_or_create_credentials(
+71
View File
@@ -122,6 +122,77 @@ async def test_fetch_auth_providers_skips_providers_that_cannot_start_login(
assert (await resp.json())["providers"] == []
async def test_login_flow_refuses_providers_that_cannot_start_login(
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator
) -> None:
"""Test a provider left off the login screen can't be started directly."""
client = await async_setup_auth(hass, aiohttp_client, BASE_CONFIG)
with patch(
"homeassistant.auth.providers.insecure_example.ExampleAuthProvider"
".async_can_start_login",
return_value=False,
):
resp = await client.post(
"/auth/login_flow",
json={
"client_id": CLIENT_ID,
"handler": ["insecure_example", None],
"redirect_uri": CLIENT_REDIRECT_URI,
},
)
assert resp.status == HTTPStatus.NOT_FOUND
assert await resp.json() == {"message": "Invalid handler specified"}
async def test_login_flow_trusted_networks_from_trusted_network(
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator
) -> None:
"""Test a trusted networks login can be started from a trusted network."""
client = await async_setup_auth(
hass, aiohttp_client, [_TRUSTED_NETWORKS_CONFIG], custom_ip="192.168.0.1"
)
resp = await client.post(
"/auth/login_flow",
json={
"client_id": CLIENT_ID,
"handler": ["trusted_networks", None],
"redirect_uri": CLIENT_REDIRECT_URI,
},
)
assert resp.status == HTTPStatus.OK
@pytest.mark.parametrize(
"provider_configs",
[
pytest.param([_TRUSTED_NETWORKS_CONFIG], id="untrusted-network"),
pytest.param(BASE_CONFIG, id="not-configured"),
],
)
async def test_login_flow_hides_unavailable_trusted_networks(
hass: HomeAssistant,
aiohttp_client: ClientSessionGenerator,
provider_configs: list[dict[str, Any]],
) -> None:
"""Test trusted networks from outside is answered like it isn't configured."""
client = await async_setup_auth(
hass, aiohttp_client, provider_configs, custom_ip="1.2.3.4"
)
resp = await client.post(
"/auth/login_flow",
json={
"client_id": CLIENT_ID,
"handler": ["trusted_networks", None],
"redirect_uri": CLIENT_REDIRECT_URI,
},
)
assert resp.status == HTTPStatus.NOT_FOUND
assert await resp.json() == {"message": "Invalid handler specified"}
async def test_fetch_auth_providers_onboarding(
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator
) -> None: