PluginProbe
Two Factor Authentication / 1.2.6
Two Factor Authentication v1.2.6
1.12.2 1.13.0 1.14.10 1.14.11 1.14.14 1.14.15 1.14.16 1.14.17 1.14.23 1.14.24 1.14.26 1.14.27 1.14.3 1.14.4 1.14.5 1.14.7 1.14.8 1.15.5 1.16.0 1.2.10 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 All 98 releases
two-factor-authentication / includes / tfa.js

tfa.js in Two Factor Authentication 1.2.6, at includes/tfa.js

109 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(document).ready(function($) {
2
3 // Return value: whether to submit the form or not
4 function runGenerateOTPCall() {
5
6 var username = $('#user_login').val() || $('[name="log"]').val();
7
8 if(!username.length) return false;
9
10 // If this is a "lost password" form, then exit
11 if ($('#user_login').parents('#lostpasswordform, #resetpasswordform').length) return false;
12
13 if (simba_tfasettings.hasOwnProperty('spinnerimg')) {
14 var styling = 'float:right; margin:6px 12px;';
15 if ($('#theme-my-login #wp-submit').length >0) {
16 var styling = 'margin-left: 4px; position: relative; top: 4px; border:0px; box-shadow:none;';
17 }
18 $('#wp-submit').after('<img class="simbaotp_spinner" src="'+simba_tfasettings.spinnerimg+'" style="'+styling+'">');
19 }
20
21 $.ajax({
22 url: simba_tfasettings.ajaxurl,
23 type: 'POST',
24 data: {
25 action: 'simbatfa-init-otp',
26 user: username
27 },
28 dataType: 'text',
29 success: function(resp) {
30 try {
31 var json_begins = resp.search('{"jsonstarter":"justhere"');
32 if (json_begins > -1) {
33 if (json_begins > 0) {
34 console.log("Expected JSON marker found at position: "+json_begins);
35 resp = resp.substring(json_begins);
36 }
37 } else {
38 console.log("Expected JSON marker not found");
39 console.log(resp);
40 }
41 response = $.parseJSON(resp);
42 if (response.hasOwnProperty('php_output')) {
43 console.log("PHP output was returned (follows)");
44 console.log(response.php_output);
45 }
46 if (response.hasOwnProperty('extra_output')) {
47 console.log("Extra output was returned (follows)");
48 console.log(response.extra_output);
49 }
50 if (response.status === true) {
51 // Don't bother to remove the spinner if the form is being submitted.
52 $('.simbaotp_spinner').remove();
53 console.log("Simba TFA: User has OTP enabled: showing OTP field");
54 tfaShowOTPField();
55 } else {
56 console.log("Simba TFA: User does not have OTP enabled: submitting form");
57 $('#wp-submit').parents('form:first').submit();
58 }
59 } catch(err) {
60 $('#login').html(resp);
61 console.log("Simba TFA: Error when processing response");
62 console.log(err);
63 console.log(resp);
64 }
65 },
66 error: function(jq_xhr, text_status, error_thrown) {
67 console.log("Simba TFA: AJAX error: "+error_thrown+": "+text_status);
68 console.log(jq_xhr);
69 if (jq_xhr.hasOwnProperty('responseText')) { console.log(jq_xhr.responseText);}
70 }
71 });
72 return true;
73 }
74
75 function tfaShowOTPField() {
76 //Hide all elements in sa browser safe way
77 $('#wp-submit').parents('form:first').find('p').each(function(i) {
78 $(this).css('visibility','hidden').css('position', 'absolute');
79 });
80 $('#wp-submit').attr('disabled', 'disabled');
81
82 //Add new field and controls
83 var html = '';
84 html += '<label for="simba_two_factor_auth">' + simba_tfasettings.otp + '<br><input type="text" name="two_factor_code" id="simba_two_factor_auth" autocomplete="off"></label>';
85 html += '<p class="forgetmenot" style="font-size:small; max-width: 60%">' + simba_tfasettings.otp_login_help + '</p>';
86 html += '<p class="submit"><input id="tfa_login_btn" class="button button-primary button-large" type="submit" value="' + $('#wp-submit').val() + '"></p>';
87
88 $('#wp-submit').parents('form:first').prepend(html);
89 $('#simba_two_factor_auth').focus();
90 }
91
92 var tfa_cb = function(e) {
93 console.log("Simba TFA: form submit request");
94
95 var res = runGenerateOTPCall();
96
97 $('#wp-submit').parents('form:first').off();
98
99 if(!res) return true;
100
101 e.preventDefault();
102 return false;
103 };
104
105
106
107 $('#wp-submit').parents('form:first').on('submit', tfa_cb);
108 });
109