Added a token expiration check to warn users they should request another

This commit is contained in:
Alejandro Gallardo Escobar 2016-05-09 16:22:01 +02:00
parent 82bd565f5d
commit 2b88b58448
2 changed files with 34 additions and 3 deletions

View File

@ -135,6 +135,15 @@ echo '<input type="button" id="run-ehorus-client" class="sub next" value="' . __
echo '</td>';
echo '</tr></table>';
echo '<div id="expired_message" style="display: none;">';
ui_print_error_message(
__('The connection was lost and the authorization token was expired')
. '. ' .
__('Reload the page to request a new authorization token')
. '. '
);
echo '</div>';
echo '<div id="ehorus-client-iframe"></div>';
$query_data = array(
@ -142,6 +151,7 @@ $query_data = array(
'hostname' => (string) $agent_data['serverAddress'],
'port' => (int) $agent_data['serverPort'],
'token' => (string) $response_auth['token'],
'expiration' => (int) $response_auth['exp'],
'is_busy' => (bool) $agent_data['isBusy'],
'last_connection' => (int) $agent_data['lastConnection'],
'section' => $client_tab
@ -200,7 +210,6 @@ $client_url = $config['homeurl'] . 'operation/agentes/ehorus_client.php?' . $que
event.source !== iframe.contentWindow) {
return;
}
console.log('message from iframe', event.data);
if (typeof actionHandlers === 'undefined') return;
if (event.data.action in actionHandlers) {
@ -231,6 +240,17 @@ $client_url = $config['homeurl'] . 'operation/agentes/ehorus_client.php?' . $que
$('a.tab_processes').click(handleTabClick('processes', messageToIframe));
$('a.tab_services').click(handleTabClick('services', messageToIframe));
$('a.tab_files').click(handleTabClick('files', messageToIframe));
},
expired: function () {
$(iframe).remove();
$('a.ehorus_tab').unbind('click');
$('a.tab_terminal').unbind('click');
$('a.tab_display').unbind('click');
$('a.tab_processes').unbind('click');
$('a.tab_services').unbind('click');
$('a.tab_files').unbind('click');
iframe = null;
$('div#expired_message').show();
}
}

View File

@ -28,6 +28,7 @@ $agent_id = (string) get_parameter_get('agent_id');
$hostname = (string) get_parameter_get('hostname');
$port = (int) get_parameter_get('port');
$token = (string) get_parameter_get('token');
$expiration = (int) get_parameter_get('expiration');
$is_busy = (bool) get_parameter_get('is_busy');
$last_connection = (int) get_parameter_get('last_connection');
$section = (string) get_parameter_get('section');
@ -92,7 +93,6 @@ $section = (string) get_parameter_get('section');
event.source !== window.parent) {
return;
}
console.log('message from parent', event.data);
if (typeof actionHandlers === 'undefined') return;
if (event.data.action in actionHandlers) {
@ -102,6 +102,7 @@ $section = (string) get_parameter_get('section');
}
window.onload = function () {
var expiration = <?php echo $expiration; ?>;
// Start client
var ehorusContainer = document.getElementById('ehorus-client-container');
var eHorus = runClient(ehorusContainer, {
@ -115,11 +116,21 @@ $section = (string) get_parameter_get('section');
section: '<?php echo $section; ?>'
});
eHorus.remote.onClose(function () {
if (expiration && expiration < Date.now() / 1000) {
eHorus.remote.close();
// Send expired message
messageToParent({
action: 'expired',
payload: {}
});
}
});
// Listen for messages
var actionHandlers = {
change_section: function (payload) {
eHorus.changeSection(payload.section);
console.log('Changing section', payload.section);
}
}
window.addEventListener('message', handleMessage(actionHandlers));