Use IndieAuth for client ID (#15369)
* Use IndieAuth for client ID * Lint * Lint & Fix tests * Allow local IP addresses * Update comment
This commit is contained in:
1 parent
f7d7d825b0
commit
0d4841cbea
14 files changed
+329
-347
No files matched your search
+2
-2
@@ -31,6 +31,8 @@ from homeassistant.util.async_ import (
|
||||
_TEST_INSTANCE_PORT = SERVER_PORT
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
INSTANCES = []
|
||||
CLIENT_ID = 'https://example.com/app'
|
||||
CLIENT_REDIRECT_URI = 'https://example.com/app/callback'
|
||||
|
||||
|
||||
def threadsafe_callback_factory(func):
|
||||
@@ -330,8 +332,6 @@ class MockUser(auth.User):
|
||||
def ensure_auth_manager_loaded(auth_mgr):
|
||||
"""Ensure an auth manager is considered loaded."""
|
||||
store = auth_mgr._store
|
||||
if store._clients is None:
|
||||
store._clients = {}
|
||||
if store._users is None:
|
||||
store._users = {}
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
"""Tests for the auth component."""
|
||||
from aiohttp.helpers import BasicAuth
|
||||
|
||||
from homeassistant import auth
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -16,10 +14,6 @@ BASE_CONFIG = [{
|
||||
'name': 'Test Name'
|
||||
}]
|
||||
}]
|
||||
CLIENT_ID = 'test-id'
|
||||
CLIENT_SECRET = 'test-secret'
|
||||
CLIENT_AUTH = BasicAuth(CLIENT_ID, CLIENT_SECRET)
|
||||
CLIENT_REDIRECT_URI = 'http://example.com/callback'
|
||||
|
||||
|
||||
async def async_setup_auth(hass, aiohttp_client, provider_configs=BASE_CONFIG,
|
||||
@@ -32,9 +26,6 @@ async def async_setup_auth(hass, aiohttp_client, provider_configs=BASE_CONFIG,
|
||||
'api_password': 'bla'
|
||||
}
|
||||
})
|
||||
client = auth.Client('Test Client', CLIENT_ID, CLIENT_SECRET,
|
||||
redirect_uris=[CLIENT_REDIRECT_URI])
|
||||
hass.auth._store._clients[client.id] = client
|
||||
if setup_api:
|
||||
await async_setup_component(hass, 'api', {})
|
||||
return await aiohttp_client(hass.http.app)
|
||||
@@ -1,70 +0,0 @@
|
||||
"""Tests for the client validator."""
|
||||
from aiohttp.helpers import BasicAuth
|
||||
import pytest
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.auth.client import verify_client
|
||||
from homeassistant.components.http.view import HomeAssistantView
|
||||
|
||||
from . import async_setup_auth
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_view(hass):
|
||||
"""Register a view that verifies client id/secret."""
|
||||
hass.loop.run_until_complete(async_setup_component(hass, 'http', {}))
|
||||
|
||||
clients = []
|
||||
|
||||
class ClientView(HomeAssistantView):
|
||||
url = '/'
|
||||
name = 'bla'
|
||||
|
||||
@verify_client
|
||||
async def get(self, request, client):
|
||||
"""Handle GET request."""
|
||||
clients.append(client)
|
||||
|
||||
hass.http.register_view(ClientView)
|
||||
return clients
|
||||
|
||||
|
||||
async def test_verify_client(hass, aiohttp_client, mock_view):
|
||||
"""Test that verify client can extract client auth from a request."""
|
||||
http_client = await async_setup_auth(hass, aiohttp_client)
|
||||
client = await hass.auth.async_create_client('Hello')
|
||||
|
||||
resp = await http_client.get('/', auth=BasicAuth(client.id, client.secret))
|
||||
assert resp.status == 200
|
||||
assert mock_view[0] is client
|
||||
|
||||
|
||||
async def test_verify_client_no_auth_header(hass, aiohttp_client, mock_view):
|
||||
"""Test that verify client will decline unknown client id."""
|
||||
http_client = await async_setup_auth(hass, aiohttp_client)
|
||||
|
||||
resp = await http_client.get('/')
|
||||
assert resp.status == 401
|
||||
assert mock_view == []
|
||||
|
||||
|
||||
async def test_verify_client_invalid_client_id(hass, aiohttp_client,
|
||||
mock_view):
|
||||
"""Test that verify client will decline unknown client id."""
|
||||
http_client = await async_setup_auth(hass, aiohttp_client)
|
||||
client = await hass.auth.async_create_client('Hello')
|
||||
|
||||
resp = await http_client.get('/', auth=BasicAuth('invalid', client.secret))
|
||||
assert resp.status == 401
|
||||
assert mock_view == []
|
||||
|
||||
|
||||
async def test_verify_client_invalid_client_secret(hass, aiohttp_client,
|
||||
mock_view):
|
||||
"""Test that verify client will decline incorrect client secret."""
|
||||
http_client = await async_setup_auth(hass, aiohttp_client)
|
||||
client = await hass.auth.async_create_client('Hello')
|
||||
|
||||
resp = await http_client.get('/', auth=BasicAuth(client.id, 'invalid'))
|
||||
assert resp.status == 401
|
||||
assert mock_view == []
|
||||
@@ -0,0 +1,110 @@
|
||||
"""Tests for the client validator."""
|
||||
from homeassistant.components.auth import indieauth
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_client_id_scheme():
|
||||
"""Test we enforce valid scheme."""
|
||||
assert indieauth._parse_client_id('http://ex.com/')
|
||||
assert indieauth._parse_client_id('https://ex.com/')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
indieauth._parse_client_id('ftp://ex.com')
|
||||
|
||||
|
||||
def test_client_id_path():
|
||||
"""Test we enforce valid path."""
|
||||
assert indieauth._parse_client_id('http://ex.com').path == '/'
|
||||
assert indieauth._parse_client_id('http://ex.com/hello').path == '/hello'
|
||||
assert indieauth._parse_client_id(
|
||||
'http://ex.com/hello/.world').path == '/hello/.world'
|
||||
assert indieauth._parse_client_id(
|
||||
'http://ex.com/hello./.world').path == '/hello./.world'
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
indieauth._parse_client_id('http://ex.com/.')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
indieauth._parse_client_id('http://ex.com/hello/./yo')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
indieauth._parse_client_id('http://ex.com/hello/../yo')
|
||||
|
||||
|
||||
def test_client_id_fragment():
|
||||
"""Test we enforce valid fragment."""
|
||||
with pytest.raises(ValueError):
|
||||
indieauth._parse_client_id('http://ex.com/#yoo')
|
||||
|
||||
|
||||
def test_client_id_user_pass():
|
||||
"""Test we enforce valid username/password."""
|
||||
with pytest.raises(ValueError):
|
||||
indieauth._parse_client_id('http://user@ex.com/')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
indieauth._parse_client_id('http://user:pass@ex.com/')
|
||||
|
||||
|
||||
def test_client_id_hostname():
|
||||
"""Test we enforce valid hostname."""
|
||||
assert indieauth._parse_client_id('http://www.home-assistant.io/')
|
||||
assert indieauth._parse_client_id('http://[::1]')
|
||||
assert indieauth._parse_client_id('http://127.0.0.1')
|
||||
assert indieauth._parse_client_id('http://10.0.0.0')
|
||||
assert indieauth._parse_client_id('http://10.255.255.255')
|
||||
assert indieauth._parse_client_id('http://172.16.0.0')
|
||||
assert indieauth._parse_client_id('http://172.31.255.255')
|
||||
assert indieauth._parse_client_id('http://192.168.0.0')
|
||||
assert indieauth._parse_client_id('http://192.168.255.255')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
assert indieauth._parse_client_id('http://255.255.255.255/')
|
||||
with pytest.raises(ValueError):
|
||||
assert indieauth._parse_client_id('http://11.0.0.0/')
|
||||
with pytest.raises(ValueError):
|
||||
assert indieauth._parse_client_id('http://172.32.0.0/')
|
||||
with pytest.raises(ValueError):
|
||||
assert indieauth._parse_client_id('http://192.167.0.0/')
|
||||
|
||||
|
||||
def test_parse_url_lowercase_host():
|
||||
"""Test we update empty paths."""
|
||||
assert indieauth._parse_url('http://ex.com/hello').path == '/hello'
|
||||
assert indieauth._parse_url('http://EX.COM/hello').hostname == 'ex.com'
|
||||
|
||||
parts = indieauth._parse_url('http://EX.COM:123/HELLO')
|
||||
assert parts.netloc == 'ex.com:123'
|
||||
assert parts.path == '/HELLO'
|
||||
|
||||
|
||||
def test_parse_url_path():
|
||||
"""Test we update empty paths."""
|
||||
assert indieauth._parse_url('http://ex.com').path == '/'
|
||||
|
||||
|
||||
def test_verify_redirect_uri():
|
||||
"""Test that we verify redirect uri correctly."""
|
||||
assert indieauth.verify_redirect_uri(
|
||||
'http://ex.com',
|
||||
'http://ex.com/callback'
|
||||
)
|
||||
|
||||
# Different domain
|
||||
assert not indieauth.verify_redirect_uri(
|
||||
'http://ex.com',
|
||||
'http://different.com/callback'
|
||||
)
|
||||
|
||||
# Different scheme
|
||||
assert not indieauth.verify_redirect_uri(
|
||||
'http://ex.com',
|
||||
'https://ex.com/callback'
|
||||
)
|
||||
|
||||
# Different subdomain
|
||||
assert not indieauth.verify_redirect_uri(
|
||||
'https://sub1.ex.com',
|
||||
'https://sub2.ex.com/callback'
|
||||
)
|
||||
@@ -1,22 +1,26 @@
|
||||
"""Integration tests for the auth component."""
|
||||
from . import async_setup_auth, CLIENT_AUTH, CLIENT_REDIRECT_URI
|
||||
from . import async_setup_auth
|
||||
|
||||
from tests.common import CLIENT_ID, CLIENT_REDIRECT_URI
|
||||
|
||||
|
||||
async def test_login_new_user_and_refresh_token(hass, aiohttp_client):
|
||||
"""Test logging in with new user and refreshing tokens."""
|
||||
client = await async_setup_auth(hass, aiohttp_client, setup_api=True)
|
||||
resp = await client.post('/auth/login_flow', json={
|
||||
'client_id': CLIENT_ID,
|
||||
'handler': ['insecure_example', None],
|
||||
'redirect_uri': CLIENT_REDIRECT_URI,
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
|
||||
resp = await client.post(
|
||||
'/auth/login_flow/{}'.format(step['flow_id']), json={
|
||||
'client_id': CLIENT_ID,
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
@@ -24,9 +28,10 @@ async def test_login_new_user_and_refresh_token(hass, aiohttp_client):
|
||||
|
||||
# Exchange code for tokens
|
||||
resp = await client.post('/auth/token', data={
|
||||
'client_id': CLIENT_ID,
|
||||
'grant_type': 'authorization_code',
|
||||
'code': code
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
@@ -35,9 +40,10 @@ async def test_login_new_user_and_refresh_token(hass, aiohttp_client):
|
||||
|
||||
# Use refresh token to get more tokens.
|
||||
resp = await client.post('/auth/token', data={
|
||||
'client_id': CLIENT_ID,
|
||||
'grant_type': 'refresh_token',
|
||||
'refresh_token': tokens['refresh_token']
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
"""Tests for the link user flow."""
|
||||
from . import async_setup_auth, CLIENT_AUTH, CLIENT_ID, CLIENT_REDIRECT_URI
|
||||
from . import async_setup_auth
|
||||
|
||||
from tests.common import CLIENT_ID, CLIENT_REDIRECT_URI
|
||||
|
||||
|
||||
async def async_get_code(hass, aiohttp_client):
|
||||
@@ -25,17 +27,19 @@ async def async_get_code(hass, aiohttp_client):
|
||||
client = await async_setup_auth(hass, aiohttp_client, config)
|
||||
|
||||
resp = await client.post('/auth/login_flow', json={
|
||||
'client_id': CLIENT_ID,
|
||||
'handler': ['insecure_example', None],
|
||||
'redirect_uri': CLIENT_REDIRECT_URI,
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
|
||||
resp = await client.post(
|
||||
'/auth/login_flow/{}'.format(step['flow_id']), json={
|
||||
'client_id': CLIENT_ID,
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
@@ -43,9 +47,10 @@ async def async_get_code(hass, aiohttp_client):
|
||||
|
||||
# Exchange code for tokens
|
||||
resp = await client.post('/auth/token', data={
|
||||
'client_id': CLIENT_ID,
|
||||
'grant_type': 'authorization_code',
|
||||
'code': code
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
@@ -57,17 +62,19 @@ async def async_get_code(hass, aiohttp_client):
|
||||
|
||||
# Now authenticate with the 2nd flow
|
||||
resp = await client.post('/auth/login_flow', json={
|
||||
'client_id': CLIENT_ID,
|
||||
'handler': ['insecure_example', '2nd auth'],
|
||||
'redirect_uri': CLIENT_REDIRECT_URI,
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
|
||||
resp = await client.post(
|
||||
'/auth/login_flow/{}'.format(step['flow_id']), json={
|
||||
'client_id': CLIENT_ID,
|
||||
'username': '2nd-user',
|
||||
'password': '2nd-pass',
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
"""Tests for the login flow."""
|
||||
from aiohttp.helpers import BasicAuth
|
||||
from . import async_setup_auth
|
||||
|
||||
from . import async_setup_auth, CLIENT_AUTH, CLIENT_REDIRECT_URI
|
||||
from tests.common import CLIENT_ID, CLIENT_REDIRECT_URI
|
||||
|
||||
|
||||
async def test_fetch_auth_providers(hass, aiohttp_client):
|
||||
"""Test fetching auth providers."""
|
||||
client = await async_setup_auth(hass, aiohttp_client)
|
||||
resp = await client.get('/auth/providers', auth=CLIENT_AUTH)
|
||||
resp = await client.get('/auth/providers')
|
||||
assert await resp.json() == [{
|
||||
'name': 'Example',
|
||||
'type': 'insecure_example',
|
||||
@@ -15,14 +15,6 @@ async def test_fetch_auth_providers(hass, aiohttp_client):
|
||||
}]
|
||||
|
||||
|
||||
async def test_fetch_auth_providers_require_valid_client(hass, aiohttp_client):
|
||||
"""Test fetching auth providers."""
|
||||
client = await async_setup_auth(hass, aiohttp_client)
|
||||
resp = await client.get('/auth/providers',
|
||||
auth=BasicAuth('invalid', 'bla'))
|
||||
assert resp.status == 401
|
||||
|
||||
|
||||
async def test_cannot_get_flows_in_progress(hass, aiohttp_client):
|
||||
"""Test we cannot get flows in progress."""
|
||||
client = await async_setup_auth(hass, aiohttp_client, [])
|
||||
@@ -34,18 +26,20 @@ async def test_invalid_username_password(hass, aiohttp_client):
|
||||
"""Test we cannot get flows in progress."""
|
||||
client = await async_setup_auth(hass, aiohttp_client)
|
||||
resp = await client.post('/auth/login_flow', json={
|
||||
'client_id': CLIENT_ID,
|
||||
'handler': ['insecure_example', None],
|
||||
'redirect_uri': CLIENT_REDIRECT_URI
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
|
||||
# Incorrect username
|
||||
resp = await client.post(
|
||||
'/auth/login_flow/{}'.format(step['flow_id']), json={
|
||||
'client_id': CLIENT_ID,
|
||||
'username': 'wrong-user',
|
||||
'password': 'test-pass',
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
@@ -56,9 +50,10 @@ async def test_invalid_username_password(hass, aiohttp_client):
|
||||
# Incorrect password
|
||||
resp = await client.post(
|
||||
'/auth/login_flow/{}'.format(step['flow_id']), json={
|
||||
'client_id': CLIENT_ID,
|
||||
'username': 'test-user',
|
||||
'password': 'wrong-pass',
|
||||
}, auth=CLIENT_AUTH)
|
||||
})
|
||||
|
||||
assert resp.status == 200
|
||||
step = await resp.json()
|
||||
|
||||
@@ -3,7 +3,7 @@ import pytest
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockUser
|
||||
from tests.common import MockUser, CLIENT_ID
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -28,11 +28,6 @@ def hass_ws_client(aiohttp_client):
|
||||
def hass_access_token(hass):
|
||||
"""Return an access token to access Home Assistant."""
|
||||
user = MockUser().add_to_hass(hass)
|
||||
client = hass.loop.run_until_complete(hass.auth.async_create_client(
|
||||
'Access Token Fixture',
|
||||
redirect_uris=['/'],
|
||||
no_secret=True,
|
||||
))
|
||||
refresh_token = hass.loop.run_until_complete(
|
||||
hass.auth.async_create_refresh_token(user, client))
|
||||
hass.auth.async_create_refresh_token(user, CLIENT_ID))
|
||||
yield hass.auth.async_create_access_token(refresh_token)
|
||||
+8
-30
@@ -6,7 +6,8 @@ import pytest
|
||||
|
||||
from homeassistant import auth, data_entry_flow
|
||||
from homeassistant.util import dt as dt_util
|
||||
from tests.common import MockUser, ensure_auth_manager_loaded, flush_store
|
||||
from tests.common import (
|
||||
MockUser, ensure_auth_manager_loaded, flush_store, CLIENT_ID)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -181,10 +182,7 @@ async def test_saving_loading(hass, hass_storage):
|
||||
})
|
||||
user = await manager.async_get_or_create_user(step['result'])
|
||||
|
||||
client = await manager.async_create_client(
|
||||
'test', redirect_uris=['https://example.com'])
|
||||
|
||||
refresh_token = await manager.async_create_refresh_token(user, client)
|
||||
refresh_token = await manager.async_create_refresh_token(user, CLIENT_ID)
|
||||
|
||||
manager.async_create_access_token(refresh_token)
|
||||
|
||||
@@ -195,10 +193,6 @@ async def test_saving_loading(hass, hass_storage):
|
||||
assert len(users) == 1
|
||||
assert users[0] == user
|
||||
|
||||
clients = await store2.async_get_clients()
|
||||
assert len(clients) == 1
|
||||
assert clients[0] == client
|
||||
|
||||
|
||||
def test_access_token_expired():
|
||||
"""Test that the expired property on access tokens work."""
|
||||
@@ -225,11 +219,10 @@ def test_access_token_expired():
|
||||
async def test_cannot_retrieve_expired_access_token(hass):
|
||||
"""Test that we cannot retrieve expired access tokens."""
|
||||
manager = await auth.auth_manager_from_config(hass, [])
|
||||
client = await manager.async_create_client('test')
|
||||
user = MockUser().add_to_auth_manager(manager)
|
||||
refresh_token = await manager.async_create_refresh_token(user, client)
|
||||
refresh_token = await manager.async_create_refresh_token(user, CLIENT_ID)
|
||||
assert refresh_token.user.id is user.id
|
||||
assert refresh_token.client_id is client.id
|
||||
assert refresh_token.client_id == CLIENT_ID
|
||||
|
||||
access_token = manager.async_create_access_token(refresh_token)
|
||||
assert manager.async_get_access_token(access_token.token) is access_token
|
||||
@@ -242,19 +235,6 @@ async def test_cannot_retrieve_expired_access_token(hass):
|
||||
assert manager.async_get_access_token(access_token.token) is None
|
||||
|
||||
|
||||
async def test_get_or_create_client(hass):
|
||||
"""Test that get_or_create_client works."""
|
||||
manager = await auth.auth_manager_from_config(hass, [])
|
||||
|
||||
client1 = await manager.async_get_or_create_client(
|
||||
'Test Client', redirect_uris=['https://test.com/1'])
|
||||
assert client1.name is 'Test Client'
|
||||
|
||||
client2 = await manager.async_get_or_create_client(
|
||||
'Test Client', redirect_uris=['https://test.com/1'])
|
||||
assert client2.id is client1.id
|
||||
|
||||
|
||||
async def test_generating_system_user(hass):
|
||||
"""Test that we can add a system user."""
|
||||
manager = await auth.auth_manager_from_config(hass, [])
|
||||
@@ -274,10 +254,9 @@ async def test_refresh_token_requires_client_for_user(hass):
|
||||
with pytest.raises(ValueError):
|
||||
await manager.async_create_refresh_token(user)
|
||||
|
||||
client = await manager.async_get_or_create_client('Test client')
|
||||
token = await manager.async_create_refresh_token(user, client)
|
||||
token = await manager.async_create_refresh_token(user, CLIENT_ID)
|
||||
assert token is not None
|
||||
assert token.client_id == client.id
|
||||
assert token.client_id == CLIENT_ID
|
||||
|
||||
|
||||
async def test_refresh_token_not_requires_client_for_system_user(hass):
|
||||
@@ -285,10 +264,9 @@ async def test_refresh_token_not_requires_client_for_system_user(hass):
|
||||
manager = await auth.auth_manager_from_config(hass, [])
|
||||
user = await manager.async_create_system_user('Hass.io')
|
||||
assert user.system_generated is True
|
||||
client = await manager.async_get_or_create_client('Test client')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
await manager.async_create_refresh_token(user, client)
|
||||
await manager.async_create_refresh_token(user, CLIENT_ID)
|
||||
|
||||
token = await manager.async_create_refresh_token(user)
|
||||
assert token is not None
|
||||
|
||||
Reference in new issue
Block a user