PluginProbe
Two Factor Authentication / 1.2.16
Two Factor Authentication v1.2.16
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.16, at includes/tfa.js

133 lines 4.5 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 // Form types: 1 = anything else, 2 = TML widget
5 // The form parameter is only used for form_type == 2, which is essentially a later bolt-on extra (which explains why there are apparently other form types handled below still covered under 1)
6 function runGenerateOTPCall(form_type, form) {
7
8 if (2 == form_type) {
9 var username = $(form).find('[name="log"]').val();
10 } else {
11 var username = $('#user_login').val() || $('[name="log"]').val();
12 }
13
14 if(!username.length) return false;
15
16 var $submit_button = (null === form) ? $('#wp-submit') : $(form).find('input[name="wp-submit"]');
17
18 // If this is a "lost password" form, then exit
19 if ($('#user_login').parents('#lostpasswordform, #resetpasswordform').length) return false;
20
21 if (simba_tfasettings.hasOwnProperty('spinnerimg')) {
22 var styling = 'float:right; margin:6px 12px;';
23 if ($('#theme-my-login #wp-submit').length >0) {
24 var styling = 'margin-left: 4px; position: relative; top: 4px; border:0px; box-shadow:none;';
25 }
26 $submit_button.after('<img class="simbaotp_spinner" src="'+simba_tfasettings.spinnerimg+'" style="'+styling+'">');
27 }
28
29 $.ajax({
30 url: simba_tfasettings.ajaxurl,
31 type: 'POST',
32 data: {
33 action: 'simbatfa-init-otp',
34 user: username
35 },
36 dataType: 'text',
37 success: function(resp) {
38 try {
39 var json_begins = resp.search('{"jsonstarter":"justhere"');
40 if (json_begins > -1) {
41 if (json_begins > 0) {
42 console.log("Expected JSON marker found at position: "+json_begins);
43 resp = resp.substring(json_begins);
44 }
45 } else {
46 console.log("Expected JSON marker not found");
47 console.log(resp);
48 }
49 response = $.parseJSON(resp);
50 if (response.hasOwnProperty('php_output')) {
51 console.log("PHP output was returned (follows)");
52 console.log(response.php_output);
53 }
54 if (response.hasOwnProperty('extra_output')) {
55 console.log("Extra output was returned (follows)");
56 console.log(response.extra_output);
57 }
58 if (response.status === true) {
59 // Don't bother to remove the spinner if the form is being submitted.
60 $('.simbaotp_spinner').remove();
61 console.log("Simba TFA: User has OTP enabled: showing OTP field");
62 tfaShowOTPField(form_type, form);
63 } else {
64 console.log("Simba TFA: User does not have OTP enabled: submitting form");
65 $('#wp-submit').parents('form:first').submit();
66 }
67 } catch(err) {
68 $('#login').html(resp);
69 console.log("Simba TFA: Error when processing response");
70 console.log(err);
71 console.log(resp);
72 }
73 },
74 error: function(jq_xhr, text_status, error_thrown) {
75 console.log("Simba TFA: AJAX error: "+error_thrown+": "+text_status);
76 console.log(jq_xhr);
77 if (jq_xhr.hasOwnProperty('responseText')) { console.log(jq_xhr.responseText);}
78 }
79 });
80 return true;
81 }
82
83 // Parameters: see runGenerateOTPCall
84 function tfaShowOTPField(form_type, form) {
85
86 var $submit_button = (null === form) ? $('#wp-submit') : $(form).find('input[name="wp-submit"]');
87
88 //Hide all elements in sa browser safe way
89 $submit_button.parents('form:first').find('p').each(function(i) {
90 $(this).css('visibility','hidden').css('position', 'absolute');
91 });
92 $submit_button.attr('disabled', 'disabled');
93
94 //Add new field and controls
95 var html = '';
96 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>';
97 html += '<p class="forgetmenot" style="font-size:small; max-width: 60%">' + simba_tfasettings.otp_login_help + '</p>';
98 html += '<p class="submit"><input id="tfa_login_btn" class="button button-primary button-large" type="submit" value="' + $submit_button.val() + '"></p>';
99
100 $submit_button.parents('form:first').prepend(html);
101 $('#simba_two_factor_auth').focus();
102
103 }
104
105 var tfa_cb = function(e) {
106 console.log("Simba TFA: form submit request");
107
108 var form_type = 1;
109 var form = null;
110
111 if ($(e.target).parent('.tml-login').parent('.widget').length > 0) {
112 $(e.target).off();
113 form_type = 2;
114 form = e.target;
115 } else {
116 $('#wp-submit').parents('form:first').off();
117 }
118
119 var res = runGenerateOTPCall(form_type, form);
120
121 if (!res) return true;
122
123 e.preventDefault();
124 return false;
125 };
126
127
128
129 $('#wp-submit').parents('form:first').on('submit', tfa_cb);
130 $('.widget .tml-login form').on('submit', tfa_cb);
131
132 });
133