wait for page loading to check authentication (#14575)
Log an error if both auth success and failure functions return true.
This commit is contained in:
parent
7030f3e5e7
commit
eee5c87346
|
@ -23,6 +23,7 @@ var input = JSON.parse(system.stdin.read(2000)); // no .readAll()...
|
|||
headers_list = [];
|
||||
|
||||
var output = {'stderr': null, 'result': null };
|
||||
var timer = 0;
|
||||
|
||||
function mandaye_exit(message){
|
||||
console.log('<mandayejs>'+message+'</mandayejs>')
|
||||
|
@ -62,38 +63,73 @@ page.open(input.address, function(status) {
|
|||
output['result'] = 'failed to open resource';
|
||||
mandaye_exit(JSON.stringify(output));
|
||||
}
|
||||
page.onLoadFinished = function() {
|
||||
if (page.injectJs(input.auth_checker)){
|
||||
input.auth_success = page.evaluate(function(){
|
||||
return auth_success();
|
||||
});
|
||||
input.password_change_required = page.evaluate(function() {
|
||||
if (typeof(password_change_required) == 'function')
|
||||
return password_change_required();
|
||||
return false;
|
||||
});
|
||||
}
|
||||
output['headers'] = headers_list;
|
||||
output['cookies'] = page.cookies;
|
||||
|
||||
if (input.password_change_required) {
|
||||
output['result'] = 'redirect';
|
||||
output['reason'] = 'password_change_required';
|
||||
output['url'] = page.url;
|
||||
function check_auth(page) {
|
||||
input.auth_success = page.evaluate(function() {
|
||||
return auth_success();
|
||||
});
|
||||
input.auth_failure = page.evaluate(function() {
|
||||
if (typeof(auth_failure) == 'function')
|
||||
return auth_failure();
|
||||
return undefined;
|
||||
});
|
||||
input.password_change_required = page.evaluate(function() {
|
||||
if (typeof(password_change_required) == 'function')
|
||||
return password_change_required();
|
||||
return false;
|
||||
});
|
||||
|
||||
output['headers'] = headers_list;
|
||||
output['cookies'] = page.cookies;
|
||||
|
||||
if (input.password_change_required) {
|
||||
output['result'] = 'redirect';
|
||||
output['reason'] = 'password_change_required';
|
||||
output['url'] = page.url;
|
||||
mandaye_exit(JSON.stringify(output));
|
||||
}
|
||||
|
||||
if (input.auth_failure == undefined) {
|
||||
if (input.auth_success) {
|
||||
output['result'] = 'ok';
|
||||
output['url'] = page.frameUrl;
|
||||
} else {
|
||||
output['result'] = 'failure';
|
||||
output['reason'] = 'authentication';
|
||||
}
|
||||
mandaye_exit(JSON.stringify(output));
|
||||
}
|
||||
|
||||
// run repeatedly the function, if authentication did not
|
||||
// succeed and did not fail
|
||||
if (!input.auth_success && !input.auth_failure)
|
||||
return;
|
||||
|
||||
if (input.auth_failure) {
|
||||
output['result'] = 'failure';
|
||||
output['reason'] = 'authentication';
|
||||
if (input.auth_success) {
|
||||
output['result'] = 'error';
|
||||
output['reason'] = 'auth success and failure';
|
||||
}
|
||||
}
|
||||
|
||||
if (input.auth_success) {
|
||||
output['result'] = 'ok';
|
||||
output['url'] = page.frameUrl;
|
||||
}
|
||||
mandaye_exit(JSON.stringify(output));
|
||||
}
|
||||
|
||||
if (!input.auth_success){
|
||||
output['result'] = 'failure';
|
||||
output['reason'] = 'authentication';
|
||||
mandaye_exit(JSON.stringify(output));
|
||||
page.onLoadStarted = function() {
|
||||
// reset authentication check when the page reloads
|
||||
clearInterval(timer);
|
||||
}
|
||||
|
||||
output['result'] = 'ok';
|
||||
output['url'] = page.frameUrl;
|
||||
|
||||
mandaye_exit(JSON.stringify(output));
|
||||
}
|
||||
page.onLoadFinished = function() {
|
||||
if (page.injectJs(input.auth_checker))
|
||||
timer = setInterval(check_auth, 100, page);
|
||||
}
|
||||
|
||||
page.evaluate(function(input) {
|
||||
var locators = input.locators;
|
||||
|
|
|
@ -54,6 +54,8 @@ def exec_phantom(data, script='do_login.js'):
|
|||
|
||||
if result.get('stderr'):
|
||||
logger.warning(result['stderr'])
|
||||
if result.get('error'):
|
||||
logger.error(result.get('reason'))
|
||||
|
||||
send_end.send(result)
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
$(function(){
|
||||
window.auth_success = function(){
|
||||
var found = $('.connectBox').text().indexOf("L'utilisateur spécifié est introuvable, vérifiez la syntaxe et réessayez. Mot de passe oublié ?");
|
||||
if (found == -1){
|
||||
window.auth_success = function() {
|
||||
if ($('a.account_logoff').length >= 1)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
window.auth_failure = function() {
|
||||
return $('#logon-error-message').text() == "L'utilisateur spécifié est introuvable, vérifiez la syntaxe et réessayez. Mot de passe oublié ?";
|
||||
};
|
||||
|
|
Reference in New Issue