PluginProbe
Two Factor Authentication / 1.14.8
Two Factor Authentication v1.14.8
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 / simba-tfa / includes / tfa.js

tfa.js in Two Factor Authentication 1.14.8, at simba-tfa/includes/tfa.js

232 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(function($) {
2
3 /**
4 * Check if the user requires an OTP field and if so, display it
5 *
6 * @param String form - DOM selector string
7 *
8 * @uses show_otp_field()
9 *
10 * @return Boolean - true if we got involved
11 */
12 function check_and_possibly_show_otp_field(form) {
13
14 // If this is a "lost password" form, then exit
15 if ($(form).attr('id') === 'lostpasswordform' || $(form).attr('id') === 'resetpasswordform') return false;
16
17 // 'username' is used by WooCommerce
18 var username = $(form).find('[name="log"], [name="username"], #user_login, #affwp-login-user-login, #affwp-user-login').first().val();
19
20 if (!username.length) return false;
21
22 var $submit_button = $(form).find('input[name="wp-submit"], input[type="submit"], button[type="submit"]').first();
23
24 if (simba_tfasettings.hasOwnProperty('spinnerimg')) {
25 var styling = 'float:right; margin:6px 12px; width: 20px; height: 20px;';
26 if ($('#theme-my-login #wp-submit').length >0) {
27 styling = 'margin-left: 4px; position: relative; top: 4px; width: 20px; height: 20px; border:0px; box-shadow:none;';
28 }
29 $submit_button.after('<img class="simbaotp_spinner" src="'+simba_tfasettings.spinnerimg+'" style="'+styling+'">');
30 }
31
32 $.ajax({
33 url: simba_tfasettings.ajaxurl,
34 type: 'POST',
35 data: {
36 action: 'simbatfa-init-otp',
37 user: username
38 },
39 dataType: 'text',
40 success: function(resp) {
41 try {
42 var json_begins = resp.search('{"jsonstarter":"justhere"');
43 if (json_begins > -1) {
44 if (json_begins > 0) {
45 console.log("Expected JSON marker found at position: "+json_begins);
46 resp = resp.substring(json_begins);
47 }
48 } else {
49 console.log("Expected JSON marker not found");
50 console.log(resp);
51 }
52
53 response = JSON.parse(resp);
54
55 if (response.hasOwnProperty('php_output')) {
56 console.log("PHP output was returned (follows)");
57 console.log(response.php_output);
58 }
59
60 if (response.hasOwnProperty('extra_output')) {
61 console.log("Extra output was returned (follows)");
62 console.log(response.extra_output);
63 }
64
65 if (true === response.status) {
66 // Don't bother to remove the spinner if the form is being submitted.
67 $('.simbaotp_spinner').remove();
68
69 var user_can_trust = (response.hasOwnProperty('user_can_trust') && response.user_can_trust) ? true : false;
70
71 var user_already_trusted = (response.hasOwnProperty('user_already_trusted') && response.user_can_trust) ? true : false;
72
73 console.log("Simba TFA: User has OTP enabled: showing OTP field (user_can_trust="+user_can_trust+")");
74
75 show_otp_field(form, user_can_trust, user_already_trusted);
76
77 } else {
78 console.log("Simba TFA: User does not have OTP enabled: submitting form");
79
80 // For some reason, .submit() stopped working with TML 7.x. N.B. Used to do this only for form_type == 2 ("TML shortcode or widget, WP Members, bbPress, Ultimate Membership Pro, WooCommerce or Elementor login form")
81 $(form).find('input[type="submit"], button[type="submit"]').first().trigger('click');
82 // $('#wp-submit').parents('form').first().trigger('submit');
83 }
84
85 } catch(err) {
86 $('#login').html(resp);
87 console.log("Simba TFA: Error when processing response");
88 console.log(err);
89 console.log(resp);
90 }
91 },
92 error: function(jq_xhr, text_status, error_thrown) {
93 console.log("Simba TFA: AJAX error: "+error_thrown+": "+text_status);
94 console.log(jq_xhr);
95 if (jq_xhr.hasOwnProperty('responseText')) {
96 console.log(jq_xhr.responseText);
97 $(form).append('<p class="error" style="clear:left;">'+simba_tfasettings.error+'</p>');
98 }
99 }
100 });
101 return true;
102 }
103
104 // Parameters: see check_and_possibly_show_otp_field
105 function show_otp_field(form, user_can_trust, user_already_trusted) {
106
107 var $submit_button;
108
109 user_can_trust = ('undefined' == typeof user_can_trust) ? false : user_can_trust;
110 user_already_trusted = ('undefined' == typeof user_already_trusted) ? false : user_already_trusted;
111
112 if ('https:' != window.location.protocol && 'localhost' !== location.hostname && '127.0.0.1' !== location.hostname && /^\.localdomain$/.test(location.hostname)) {
113 user_can_trust = false;
114 }
115
116 if (!user_can_trust) { user_already_trusted = false; }
117
118 // name="Submit" is WP-Members. 'submit' is Theme My Login starting from 7.x
119 $submit_button = $(form).find('input[name="wp-submit"], input[name="Submit"], input[name="submit"]');
120 // This hasn't been needed for anything yet (Jul 2018), but is a decent back-stop that would have prevented some breakage in the past that needed manual attention:
121 if (0 == $submit_button.length) {
122 $submit_button = $(form).find('input[type="submit"], button[type="submit"]').first();
123 }
124
125 // Hide all elements in a browser-safe way
126 // .user-pass-wrap is the wrapper used (instead of a paragraph) on wp-login.php from WP 5.3
127 $submit_button.parents('form').first().find('p, .impu-form-line-fr, .tml-field-wrap, .user-pass-wrap, .elementor-field-type-text, .elementor-field-type-submit, .elementor-remember-me, .bbp-username, .bbp-password, .bbp-submit-wrapper').each(function(i) {
128 $(this).css('visibility', 'hidden').css('position', 'absolute');
129 // On the WooCommerce form, the 'required' asterisk in the child <span> still shows without this
130 $(this).find('span').css('visibility', 'hidden').css('position', 'absolute');
131 });
132
133 // WP-Members
134 $submit_button.parents('#wpmem_login').find('fieldset').css('visibility', 'hidden').css('position', 'absolute');
135
136 // Add new field and controls
137 var html = '';
138
139 if (user_already_trusted) {
140
141 html += '<br><span class="simbaotp_is_trusted">'+simba_tfasettings.is_trusted+'</span>';
142
143 } else {
144
145 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" data-lpignore="true"';
146
147 if ($(form).hasClass('woocommerce-form-login')) {
148 // Retain compatibility with previous full-width layout
149 html += ' style="width: 100%;"';
150 }
151
152 html += '></label>';
153
154 html += '<p class="forgetmenot" style="font-size:small;';
155 if (!$(form).hasClass('woocommerce-form-login')) {
156 // Retain compatibility with previous full-width layout
157 html += ' max-width: 60%;';
158 }
159 html += '">'+simba_tfasettings.otp_login_help;
160
161 if (user_can_trust) {
162
163 html += '<br><input type="checkbox" name="simba_tfa_mark_as_trusted" id="simba_tfa_mark_as_trusted" value="1"><label for="simba_tfa_mark_as_trusted">'+ simba_tfasettings.mark_as_trusted+'</label>';
164
165 }
166 }
167
168 html += '</p>';
169
170 var submit_button_text;
171 var submit_button_name;
172
173 if ('button' == $submit_button.prop('nodeName').toLowerCase()) {
174 submit_button_text = $submit_button.text().trim();
175 submit_button_name = $submit_button.attr('name');
176 } else {
177 submit_button_text = $submit_button.val();
178 submit_button_name = $submit_button.attr('name');
179 }
180
181 html += '<p class="submit"><input id="tfa_login_btn" class="button button-primary button-large" type="submit" ';
182
183 if ('undefined' !== typeof submit_button_name && '' != submit_button_name) { html += 'name="'+submit_button_name+'" '; }
184
185 html += 'value="' + submit_button_text + '"></p>';
186
187 $submit_button.prop('disabled', true);
188
189 $submit_button.parents('form').first().prepend(html);
190
191 $('#login_error').hide();
192
193 if (user_already_trusted) {
194 $('#tfa_login_btn').trigger('click');
195 } else {
196
197 $('#simba_two_factor_auth').trigger('focus');
198 }
199
200 }
201
202 /**
203 * This function gets attached to a form submission handler and decides whether to add an OTP field or not.
204 *
205 * @param Object e - submission event
206 *
207 * @return Boolean - whether to proceed with the submission or not
208 */
209 var form_submit_handler = function(e) {
210
211 console.log('Simba TFA: form submit request');
212
213 var form = e.target;
214 $(form).off();
215
216 if (check_and_possibly_show_otp_field(form)) {
217 e.preventDefault();
218 return false;
219 }
220
221 return true;
222
223 };
224
225 if (simba_tfasettings.login_form_off_selectors) {
226 $(simba_tfasettings.login_form_off_selectors).off('submit');
227 }
228
229 $(simba_tfasettings.login_form_selectors).on('submit', form_submit_handler);
230
231 });
232