Disable user profiles on login screen (#105749)
This commit is contained in:
parent
4aa03b33f6
commit
dbfc5ea8f9
4 changed files with 7 additions and 102 deletions
|
@ -91,7 +91,6 @@ from homeassistant.components.http.data_validator import RequestDataValidator
|
||||||
from homeassistant.components.http.view import HomeAssistantView
|
from homeassistant.components.http.view import HomeAssistantView
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.network import is_cloud_connection
|
from homeassistant.helpers.network import is_cloud_connection
|
||||||
from homeassistant.util.network import is_local
|
|
||||||
|
|
||||||
from . import indieauth
|
from . import indieauth
|
||||||
|
|
||||||
|
@ -165,8 +164,6 @@ class AuthProvidersView(HomeAssistantView):
|
||||||
|
|
||||||
providers = []
|
providers = []
|
||||||
for provider in hass.auth.auth_providers:
|
for provider in hass.auth.auth_providers:
|
||||||
additional_data = {}
|
|
||||||
|
|
||||||
if provider.type == "trusted_networks":
|
if provider.type == "trusted_networks":
|
||||||
if cloud_connection:
|
if cloud_connection:
|
||||||
# Skip quickly as trusted networks are not available on cloud
|
# Skip quickly as trusted networks are not available on cloud
|
||||||
|
@ -179,30 +176,12 @@ class AuthProvidersView(HomeAssistantView):
|
||||||
except InvalidAuthError:
|
except InvalidAuthError:
|
||||||
# Not a trusted network, so we don't expose that trusted_network authenticator is setup
|
# Not a trusted network, so we don't expose that trusted_network authenticator is setup
|
||||||
continue
|
continue
|
||||||
elif (
|
|
||||||
provider.type == "homeassistant"
|
|
||||||
and not cloud_connection
|
|
||||||
and is_local(remote_address)
|
|
||||||
and "person" in hass.config.components
|
|
||||||
):
|
|
||||||
# We are local, return user id and username
|
|
||||||
users = await provider.store.async_get_users()
|
|
||||||
additional_data["users"] = {
|
|
||||||
user.id: credentials.data["username"]
|
|
||||||
for user in users
|
|
||||||
for credentials in user.credentials
|
|
||||||
if (
|
|
||||||
credentials.auth_provider_type == provider.type
|
|
||||||
and credentials.auth_provider_id == provider.id
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
providers.append(
|
providers.append(
|
||||||
{
|
{
|
||||||
"name": provider.name,
|
"name": provider.name,
|
||||||
"id": provider.id,
|
"id": provider.id,
|
||||||
"type": provider.type,
|
"type": provider.type,
|
||||||
**additional_data,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from ipaddress import ip_address
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
@ -51,12 +50,10 @@ from homeassistant.helpers import (
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
from homeassistant.helpers.network import is_cloud_connection
|
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.util.network import is_local
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -588,33 +585,8 @@ class ListPersonsView(HomeAssistantView):
|
||||||
|
|
||||||
async def get(self, request: web.Request) -> web.Response:
|
async def get(self, request: web.Request) -> web.Response:
|
||||||
"""Return a list of persons if request comes from a local IP."""
|
"""Return a list of persons if request comes from a local IP."""
|
||||||
try:
|
return self.json_message(
|
||||||
remote_address = ip_address(request.remote) # type: ignore[arg-type]
|
message="Not local",
|
||||||
except ValueError:
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
return self.json_message(
|
message_code="not_local",
|
||||||
message="Invalid remote IP",
|
|
||||||
status_code=HTTPStatus.BAD_REQUEST,
|
|
||||||
message_code="invalid_remote_ip",
|
|
||||||
)
|
|
||||||
|
|
||||||
hass: HomeAssistant = request.app["hass"]
|
|
||||||
if is_cloud_connection(hass) or not is_local(remote_address):
|
|
||||||
return self.json_message(
|
|
||||||
message="Not local",
|
|
||||||
status_code=HTTPStatus.BAD_REQUEST,
|
|
||||||
message_code="not_local",
|
|
||||||
)
|
|
||||||
|
|
||||||
yaml, storage, _ = hass.data[DOMAIN]
|
|
||||||
persons = [*yaml.async_items(), *storage.async_items()]
|
|
||||||
|
|
||||||
return self.json(
|
|
||||||
{
|
|
||||||
person[ATTR_USER_ID]: {
|
|
||||||
ATTR_NAME: person[ATTR_NAME],
|
|
||||||
CONF_PICTURE: person.get(CONF_PICTURE),
|
|
||||||
}
|
|
||||||
for person in persons
|
|
||||||
if person.get(ATTR_USER_ID)
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
"""Tests for the login flow."""
|
"""Tests for the login flow."""
|
||||||
from collections.abc import Callable
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.auth.models import User
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
@ -67,22 +65,16 @@ async def _test_fetch_auth_providers_home_assistant(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
aiohttp_client: ClientSessionGenerator,
|
aiohttp_client: ClientSessionGenerator,
|
||||||
ip: str,
|
ip: str,
|
||||||
additional_expected_fn: Callable[[User], dict[str, Any]],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test fetching auth providers for homeassistant auth provider."""
|
"""Test fetching auth providers for homeassistant auth provider."""
|
||||||
client = await async_setup_auth(
|
client = await async_setup_auth(
|
||||||
hass, aiohttp_client, [{"type": "homeassistant"}], custom_ip=ip
|
hass, aiohttp_client, [{"type": "homeassistant"}], custom_ip=ip
|
||||||
)
|
)
|
||||||
|
|
||||||
provider = hass.auth.auth_providers[0]
|
|
||||||
credentials = await provider.async_get_or_create_credentials({"username": "hello"})
|
|
||||||
user = await hass.auth.async_get_or_create_user(credentials)
|
|
||||||
|
|
||||||
expected = {
|
expected = {
|
||||||
"name": "Home Assistant Local",
|
"name": "Home Assistant Local",
|
||||||
"type": "homeassistant",
|
"type": "homeassistant",
|
||||||
"id": None,
|
"id": None,
|
||||||
**additional_expected_fn(user),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = await client.get("/auth/providers")
|
resp = await client.get("/auth/providers")
|
||||||
|
@ -105,9 +97,7 @@ async def test_fetch_auth_providers_home_assistant_person_not_loaded(
|
||||||
ip: str,
|
ip: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test fetching auth providers for homeassistant auth provider, where person integration is not loaded."""
|
"""Test fetching auth providers for homeassistant auth provider, where person integration is not loaded."""
|
||||||
await _test_fetch_auth_providers_home_assistant(
|
await _test_fetch_auth_providers_home_assistant(hass, aiohttp_client, ip)
|
||||||
hass, aiohttp_client, ip, lambda _: {}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
@ -134,7 +124,6 @@ async def test_fetch_auth_providers_home_assistant_person_loaded(
|
||||||
hass,
|
hass,
|
||||||
aiohttp_client,
|
aiohttp_client,
|
||||||
ip,
|
ip,
|
||||||
lambda user: {"users": {user.id: user.name}} if is_local else {},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
"""The tests for the person component."""
|
"""The tests for the person component."""
|
||||||
from collections.abc import Callable
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
@ -31,7 +30,6 @@ from homeassistant.setup import async_setup_component
|
||||||
from .conftest import DEVICE_TRACKER, DEVICE_TRACKER_2
|
from .conftest import DEVICE_TRACKER, DEVICE_TRACKER_2
|
||||||
|
|
||||||
from tests.common import MockUser, mock_component, mock_restore_cache
|
from tests.common import MockUser, mock_component, mock_restore_cache
|
||||||
from tests.test_util import mock_real_ip
|
|
||||||
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
||||||
|
|
||||||
|
|
||||||
|
@ -852,42 +850,10 @@ async def test_entities_in_person(hass: HomeAssistant) -> None:
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
("ip", "status_code", "expected_fn"),
|
|
||||||
[
|
|
||||||
(
|
|
||||||
"192.168.0.10",
|
|
||||||
HTTPStatus.OK,
|
|
||||||
lambda user: {
|
|
||||||
user["user_id"]: {"name": user["name"], "picture": user["picture"]}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"::ffff:192.168.0.10",
|
|
||||||
HTTPStatus.OK,
|
|
||||||
lambda user: {
|
|
||||||
user["user_id"]: {"name": user["name"], "picture": user["picture"]}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"1.2.3.4",
|
|
||||||
HTTPStatus.BAD_REQUEST,
|
|
||||||
lambda _: {"code": "not_local", "message": "Not local"},
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"2001:db8::1",
|
|
||||||
HTTPStatus.BAD_REQUEST,
|
|
||||||
lambda _: {"code": "not_local", "message": "Not local"},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_list_persons(
|
async def test_list_persons(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client_no_auth: ClientSessionGenerator,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
hass_admin_user: MockUser,
|
hass_admin_user: MockUser,
|
||||||
ip: str,
|
|
||||||
status_code: HTTPStatus,
|
|
||||||
expected_fn: Callable[[dict[str, Any]], dict[str, Any]],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test listing persons from a not local ip address."""
|
"""Test listing persons from a not local ip address."""
|
||||||
|
|
||||||
|
@ -902,11 +868,10 @@ async def test_list_persons(
|
||||||
assert await async_setup_component(hass, DOMAIN, config)
|
assert await async_setup_component(hass, DOMAIN, config)
|
||||||
|
|
||||||
await async_setup_component(hass, "api", {})
|
await async_setup_component(hass, "api", {})
|
||||||
mock_real_ip(hass.http.app)(ip)
|
|
||||||
client = await hass_client_no_auth()
|
client = await hass_client_no_auth()
|
||||||
|
|
||||||
resp = await client.get("/api/person/list")
|
resp = await client.get("/api/person/list")
|
||||||
|
|
||||||
assert resp.status == status_code
|
assert resp.status == HTTPStatus.BAD_REQUEST
|
||||||
result = await resp.json()
|
result = await resp.json()
|
||||||
assert result == expected_fn(admin)
|
assert result == {"code": "not_local", "message": "Not local"}
|
||||||
|
|
Loading…
Add table
Reference in a new issue