mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-08 14:11:21 +00:00
GUAC-919: Rename LoginRESTService to TokenRESTService and provide logout semantics. Move Angular auth stuff to own module. Actually logout user.
This commit is contained in:
@@ -21,33 +21,6 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* A service for authenticating a user against the REST API.
|
||||
* The module for authentication and management of tokens.
|
||||
*/
|
||||
angular.module('index').factory('authenticationService', ['$http',
|
||||
function authenticationService($http) {
|
||||
var service = {};
|
||||
|
||||
/**
|
||||
* Makes a request to authenticate a user using the login REST API endpoint,
|
||||
* returning a promise that can be used for processing the results of the call.
|
||||
*
|
||||
* @param {string} username The username to log in with.
|
||||
* @param {string} password The password to log in with.
|
||||
* @returns {promise} A promise for the HTTP call.
|
||||
*/
|
||||
service.login = function login(username, password) {
|
||||
return $http({
|
||||
method: 'POST',
|
||||
url: 'api/login',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: $.param({
|
||||
username: username,
|
||||
password: password
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
return service;
|
||||
}]);
|
||||
angular.module('auth', ['util']);
|
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Glyptodon LLC
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A service for authenticating a user against the REST API.
|
||||
*/
|
||||
angular.module('auth').factory('authenticationService', ['$http', '$injector',
|
||||
function authenticationService($http, $injector) {
|
||||
|
||||
var localStorageUtility = $injector.get("localStorageUtility");
|
||||
var service = {};
|
||||
|
||||
/**
|
||||
* Makes a request to authenticate a user using the token REST API endpoint,
|
||||
* returning a promise that can be used for processing the results of the call.
|
||||
*
|
||||
* @param {String} username The username to log in with.
|
||||
* @param {String} password The password to log in with.
|
||||
* @returns {Promise} A promise for the HTTP call.
|
||||
*/
|
||||
service.login = function login(username, password) {
|
||||
return $http({
|
||||
method: 'POST',
|
||||
url: 'api/token',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: $.param({
|
||||
username: username,
|
||||
password: password
|
||||
})
|
||||
}).success(function success(data, status, headers, config) {
|
||||
localStorageUtility.set('authToken', data.authToken);
|
||||
localStorageUtility.set('userID', data.userID);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Makes a request to logout a user using the login REST API endpoint,
|
||||
* returning a promise that can be used for processing the results of the call.
|
||||
*
|
||||
* @returns {Promise} A promise for the HTTP call.
|
||||
*/
|
||||
service.logout = function logout() {
|
||||
return $http({
|
||||
method: 'DELETE',
|
||||
url: 'api/token/' + encodeURIComponent(service.getCurrentToken())
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the user ID of the current user.
|
||||
*
|
||||
* @returns {String} The user ID of the current user.
|
||||
*/
|
||||
service.getCurrentUserID = function getCurrentUserID() {
|
||||
return localStorageUtility.get('userID');
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the auth token associated with the current user. If the current
|
||||
* user is not logged in, this token may not be valid.
|
||||
*
|
||||
* @returns {String} The auth token associated with the current user.
|
||||
*/
|
||||
service.getCurrentToken = function getCurrentToken() {
|
||||
return localStorageUtility.get('authToken');
|
||||
};
|
||||
|
||||
return service;
|
||||
}]);
|
@@ -49,7 +49,7 @@
|
||||
|
||||
<div class="logout-panel">
|
||||
<a class="manage button" ng-show="currentUserHasUpdate" href="#/manage">{{'home.manage' | translate}}</a>
|
||||
<a class="logout button" href="#/login">{{'home.logout' | translate}}</a>
|
||||
<a class="logout button" ng-click="logout()">{{'home.logout' | translate}}</a>
|
||||
</div>
|
||||
|
||||
<!-- The recent connections for this user -->
|
||||
|
@@ -29,7 +29,7 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
|
||||
// Get the dependencies commonJS style
|
||||
var permissionDAO = $injector.get("permissionDAO"),
|
||||
permissionCheckService = $injector.get("permissionCheckService"),
|
||||
localStorageUtility = $injector.get("localStorageUtility"),
|
||||
authenticationService = $injector.get("authenticationService"),
|
||||
$q = $injector.get("$q"),
|
||||
$document = $injector.get("$document"),
|
||||
$window = $injector.get("$window"),
|
||||
@@ -64,7 +64,7 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
|
||||
var permissionsLoaded= $q.defer();
|
||||
$scope.basicPermissionsLoaded = permissionsLoaded.promise;
|
||||
|
||||
$scope.currentUserID = localStorageUtility.get('userID');
|
||||
$scope.currentUserID = authenticationService.getCurrentUserID();
|
||||
|
||||
// If the user is unknown, force a login
|
||||
if(!$scope.currentUserID)
|
||||
@@ -86,6 +86,13 @@ angular.module('index').controller('indexController', ['$scope', '$injector',
|
||||
permissionsLoaded.resolve();
|
||||
});
|
||||
};
|
||||
|
||||
// Provide simple mechanism for logging out the current user
|
||||
$scope.logout = function logout() {
|
||||
authenticationService.logout().success(function logoutSuccess() {
|
||||
$location.path('/login');
|
||||
});
|
||||
};
|
||||
|
||||
// Try to load them now
|
||||
$scope.loadBasicPermissions();
|
||||
|
@@ -23,4 +23,4 @@
|
||||
/**
|
||||
* The module for the root of the application.
|
||||
*/
|
||||
angular.module('index', ['ngRoute', 'pascalprecht.translate', 'home', 'manage', 'login', 'client']);
|
||||
angular.module('index', ['ngRoute', 'pascalprecht.translate', 'auth', 'home', 'manage', 'login', 'client']);
|
||||
|
@@ -24,22 +24,14 @@ angular.module('login').controller('loginController', ['$scope', '$injector',
|
||||
function loginController($scope, $injector) {
|
||||
|
||||
// Get the dependencies commonJS style
|
||||
var authenticationService = $injector.get("authenticationService");
|
||||
var localStorageUtility = $injector.get("localStorageUtility");
|
||||
var $location = $injector.get("$location");
|
||||
var authenticationService = $injector.get("authenticationService");
|
||||
var $location = $injector.get("$location");
|
||||
|
||||
// Clear the auth token and userID to log out the user
|
||||
localStorageUtility.clear("authToken");
|
||||
localStorageUtility.clear("userID");
|
||||
|
||||
$scope.loginError = false;
|
||||
|
||||
$scope.login = function login() {
|
||||
authenticationService.login($scope.username, $scope.password)
|
||||
.success(function success(data, status, headers, config) {
|
||||
localStorageUtility.set('authToken', data.authToken);
|
||||
localStorageUtility.set('userID', data.userID);
|
||||
|
||||
// Set up the basic permissions for the user
|
||||
$scope.loadBasicPermissions();
|
||||
$location.path('/');
|
||||
@@ -47,4 +39,5 @@ angular.module('login').controller('loginController', ['$scope', '$injector',
|
||||
$scope.loginError = true;
|
||||
});
|
||||
};
|
||||
|
||||
}]);
|
||||
|
@@ -44,7 +44,7 @@ THE SOFTWARE.
|
||||
|
||||
<div class="logout-panel">
|
||||
<a class="back button" href="#/">{{'manage.back' | translate}}</a>
|
||||
<a class="logout button" href="#/login">{{'home.logout' | translate}}</a>
|
||||
<a class="logout button" ng-click="logout()">{{'home.logout' | translate}}</a>
|
||||
</div>
|
||||
|
||||
<h2>{{'manage.administration' | translate}}</h2>
|
||||
|
Reference in New Issue
Block a user