mirror of
				https://github.com/gyurix1968/guacamole-client.git
				synced 2025-10-31 00:53:21 +00:00 
			
		
		
		
	Ticket #362: Cleaned up a little bit.
This commit is contained in:
		| @@ -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; | ||||
|     } | ||||
| } | ||||
| @@ -1,16 +1,20 @@ | ||||
| package org.glyptodon.guacamole.net.basic.rest.auth; | ||||
|  | ||||
| import com.google.inject.Inject; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import javax.ws.rs.POST; | ||||
| import javax.ws.rs.Path; | ||||
| import javax.ws.rs.Produces; | ||||
| import javax.ws.rs.QueryParam; | ||||
| import javax.ws.rs.WebApplicationException; | ||||
| import javax.ws.rs.core.MediaType; | ||||
| import javax.ws.rs.core.Response; | ||||
| import org.glyptodon.guacamole.GuacamoleException; | ||||
| import org.glyptodon.guacamole.net.auth.AuthenticationProvider; | ||||
| import org.glyptodon.guacamole.net.auth.Credentials; | ||||
| 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.LoggerFactory; | ||||
|  | ||||
| @@ -42,6 +46,7 @@ import org.slf4j.LoggerFactory; | ||||
|  | ||||
|  | ||||
| @Path("/api/login") | ||||
| @Produces(MediaType.APPLICATION_JSON) | ||||
| public class LoginRESTService { | ||||
|      | ||||
|     /** | ||||
| @@ -76,7 +81,7 @@ public class LoginRESTService { | ||||
|      * @return The auth token for the newly logged-in user. | ||||
|      */ | ||||
|     @POST | ||||
|     public String login(@QueryParam("username") String username, | ||||
|     public APIAuthToken login(@QueryParam("username") String username, | ||||
|             @QueryParam("password") String password) { | ||||
|          | ||||
|         Credentials credentials = new Credentials(); | ||||
| @@ -89,18 +94,21 @@ public class LoginRESTService { | ||||
|             userContext = authProvider.getUserContext(credentials); | ||||
|         } catch(GuacamoleException 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. | ||||
|         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(); | ||||
|          | ||||
|         tokenUserMap.put(authToken, userContext); | ||||
|          | ||||
|         return authToken; | ||||
|         return new APIAuthToken(authToken); | ||||
|     } | ||||
|      | ||||
| } | ||||
|   | ||||
| @@ -69,34 +69,66 @@ public class Connection { | ||||
|         this.history = connection.getHistory(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the name of this connection. | ||||
|      * @return The name of this connection. | ||||
|      */ | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Set the name of this connection. | ||||
|      * @param name The name of this connection. | ||||
|      */ | ||||
|     public void setName(String name) { | ||||
|         this.name = name; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the identifier of this connection. | ||||
|      * @return The identifier of this connection. | ||||
|      */ | ||||
|     public String getIdentifier() { | ||||
|         return identifier; | ||||
|     } | ||||
|  | ||||
|      | ||||
|     /** | ||||
|      * Set the identifier of this connection. | ||||
|      * @param identifier The identifier of this connection. | ||||
|      */ | ||||
|     public void setIdentifier(String identifier) { | ||||
|         this.identifier = identifier; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the configuration for this connection. | ||||
|      * @return The configuration for this connection. | ||||
|      */ | ||||
|     public GuacamoleConfiguration getConfiguration() { | ||||
|         return configuration; | ||||
|     } | ||||
|  | ||||
|      | ||||
|     /** | ||||
|      * Set the configuration for this connection. | ||||
|      * @param configuration The configuration for this connection. | ||||
|      */ | ||||
|     public void setConfiguration(GuacamoleConfiguration configuration) { | ||||
|         this.configuration = configuration; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the history records for this connection. | ||||
|      * @return The history records for this connection. | ||||
|      */ | ||||
|     public List<? extends ConnectionRecord> getHistory() { | ||||
|         return history; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Set the history records for this connection. | ||||
|      * @param history The history records for this connection. | ||||
|      */ | ||||
|     public void setHistory(List<? extends ConnectionRecord> history) { | ||||
|         this.history = history; | ||||
|     } | ||||
|   | ||||
| @@ -43,7 +43,7 @@ import org.glyptodon.guacamole.net.basic.rest.auth.TokenUserContextMap; | ||||
|  * @author James Muehlner | ||||
|  */ | ||||
| @Path("/api/connection") | ||||
| @RequestScoped | ||||
| @Produces(MediaType.APPLICATION_JSON) | ||||
| public class ConnectionRESTService { | ||||
|      | ||||
|     /** | ||||
| @@ -59,14 +59,14 @@ public class ConnectionRESTService { | ||||
|     private ConnectionService connectionService; | ||||
|      | ||||
|     @GET | ||||
|     @Produces(MediaType.APPLICATION_JSON) | ||||
|     public List<Connection> getConnections(@QueryParam("token") String authToken, @QueryParam("parentID") String parentID) { | ||||
|         UserContext userContext = tokenUserMap.get(authToken); | ||||
|         | ||||
|         // authentication failed. | ||||
|         if(userContext == null) | ||||
|             throw new WebApplicationException(Response.Status.UNAUTHORIZED); | ||||
|          | ||||
|             throw new WebApplicationException( | ||||
|                 Response.status(Response.Status.UNAUTHORIZED) | ||||
|                 .entity(new APIError("Permission Denied.")).build()); | ||||
|          | ||||
|         try { | ||||
|             // If the parent connection group is passed in, try to find it. | ||||
| @@ -99,9 +99,13 @@ public class ConnectionRESTService { | ||||
|              | ||||
|             return connectionService.convertConnectionList(connections); | ||||
|         } 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) { | ||||
|             throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); | ||||
|             throw new WebApplicationException( | ||||
|                 Response.status(Response.Status.INTERNAL_SERVER_ERROR) | ||||
|                     .entity(e).build()); | ||||
|         } | ||||
|     }    | ||||
|      | ||||
|   | ||||
		Reference in New Issue
	
	Block a user