You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.5 KiB

  1. $(function() {
  2. var form = document.getElementById("add-credential-form");
  3. var optionsBytes = Uint8Array.from(atob(form.dataset.options), c => c.charCodeAt(0));
  4. // cbor.js expects ArrayBuffer as input when decoding
  5. var options = CBOR.decode(optionsBytes.buffer);
  6. function b64(arraybuffer) {
  7. return btoa(String.fromCharCode.apply(null, new Uint8Array(arraybuffer)));
  8. }
  9. function requestCredentials() {
  10. // Hide error & success messages, show the "waiting" message
  11. $("#name-next").addClass("hide");
  12. $("#waiting").removeClass("hide");
  13. $("#error").addClass("hide");
  14. $("#success").addClass("hide");
  15. navigator.credentials.create(options).then(function(attestation) {
  16. $("#attestation_object").val(b64(attestation.response.attestationObject));
  17. $("#client_data_json").val(b64(attestation.response.clientDataJSON));
  18. // Show the success message and save button
  19. $("#waiting").addClass("hide");
  20. $("#success").removeClass("hide");
  21. }).catch(function(err) {
  22. // Show the error message
  23. $("#waiting").addClass("hide");
  24. $("#error-text").text(err);
  25. $("#error").removeClass("hide");
  26. });
  27. }
  28. $("#name").on('keypress',function(e) {
  29. if (e.which == 13) {
  30. e.preventDefault();
  31. requestCredentials();
  32. }
  33. });
  34. $("#name-next").click(requestCredentials);
  35. $("#retry").click(requestCredentials);
  36. });