GUACAMOLE-5: Implement JavaScript service for retrieving sharing credentials from an active connection.

This commit is contained in:
Michael Jumper
2016-07-15 18:41:17 -07:00
parent c6ce92bd0a
commit 62dcd9e9ec
3 changed files with 205 additions and 0 deletions

View File

@@ -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.<UserCredentials>}
* 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;
}]);

View File

@@ -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.<UserCredentials>}
* 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

View File

@@ -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.<String, String>
*/
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;
}]);