| 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 |
} |
| 98 |
} |
| 99 |
}); |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
// Parameters: see check_and_possibly_show_otp_field |
| 104 |
function show_otp_field(form, user_can_trust, user_already_trusted) { |
| 105 |
|
| 106 |
var $submit_button; |
| 107 |
|
| 108 |
user_can_trust = ('undefined' == typeof user_can_trust) ? false : user_can_trust; |
| 109 |
user_already_trusted = ('undefined' == typeof user_already_trusted) ? false : user_already_trusted; |
| 110 |
|
| 111 |
// name="Submit" is WP-Members. 'submit' is Theme My Login starting from 7.x |
| 112 |
$submit_button = $(form).find('input[name="wp-submit"], input[name="Submit"], input[name="submit"]'); |
| 113 |
// 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: |
| 114 |
if (0 == $submit_button.length) { |
| 115 |
$submit_button = $(form).find('input[type="submit"], button[type="submit"]').first(); |
| 116 |
} |
| 117 |
|
| 118 |
// Hide all elements in a browser-safe way |
| 119 |
// .user-pass-wrap is the wrapper used (instead of a paragraph) on wp-login.php from WP 5.3 |
| 120 |
$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) { |
| 121 |
$(this).css('visibility', 'hidden').css('position', 'absolute'); |
| 122 |
// On the WooCommerce form, the 'required' asterisk in the child <span> still shows without this |
| 123 |
$(this).find('span').css('visibility', 'hidden').css('position', 'absolute'); |
| 124 |
}); |
| 125 |
|
| 126 |
// WP-Members |
| 127 |
$submit_button.parents('#wpmem_login').find('fieldset').css('visibility', 'hidden').css('position', 'absolute'); |
| 128 |
|
| 129 |
// Add new field and controls |
| 130 |
var html = ''; |
| 131 |
|
| 132 |
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"'; |
| 133 |
|
| 134 |
if ($(form).hasClass('woocommerce-form-login')) { |
| 135 |
// Retain compatibility with previous full-width layout |
| 136 |
html += ' style="width: 100%;"'; |
| 137 |
} |
| 138 |
|
| 139 |
html += '></label>'; |
| 140 |
|
| 141 |
html += '<p class="forgetmenot" style="font-size:small;'; |
| 142 |
|
| 143 |
if (!$(form).hasClass('woocommerce-form-login')) { |
| 144 |
// Retain compatibility with previous full-width layout |
| 145 |
html += ' max-width: 60%;'; |
| 146 |
} |
| 147 |
|
| 148 |
html += '">'+simba_tfasettings.otp_login_help; |
| 149 |
|
| 150 |
if (user_can_trust && ('https:' == window.location.protocol || 'localhost' === location.hostname || '127.0.0.1' === location.hostname)) { |
| 151 |
|
| 152 |
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>'; |
| 153 |
|
| 154 |
} else { |
| 155 |
user_already_trusted = false; |
| 156 |
} |
| 157 |
|
| 158 |
html += '</p>'; |
| 159 |
|
| 160 |
var submit_button_text; |
| 161 |
var submit_button_name; |
| 162 |
|
| 163 |
if ('button' == $submit_button.prop('nodeName').toLowerCase()) { |
| 164 |
submit_button_text = $submit_button.text().trim(); |
| 165 |
submit_button_name = $submit_button.attr('name'); |
| 166 |
} else { |
| 167 |
submit_button_text = $submit_button.val(); |
| 168 |
submit_button_name = $submit_button.attr('name'); |
| 169 |
} |
| 170 |
|
| 171 |
html += '<p class="submit"><input id="tfa_login_btn" class="button button-primary button-large" type="submit" '; |
| 172 |
|
| 173 |
if ('undefined' !== typeof submit_button_name && '' != submit_button_name) { html += 'name="'+submit_button_name+'" '; } |
| 174 |
|
| 175 |
html += 'value="' + submit_button_text + '"></p>'; |
| 176 |
|
| 177 |
$submit_button.prop('disabled', true); |
| 178 |
|
| 179 |
$submit_button.parents('form').first().prepend(html); |
| 180 |
$('#simba_two_factor_auth').trigger('focus'); |
| 181 |
|
| 182 |
if (user_can_trust && user_already_trusted) { |
| 183 |
$('#simba_two_factor_auth').val(simba_tfasettings.is_trusted); |
| 184 |
$('#tfa_login_btn').trigger('click'); |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* This function gets attached to a form submission handler and decides whether to add an OTP field or not. |
| 191 |
* |
| 192 |
* @param Object e - submission event |
| 193 |
* |
| 194 |
* @return Boolean - whether to proceed with the submission or not |
| 195 |
*/ |
| 196 |
var form_submit_handler = function(e) { |
| 197 |
|
| 198 |
console.log('Simba TFA: form submit request'); |
| 199 |
|
| 200 |
var form = e.target; |
| 201 |
$(form).off(); |
| 202 |
|
| 203 |
if (check_and_possibly_show_otp_field(form)) { |
| 204 |
e.preventDefault(); |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
return true; |
| 209 |
|
| 210 |
}; |
| 211 |
|
| 212 |
if (simba_tfasettings.login_form_off_selectors) { |
| 213 |
$(simba_tfasettings.login_form_off_selectors).off('submit'); |
| 214 |
} |
| 215 |
|
| 216 |
$(simba_tfasettings.login_form_selectors).on('submit', form_submit_handler); |
| 217 |
|
| 218 |
}); |
| 219 |
|