| 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 |
|