Merge pull request #60 from glyptodon/secondary-auth

GUAC-971: Restore secondary authentication
This commit is contained in:
James Muehlner
2015-01-05 21:03:37 -08:00
5 changed files with 295 additions and 91 deletions

View File

@@ -37,7 +37,81 @@ angular.module('auth').factory('authenticationService', ['$http', '$cookieStore'
var AUTH_COOKIE_ID = "GUAC_AUTH";
/**
* Makes a request to authenticate a user using the token REST API endpoint,
* Makes a request to authenticate a user using the token REST API endpoint
* and given arbitrary parameters, returning a promise that succeeds only
* if the authentication operation was successful. The resulting
* authentication data can be retrieved later via getCurrentToken() or
* getCurrentUserID().
*
* The provided parameters can be virtually any object, as each property
* will be sent as an HTTP parameter in the authentication request.
* Standard parameters include "username" for the user's username,
* "password" for the user's associated password, and "token" for the
* auth token to check/update.
*
* If a token is provided, it will be reused if possible.
*
* @param {Object} parameters
* Arbitrary parameters to authenticate with.
*
* @returns {Promise}
* A promise which succeeds only if the login operation was successful.
*/
service.authenticate = function authenticate(parameters) {
return $http({
method: 'POST',
url: 'api/tokens',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: $.param(parameters),
}).success(function success(data, status, headers, config) {
$cookieStore.put(AUTH_COOKIE_ID, {
authToken : data.authToken,
userID : data.userID
});
});
};
/**
* Makes a request to update the current auth token, if any, using the
* token REST API endpoint. If the optional parameters object is provided,
* its properties will be included as parameters in the update request.
* This function returns a promise that succeeds only if the authentication
* operation was successful. The resulting authentication data can be
* retrieved later via getCurrentToken() or getCurrentUserID().
*
* If there is no current auth token, this function behaves identically to
* authenticate(), and makes a general authentication request.
*
* @param {Object} [parameters]
* Arbitrary parameters to authenticate with, if any.
*
* @returns {Promise}
* A promise which succeeds only if the login operation was successful.
*/
service.updateCurrentToken = function updateCurrentToken(parameters) {
// HTTP parameters for the authentication request
var httpParameters = {};
// Add token parameter if current token is known
var token = service.getCurrentToken();
if (token)
httpParameters.token = service.getCurrentToken();
// Add any additional parameters
if (parameters)
angular.extend(httpParameters, parameters);
// Make the request
return service.authenticate(httpParameters);
};
/**
* Makes a request to authenticate a user using the token REST API endpoint
* with a username and password, ignoring any currently-stored token,
* returning a promise that succeeds only if the login operation was
* successful. The resulting authentication data can be retrieved later
* via getCurrentToken() or getCurrentUserID().
@@ -52,21 +126,9 @@ angular.module('auth').factory('authenticationService', ['$http', '$cookieStore'
* A promise which succeeds only if the login operation was successful.
*/
service.login = function login(username, password) {
return $http({
method: 'POST',
url: 'api/tokens',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: $.param({
username: username,
password: password
})
}).success(function success(data, status, headers, config) {
$cookieStore.put(AUTH_COOKIE_ID, {
authToken : data.authToken,
userID : data.userID
});
return service.authenticate({
username: username,
password: password
});
};

View File

@@ -25,55 +25,113 @@
*/
angular.module('index').config(['$routeProvider', '$locationProvider',
function indexRouteConfig($routeProvider, $locationProvider) {
// Disable HTML5 mode (use # for routing)
$locationProvider.html5Mode(false);
$routeProvider
.when('/', {
title: 'APP.NAME',
bodyClassName: 'home',
templateUrl: 'app/home/templates/home.html',
controller: 'homeController'
})
.when('/manage/', {
title: 'APP.NAME',
bodyClassName: 'manage',
templateUrl: 'app/manage/templates/manage.html',
controller: 'manageController'
})
.when('/manage/connections/:id?', {
title: 'APP.NAME',
bodyClassName: 'manage',
templateUrl: 'app/manage/templates/manageConnection.html',
controller: 'manageConnectionController'
})
.when('/manage/connectionGroups/:id?', {
title: 'APP.NAME',
bodyClassName: 'manage',
templateUrl: 'app/manage/templates/manageConnectionGroup.html',
controller: 'manageConnectionGroupController'
})
.when('/manage/users/:id', {
title: 'APP.NAME',
bodyClassName: 'manage',
templateUrl: 'app/manage/templates/manageUser.html',
controller: 'manageUserController'
})
.when('/login/', {
title: 'APP.NAME',
bodyClassName: 'login',
templateUrl: 'app/login/templates/login.html',
controller: 'loginController'
})
.when('/client/:type/:id/:params?', {
bodyClassName: 'client',
templateUrl: 'app/client/templates/client.html',
controller: 'clientController'
})
.otherwise({
redirectTo: '/'
/**
* Attempts to re-authenticate with the Guacamole server, sending any
* query parameters in the URL, along with the current auth token, and
* updating locally stored token if necessary.
*
* @param {Service} $injector
* The Angular $injector service.
*
* @returns {Promise}
* A promise which resolves successfully only after an attempt to
* re-authenticate has been made.
*/
var updateCurrentToken = ['$injector', function updateCurrentToken($injector) {
// Required services
var $location = $injector.get('$location');
var $q = $injector.get('$q');
var authenticationService = $injector.get('authenticationService');
// Promise for authentication attempt
var authAttempt = $q.defer();
// Re-authenticate including any parameters in URL
authenticationService.updateCurrentToken($location.search())
['finally'](function authenticationAttemptComplete() {
authAttempt.resolve();
});
// Return promise that will resolve regardless of success/failure
return authAttempt.promise;
}];
// Configure each possible route
$routeProvider
// Home screen
.when('/', {
title : 'APP.NAME',
bodyClassName : 'home',
templateUrl : 'app/home/templates/home.html',
controller : 'homeController',
resolve : { updateCurrentToken: updateCurrentToken }
})
// Management screen
.when('/manage/', {
title : 'APP.NAME',
bodyClassName : 'manage',
templateUrl : 'app/manage/templates/manage.html',
controller : 'manageController',
resolve : { updateCurrentToken: updateCurrentToken }
})
// Connection editor
.when('/manage/connections/:id?', {
title : 'APP.NAME',
bodyClassName : 'manage',
templateUrl : 'app/manage/templates/manageConnection.html',
controller : 'manageConnectionController',
resolve : { updateCurrentToken: updateCurrentToken }
})
// Connection group editor
.when('/manage/connectionGroups/:id?', {
title : 'APP.NAME',
bodyClassName : 'manage',
templateUrl : 'app/manage/templates/manageConnectionGroup.html',
controller : 'manageConnectionGroupController',
resolve : { updateCurrentToken: updateCurrentToken }
})
// User editor
.when('/manage/users/:id', {
title : 'APP.NAME',
bodyClassName : 'manage',
templateUrl : 'app/manage/templates/manageUser.html',
controller : 'manageUserController',
resolve : { updateCurrentToken: updateCurrentToken }
})
// Login screen
.when('/login/', {
title : 'APP.NAME',
bodyClassName : 'login',
templateUrl : 'app/login/templates/login.html',
controller : 'loginController'
// No need to update token here - the login screen ignores all auth
})
// Client view
.when('/client/:type/:id/:params?', {
bodyClassName : 'client',
templateUrl : 'app/client/templates/client.html',
controller : 'clientController',
resolve : { updateCurrentToken: updateCurrentToken }
})
// Redirect to home screen if page not found
.otherwise({
redirectTo : '/'
});
}]);