Support registering a new authenticator
This adds Yubico's FIDO2 library and two APIs for working with the
"navigator.credentials.create()" function in JavaScript. The GET
API uses the library to generate options for the "create()" function,
and the POST API decodes and verifies the resulting credential. While
the options and response are dict-like, CBOR is necessary to encode
some of the byte-level values, which can't be represented in JSON.
Much of the code here is based on the Yubico library example [1][2].
Implementation notes:
- There are definitely better ways to alert the user about failure, but
window.alert() will do for the time being. Using location.reload() is
also a bit jarring if the page scrolls, but not a major issue.
- Ideally we would use window.fetch() to do AJAX calls, but we don't
have a polyfill for this, and we use $.ajax() elsewhere [3]. We need
to do a few weird tricks [6] to stop jQuery trashing the data.
- The FIDO2 server doesn't serve web requests; it's just a "server" in
the sense of WebAuthn terminology. It lives in its own module, since it
needs to be initialised with the app / config.
- $.ajax returns a promise-like object. Although we've used ".fail()"
elsewhere [3], I couldn't find a stub object that supports it, so I've
gone for ".catch()", and used a Promise stub object in tests.
- WebAuthn only works over HTTPS, but there's an exception for "localhost"
[4]. However, the library is a bit too strict [5], so we have to disable
origin verification to avoid needing HTTPS for dev work.
[1]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/examples/server/server.py
[2]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/examples/server/static/register.html
[3]: https://github.com/alphagov/notifications-admin/blob/91453d36395b7a0cf2998dfb8a5f52cc9e96640f/app/assets/javascripts/updateContent.js#L33
[4]: https://stackoverflow.com/questions/55971593/navigator-credentials-is-null-on-local-server
[5]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/rpid.py#L69
[6]: https://stackoverflow.com/questions/12394622/does-jquery-ajax-or-load-allow-for-responsetype-arraybuffer
2021-05-07 18:10:07 +01:00
|
|
|
from fido2 import cbor
|
|
|
|
|
from flask import current_app, request, session
|
|
|
|
|
from flask_login import current_user
|
|
|
|
|
|
|
|
|
|
from app.main import main
|
|
|
|
|
from app.models.webauthn_credential import WebAuthnCredential
|
|
|
|
|
from app.notify_client.user_api_client import user_api_client
|
|
|
|
|
from app.utils import user_is_platform_admin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/webauthn/register')
|
|
|
|
|
@user_is_platform_admin
|
|
|
|
|
def webauthn_begin_register():
|
|
|
|
|
server = current_app.webauthn_server
|
|
|
|
|
|
|
|
|
|
registration_data, state = server.register_begin(
|
|
|
|
|
{
|
|
|
|
|
"id": bytes(current_user.id, 'utf-8'),
|
|
|
|
|
"name": current_user.email_address,
|
|
|
|
|
"displayName": current_user.name,
|
|
|
|
|
},
|
2021-05-11 14:22:41 +01:00
|
|
|
credentials=[
|
|
|
|
|
credential.to_credential_data()
|
|
|
|
|
for credential in current_user.webauthn_credentials
|
|
|
|
|
],
|
Support registering a new authenticator
This adds Yubico's FIDO2 library and two APIs for working with the
"navigator.credentials.create()" function in JavaScript. The GET
API uses the library to generate options for the "create()" function,
and the POST API decodes and verifies the resulting credential. While
the options and response are dict-like, CBOR is necessary to encode
some of the byte-level values, which can't be represented in JSON.
Much of the code here is based on the Yubico library example [1][2].
Implementation notes:
- There are definitely better ways to alert the user about failure, but
window.alert() will do for the time being. Using location.reload() is
also a bit jarring if the page scrolls, but not a major issue.
- Ideally we would use window.fetch() to do AJAX calls, but we don't
have a polyfill for this, and we use $.ajax() elsewhere [3]. We need
to do a few weird tricks [6] to stop jQuery trashing the data.
- The FIDO2 server doesn't serve web requests; it's just a "server" in
the sense of WebAuthn terminology. It lives in its own module, since it
needs to be initialised with the app / config.
- $.ajax returns a promise-like object. Although we've used ".fail()"
elsewhere [3], I couldn't find a stub object that supports it, so I've
gone for ".catch()", and used a Promise stub object in tests.
- WebAuthn only works over HTTPS, but there's an exception for "localhost"
[4]. However, the library is a bit too strict [5], so we have to disable
origin verification to avoid needing HTTPS for dev work.
[1]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/examples/server/server.py
[2]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/examples/server/static/register.html
[3]: https://github.com/alphagov/notifications-admin/blob/91453d36395b7a0cf2998dfb8a5f52cc9e96640f/app/assets/javascripts/updateContent.js#L33
[4]: https://stackoverflow.com/questions/55971593/navigator-credentials-is-null-on-local-server
[5]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/rpid.py#L69
[6]: https://stackoverflow.com/questions/12394622/does-jquery-ajax-or-load-allow-for-responsetype-arraybuffer
2021-05-07 18:10:07 +01:00
|
|
|
user_verification="discouraged", # don't ask for PIN
|
|
|
|
|
authenticator_attachment="cross-platform",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
session["webauthn_registration_state"] = state
|
|
|
|
|
return cbor.encode(registration_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/webauthn/register', methods=['POST'])
|
|
|
|
|
@user_is_platform_admin
|
|
|
|
|
def webauthn_complete_register():
|
|
|
|
|
credential = WebAuthnCredential.from_registration(
|
|
|
|
|
session.pop("webauthn_registration_state"),
|
|
|
|
|
cbor.decode(request.get_data()),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
user_api_client.create_webauthn_credential_for_user(
|
|
|
|
|
current_user.id, credential
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return ''
|