mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-07 13:41:21 +00:00
Refactored as implementations of AuthenticatingHttpServlet.
This commit is contained in:
@@ -18,8 +18,11 @@ package net.sourceforge.guacamole.net.basic;
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import net.sourceforge.guacamole.GuacamoleException;
|
import net.sourceforge.guacamole.GuacamoleException;
|
||||||
import net.sourceforge.guacamole.net.InetGuacamoleSocket;
|
import net.sourceforge.guacamole.net.InetGuacamoleSocket;
|
||||||
@@ -39,10 +42,27 @@ import org.slf4j.LoggerFactory;
|
|||||||
*
|
*
|
||||||
* @author Michael Jumper
|
* @author Michael Jumper
|
||||||
*/
|
*/
|
||||||
public class BasicGuacamoleTunnelServlet extends GuacamoleHTTPTunnelServlet {
|
public class BasicGuacamoleTunnelServlet extends AuthenticatingHttpServlet {
|
||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(BasicGuacamoleTunnelServlet.class);
|
private Logger logger = LoggerFactory.getLogger(BasicGuacamoleTunnelServlet.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void authenticatedService(
|
||||||
|
Map<String, GuacamoleConfiguration> configs,
|
||||||
|
HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
|
||||||
|
// If authenticated, respond as tunnel
|
||||||
|
tunnelServlet.service(request, response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapped GuacamoleHTTPTunnelServlet which will handle all authenticated
|
||||||
|
* requests.
|
||||||
|
*/
|
||||||
|
private GuacamoleHTTPTunnelServlet tunnelServlet = new GuacamoleHTTPTunnelServlet() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
|
protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
|
||||||
|
|
||||||
@@ -88,5 +108,7 @@ public class BasicGuacamoleTunnelServlet extends GuacamoleHTTPTunnelServlet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,92 +18,30 @@ package net.sourceforge.guacamole.net.basic;
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import net.sourceforge.guacamole.net.auth.AuthenticationProvider;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.HttpServlet;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
|
||||||
import net.sourceforge.guacamole.GuacamoleException;
|
|
||||||
import net.sourceforge.guacamole.net.auth.Credentials;
|
|
||||||
import net.sourceforge.guacamole.properties.GuacamoleProperties;
|
|
||||||
import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
|
|
||||||
import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
|
import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the authorized configurations associated with a given
|
* Simple dummy AuthenticatingHttpServlet which provides an endpoint for arbitrary
|
||||||
* username/password pair using the authentication provider defined in
|
* authentication requests that do not expect a response.
|
||||||
* guacamole.properties.
|
|
||||||
*
|
|
||||||
* All authorized configurations will be stored in the current HttpSession.
|
|
||||||
*
|
|
||||||
* Success and failure are logged.
|
|
||||||
*
|
*
|
||||||
* @author Michael Jumper
|
* @author Michael Jumper
|
||||||
*/
|
*/
|
||||||
public class BasicLogin extends HttpServlet {
|
public class BasicLogin extends AuthenticatingHttpServlet {
|
||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(BasicLogin.class);
|
private Logger logger = LoggerFactory.getLogger(BasicLogin.class);
|
||||||
|
|
||||||
private AuthenticationProvider authProvider;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() throws ServletException {
|
protected void authenticatedService(
|
||||||
|
Map<String, GuacamoleConfiguration> configs,
|
||||||
// Get auth provider instance
|
HttpServletRequest request, HttpServletResponse response)
|
||||||
try {
|
|
||||||
authProvider = GuacamoleProperties.getRequiredProperty(BasicGuacamoleProperties.AUTH_PROVIDER);
|
|
||||||
}
|
|
||||||
catch (GuacamoleException e) {
|
|
||||||
logger.error("Error getting authentication provider from properties.", e);
|
|
||||||
throw new ServletException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void service(HttpServletRequest request, HttpServletResponse response)
|
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
logger.info("Login was successful.");
|
||||||
HttpSession httpSession = request.getSession(true);
|
|
||||||
|
|
||||||
// Retrieve username and password from parms
|
|
||||||
String username = request.getParameter("username");
|
|
||||||
String password = request.getParameter("password");
|
|
||||||
|
|
||||||
// Build credentials object
|
|
||||||
Credentials credentials = new Credentials ();
|
|
||||||
credentials.setSession(httpSession);
|
|
||||||
credentials.setRequest(request);
|
|
||||||
credentials.setUsername(username);
|
|
||||||
credentials.setPassword(password);
|
|
||||||
|
|
||||||
// Get authorized configs
|
|
||||||
Map<String, GuacamoleConfiguration> configs;
|
|
||||||
try {
|
|
||||||
configs = authProvider.getAuthorizedConfigurations(credentials);
|
|
||||||
}
|
|
||||||
catch (GuacamoleException e) {
|
|
||||||
logger.error("Error retrieving configuration(s) for user {}.", username);
|
|
||||||
response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (configs == null) {
|
|
||||||
logger.warn("Failed login from {} for user \"{}\".", request.getRemoteAddr(), username);
|
|
||||||
response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Successful login from {} for user \"{}\".", request.getRemoteAddr(), username);
|
|
||||||
|
|
||||||
// Associate configs with session
|
|
||||||
httpSession.setAttribute("GUAC_CONFIGS", configs);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -34,25 +34,14 @@ import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
|
|||||||
*
|
*
|
||||||
* @author Michael Jumper
|
* @author Michael Jumper
|
||||||
*/
|
*/
|
||||||
public class ConfigurationList extends HttpServlet {
|
public class ConfigurationList extends AuthenticatingHttpServlet {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void service(HttpServletRequest request, HttpServletResponse response)
|
protected void authenticatedService(
|
||||||
|
Map<String, GuacamoleConfiguration> configs,
|
||||||
|
HttpServletRequest request, HttpServletResponse response)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
HttpSession httpSession = request.getSession(true);
|
|
||||||
|
|
||||||
// Get user configuration
|
|
||||||
// Get authorized configs
|
|
||||||
Map<String, GuacamoleConfiguration> configs = (Map<String, GuacamoleConfiguration>)
|
|
||||||
httpSession.getAttribute("GUAC_CONFIGS");
|
|
||||||
|
|
||||||
// If no configs in session, not authorized
|
|
||||||
if (configs == null) {
|
|
||||||
response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not cache
|
// Do not cache
|
||||||
response.setHeader("Cache-Control", "no-cache");
|
response.setHeader("Cache-Control", "no-cache");
|
||||||
|
|
||||||
@@ -72,7 +61,6 @@ public class ConfigurationList extends HttpServlet {
|
|||||||
out.print(config.getProtocol());
|
out.print(config.getProtocol());
|
||||||
out.println("\"/>");
|
out.println("\"/>");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out.println("</configs>");
|
out.println("</configs>");
|
||||||
|
Reference in New Issue
Block a user