webauthn-registration.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2002-2024 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. "use strict";
  17. import webauthn from "./webauthn-core.js";
  18. function setVisibility(element, value) {
  19. if (!element) {
  20. return;
  21. }
  22. element.style.display = value ? "block" : "none";
  23. }
  24. function setError(ui, msg) {
  25. resetPopups(ui);
  26. const error = ui.getError();
  27. if (!error) {
  28. return;
  29. }
  30. error.textContent = msg;
  31. setVisibility(error, true);
  32. }
  33. function setSuccess(ui) {
  34. resetPopups(ui);
  35. const success = ui.getSuccess();
  36. if (!success) {
  37. return;
  38. }
  39. setVisibility(success, true);
  40. }
  41. function resetPopups(ui) {
  42. const success = ui.getSuccess();
  43. const error = ui.getError();
  44. setVisibility(success, false);
  45. setVisibility(error, false);
  46. }
  47. async function submitDeleteForm(contextPath, form, headers) {
  48. const options = {
  49. method: "DELETE",
  50. headers: {
  51. "Content-Type": "application/json",
  52. ...headers,
  53. },
  54. };
  55. await fetch(form.action, options);
  56. }
  57. /**
  58. *
  59. * @param headers headers added to the credentials creation POST request, typically CSRF
  60. * @param contextPath the contextPath from which the app is served
  61. * @param ui contains getRegisterButton(), getSuccess(), getError(), getLabelInput(), getDeleteForms()
  62. * @returns {Promise<void>}
  63. */
  64. export async function setupRegistration(headers, contextPath, ui) {
  65. resetPopups(ui);
  66. if (!window.PublicKeyCredential) {
  67. setError(ui, "WebAuthn is not supported");
  68. return;
  69. }
  70. const queryString = new URLSearchParams(window.location.search);
  71. if (queryString.has("success")) {
  72. setSuccess(ui);
  73. }
  74. ui.getRegisterButton().addEventListener("click", async () => {
  75. resetPopups(ui);
  76. const label = ui.getLabelInput().value;
  77. try {
  78. await webauthn.register(headers, contextPath, label);
  79. window.location.href = `${contextPath}/webauthn/register?success`;
  80. } catch (err) {
  81. setError(ui, err.message);
  82. console.error(err);
  83. }
  84. });
  85. ui.getDeleteForms().forEach((form) =>
  86. form.addEventListener("submit", async function (e) {
  87. e.preventDefault();
  88. try {
  89. await submitDeleteForm(contextPath, form, headers);
  90. window.location.href = `${contextPath}/webauthn/register?success`;
  91. } catch (err) {
  92. setError(ui, err.message);
  93. }
  94. }),
  95. );
  96. }