GUACAMOLE-1744: Merge automatically clean up UI if session has expired in the background.

This commit is contained in:
Virtually Nick
2023-02-27 17:56:18 -05:00
committed by GitHub
5 changed files with 157 additions and 25 deletions

View File

@@ -261,6 +261,43 @@ angular.module('auth').factory('authenticationService', ['$injector',
};
/**
* Determines whether the session associated with a particular token is
* still valid, without performing an operation that would result in that
* session being marked as active. If no token is provided, the session of
* the current user is checked.
*
* @param {string} [token]
* The authentication token to pass with the "Guacamole-Token" header.
* If omitted, and the user is logged in, the user's current
* authentication token will be used.
*
* @returns {Promise.<!boolean>}
* A promise that resolves with the boolean value "true" if the session
* is valid, and resolves with the boolean value "false" otherwise,
* including if an error prevents session validity from being
* determined. The promise is never rejected.
*/
service.getValidity = function getValidity(token) {
// NOTE: Because this is a HEAD request, we will not receive a JSON
// response body. We will only have a simple yes/no regarding whether
// the auth token can be expected to be usable.
return service.request({
method: 'HEAD',
url: 'api/session'
}, token)
.then(function sessionIsValid() {
return true;
})
['catch'](function sessionIsNotValid() {
return false;
});
};
/**
* Makes a request to revoke an authentication token using the token REST
* API endpoint, returning a promise that succeeds only if the token was

View File

@@ -23,14 +23,30 @@
angular.module('index').controller('indexController', ['$scope', '$injector',
function indexController($scope, $injector) {
/**
* The number of milliseconds that should elapse between client-side
* session checks. This DOES NOT impact whether a session expires at all;
* such checks will always be server-side. This only affects how quickly
* the client-side view can recognize that a user's session has expired
* absent any action taken by the user.
*
* @type {!number}
*/
const SESSION_VALIDITY_RECHECK_INTERVAL = 15000;
// Required types
const ManagedClientState = $injector.get('ManagedClientState');
// Required services
const $document = $injector.get('$document');
const $location = $injector.get('$location');
const $route = $injector.get('$route');
const $window = $injector.get('$window');
const clipboardService = $injector.get('clipboardService');
const guacNotification = $injector.get('guacNotification');
const guacClientManager = $injector.get('guacClientManager');
const $document = $injector.get('$document');
const $interval = $injector.get('$interval');
const $location = $injector.get('$location');
const $route = $injector.get('$route');
const $window = $injector.get('$window');
const authenticationService = $injector.get('authenticationService');
const clipboardService = $injector.get('clipboardService');
const guacNotification = $injector.get('guacNotification');
const guacClientManager = $injector.get('guacClientManager');
/**
* The error that prevents the current page from rendering at all. If no
@@ -202,6 +218,48 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
keyboard.reset();
};
/**
* Returns whether the current user has at least one active connection
* running within the current tab.
*
* @returns {!boolean}
* true if the current user has at least one active connection running
* in the current browser tab, false otherwise.
*/
var hasActiveTunnel = function hasActiveTunnel() {
var clients = guacClientManager.getManagedClients();
for (var id in clients) {
switch (clients[id].clientState.connectionState) {
case ManagedClientState.ConnectionState.CONNECTING:
case ManagedClientState.ConnectionState.WAITING:
case ManagedClientState.ConnectionState.CONNECTED:
return true;
}
}
return false;
};
// If we're logged in and not connected to anything, periodically check
// whether the current session is still valid. If the session has expired,
// refresh the auth state to reshow the login screen (rather than wait for
// the user to take some action and discover that they are not logged in
// after all). There is no need to do this if a connection is active as
// that connection activity will already automatically check session
// validity.
$interval(function cleanUpViewIfSessionInvalid() {
if ($scope.applicationState === ApplicationState.READY && !hasActiveTunnel()) {
authenticationService.getValidity().then(function validityDetermined(valid) {
if (!valid)
$scope.reAuthenticate();
});
}
}, SESSION_VALIDITY_RECHECK_INTERVAL);
// Release all keys upon form submission (there may not be corresponding
// keyup events for key presses involved in submitting a form)
$document.on('submit', function formSubmitted() {