From 62dcd9e9ec29e2656ac43a9c33522ad032332e93 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 15 Jul 2016 18:41:17 -0700 Subject: [PATCH] GUACAMOLE-5: Implement JavaScript service for retrieving sharing credentials from an active connection. --- .../rest/services/activeConnectionService.js | 35 +++++ .../webapp/app/rest/services/tunnelService.js | 37 +++++ .../webapp/app/rest/types/UserCredentials.js | 133 ++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 guacamole/src/main/webapp/app/rest/types/UserCredentials.js diff --git a/guacamole/src/main/webapp/app/rest/services/activeConnectionService.js b/guacamole/src/main/webapp/app/rest/services/activeConnectionService.js index 9c967be32..7c8fb6a35 100644 --- a/guacamole/src/main/webapp/app/rest/services/activeConnectionService.js +++ b/guacamole/src/main/webapp/app/rest/services/activeConnectionService.js @@ -166,6 +166,41 @@ angular.module('rest').factory('activeConnectionService', ['$injector', }; + /** + * Makes a request to the REST API to generate credentials which have + * access strictly to the given active connection, using the restrictions + * defined by the given sharing profile, returning a promise that provides + * the resulting @link{UserCredentials} object if successful. + * + * @param {String} id + * The identifier of the active connection being shared. + * + * @param {String} sharingProfile + * The identifier of the sharing profile dictating the + * semantics/restrictions which apply to the shared session. + * + * @returns {Promise.} + * A promise which will resolve with a @link{UserCredentials} object + * upon success. + */ + service.getSharingCredentials = function getSharingCredentials(dataSource, id, sharingProfile) { + + // Build HTTP parameters set + var httpParameters = { + token : authenticationService.getCurrentToken() + }; + + // Generate sharing credentials + return $http({ + method : 'GET', + url : 'api/session/data/' + encodeURIComponent(dataSource) + + '/activeConnections/' + encodeURIComponent(id) + + '/sharingCredentials/' + encodeURIComponent(sharingProfile), + params : httpParameters + }); + + }; + return service; }]); diff --git a/guacamole/src/main/webapp/app/rest/services/tunnelService.js b/guacamole/src/main/webapp/app/rest/services/tunnelService.js index e93c50fb9..1395b2581 100644 --- a/guacamole/src/main/webapp/app/rest/services/tunnelService.js +++ b/guacamole/src/main/webapp/app/rest/services/tunnelService.js @@ -68,6 +68,43 @@ angular.module('rest').factory('tunnelService', ['$injector', }; + /** + * Makes a request to the REST API to generate credentials which have + * access strictly to the active connection associated with the given + * tunnel, using the restrictions defined by the given sharing profile, + * returning a promise that provides the resulting @link{UserCredentials} + * object if successful. + * + * @param {String} tunnel + * The UUID of the tunnel associated with the Guacamole connection + * being shared. + * + * @param {String} sharingProfile + * The identifier of the connection object dictating the + * semantics/restrictions which apply to the shared session. + * + * @returns {Promise.} + * A promise which will resolve with a @link{UserCredentials} object + * upon success. + */ + service.getSharingCredentials = function getSharingCredentials(tunnel, sharingProfile) { + + // Build HTTP parameters set + var httpParameters = { + token : authenticationService.getCurrentToken() + }; + + // Generate sharing credentials + return $http({ + method : 'GET', + url : 'api/session/tunnels/' + encodeURIComponent(tunnel) + + '/activeConnection/sharingCredentials/' + + encodeURIComponent(sharingProfile), + params : httpParameters + }); + + }; + /** * Makes a request to the REST API to retrieve the contents of a stream * which has been created within the active Guacamole connection associated diff --git a/guacamole/src/main/webapp/app/rest/types/UserCredentials.js b/guacamole/src/main/webapp/app/rest/types/UserCredentials.js new file mode 100644 index 000000000..ddf9eaf3e --- /dev/null +++ b/guacamole/src/main/webapp/app/rest/types/UserCredentials.js @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Service which defines the UserCredentials class. + */ +angular.module('rest').factory('UserCredentials', ['$injector', function defineUserCredentials($injector) { + + // Required services + var $window = $injector.get('$window'); + + // Required types + var Field = $injector.get('Field'); + + /** + * The object returned by REST API calls to define a full set of valid + * credentials, including field definitions and corresponding expected + * values. + * + * @constructor + * @param {UserCredentials|Object} [template={}] + * The object whose properties should be copied within the new + * UserCredentials. + */ + var UserCredentials = function UserCredentials(template) { + + // Use empty object by default + template = template || {}; + + /** + * Any parameters which should be provided when these credentials are + * submitted. If no such information is available, this will be null. + * + * @type Field[] + */ + this.expected = template.expected; + + /** + * A map of all field values by field name. The fields having the names + * used within this map should be defined within the @link{Field} array + * stored under the @link{expected} property. + * + * @type Object. + */ + this.values = template.values; + + }; + + /** + * Generates a query string containing all QUERY_PARAMETER fields from the + * given UserCredentials object, along with their corresponding values. The + * parameter names and values will be appropriately URL-encoded and + * separated by ampersands. + * + * @param {UserCredentials} userCredentials + * The UserCredentials to retrieve all query parameters from. + * + * @returns {String} + * A string containing all QUERY_PARAMETER fields as name/value pairs + * separated by ampersands, where each name is separated by the value + * by an equals sign. + */ + UserCredentials.getQueryParameters = function getQueryParameters(userCredentials) { + + // Build list of parameter name/value pairs + var parameters = []; + angular.forEach(userCredentials.expected, function addQueryParameter(field) { + + // Only add query parameters + if (field.type !== Field.Type.QUERY_PARAMETER) + return; + + // Pull parameter name and value + var name = field.name; + var value = userCredentials.values[name]; + + // Properly encode name/value pair + parameters.push(encodeURIComponent(name) + '=' + encodeURIComponent(value)); + + }); + + // Separate each name/value pair by an ampersand + return parameters.join('&'); + + }; + + /** + * Returns a fully-qualified, absolute URL to Guacamole prepopulated with + * any query parameters dictated by the QUERY_PARAMETER fields defined in + * the given UserCredentials. + * + * @param {UserCredentials} userCredentials + * The UserCredentials to retrieve all query parameters from. + * + * @returns {String} + * A fully-qualified, absolute URL to Guacamole prepopulated with the + * query parameters dictated by the given UserCredentials. + */ + UserCredentials.getLink = function getLink(userCredentials) { + + // Build base link + var link = $window.location.origin + + $window.location.pathname + + '#/'; + + // Add any required parameters + var params = UserCredentials.getQueryParameters(userCredentials); + if (params) + link += '?' + params; + + return link; + + }; + + return UserCredentials; + +}]);