mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-06 19:34:15 -05:00
since we are hard-coding a generic error message on the front-end, we have no need to do anything on the back end. This is also nice as it standardises the two flows to behave more like each other (rather than previously where one would `flash` an error message and the other would return CBOR for the js to decode). Note that the register flow returns 400 while the auth flow returns 403. The js for both just checks `response.ok` so will handle both. The JS completely discards any body returned if the status isn't 200 now.
59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
(function(window) {
|
|
"use strict";
|
|
|
|
window.GOVUK.Modules.RegisterSecurityKey = function() {
|
|
this.start = function(component) {
|
|
$(component)
|
|
.on('click', function(event) {
|
|
event.preventDefault();
|
|
|
|
// hide any existing error prompt
|
|
window.GOVUK.ErrorBanner.hideBanner();
|
|
|
|
fetch('/webauthn/register')
|
|
.then((response) => {
|
|
if (!response.ok) {
|
|
throw Error(response.statusText);
|
|
}
|
|
|
|
return response.arrayBuffer();
|
|
})
|
|
.then((data) => {
|
|
var options = window.CBOR.decode(data);
|
|
// triggers browser dialogue to select authenticator
|
|
return window.navigator.credentials.create(options);
|
|
})
|
|
.then((credential) => {
|
|
return postWebAuthnCreateResponse(
|
|
credential.response, component.data('csrfToken')
|
|
);
|
|
})
|
|
.then((response) => {
|
|
if (!response.ok) {
|
|
throw Error(response.statusText);
|
|
}
|
|
|
|
window.location.reload();
|
|
})
|
|
.catch((error) => {
|
|
console.error(error);
|
|
// some browsers will show an error dialogue for some
|
|
// errors; to be safe we always display an error message on the page.
|
|
window.GOVUK.ErrorBanner.showBanner();
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
function postWebAuthnCreateResponse(response, csrf_token) {
|
|
return fetch('/webauthn/register', {
|
|
method: 'POST',
|
|
headers: { 'X-CSRFToken': csrf_token },
|
|
body: window.CBOR.encode({
|
|
attestationObject: new Uint8Array(response.attestationObject),
|
|
clientDataJSON: new Uint8Array(response.clientDataJSON),
|
|
})
|
|
});
|
|
}
|
|
})(window);
|