u2f.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /** global: AJAX */
  2. /** global: PMA_messages */
  3. /** global: u2f */
  4. AJAX.registerOnload('u2f.js', function () {
  5. var $inputReg = $('#u2f_registration_response');
  6. if ($inputReg.length > 0) {
  7. var $formReg = $inputReg.parents('form');
  8. $formReg.find('input[type=submit]').hide();
  9. setTimeout(function () {
  10. // A magic JS function that talks to the USB device. This function will keep polling for the USB device until it finds one.
  11. var request = JSON.parse($inputReg.attr('data-request'));
  12. u2f.register(request.appId, [request], JSON.parse($inputReg.attr('data-signatures')), function (data) {
  13. // Handle returning error data
  14. if (data.errorCode && data.errorCode !== 0) {
  15. if (data.errorCode === 5) {
  16. PMA_ajaxShowMessage(PMA_messages.strU2FTimeout, false);
  17. } else {
  18. PMA_ajaxShowMessage(
  19. PMA_sprintf(PMA_messages.strU2FError, data.errorCode), false
  20. );
  21. }
  22. return;
  23. }
  24. // Fill and submit form.
  25. $inputReg.val(JSON.stringify(data));
  26. $formReg.submit();
  27. });
  28. }, 1000);
  29. }
  30. var $inputAuth = $('#u2f_authentication_response');
  31. if ($inputAuth.length > 0) {
  32. var $formAuth = $inputAuth.parents('form');
  33. $formAuth.find('input[type=submit]').hide();
  34. setTimeout(function () {
  35. // Magic JavaScript talking to your HID
  36. // appid, challenge, authenticateRequests
  37. var request = JSON.parse($inputAuth.attr('data-request'));
  38. var handles = [request[0].keyHandle];
  39. u2f.sign(request[0].appId, request[0].challenge, request, function (data) {
  40. // Handle returning error data
  41. if (data.errorCode && data.errorCode !== 0) {
  42. if (data.errorCode === 5) {
  43. PMA_ajaxShowMessage(PMA_messages.strU2FTimeout, false);
  44. } else {
  45. PMA_ajaxShowMessage(
  46. PMA_sprintf(PMA_messages.strU2FError, data.errorCode), false
  47. );
  48. }
  49. return;
  50. }
  51. // Fill and submit form.
  52. $inputAuth.val(JSON.stringify(data));
  53. $formAuth.submit();
  54. });
  55. }, 1000);
  56. }
  57. });