Remove direct authentication via trusted networks or API password (#27656)
* Remove direct authentication via trusted networks and API password * Fix tests
This commit is contained in:
1 parent
97478d1ef4
commit
3231e22ddf
33 files changed
+114
-423
No files matched your search
@@ -11,10 +11,8 @@ from homeassistant.auth.providers import trusted_networks
|
||||
from homeassistant.components.http.auth import setup_auth, async_sign_path
|
||||
from homeassistant.components.http.const import KEY_AUTHENTICATED
|
||||
from homeassistant.components.http.real_ip import setup_real_ip
|
||||
from homeassistant.const import HTTP_HEADER_HA_AUTH
|
||||
from homeassistant.setup import async_setup_component
|
||||
from . import mock_real_ip
|
||||
|
||||
from . import mock_real_ip, HTTP_HEADER_HA_AUTH
|
||||
|
||||
API_PASSWORD = "test-password"
|
||||
|
||||
@@ -87,29 +85,29 @@ async def test_auth_middleware_loaded_by_default(hass):
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_access_with_password_in_header(app, aiohttp_client, legacy_auth, hass):
|
||||
async def test_cant_access_with_password_in_header(
|
||||
app, aiohttp_client, legacy_auth, hass
|
||||
):
|
||||
"""Test access with password in header."""
|
||||
setup_auth(hass, app)
|
||||
client = await aiohttp_client(app)
|
||||
user = await get_legacy_user(hass.auth)
|
||||
|
||||
req = await client.get("/", headers={HTTP_HEADER_HA_AUTH: API_PASSWORD})
|
||||
assert req.status == 200
|
||||
assert await req.json() == {"user_id": user.id}
|
||||
assert req.status == 401
|
||||
|
||||
req = await client.get("/", headers={HTTP_HEADER_HA_AUTH: "wrong-pass"})
|
||||
assert req.status == 401
|
||||
|
||||
|
||||
async def test_access_with_password_in_query(app, aiohttp_client, legacy_auth, hass):
|
||||
async def test_cant_access_with_password_in_query(
|
||||
app, aiohttp_client, legacy_auth, hass
|
||||
):
|
||||
"""Test access with password in URL."""
|
||||
setup_auth(hass, app)
|
||||
client = await aiohttp_client(app)
|
||||
user = await get_legacy_user(hass.auth)
|
||||
|
||||
resp = await client.get("/", params={"api_password": API_PASSWORD})
|
||||
assert resp.status == 200
|
||||
assert await resp.json() == {"user_id": user.id}
|
||||
assert resp.status == 401
|
||||
|
||||
resp = await client.get("/")
|
||||
assert resp.status == 401
|
||||
@@ -118,15 +116,13 @@ async def test_access_with_password_in_query(app, aiohttp_client, legacy_auth, h
|
||||
assert resp.status == 401
|
||||
|
||||
|
||||
async def test_basic_auth_works(app, aiohttp_client, hass, legacy_auth):
|
||||
async def test_basic_auth_does_not_work(app, aiohttp_client, hass, legacy_auth):
|
||||
"""Test access with basic authentication."""
|
||||
setup_auth(hass, app)
|
||||
client = await aiohttp_client(app)
|
||||
user = await get_legacy_user(hass.auth)
|
||||
|
||||
req = await client.get("/", auth=BasicAuth("homeassistant", API_PASSWORD))
|
||||
assert req.status == 200
|
||||
assert await req.json() == {"user_id": user.id}
|
||||
assert req.status == 401
|
||||
|
||||
req = await client.get("/", auth=BasicAuth("wrong_username", API_PASSWORD))
|
||||
assert req.status == 401
|
||||
@@ -138,7 +134,7 @@ async def test_basic_auth_works(app, aiohttp_client, hass, legacy_auth):
|
||||
assert req.status == 401
|
||||
|
||||
|
||||
async def test_access_with_trusted_ip(
|
||||
async def test_cannot_access_with_trusted_ip(
|
||||
hass, app2, trusted_networks_auth, aiohttp_client, hass_owner_user
|
||||
):
|
||||
"""Test access with an untrusted ip address."""
|
||||
@@ -155,8 +151,7 @@ async def test_access_with_trusted_ip(
|
||||
for remote_addr in TRUSTED_ADDRESSES:
|
||||
set_mock_ip(remote_addr)
|
||||
resp = await client.get("/")
|
||||
assert resp.status == 200, "{} should be trusted".format(remote_addr)
|
||||
assert await resp.json() == {"user_id": hass_owner_user.id}
|
||||
assert resp.status == 401, "{} shouldn't be trusted".format(remote_addr)
|
||||
|
||||
|
||||
async def test_auth_active_access_with_access_token_in_header(
|
||||
@@ -209,29 +204,24 @@ async def test_auth_active_access_with_trusted_ip(
|
||||
for remote_addr in TRUSTED_ADDRESSES:
|
||||
set_mock_ip(remote_addr)
|
||||
resp = await client.get("/")
|
||||
assert resp.status == 200, "{} should be trusted".format(remote_addr)
|
||||
assert await resp.json() == {"user_id": hass_owner_user.id}
|
||||
assert resp.status == 401, "{} shouldn't be trusted".format(remote_addr)
|
||||
|
||||
|
||||
async def test_auth_legacy_support_api_password_access(
|
||||
async def test_auth_legacy_support_api_password_cannot_access(
|
||||
app, aiohttp_client, legacy_auth, hass
|
||||
):
|
||||
"""Test access using api_password if auth.support_legacy."""
|
||||
setup_auth(hass, app)
|
||||
client = await aiohttp_client(app)
|
||||
user = await get_legacy_user(hass.auth)
|
||||
|
||||
req = await client.get("/", headers={HTTP_HEADER_HA_AUTH: API_PASSWORD})
|
||||
assert req.status == 200
|
||||
assert await req.json() == {"user_id": user.id}
|
||||
assert req.status == 401
|
||||
|
||||
resp = await client.get("/", params={"api_password": API_PASSWORD})
|
||||
assert resp.status == 200
|
||||
assert await resp.json() == {"user_id": user.id}
|
||||
assert resp.status == 401
|
||||
|
||||
req = await client.get("/", auth=BasicAuth("homeassistant", API_PASSWORD))
|
||||
assert req.status == 200
|
||||
assert await req.json() == {"user_id": user.id}
|
||||
assert req.status == 401
|
||||
|
||||
|
||||
async def test_auth_access_signed_path(hass, app, aiohttp_client, hass_access_token):
|
||||
|
||||
Reference in new issue
Block a user