This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
mandayejs/mandayejs/do_logout.js

90 lines
2.6 KiB
JavaScript

/* mandayejs - saml reverse proxy
* Copyright (C) 2015 Entr'ouvert
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var page = require('webpage').create();
var system = require('system');
var input = JSON.parse(system.stdin.read(10000));
/*
* Loading cookies
*/
for (var i=0; i < input.cookies.length; i++){
phantom.addCookie(input.cookies[i]);
}
var output = {'stderr': null, 'result': null };
page.viewportSize = {width: 1280, height: 1024};
function mandaye_exit(message){
console.log('<mandayejs>'+message+'</mandayejs>')
phantom.exit()
}
page.onResourceRequested = function(requestData, networkRequest) {
if (requestData.url.indexOf('google-analytics.com') !== -1 ||
requestData.url.indexOf('/piwik') !== -1) ||
requestData.url.indexOf('/matomo') !== -1) {
networkRequest.abort();
}
}
page.onError = function(msg, trace){
var err_stack = ['ERROR: ' + msg];
if (trace && trace.length) {
err_stack.push('TRACE:');
trace.forEach(function(t) {
err_stack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
output['stderr'] = err_stack.join('\n')
}
page.open(input.address, function(status){
if (status !== 'success'){
output['result'] = 'failed to open resource';
mandaye_exit(JSON.stringify(output));
phantom.exit();
}
page.onLoadFinished = function(status){
output['result'] = 'ok';
output['cookies'] = page.cookies;
output['url'] = page.url;
mandaye_exit(JSON.stringify(output));
};
var logout = page.evaluate(function(input){
element = input.logout_locator;
var logout_link = $(element).length > 0 ? $(element)[0] : $(element);
if ($(logout_link).length > 0){
logout_link.click();
return true;
}
return false;
}, input);
if (logout == false){
output['result'] = 'logout failed';
mandaye_exit(JSON.stringify(output));
}
});