Ticket #362: Cleaned up a little bit.

This commit is contained in:
James Muehlner
2013-09-16 21:37:24 -07:00
parent b15777a70f
commit 6f15c20e50
4 changed files with 106 additions and 13 deletions

View File

@@ -0,0 +1,49 @@
package org.glyptodon.guacamole.net.basic.rest.auth;
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A simple object to represent an auth token in the API.
*
* @author James Muehlner
*/
public class APIAuthToken {
/**
* The auth token.
*/
private String authToken;
/**
* Get the auth token.
* @return The auth token.
*/
public String getAuthToken() {
return authToken;
}
/**
* Create a new APIAuthToken Object with the given auth token.
*
* @param authToken The auth token to create the new APIAuthToken with.
*/
public APIAuthToken(String authToken) {
this.authToken = authToken;
}
}

View File

@@ -1,16 +1,20 @@
package org.glyptodon.guacamole.net.basic.rest.auth; package org.glyptodon.guacamole.net.basic.rest.auth;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.glyptodon.guacamole.GuacamoleException; import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.net.auth.AuthenticationProvider; import org.glyptodon.guacamole.net.auth.AuthenticationProvider;
import org.glyptodon.guacamole.net.auth.Credentials; import org.glyptodon.guacamole.net.auth.Credentials;
import org.glyptodon.guacamole.net.auth.UserContext; import org.glyptodon.guacamole.net.auth.UserContext;
import org.glyptodon.guacamole.net.basic.rest.RESTModule; import org.glyptodon.guacamole.net.basic.rest.APIError;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -42,6 +46,7 @@ import org.slf4j.LoggerFactory;
@Path("/api/login") @Path("/api/login")
@Produces(MediaType.APPLICATION_JSON)
public class LoginRESTService { public class LoginRESTService {
/** /**
@@ -76,7 +81,7 @@ public class LoginRESTService {
* @return The auth token for the newly logged-in user. * @return The auth token for the newly logged-in user.
*/ */
@POST @POST
public String login(@QueryParam("username") String username, public APIAuthToken login(@QueryParam("username") String username,
@QueryParam("password") String password) { @QueryParam("password") String password) {
Credentials credentials = new Credentials(); Credentials credentials = new Credentials();
@@ -89,18 +94,21 @@ public class LoginRESTService {
userContext = authProvider.getUserContext(credentials); userContext = authProvider.getUserContext(credentials);
} catch(GuacamoleException e) { } catch(GuacamoleException e) {
logger.error("Exception caught while authenticating user.", e); logger.error("Exception caught while authenticating user.", e);
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).build());
} }
// authentication failed. // authentication failed.
if(userContext == null) if(userContext == null)
throw new WebApplicationException(Response.Status.UNAUTHORIZED); throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
String authToken = authTokenGenerator.getToken(); String authToken = authTokenGenerator.getToken();
tokenUserMap.put(authToken, userContext); tokenUserMap.put(authToken, userContext);
return authToken; return new APIAuthToken(authToken);
} }
} }

View File

@@ -69,34 +69,66 @@ public class Connection {
this.history = connection.getHistory(); this.history = connection.getHistory();
} }
/**
* Get the name of this connection.
* @return The name of this connection.
*/
public String getName() { public String getName() {
return name; return name;
} }
/**
* Set the name of this connection.
* @param name The name of this connection.
*/
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
/**
* Get the identifier of this connection.
* @return The identifier of this connection.
*/
public String getIdentifier() { public String getIdentifier() {
return identifier; return identifier;
} }
/**
* Set the identifier of this connection.
* @param identifier The identifier of this connection.
*/
public void setIdentifier(String identifier) { public void setIdentifier(String identifier) {
this.identifier = identifier; this.identifier = identifier;
} }
/**
* Get the configuration for this connection.
* @return The configuration for this connection.
*/
public GuacamoleConfiguration getConfiguration() { public GuacamoleConfiguration getConfiguration() {
return configuration; return configuration;
} }
/**
* Set the configuration for this connection.
* @param configuration The configuration for this connection.
*/
public void setConfiguration(GuacamoleConfiguration configuration) { public void setConfiguration(GuacamoleConfiguration configuration) {
this.configuration = configuration; this.configuration = configuration;
} }
/**
* Get the history records for this connection.
* @return The history records for this connection.
*/
public List<? extends ConnectionRecord> getHistory() { public List<? extends ConnectionRecord> getHistory() {
return history; return history;
} }
/**
* Set the history records for this connection.
* @param history The history records for this connection.
*/
public void setHistory(List<? extends ConnectionRecord> history) { public void setHistory(List<? extends ConnectionRecord> history) {
this.history = history; this.history = history;
} }

View File

@@ -43,7 +43,7 @@ import org.glyptodon.guacamole.net.basic.rest.auth.TokenUserContextMap;
* @author James Muehlner * @author James Muehlner
*/ */
@Path("/api/connection") @Path("/api/connection")
@RequestScoped @Produces(MediaType.APPLICATION_JSON)
public class ConnectionRESTService { public class ConnectionRESTService {
/** /**
@@ -59,14 +59,14 @@ public class ConnectionRESTService {
private ConnectionService connectionService; private ConnectionService connectionService;
@GET @GET
@Produces(MediaType.APPLICATION_JSON)
public List<Connection> getConnections(@QueryParam("token") String authToken, @QueryParam("parentID") String parentID) { public List<Connection> getConnections(@QueryParam("token") String authToken, @QueryParam("parentID") String parentID) {
UserContext userContext = tokenUserMap.get(authToken); UserContext userContext = tokenUserMap.get(authToken);
// authentication failed. // authentication failed.
if(userContext == null) if(userContext == null)
throw new WebApplicationException(Response.Status.UNAUTHORIZED); throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
try { try {
// If the parent connection group is passed in, try to find it. // If the parent connection group is passed in, try to find it.
@@ -99,9 +99,13 @@ public class ConnectionRESTService {
return connectionService.convertConnectionList(connections); return connectionService.convertConnectionList(connections);
} catch(GuacamoleSecurityException e) { } catch(GuacamoleSecurityException e) {
throw new WebApplicationException(e, Response.Status.UNAUTHORIZED); throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
} catch(GuacamoleException e) { } catch(GuacamoleException e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(e).build());
} }
} }