Translated using Weblate (Ukrainian)
[phpmyadmin.git] / js / u2f.js
blob47f3e8d70112c72451c99f5b24342917a9b096a2
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                 }
25                 // Fill and submit form.
26                 $inputReg.val(JSON.stringify(data));
27                 $formReg.submit();
28             });
29         }, 1000);
30     }
31     var $inputAuth = $('#u2f_authentication_response');
32     if ($inputAuth.length > 0) {
33         var $formAuth = $inputAuth.parents('form');
34         $formAuth.find('input[type=submit]').hide();
35         setTimeout(function () {
36             // Magic JavaScript talking to your HID
37             // appid, challenge, authenticateRequests
38             var request = JSON.parse($inputAuth.attr('data-request'));
39             var handles = [request[0].keyHandle];
40             u2f.sign(request[0].appId, request[0].challenge, request, function (data) {
41                 // Handle returning error data
42                 if (data.errorCode && data.errorCode !== 0) {
43                     if (data.errorCode === 5) {
44                         PMA_ajaxShowMessage(PMA_messages.strU2FTimeout, false);
45                     } else {
46                         PMA_ajaxShowMessage(
47                             PMA_sprintf(PMA_messages.strU2FError, data.errorCode), false
48                         );
49                     }
50                     return;
51                 }
53                 // Fill and submit form.
54                 $inputAuth.val(JSON.stringify(data));
55                 $formAuth.submit();
56             });
57         }, 1000);
58     }
59 });