From c6ce92bd0a35a55dd678ceb83b888385b72ac755 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 15 Jul 2016 21:58:59 -0700 Subject: [PATCH] GUACAMOLE-5: Implement retrieval of sharing credentials from an active connection via REST. --- .../activeconnection/APIUserCredentials.java | 82 +++++++++++++++++++ .../ActiveConnectionResource.java | 27 ++++++ 2 files changed, 109 insertions(+) create mode 100644 guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/APIUserCredentials.java diff --git a/guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/APIUserCredentials.java b/guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/APIUserCredentials.java new file mode 100644 index 000000000..eb87bafcb --- /dev/null +++ b/guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/APIUserCredentials.java @@ -0,0 +1,82 @@ +/* + * 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. + */ + +package org.apache.guacamole.rest.activeconnection; + +import java.util.Collection; +import java.util.Map; +import org.apache.guacamole.form.Field; +import org.apache.guacamole.net.auth.credentials.UserCredentials; + +/** + * The object returned by REST API calls to define a full set of valid + * credentials, including field definitions and corresponding expected + * values. + * + * @author Michael Jumper + */ +public class APIUserCredentials { + + /** + * All expected request parameters, if any, as a collection of fields. + */ + private final Collection expected; + + /** + * A map of all field values by field name. + */ + private final Map values; + + /** + * Creates a new APIUserCredentials object whose required parameters and + * corresponding values are defined by the given UserCredentials. + * + * @param userCredentials + * The UserCredentials which defines the parameters and corresponding + * values of this APIUserCredentials. + */ + public APIUserCredentials(UserCredentials userCredentials) { + this.expected = userCredentials.getFields(); + this.values = userCredentials.getValues(); + } + + /** + * Returns a collection of all required parameters, where each parameter is + * represented by a field. + * + * @return + * A collection of all required parameters. + */ + public Collection getExpected() { + return expected; + } + + /** + * Returns a map of all field values by field name. The fields having the + * names used within this map should be defined within the collection of + * required parameters returned by getExpected(). + * + * @return + * A map of all field values by field name. + */ + public Map getValues() { + return values; + } + +} diff --git a/guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/ActiveConnectionResource.java b/guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/ActiveConnectionResource.java index 6bbfaf7b7..736cbf8b9 100644 --- a/guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/ActiveConnectionResource.java +++ b/guacamole/src/main/java/org/apache/guacamole/rest/activeconnection/ActiveConnectionResource.java @@ -23,7 +23,9 @@ import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.AssistedInject; import javax.ws.rs.Consumes; +import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.apache.guacamole.GuacamoleException; @@ -116,4 +118,29 @@ public class ActiveConnectionResource } + /** + * Retrieves a set of credentials which can be POSTed by another user to the + * "/api/tokens" endpoint to obtain access strictly to this connection. The + * retrieved credentials may be purpose-generated and temporary. + * + * @param sharingProfileIdentifier The identifier of the sharing connection + * defining the semantics of the shared session. + * + * @return The set of credentials which should be used to access strictly + * this connection. + * + * @throws GuacamoleException If an error occurs while retrieving the + * sharing credentials for this connection. + */ + @GET + @Path("sharingCredentials/{sharingProfile}") + public APIUserCredentials getSharingCredentials( + @PathParam("sharingProfile") String sharingProfileIdentifier) + throws GuacamoleException { + + // Generate and return sharing credentials for the active connection + return new APIUserCredentials(activeConnection.getSharingCredentials(sharingProfileIdentifier)); + + } + }