jQuery(document).ready(function($) {
// Return value: whether to submit the form or not
// Form types: 1 = anything else, 2 = TML widget
// 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)
function runGenerateOTPCall(form_type, form) {
if (2 == form_type) {
var username = $(form).find('[name="log"]').val();
} else {
var username = $('#user_login').val() || $('[name="log"]').val();
}
if(!username.length) return false;
var $submit_button = (null === form) ? $('#wp-submit') : $(form).find('input[name="wp-submit"]');
// If this is a "lost password" form, then exit
if ($('#user_login').parents('#lostpasswordform, #resetpasswordform').length) return false;
if (simba_tfasettings.hasOwnProperty('spinnerimg')) {
var styling = 'float:right; margin:6px 12px;';
if ($('#theme-my-login #wp-submit').length >0) {
var styling = 'margin-left: 4px; position: relative; top: 4px; border:0px; box-shadow:none;';
}
$submit_button.after('');
}
$.ajax({
url: simba_tfasettings.ajaxurl,
type: 'POST',
data: {
action: 'simbatfa-init-otp',
user: username
},
dataType: 'text',
success: function(resp) {
try {
var json_begins = resp.search('{"jsonstarter":"justhere"');
if (json_begins > -1) {
if (json_begins > 0) {
console.log("Expected JSON marker found at position: "+json_begins);
resp = resp.substring(json_begins);
}
} else {
console.log("Expected JSON marker not found");
console.log(resp);
}
response = $.parseJSON(resp);
if (response.hasOwnProperty('php_output')) {
console.log("PHP output was returned (follows)");
console.log(response.php_output);
}
if (response.hasOwnProperty('extra_output')) {
console.log("Extra output was returned (follows)");
console.log(response.extra_output);
}
if (response.status === true) {
// Don't bother to remove the spinner if the form is being submitted.
$('.simbaotp_spinner').remove();
console.log("Simba TFA: User has OTP enabled: showing OTP field");
tfaShowOTPField(form_type, form);
} else {
console.log("Simba TFA: User does not have OTP enabled: submitting form");
$('#wp-submit').parents('form:first').submit();
}
} catch(err) {
$('#login').html(resp);
console.log("Simba TFA: Error when processing response");
console.log(err);
console.log(resp);
}
},
error: function(jq_xhr, text_status, error_thrown) {
console.log("Simba TFA: AJAX error: "+error_thrown+": "+text_status);
console.log(jq_xhr);
if (jq_xhr.hasOwnProperty('responseText')) { console.log(jq_xhr.responseText);}
}
});
return true;
}
// Parameters: see runGenerateOTPCall
function tfaShowOTPField(form_type, form) {
var $submit_button = (null === form) ? $('#wp-submit') : $(form).find('input[name="wp-submit"]');
//Hide all elements in sa browser safe way
$submit_button.parents('form:first').find('p').each(function(i) {
$(this).css('visibility','hidden').css('position', 'absolute');
});
$submit_button.attr('disabled', 'disabled');
//Add new field and controls
var html = '';
html += '';
html += '
' + simba_tfasettings.otp_login_help + '
'; html += ''; $submit_button.parents('form:first').prepend(html); $('#simba_two_factor_auth').focus(); } var tfa_cb = function(e) { console.log("Simba TFA: form submit request"); var form_type = 1; var form = null; if ($(e.target).parent('.tml-login').parent('.widget').length > 0) { $(e.target).off(); form_type = 2; form = e.target; } else { $('#wp-submit').parents('form:first').off(); } var res = runGenerateOTPCall(form_type, form); if (!res) return true; e.preventDefault(); return false; }; $('#wp-submit').parents('form:first').on('submit', tfa_cb); $('.widget .tml-login form').on('submit', tfa_cb); });