From dc9d9da8d27b314897ac8842f63b9b3b6031e307 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 18 Dec 2014 01:10:43 -0800 Subject: [PATCH] GUAC-932: Require auth token for listing protocols. Clean style of JS and Java. --- .../rest/protocol/ProtocolRESTService.java | 30 +++++++++++++++---- .../app/rest/services/protocolService.js | 17 +++++++++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/protocol/ProtocolRESTService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/protocol/ProtocolRESTService.java index dacdf8f56..537e53c4d 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/protocol/ProtocolRESTService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/protocol/ProtocolRESTService.java @@ -24,14 +24,15 @@ package org.glyptodon.guacamole.net.basic.rest.protocol; import com.google.inject.Inject; import java.util.Map; -import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.glyptodon.guacamole.GuacamoleException; import org.glyptodon.guacamole.net.basic.ProtocolInfo; import org.glyptodon.guacamole.net.basic.rest.AuthProviderRESTExposure; +import org.glyptodon.guacamole.net.basic.rest.auth.AuthenticationService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,9 +41,8 @@ import org.slf4j.LoggerFactory; * * @author James Muehlner */ -@Path("/protocol") +@Path("/protocols") @Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) public class ProtocolRESTService { /** @@ -50,6 +50,12 @@ public class ProtocolRESTService { */ private static final Logger logger = LoggerFactory.getLogger(ProtocolRESTService.class); + /** + * A service for authenticating users from auth tokens. + */ + @Inject + private AuthenticationService authenticationService; + /** * Service for retrieving protocol definitions. */ @@ -59,15 +65,27 @@ public class ProtocolRESTService { /** * Gets a map of protocols defined in the system - protocol name to protocol. * - * @return The protocol map. - * @throws GuacamoleException If a problem is encountered while listing protocols. + * @param authToken + * The authentication token that is used to authenticate the user + * performing the operation. + * + * @return + * A map of protocol information, where each key is the unique name + * associated with that protocol. + * + * @throws GuacamoleException + * If an error occurs while retrieving the available protocols. */ @GET @AuthProviderRESTExposure - public Map getProtocols() throws GuacamoleException { + public Map getProtocols(@QueryParam("token") String authToken) throws GuacamoleException { + // Verify the given auth token is valid + authenticationService.getUserContext(authToken); + // Get and return a map of all protocols. return protocolRetrievalservice.getProtocolMap(); + } } diff --git a/guacamole/src/main/webapp/app/rest/services/protocolService.js b/guacamole/src/main/webapp/app/rest/services/protocolService.js index 3a3180750..1a863107a 100644 --- a/guacamole/src/main/webapp/app/rest/services/protocolService.js +++ b/guacamole/src/main/webapp/app/rest/services/protocolService.js @@ -23,7 +23,8 @@ /** * Service for operating on protocol metadata via the REST API. */ -angular.module('rest').factory('protocolService', ['$http', function protocolService($http) { +angular.module('rest').factory('protocolService', ['$http', 'authenticationService', + function protocolService($http, authenticationService) { var service = {}; @@ -37,7 +38,19 @@ angular.module('rest').factory('protocolService', ['$http', function protocolSer * objects upon success. */ service.getProtocols = function getProtocols() { - return $http.get("api/protocol"); + + // Build HTTP parameters set + var httpParameters = { + token : authenticationService.getCurrentToken() + }; + + // Retrieve available protocols + return $http({ + method : 'GET', + url : 'api/protocols', + params : httpParameters + }); + }; return service;