Allow owner users to change password of any user (#39242)
This commit is contained in:
1 parent
ef35eea0f6
commit
13df3bce1b
3 files changed
+251
-127
No files matched your search
@@ -4,7 +4,7 @@ import pytest
|
||||
from homeassistant.auth.providers import homeassistant as prov_ha
|
||||
from homeassistant.components.config import auth_provider_homeassistant as auth_ha
|
||||
|
||||
from tests.common import MockUser, register_auth_provider
|
||||
from tests.common import CLIENT_ID, MockUser, register_auth_provider
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -16,17 +16,44 @@ def setup_config(hass):
|
||||
hass.loop.run_until_complete(auth_ha.async_setup(hass))
|
||||
|
||||
|
||||
async def test_create_auth_system_generated_user(
|
||||
hass, hass_access_token, hass_ws_client
|
||||
):
|
||||
@pytest.fixture
|
||||
async def auth_provider(hass):
|
||||
"""Hass auth provider."""
|
||||
provider = hass.auth.auth_providers[0]
|
||||
await provider.async_initialize()
|
||||
return provider
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def owner_access_token(hass, hass_owner_user):
|
||||
"""Access token for owner user."""
|
||||
refresh_token = await hass.auth.async_create_refresh_token(
|
||||
hass_owner_user, CLIENT_ID
|
||||
)
|
||||
return hass.auth.async_create_access_token(refresh_token)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def test_user_credential(hass, auth_provider):
|
||||
"""Add a test user."""
|
||||
await hass.async_add_executor_job(
|
||||
auth_provider.data.add_auth, "test-user", "test-pass"
|
||||
)
|
||||
|
||||
return await auth_provider.async_get_or_create_credentials(
|
||||
{"username": "test-user"}
|
||||
)
|
||||
|
||||
|
||||
async def test_create_auth_system_generated_user(hass, hass_ws_client):
|
||||
"""Test we can't add auth to system generated users."""
|
||||
system_user = MockUser(system_generated=True).add_to_hass(hass)
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": auth_ha.WS_TYPE_CREATE,
|
||||
"type": "config/auth_provider/homeassistant/create",
|
||||
"user_id": system_user.id,
|
||||
"username": "test-user",
|
||||
"password": "test-pass",
|
||||
@@ -44,14 +71,14 @@ async def test_create_auth_user_already_credentials():
|
||||
# assert False
|
||||
|
||||
|
||||
async def test_create_auth_unknown_user(hass_ws_client, hass, hass_access_token):
|
||||
async def test_create_auth_unknown_user(hass_ws_client, hass):
|
||||
"""Test create pointing at unknown user."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": auth_ha.WS_TYPE_CREATE,
|
||||
"type": "config/auth_provider/homeassistant/create",
|
||||
"user_id": "test-id",
|
||||
"username": "test-user",
|
||||
"password": "test-pass",
|
||||
@@ -73,7 +100,7 @@ async def test_create_auth_requires_admin(
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": auth_ha.WS_TYPE_CREATE,
|
||||
"type": "config/auth_provider/homeassistant/create",
|
||||
"user_id": "test-id",
|
||||
"username": "test-user",
|
||||
"password": "test-pass",
|
||||
@@ -85,9 +112,9 @@ async def test_create_auth_requires_admin(
|
||||
assert result["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
async def test_create_auth(hass, hass_ws_client, hass_access_token, hass_storage):
|
||||
async def test_create_auth(hass, hass_ws_client, hass_storage):
|
||||
"""Test create auth command works."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
user = MockUser().add_to_hass(hass)
|
||||
|
||||
assert len(user.credentials) == 0
|
||||
@@ -95,7 +122,7 @@ async def test_create_auth(hass, hass_ws_client, hass_access_token, hass_storage
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": auth_ha.WS_TYPE_CREATE,
|
||||
"type": "config/auth_provider/homeassistant/create",
|
||||
"user_id": user.id,
|
||||
"username": "test-user",
|
||||
"password": "test-pass",
|
||||
@@ -114,11 +141,9 @@ async def test_create_auth(hass, hass_ws_client, hass_access_token, hass_storage
|
||||
assert entry["username"] == "test-user"
|
||||
|
||||
|
||||
async def test_create_auth_duplicate_username(
|
||||
hass, hass_ws_client, hass_access_token, hass_storage
|
||||
):
|
||||
async def test_create_auth_duplicate_username(hass, hass_ws_client, hass_storage):
|
||||
"""Test we can't create auth with a duplicate username."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
user = MockUser().add_to_hass(hass)
|
||||
|
||||
hass_storage[prov_ha.STORAGE_KEY] = {
|
||||
@@ -129,7 +154,7 @@ async def test_create_auth_duplicate_username(
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": auth_ha.WS_TYPE_CREATE,
|
||||
"type": "config/auth_provider/homeassistant/create",
|
||||
"user_id": user.id,
|
||||
"username": "test-user",
|
||||
"password": "test-pass",
|
||||
@@ -141,11 +166,9 @@ async def test_create_auth_duplicate_username(
|
||||
assert result["error"]["code"] == "username_exists"
|
||||
|
||||
|
||||
async def test_delete_removes_just_auth(
|
||||
hass_ws_client, hass, hass_storage, hass_access_token
|
||||
):
|
||||
async def test_delete_removes_just_auth(hass_ws_client, hass, hass_storage):
|
||||
"""Test deleting an auth without being connected to a user."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
hass_storage[prov_ha.STORAGE_KEY] = {
|
||||
"version": 1,
|
||||
@@ -153,7 +176,11 @@ async def test_delete_removes_just_auth(
|
||||
}
|
||||
|
||||
await client.send_json(
|
||||
{"id": 5, "type": auth_ha.WS_TYPE_DELETE, "username": "test-user"}
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config/auth_provider/homeassistant/delete",
|
||||
"username": "test-user",
|
||||
}
|
||||
)
|
||||
|
||||
result = await client.receive_json()
|
||||
@@ -161,11 +188,9 @@ async def test_delete_removes_just_auth(
|
||||
assert len(hass_storage[prov_ha.STORAGE_KEY]["data"]["users"]) == 0
|
||||
|
||||
|
||||
async def test_delete_removes_credential(
|
||||
hass, hass_ws_client, hass_access_token, hass_storage
|
||||
):
|
||||
async def test_delete_removes_credential(hass, hass_ws_client, hass_storage):
|
||||
"""Test deleting auth that is connected to a user."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
user = MockUser().add_to_hass(hass)
|
||||
hass_storage[prov_ha.STORAGE_KEY] = {
|
||||
@@ -180,7 +205,11 @@ async def test_delete_removes_credential(
|
||||
)
|
||||
|
||||
await client.send_json(
|
||||
{"id": 5, "type": auth_ha.WS_TYPE_DELETE, "username": "test-user"}
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config/auth_provider/homeassistant/delete",
|
||||
"username": "test-user",
|
||||
}
|
||||
)
|
||||
|
||||
result = await client.receive_json()
|
||||
@@ -193,7 +222,11 @@ async def test_delete_requires_admin(hass, hass_ws_client, hass_read_only_access
|
||||
client = await hass_ws_client(hass, hass_read_only_access_token)
|
||||
|
||||
await client.send_json(
|
||||
{"id": 5, "type": auth_ha.WS_TYPE_DELETE, "username": "test-user"}
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config/auth_provider/homeassistant/delete",
|
||||
"username": "test-user",
|
||||
}
|
||||
)
|
||||
|
||||
result = await client.receive_json()
|
||||
@@ -201,12 +234,16 @@ async def test_delete_requires_admin(hass, hass_ws_client, hass_read_only_access
|
||||
assert result["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
async def test_delete_unknown_auth(hass, hass_ws_client, hass_access_token):
|
||||
async def test_delete_unknown_auth(hass, hass_ws_client):
|
||||
"""Test trying to delete an unknown auth username."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{"id": 5, "type": auth_ha.WS_TYPE_DELETE, "username": "test-user"}
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config/auth_provider/homeassistant/delete",
|
||||
"username": "test-user",
|
||||
}
|
||||
)
|
||||
|
||||
result = await client.receive_json()
|
||||
@@ -214,25 +251,17 @@ async def test_delete_unknown_auth(hass, hass_ws_client, hass_access_token):
|
||||
assert result["error"]["code"] == "auth_not_found"
|
||||
|
||||
|
||||
async def test_change_password(hass, hass_ws_client, hass_access_token):
|
||||
async def test_change_password(
|
||||
hass, hass_ws_client, hass_admin_user, auth_provider, test_user_credential
|
||||
):
|
||||
"""Test that change password succeeds with valid password."""
|
||||
provider = hass.auth.auth_providers[0]
|
||||
await provider.async_initialize()
|
||||
await hass.async_add_executor_job(provider.data.add_auth, "test-user", "test-pass")
|
||||
await hass.auth.async_link_user(hass_admin_user, test_user_credential)
|
||||
|
||||
credentials = await provider.async_get_or_create_credentials(
|
||||
{"username": "test-user"}
|
||||
)
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
|
||||
user = refresh_token.user
|
||||
await hass.auth.async_link_user(user, credentials)
|
||||
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": auth_ha.WS_TYPE_CHANGE_PASSWORD,
|
||||
"type": "config/auth_provider/homeassistant/change_password",
|
||||
"current_password": "test-pass",
|
||||
"new_password": "new-pass",
|
||||
}
|
||||
@@ -240,28 +269,20 @@ async def test_change_password(hass, hass_ws_client, hass_access_token):
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result["success"], result
|
||||
await provider.async_validate_login("test-user", "new-pass")
|
||||
await auth_provider.async_validate_login("test-user", "new-pass")
|
||||
|
||||
|
||||
async def test_change_password_wrong_pw(hass, hass_ws_client, hass_access_token):
|
||||
async def test_change_password_wrong_pw(
|
||||
hass, hass_ws_client, hass_admin_user, auth_provider, test_user_credential
|
||||
):
|
||||
"""Test that change password fails with invalid password."""
|
||||
provider = hass.auth.auth_providers[0]
|
||||
await provider.async_initialize()
|
||||
await hass.async_add_executor_job(provider.data.add_auth, "test-user", "test-pass")
|
||||
await hass.auth.async_link_user(hass_admin_user, test_user_credential)
|
||||
|
||||
credentials = await provider.async_get_or_create_credentials(
|
||||
{"username": "test-user"}
|
||||
)
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
|
||||
user = refresh_token.user
|
||||
await hass.auth.async_link_user(user, credentials)
|
||||
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": auth_ha.WS_TYPE_CHANGE_PASSWORD,
|
||||
"type": "config/auth_provider/homeassistant/change_password",
|
||||
"current_password": "wrong-pass",
|
||||
"new_password": "new-pass",
|
||||
}
|
||||
@@ -271,17 +292,17 @@ async def test_change_password_wrong_pw(hass, hass_ws_client, hass_access_token)
|
||||
assert not result["success"], result
|
||||
assert result["error"]["code"] == "invalid_password"
|
||||
with pytest.raises(prov_ha.InvalidAuth):
|
||||
await provider.async_validate_login("test-user", "new-pass")
|
||||
await auth_provider.async_validate_login("test-user", "new-pass")
|
||||
|
||||
|
||||
async def test_change_password_no_creds(hass, hass_ws_client, hass_access_token):
|
||||
async def test_change_password_no_creds(hass, hass_ws_client):
|
||||
"""Test that change password fails with no credentials."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": auth_ha.WS_TYPE_CHANGE_PASSWORD,
|
||||
"type": "config/auth_provider/homeassistant/change_password",
|
||||
"current_password": "test-pass",
|
||||
"new_password": "new-pass",
|
||||
}
|
||||
@@ -290,3 +311,65 @@ async def test_change_password_no_creds(hass, hass_ws_client, hass_access_token)
|
||||
result = await client.receive_json()
|
||||
assert not result["success"], result
|
||||
assert result["error"]["code"] == "credentials_not_found"
|
||||
|
||||
|
||||
async def test_admin_change_password_not_owner(
|
||||
hass, hass_ws_client, auth_provider, test_user_credential
|
||||
):
|
||||
"""Test that change password fails when not owner."""
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": "config/auth_provider/homeassistant/admin_change_password",
|
||||
"username": "test-user",
|
||||
"password": "new-pass",
|
||||
}
|
||||
)
|
||||
|
||||
result = await client.receive_json()
|
||||
assert not result["success"], result
|
||||
assert result["error"]["code"] == "unauthorized"
|
||||
|
||||
# Validate old login still works
|
||||
await auth_provider.async_validate_login("test-user", "test-pass")
|
||||
|
||||
|
||||
async def test_admin_change_password_no_creds(hass, hass_ws_client, owner_access_token):
|
||||
"""Test that change password fails with unknown credentials."""
|
||||
client = await hass_ws_client(hass, owner_access_token)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": "config/auth_provider/homeassistant/admin_change_password",
|
||||
"username": "non-existing",
|
||||
"password": "new-pass",
|
||||
}
|
||||
)
|
||||
|
||||
result = await client.receive_json()
|
||||
assert not result["success"], result
|
||||
assert result["error"]["code"] == "credentials_not_found"
|
||||
|
||||
|
||||
async def test_admin_change_password(
|
||||
hass, hass_ws_client, owner_access_token, auth_provider, test_user_credential
|
||||
):
|
||||
"""Test that owners can change any password."""
|
||||
client = await hass_ws_client(hass, owner_access_token)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": "config/auth_provider/homeassistant/admin_change_password",
|
||||
"username": "test-user",
|
||||
"password": "new-pass",
|
||||
}
|
||||
)
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result["success"], result
|
||||
|
||||
await auth_provider.async_validate_login("test-user", "new-pass")
|
||||
Reference in new issue
Block a user