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
|
2021-05-14 17:37:57 +01:00
|
|
|
|
from fido2.client import ClientData
|
|
|
|
|
|
from fido2.ctap2 import AuthenticatorData
|
2021-05-17 15:56:15 +01:00
|
|
|
|
from flask import abort, current_app, flash, redirect, request, session, url_for
|
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 flask_login import current_user
|
2021-05-27 14:30:11 +01:00
|
|
|
|
from werkzeug.exceptions import Forbidden
|
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 app.main import main
|
2021-05-17 15:56:15 +01:00
|
|
|
|
from app.main.views.two_factor import log_in_user
|
2021-05-14 17:37:57 +01:00
|
|
|
|
from app.models.user import User
|
Handle errors when registration fails
Previously we would raise a 500 error in a variety of cases:
- If a second key was being registered simultaneously (e.g. in a
separate tab), which means the registration state could be missing
after the first registration completes. That smells like an attack.
- If the server-side verification failed e.g. origin verification,
challenge verification, etc. The library seems to use 'ValueError'
for all such errors [1] (after auditing its 'raise' statements, and
excluding AttestationError [2], since we're not doing that).
- If a key is used that attempts to sign with an unsupported
algorithm. This would normally raise a NotImplemented error as part
of verifying attestation [3], but we don't do that, so we need to
verify the algorithm is supported by the library manually.
This adds error handling to return a 400 response and error message
in these cases, since the error is not unexpected (i.e. not a 500).
A 400 seems more appropriate than a 403, since in many cases it's
not clear if the request data is valid.
I've used CBOR for the transport encoding, to match the successful
request / response encoding. Note that the ordering of then/catch
matters in JS - we don't want to catch our own throws!
[1]: https://github.com/Yubico/python-fido2/blob/142587b3e698ca0e253c78d75758fda635cac51a/fido2/server.py#L255
[2]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/attestation/base.py#L39
[3]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/cose.py#L92
2021-05-14 09:17:12 +01:00
|
|
|
|
from app.models.webauthn_credential import RegistrationError, WebAuthnCredential
|
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 app.notify_client.user_api_client import user_api_client
|
2021-05-17 15:56:15 +01:00
|
|
|
|
from app.utils import (
|
|
|
|
|
|
is_less_than_days_ago,
|
|
|
|
|
|
redirect_to_sign_in,
|
|
|
|
|
|
user_is_platform_admin,
|
|
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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-06-08 09:41:39 +01:00
|
|
|
|
credentials=current_user.webauthn_credentials.as_cbor,
|
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():
|
Handle errors when registration fails
Previously we would raise a 500 error in a variety of cases:
- If a second key was being registered simultaneously (e.g. in a
separate tab), which means the registration state could be missing
after the first registration completes. That smells like an attack.
- If the server-side verification failed e.g. origin verification,
challenge verification, etc. The library seems to use 'ValueError'
for all such errors [1] (after auditing its 'raise' statements, and
excluding AttestationError [2], since we're not doing that).
- If a key is used that attempts to sign with an unsupported
algorithm. This would normally raise a NotImplemented error as part
of verifying attestation [3], but we don't do that, so we need to
verify the algorithm is supported by the library manually.
This adds error handling to return a 400 response and error message
in these cases, since the error is not unexpected (i.e. not a 500).
A 400 seems more appropriate than a 403, since in many cases it's
not clear if the request data is valid.
I've used CBOR for the transport encoding, to match the successful
request / response encoding. Note that the ordering of then/catch
matters in JS - we don't want to catch our own throws!
[1]: https://github.com/Yubico/python-fido2/blob/142587b3e698ca0e253c78d75758fda635cac51a/fido2/server.py#L255
[2]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/attestation/base.py#L39
[3]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/cose.py#L92
2021-05-14 09:17:12 +01:00
|
|
|
|
if 'webauthn_registration_state' not in session:
|
|
|
|
|
|
return cbor.encode("No registration in progress"), 400
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
credential = WebAuthnCredential.from_registration(
|
|
|
|
|
|
session.pop("webauthn_registration_state"),
|
|
|
|
|
|
cbor.decode(request.get_data()),
|
|
|
|
|
|
)
|
|
|
|
|
|
except RegistrationError as e:
|
2021-06-03 17:31:27 +01:00
|
|
|
|
current_app.logger.info(f'User {current_user.id} could not register a new webauthn token - {e}')
|
Handle errors when registration fails
Previously we would raise a 500 error in a variety of cases:
- If a second key was being registered simultaneously (e.g. in a
separate tab), which means the registration state could be missing
after the first registration completes. That smells like an attack.
- If the server-side verification failed e.g. origin verification,
challenge verification, etc. The library seems to use 'ValueError'
for all such errors [1] (after auditing its 'raise' statements, and
excluding AttestationError [2], since we're not doing that).
- If a key is used that attempts to sign with an unsupported
algorithm. This would normally raise a NotImplemented error as part
of verifying attestation [3], but we don't do that, so we need to
verify the algorithm is supported by the library manually.
This adds error handling to return a 400 response and error message
in these cases, since the error is not unexpected (i.e. not a 500).
A 400 seems more appropriate than a 403, since in many cases it's
not clear if the request data is valid.
I've used CBOR for the transport encoding, to match the successful
request / response encoding. Note that the ordering of then/catch
matters in JS - we don't want to catch our own throws!
[1]: https://github.com/Yubico/python-fido2/blob/142587b3e698ca0e253c78d75758fda635cac51a/fido2/server.py#L255
[2]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/attestation/base.py#L39
[3]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/cose.py#L92
2021-05-14 09:17:12 +01:00
|
|
|
|
return cbor.encode(str(e)), 400
|
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
|
|
|
|
|
2021-06-07 13:51:39 +01:00
|
|
|
|
current_user.create_webauthn_credential(credential)
|
2021-06-07 13:53:33 +01:00
|
|
|
|
current_user.update(auth_type='webauthn_auth')
|
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
|
|
|
|
|
2021-06-01 17:07:23 +01:00
|
|
|
|
flash((
|
|
|
|
|
|
'Registration complete. Next time you sign in to Notify '
|
|
|
|
|
|
'you’ll be asked to use your security key.'
|
|
|
|
|
|
), 'default_with_tick')
|
|
|
|
|
|
|
Handle errors when registration fails
Previously we would raise a 500 error in a variety of cases:
- If a second key was being registered simultaneously (e.g. in a
separate tab), which means the registration state could be missing
after the first registration completes. That smells like an attack.
- If the server-side verification failed e.g. origin verification,
challenge verification, etc. The library seems to use 'ValueError'
for all such errors [1] (after auditing its 'raise' statements, and
excluding AttestationError [2], since we're not doing that).
- If a key is used that attempts to sign with an unsupported
algorithm. This would normally raise a NotImplemented error as part
of verifying attestation [3], but we don't do that, so we need to
verify the algorithm is supported by the library manually.
This adds error handling to return a 400 response and error message
in these cases, since the error is not unexpected (i.e. not a 500).
A 400 seems more appropriate than a 403, since in many cases it's
not clear if the request data is valid.
I've used CBOR for the transport encoding, to match the successful
request / response encoding. Note that the ordering of then/catch
matters in JS - we don't want to catch our own throws!
[1]: https://github.com/Yubico/python-fido2/blob/142587b3e698ca0e253c78d75758fda635cac51a/fido2/server.py#L255
[2]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/attestation/base.py#L39
[3]: https://github.com/Yubico/python-fido2/blob/c42d9628a4f33d20c4401096fa8d3fc466d5b77f/fido2/cose.py#L92
2021-05-14 09:17:12 +01:00
|
|
|
|
return cbor.encode('')
|
2021-05-14 17:37:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/webauthn/authenticate', methods=['GET'])
|
|
|
|
|
|
@redirect_to_sign_in
|
|
|
|
|
|
def webauthn_begin_authentication():
|
2021-06-03 16:58:59 +01:00
|
|
|
|
"""
|
|
|
|
|
|
Initiate the authentication flow. This is called after the user clicks the "Check security key" button.
|
|
|
|
|
|
|
|
|
|
|
|
1. Get the user's credentials out of the database to present to the browser. The browser will only let you use a
|
|
|
|
|
|
credential in that list.
|
|
|
|
|
|
2. Call webauthn_server.authenticate_begin. This returns the authentication data, which includes the challenge and
|
|
|
|
|
|
the origin domain to authenticate with. This also returns the state, which we store in the cookie so we can ensure
|
|
|
|
|
|
the challenge is correct in webauthn_complete_authentication
|
|
|
|
|
|
"""
|
2021-05-14 17:37:57 +01:00
|
|
|
|
# get user from session
|
|
|
|
|
|
user_to_login = User.from_id(session['user_details']['id'])
|
|
|
|
|
|
|
2021-05-14 18:14:13 +01:00
|
|
|
|
if not user_to_login.webauthn_auth:
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
if not user_to_login.platform_admin:
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
2021-05-14 17:37:57 +01:00
|
|
|
|
authentication_data, state = current_app.webauthn_server.authenticate_begin(
|
2021-06-08 09:41:39 +01:00
|
|
|
|
credentials=user_to_login.webauthn_credentials.as_cbor,
|
2021-06-03 17:01:04 +01:00
|
|
|
|
user_verification="discouraged", # don't ask for PIN
|
2021-05-14 17:37:57 +01:00
|
|
|
|
)
|
|
|
|
|
|
session["webauthn_authentication_state"] = state
|
|
|
|
|
|
return cbor.encode(authentication_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/webauthn/authenticate', methods=['POST'])
|
|
|
|
|
|
@redirect_to_sign_in
|
|
|
|
|
|
def webauthn_complete_authentication():
|
2021-06-03 16:58:59 +01:00
|
|
|
|
"""
|
|
|
|
|
|
Complete the authentication flow. This is called after the user taps on their security key.
|
|
|
|
|
|
|
|
|
|
|
|
1. Try verifying the signed challenge returned from the browser with each public key we have in the database for
|
|
|
|
|
|
that user.
|
|
|
|
|
|
2. If succesful, log the user in, setting up the session etc. Then return the URL they should be redirected to.
|
|
|
|
|
|
"""
|
2021-05-14 17:37:57 +01:00
|
|
|
|
user_id = session['user_details']['id']
|
|
|
|
|
|
user_to_login = User.from_id(user_id)
|
|
|
|
|
|
|
2021-05-14 18:14:13 +01:00
|
|
|
|
if not user_to_login.webauthn_auth:
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
if not user_to_login.platform_admin:
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
2021-05-27 14:30:11 +01:00
|
|
|
|
try:
|
2021-06-02 11:25:02 +01:00
|
|
|
|
_verify_webauthn_authentication(user_to_login)
|
|
|
|
|
|
redirect = _complete_webauthn_login_attempt(user_to_login)
|
2021-05-27 14:30:11 +01:00
|
|
|
|
except Forbidden:
|
|
|
|
|
|
# We don't expect to reach this case in normal situations - normally errors (such as using the wrong
|
|
|
|
|
|
# security key) will be caught in the browser inside `window.navigator.credentials.get`, and the js will
|
|
|
|
|
|
# error first meaning it doesn't send the POST request to this method. If this method is called but the key
|
|
|
|
|
|
# couldn't be authenticated, something went wrong along the way, probably:
|
|
|
|
|
|
# * The browser didn't implement the webauthn standard correctly, and let something through it shouldn't have
|
|
|
|
|
|
# * The key itself is in some way corrupted, or of lower security standard
|
|
|
|
|
|
flash('Security key not recognised')
|
|
|
|
|
|
|
|
|
|
|
|
# flash sets the error message in the user's session cookie, and flask renders it next time `render_template`
|
|
|
|
|
|
# is called. In authenticateSecurityKey.js we refresh the page if this POST returns a 403.
|
|
|
|
|
|
# we can't use `abort(403)` here, and just return an empty body instead as our 403 error handler would return
|
|
|
|
|
|
# an error page response containing the flash, but our javascript ignores the body of the error response and
|
|
|
|
|
|
# just looks at the error code
|
|
|
|
|
|
return '', 403
|
2021-05-17 15:56:15 +01:00
|
|
|
|
|
2021-05-25 15:50:14 +01:00
|
|
|
|
return cbor.encode({'redirect_url': redirect.location}), 200
|
2021-05-17 15:56:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-06-02 11:25:02 +01:00
|
|
|
|
def _verify_webauthn_authentication(user):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Check that the presented security key is valid, has signed the right challenge, and belongs to the user
|
|
|
|
|
|
we're trying to log in.
|
|
|
|
|
|
"""
|
2021-05-14 18:14:13 +01:00
|
|
|
|
state = session.pop("webauthn_authentication_state")
|
|
|
|
|
|
request_data = cbor.decode(request.get_data())
|
|
|
|
|
|
|
2021-05-14 17:37:57 +01:00
|
|
|
|
try:
|
|
|
|
|
|
current_app.webauthn_server.authenticate_complete(
|
|
|
|
|
|
state=state,
|
2021-06-08 09:41:39 +01:00
|
|
|
|
credentials=user.webauthn_credentials.as_cbor,
|
2021-05-14 17:37:57 +01:00
|
|
|
|
credential_id=request_data['credentialId'],
|
|
|
|
|
|
client_data=ClientData(request_data['clientDataJSON']),
|
|
|
|
|
|
auth_data=AuthenticatorData(request_data['authenticatorData']),
|
|
|
|
|
|
signature=request_data['signature']
|
|
|
|
|
|
)
|
|
|
|
|
|
except ValueError as exc:
|
2021-05-17 15:56:15 +01:00
|
|
|
|
current_app.logger.info(f'User {user.id} could not sign in using their webauthn token - {exc}')
|
2021-06-02 11:25:02 +01:00
|
|
|
|
user.complete_webauthn_login_attempt(is_successful=False)
|
2021-05-14 17:37:57 +01:00
|
|
|
|
abort(403)
|
|
|
|
|
|
|
2021-05-17 15:56:15 +01:00
|
|
|
|
|
2021-06-02 11:25:02 +01:00
|
|
|
|
def _complete_webauthn_login_attempt(user):
|
2021-05-17 15:56:15 +01:00
|
|
|
|
"""
|
|
|
|
|
|
* check the user hasn't gone over their max logins
|
|
|
|
|
|
* check that the user's email is validated
|
|
|
|
|
|
* if succesful, update current_session_id, log in date, and then redirect
|
|
|
|
|
|
"""
|
|
|
|
|
|
redirect_url = request.args.get('next')
|
|
|
|
|
|
|
|
|
|
|
|
# normally API handles this when verifying an sms or email code but since the webauthn logic happens in the
|
|
|
|
|
|
# admin we need a separate call that just finalises the login in the database
|
2021-06-02 11:25:02 +01:00
|
|
|
|
logged_in, _ = user.complete_webauthn_login_attempt()
|
2021-05-17 15:56:15 +01:00
|
|
|
|
if not logged_in:
|
|
|
|
|
|
# user account is locked as too many failed logins
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
if not is_less_than_days_ago(user.email_access_validated_at, 90):
|
|
|
|
|
|
user_api_client.send_verify_code(user.id, 'email', None, redirect_url)
|
|
|
|
|
|
return redirect(url_for('.revalidate_email_sent', next=redirect_url))
|
|
|
|
|
|
|
|
|
|
|
|
return log_in_user(user.id)
|