Use JWT for access tokens (#15972)
* Use JWT for access tokens * Update requirements * Improvements
This commit is contained in:
1 parent
899c2057b7
commit
f5df567d09
20 files changed
+203
-155
No files matched your search
@@ -44,7 +44,10 @@ async def test_login_new_user_and_trying_refresh_token(hass, aiohttp_client):
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
|
||||
assert hass.auth.async_get_access_token(tokens['access_token']) is not None
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens['access_token'])
|
||||
is not None
|
||||
)
|
||||
|
||||
# Use refresh token to get more tokens.
|
||||
resp = await client.post('/auth/token', data={
|
||||
@@ -56,7 +59,10 @@ async def test_login_new_user_and_trying_refresh_token(hass, aiohttp_client):
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
assert 'refresh_token' not in tokens
|
||||
assert hass.auth.async_get_access_token(tokens['access_token']) is not None
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens['access_token'])
|
||||
is not None
|
||||
)
|
||||
|
||||
# Test using access token to hit API.
|
||||
resp = await client.get('/api/')
|
||||
@@ -98,7 +104,9 @@ async def test_ws_current_user(hass, hass_ws_client, hass_access_token):
|
||||
}
|
||||
})
|
||||
|
||||
user = hass_access_token.refresh_token.user
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
user = refresh_token.user
|
||||
credential = Credentials(auth_provider_type='homeassistant',
|
||||
auth_provider_id=None,
|
||||
data={}, id='test-id')
|
||||
@@ -169,7 +177,10 @@ async def test_refresh_token_system_generated(hass, aiohttp_client):
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
assert hass.auth.async_get_access_token(tokens['access_token']) is not None
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens['access_token'])
|
||||
is not None
|
||||
)
|
||||
|
||||
|
||||
async def test_refresh_token_different_client_id(hass, aiohttp_client):
|
||||
@@ -208,4 +219,7 @@ async def test_refresh_token_different_client_id(hass, aiohttp_client):
|
||||
|
||||
assert resp.status == 200
|
||||
tokens = await resp.json()
|
||||
assert hass.auth.async_get_access_token(tokens['access_token']) is not None
|
||||
assert (
|
||||
await hass.auth.async_validate_access_token(tokens['access_token'])
|
||||
is not None
|
||||
)
|
||||
@@ -52,7 +52,7 @@ async def async_get_code(hass, aiohttp_client):
|
||||
'user': user,
|
||||
'code': step['result'],
|
||||
'client': client,
|
||||
'access_token': access_token.token,
|
||||
'access_token': access_token,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -122,11 +122,13 @@ async def test_delete_unable_self_account(hass, hass_ws_client,
|
||||
hass_access_token):
|
||||
"""Test we cannot delete our own account."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
'type': auth_config.WS_TYPE_DELETE,
|
||||
'user_id': hass_access_token.refresh_token.user.id,
|
||||
'user_id': refresh_token.user.id,
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
@@ -137,7 +139,9 @@ async def test_delete_unable_self_account(hass, hass_ws_client,
|
||||
async def test_delete_unknown_user(hass, hass_ws_client, hass_access_token):
|
||||
"""Test we cannot delete an unknown user."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
@@ -153,7 +157,9 @@ async def test_delete_unknown_user(hass, hass_ws_client, hass_access_token):
|
||||
async def test_delete(hass, hass_ws_client, hass_access_token):
|
||||
"""Test delete command works."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
test_user = MockUser(
|
||||
id='efg',
|
||||
).add_to_hass(hass)
|
||||
@@ -174,7 +180,9 @@ async def test_delete(hass, hass_ws_client, hass_access_token):
|
||||
async def test_create(hass, hass_ws_client, hass_access_token):
|
||||
"""Test create command works."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
assert len(await hass.auth.async_get_users()) == 1
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ from tests.common import MockUser, register_auth_provider
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_config(hass, aiohttp_client):
|
||||
def setup_config(hass):
|
||||
"""Fixture that sets up the auth provider homeassistant module."""
|
||||
hass.loop.run_until_complete(register_auth_provider(hass, {
|
||||
'type': 'homeassistant'
|
||||
@@ -22,7 +22,9 @@ async def test_create_auth_system_generated_user(hass, hass_access_token,
|
||||
"""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)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
@@ -47,7 +49,9 @@ async def test_create_auth_unknown_user(hass_ws_client, hass,
|
||||
hass_access_token):
|
||||
"""Test create pointing at unknown user."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
@@ -86,7 +90,9 @@ async def test_create_auth(hass, hass_ws_client, hass_access_token,
|
||||
"""Test create auth command works."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
user = MockUser().add_to_hass(hass)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
assert len(user.credentials) == 0
|
||||
|
||||
@@ -117,7 +123,9 @@ async def test_create_auth_duplicate_username(hass, hass_ws_client,
|
||||
"""Test we can't create auth with a duplicate username."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
user = MockUser().add_to_hass(hass)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
hass_storage[prov_ha.STORAGE_KEY] = {
|
||||
'version': 1,
|
||||
@@ -145,7 +153,9 @@ async def test_delete_removes_just_auth(hass_ws_client, hass, hass_storage,
|
||||
hass_access_token):
|
||||
"""Test deleting an auth without being connected to a user."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
hass_storage[prov_ha.STORAGE_KEY] = {
|
||||
'version': 1,
|
||||
@@ -171,7 +181,9 @@ async def test_delete_removes_credential(hass, hass_ws_client,
|
||||
hass_access_token, hass_storage):
|
||||
"""Test deleting auth that is connected to a user."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
user = MockUser().add_to_hass(hass)
|
||||
user.credentials.append(
|
||||
@@ -216,7 +228,9 @@ async def test_delete_requires_owner(hass, hass_ws_client, hass_access_token):
|
||||
async def test_delete_unknown_auth(hass, hass_ws_client, hass_access_token):
|
||||
"""Test trying to delete an unknown auth username."""
|
||||
client = await hass_ws_client(hass, hass_access_token)
|
||||
hass_access_token.refresh_token.user.is_owner = True
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_owner = True
|
||||
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
@@ -240,7 +254,9 @@ async def test_change_password(hass, hass_ws_client, hass_access_token):
|
||||
'username': 'test-user'
|
||||
})
|
||||
|
||||
user = hass_access_token.refresh_token.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)
|
||||
@@ -268,7 +284,9 @@ async def test_change_password_wrong_pw(hass, hass_ws_client,
|
||||
'username': 'test-user'
|
||||
})
|
||||
|
||||
user = hass_access_token.refresh_token.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)
|
||||
|
||||
@@ -28,7 +28,7 @@ def hass_ws_client(aiohttp_client):
|
||||
|
||||
await websocket.send_json({
|
||||
'type': websocket_api.TYPE_AUTH,
|
||||
'access_token': access_token.token
|
||||
'access_token': access_token
|
||||
})
|
||||
|
||||
auth_ok = await websocket.receive_json()
|
||||
|
||||
@@ -106,7 +106,11 @@ async def test_setup_api_push_api_data_default(hass, aioclient_mock,
|
||||
)
|
||||
assert hassio_user is not None
|
||||
assert hassio_user.system_generated
|
||||
assert refresh_token in hassio_user.refresh_tokens
|
||||
for token in hassio_user.refresh_tokens.values():
|
||||
if token.token == refresh_token:
|
||||
break
|
||||
else:
|
||||
assert False, 'refresh token not found'
|
||||
|
||||
|
||||
async def test_setup_api_push_api_data_no_auth(hass, aioclient_mock,
|
||||
|
||||
@@ -156,9 +156,9 @@ async def test_access_with_trusted_ip(app2, aiohttp_client):
|
||||
|
||||
|
||||
async def test_auth_active_access_with_access_token_in_header(
|
||||
app, aiohttp_client, hass_access_token):
|
||||
hass, app, aiohttp_client, hass_access_token):
|
||||
"""Test access with access token in header."""
|
||||
token = hass_access_token.token
|
||||
token = hass_access_token
|
||||
setup_auth(app, [], True, api_password=None)
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
@@ -182,7 +182,9 @@ async def test_auth_active_access_with_access_token_in_header(
|
||||
'/', headers={'Authorization': 'BEARER {}'.format(token)})
|
||||
assert req.status == 401
|
||||
|
||||
hass_access_token.refresh_token.user.is_active = False
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_active = False
|
||||
req = await client.get(
|
||||
'/', headers={'Authorization': 'Bearer {}'.format(token)})
|
||||
assert req.status == 401
|
||||
|
||||
@@ -448,13 +448,15 @@ async def test_api_fire_event_context(hass, mock_api_client,
|
||||
await mock_api_client.post(
|
||||
const.URL_API_EVENTS_EVENT.format("test.event"),
|
||||
headers={
|
||||
'authorization': 'Bearer {}'.format(hass_access_token.token)
|
||||
'authorization': 'Bearer {}'.format(hass_access_token)
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
|
||||
assert len(test_value) == 1
|
||||
assert test_value[0].context.user_id == \
|
||||
hass_access_token.refresh_token.user.id
|
||||
assert test_value[0].context.user_id == refresh_token.user.id
|
||||
|
||||
|
||||
async def test_api_call_service_context(hass, mock_api_client,
|
||||
@@ -465,12 +467,15 @@ async def test_api_call_service_context(hass, mock_api_client,
|
||||
await mock_api_client.post(
|
||||
'/api/services/test_domain/test_service',
|
||||
headers={
|
||||
'authorization': 'Bearer {}'.format(hass_access_token.token)
|
||||
'authorization': 'Bearer {}'.format(hass_access_token)
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.user_id == hass_access_token.refresh_token.user.id
|
||||
assert calls[0].context.user_id == refresh_token.user.id
|
||||
|
||||
|
||||
async def test_api_set_state_context(hass, mock_api_client, hass_access_token):
|
||||
@@ -481,8 +486,11 @@ async def test_api_set_state_context(hass, mock_api_client, hass_access_token):
|
||||
'state': 'on'
|
||||
},
|
||||
headers={
|
||||
'authorization': 'Bearer {}'.format(hass_access_token.token)
|
||||
'authorization': 'Bearer {}'.format(hass_access_token)
|
||||
})
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
|
||||
state = hass.states.get('light.kitchen')
|
||||
assert state.context.user_id == hass_access_token.refresh_token.user.id
|
||||
assert state.context.user_id == refresh_token.user.id
|
||||
@@ -334,7 +334,7 @@ async def test_auth_active_with_token(hass, aiohttp_client, hass_access_token):
|
||||
|
||||
await ws.send_json({
|
||||
'type': wapi.TYPE_AUTH,
|
||||
'access_token': hass_access_token.token
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
@@ -344,7 +344,9 @@ async def test_auth_active_with_token(hass, aiohttp_client, hass_access_token):
|
||||
async def test_auth_active_user_inactive(hass, aiohttp_client,
|
||||
hass_access_token):
|
||||
"""Test authenticating with a token."""
|
||||
hass_access_token.refresh_token.user.is_active = False
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
refresh_token.user.is_active = False
|
||||
assert await async_setup_component(hass, 'websocket_api', {
|
||||
'http': {
|
||||
'api_password': API_PASSWORD
|
||||
@@ -361,7 +363,7 @@ async def test_auth_active_user_inactive(hass, aiohttp_client,
|
||||
|
||||
await ws.send_json({
|
||||
'type': wapi.TYPE_AUTH,
|
||||
'access_token': hass_access_token.token
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
@@ -465,7 +467,7 @@ async def test_call_service_context_with_user(hass, aiohttp_client,
|
||||
|
||||
await ws.send_json({
|
||||
'type': wapi.TYPE_AUTH,
|
||||
'access_token': hass_access_token.token
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
@@ -484,12 +486,15 @@ async def test_call_service_context_with_user(hass, aiohttp_client,
|
||||
msg = await ws.receive_json()
|
||||
assert msg['success']
|
||||
|
||||
refresh_token = await hass.auth.async_validate_access_token(
|
||||
hass_access_token)
|
||||
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == 'domain_test'
|
||||
assert call.service == 'test_service'
|
||||
assert call.data == {'hello': 'world'}
|
||||
assert call.context.user_id == hass_access_token.refresh_token.user.id
|
||||
assert call.context.user_id == refresh_token.user.id
|
||||
|
||||
|
||||
async def test_call_service_context_no_user(hass, aiohttp_client):
|
||||
|
||||
Reference in new issue
Block a user