mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-07 21:51:23 +00:00
GUACAMOLE-526: Remove unnecessary use of $q within authenticationService. Rely on requestService.
This commit is contained in:
@@ -21,5 +21,6 @@
|
|||||||
* The module for authentication and management of tokens.
|
* The module for authentication and management of tokens.
|
||||||
*/
|
*/
|
||||||
angular.module('auth', [
|
angular.module('auth', [
|
||||||
|
'rest',
|
||||||
'storage'
|
'storage'
|
||||||
]);
|
]);
|
||||||
|
@@ -46,10 +46,9 @@ angular.module('auth').factory('authenticationService', ['$injector',
|
|||||||
var Error = $injector.get('Error');
|
var Error = $injector.get('Error');
|
||||||
|
|
||||||
// Required services
|
// Required services
|
||||||
var $http = $injector.get('$http');
|
|
||||||
var $q = $injector.get('$q');
|
|
||||||
var $rootScope = $injector.get('$rootScope');
|
var $rootScope = $injector.get('$rootScope');
|
||||||
var localStorageService = $injector.get('localStorageService');
|
var localStorageService = $injector.get('localStorageService');
|
||||||
|
var requestService = $injector.get('requestService');
|
||||||
|
|
||||||
var service = {};
|
var service = {};
|
||||||
|
|
||||||
@@ -155,27 +154,8 @@ angular.module('auth').factory('authenticationService', ['$injector',
|
|||||||
*/
|
*/
|
||||||
service.authenticate = function authenticate(parameters) {
|
service.authenticate = function authenticate(parameters) {
|
||||||
|
|
||||||
var authenticationProcess = $q.defer();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores the given authentication data within the browser and marks
|
|
||||||
* the authentication process as completed.
|
|
||||||
*
|
|
||||||
* @param {Object} data
|
|
||||||
* The authentication data returned by the token REST endpoint.
|
|
||||||
*/
|
|
||||||
var completeAuthentication = function completeAuthentication(data) {
|
|
||||||
|
|
||||||
// Store auth data
|
|
||||||
setAuthenticationResult(new AuthenticationResult(data));
|
|
||||||
|
|
||||||
// Process is complete
|
|
||||||
authenticationProcess.resolve();
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// Attempt authentication
|
// Attempt authentication
|
||||||
$http({
|
return requestService({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: 'api/tokens',
|
url: 'api/tokens',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -185,44 +165,32 @@ angular.module('auth').factory('authenticationService', ['$injector',
|
|||||||
})
|
})
|
||||||
|
|
||||||
// If authentication succeeds, handle received auth data
|
// If authentication succeeds, handle received auth data
|
||||||
.then(function authenticationSuccessful(response) {
|
.then(function authenticationSuccessful(data) {
|
||||||
var data = response.data;
|
|
||||||
|
|
||||||
var currentToken = service.getCurrentToken();
|
var currentToken = service.getCurrentToken();
|
||||||
|
setAuthenticationResult(new AuthenticationResult(data));
|
||||||
|
|
||||||
// If a new token was received, ensure the old token is invalidated,
|
// If a new token was received, ensure the old token is invalidated,
|
||||||
// if any, and notify listeners of the new token
|
// if any, and notify listeners of the new token
|
||||||
if (data.authToken !== currentToken) {
|
if (data.authToken !== currentToken) {
|
||||||
|
|
||||||
// If an old token existed, explicitly logout first
|
// If an old token existed, request that the token be revoked
|
||||||
if (currentToken) {
|
if (currentToken) {
|
||||||
service.logout()
|
service.logout().catch(angular.noop)
|
||||||
['finally'](function logoutComplete() {
|
|
||||||
completeAuthentication(data);
|
|
||||||
$rootScope.$broadcast('guacLogin', data.authToken);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, simply complete authentication and notify of login
|
// Notify of login and new token
|
||||||
else {
|
$rootScope.$broadcast('guacLogin', data.authToken);
|
||||||
completeAuthentication(data);
|
|
||||||
$rootScope.$broadcast('guacLogin', data.authToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, just finish the auth process
|
// Authentication was successful
|
||||||
else
|
return data;
|
||||||
completeAuthentication(data);
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// If authentication fails, propogate failure to returned promise
|
// If authentication fails, propogate failure to returned promise
|
||||||
['catch'](function authenticationFailed(response) {
|
['catch'](function authenticationFailed(error) {
|
||||||
|
|
||||||
// Ensure error object exists, even if the error response is not
|
|
||||||
// coming from the authentication REST endpoint
|
|
||||||
var error = new Error(response.data);
|
|
||||||
|
|
||||||
// Request credentials if provided credentials were invalid
|
// Request credentials if provided credentials were invalid
|
||||||
if (error.type === Error.Type.INVALID_CREDENTIALS)
|
if (error.type === Error.Type.INVALID_CREDENTIALS)
|
||||||
@@ -232,10 +200,10 @@ angular.module('auth').factory('authenticationService', ['$injector',
|
|||||||
else if (error.type === Error.Type.INSUFFICIENT_CREDENTIALS)
|
else if (error.type === Error.Type.INSUFFICIENT_CREDENTIALS)
|
||||||
$rootScope.$broadcast('guacInsufficientCredentials', parameters, error);
|
$rootScope.$broadcast('guacInsufficientCredentials', parameters, error);
|
||||||
|
|
||||||
authenticationProcess.reject(error);
|
// Authentication failed
|
||||||
});
|
throw error;
|
||||||
|
|
||||||
return authenticationProcess.promise;
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -317,7 +285,7 @@ angular.module('auth').factory('authenticationService', ['$injector',
|
|||||||
$rootScope.$broadcast('guacLogout', token);
|
$rootScope.$broadcast('guacLogout', token);
|
||||||
|
|
||||||
// Delete old token
|
// Delete old token
|
||||||
return $http({
|
return requestService({
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
url: 'api/tokens/' + token
|
url: 'api/tokens/' + token
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user