Avoid registering the same authenticator twice

This passes existing credentials in the server response, to allow
the browser to prevent re-registering the same key for the same
user. Registering the same key multiple times doesn't seem to be
an issue technically; the user has likely got their keys mixed up.

- Chrome says "you don't need to register it again".
- Safari exits with an InvalidStateError.
- Firefox exits with a DOMException.
This commit is contained in:
Ben Thorner
2021-05-11 14:22:41 +01:00
parent e2cf3e2c70
commit 957dba4356
5 changed files with 27 additions and 9 deletions

View File

@@ -51,6 +51,24 @@ def test_begin_register_returns_encoded_options(
assert relying_party_options['id'] == 'localhost'
def test_begin_register_includes_existing_credentials(
platform_admin_client,
webauthn_credential,
mocker,
):
mocker.patch(
'app.user_api_client.get_webauthn_credentials_for_user',
return_value=[webauthn_credential, webauthn_credential]
)
response = platform_admin_client.get(
url_for('main.webauthn_begin_register')
)
webauthn_options = cbor.decode(response.data)['publicKey']
assert len(webauthn_options['excludeCredentials']) == 2
def test_begin_register_stores_state_in_session(
platform_admin_client,
):

View File

@@ -243,7 +243,7 @@ def test_add_user_to_service_calls_correct_endpoint_and_deletes_keys_from_cache(
def test_get_webauthn_credentials_for_user_returns_stubbed_data():
credentials = user_api_client.get_webauthn_credentials_for_user('id')
assert credentials[0]['name'] == 'Ben test'
assert len(credentials) == 0
def test_create_webauthn_credential_for_user_stores_stubbed_data(webauthn_credential):

View File

@@ -4485,7 +4485,7 @@ def mock_get_invited_org_user_by_id(mocker, sample_org_invite):
def webauthn_credential():
return {
'name': 'Test credential',
'credential_data': 'credential_data',
'credential_data': b'WJ0AAAAAAAAAAAAAAAAAAAAAAECKU1ppjl9gmhHWyDkgHsUvZmhr6oF3/lD3llzLE2SaOSgOGIsIuAQqgp8JQSUu3r/oOaP8RS44dlQjrH+ALfYtpAECAyYhWCAxnqAfESXOYjKUc2WACuXZ3ch0JHxV0VFrrTyjyjIHXCJYIFnx8H87L4bApR4M+hPcV+fHehEOeW+KCyd0H+WGY8s6', # noqa
'registration_response': 'anything',
'created_at': '2017-10-18T16:57:14.154185Z',
}