Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | window.addEventListener("load", function() { |
| 2 | document.querySelector(".addsecuritykey").addEventListener("click", function() { |
| 3 | document.querySelector("#addsecuritykey").showModal(); |
| 4 | /* Or dialog.show(); to show the dialog without a backdrop. */ |
| 5 | }); |
| 6 | |
| 7 | document.getElementById("registersecuritykey").addEventListener("click", e => { |
| 8 | e.preventDefault(); |
| 9 | |
| 10 | if (document.getElementById("addsecuritykeyform").reportValidity()) { |
| 11 | fetch("ajax/addsecuritykey.php", { |
| 12 | method: "POST" |
| 13 | }).then(response => { |
| 14 | if (response.status !== 200) { |
| 15 | response.text(); // @TODO: Remove this. It is only used so the response is available in Chrome Dev Tools |
| 16 | throw new Error("HTTP status is not 200."); |
| 17 | } |
| 18 | |
| 19 | return response.json(); |
| 20 | }).then(response => { |
| 21 | recursiveBase64StrToArrayBuffer(response); |
| 22 | return response; |
| 23 | }).then(createCredentialArgs => { |
| 24 | return navigator.credentials.create(createCredentialArgs); |
| 25 | }).then(cred => { |
| 26 | return { |
| 27 | clientDataJSON: cred.response.clientDataJSON ? arrayBufferToBase64(cred.response.clientDataJSON) : null, |
| 28 | attestationObject: cred.response.attestationObject ? arrayBufferToBase64(cred.response.attestationObject) : null, |
| 29 | name: document.getElementById("name").value |
| 30 | }; |
| 31 | }).then(JSON.stringify).then(AuthenticatorAttestationResponse => { |
| 32 | return window.fetch("ajax/addsecuritykey2.php", { |
| 33 | method: "POST", |
| 34 | body: AuthenticatorAttestationResponse, |
| 35 | }); |
| 36 | }).then(response => { |
| 37 | if (response.status !== 200) { |
| 38 | response.text(); // @TODO: remove this. It is only used so the response is available in Chrome Dev Tools |
| 39 | throw new Error("HTTP status is not 200 (2)."); |
| 40 | } |
| 41 | |
| 42 | return response.json(); |
| 43 | }).then(json => { |
| 44 | if (json.status == "ok") { |
| 45 | document.location = "securitykeys.php?msg=securitykeyadded"; |
| 46 | } |
| 47 | }).catch(err => console.error("An unexpected error occurred.", err)); |
| 48 | } |
| 49 | }); |
| 50 | }); |