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
@@ -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)
|
||||
Reference in new issue
Block a user