Migrated to improved API.

This commit is contained in:
Michael Jumper
2011-01-02 02:37:09 -08:00
parent 6e7eeec5b0
commit 1dec9e889b
3 changed files with 57 additions and 98 deletions

View File

@@ -27,6 +27,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.net.Configuration;
import net.sourceforge.guacamole.net.GuacamoleProperties; import net.sourceforge.guacamole.net.GuacamoleProperties;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@@ -80,7 +81,7 @@ public class BasicFileAuthenticationProvider implements BasicLogin.Authenticatio
} }
@Override @Override
public BasicLogin.AuthorizedConfiguration getAuthorizedConfiguration(String username, String password) throws GuacamoleException { public Configuration getAuthorizedConfiguration(String username, String password) throws GuacamoleException {
// Check mapping file mod time // Check mapping file mod time
File userMappingFile = getUserMappingFile(); File userMappingFile = getUserMappingFile();
@@ -95,13 +96,19 @@ public class BasicFileAuthenticationProvider implements BasicLogin.Authenticatio
} }
AuthInfo info = mapping.get(username); AuthInfo info = mapping.get(username);
if (info != null && info.validate(username, password)) if (info != null && info.validate(username, password)) {
return new BasicLogin.AuthorizedConfiguration(
info.getProtocol(), Configuration config = new Configuration();
info.getHostname(),
info.getPort(), // TODO: Migrate user-mapping to general form
info.getPassword() config.setProtocol(info.getProtocol());
); config.setParameter("hostname", info.getHostname());
config.setParameter("port", Integer.toString(info.getPort()));
config.setParameter("password", info.getPassword());
return config;
}
return null; return null;

View File

@@ -2,9 +2,11 @@
package net.sourceforge.guacamole.net.authentication.basic; package net.sourceforge.guacamole.net.authentication.basic;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import net.sourceforge.guacamole.GuacamoleClient;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.net.Configuration;
import net.sourceforge.guacamole.net.GuacamoleSession; import net.sourceforge.guacamole.net.GuacamoleSession;
import net.sourceforge.guacamole.net.authentication.GuacamoleSessionProvider; import net.sourceforge.guacamole.net.authentication.GuacamoleClientProvider;
/* /*
* Guacamole - Clientless Remote Desktop * Guacamole - Clientless Remote Desktop
@@ -24,26 +26,25 @@ import net.sourceforge.guacamole.net.authentication.GuacamoleSessionProvider;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
public class BasicGuacamoleSessionProvider implements GuacamoleSessionProvider { public class BasicGuacamoleClientProvider implements GuacamoleClientProvider {
public GuacamoleSession createSession(HttpSession session) throws GuacamoleException { public GuacamoleClient createClient(HttpSession session) throws GuacamoleException {
// Retrieve authorized config data from session // Retrieve authorized config data from session
BasicLogin.AuthorizedConfiguration config = (BasicLogin.AuthorizedConfiguration) Configuration config = (Configuration) session.getAttribute("BASIC-LOGIN-AUTH");
session.getAttribute("BASIC-LOGIN-AUTH");
// If no data, not authorized // If no data, not authorized
if (config == null) if (config == null)
throw new GuacamoleException("Unauthorized"); throw new GuacamoleException("Unauthorized");
// Configure session from authorized config info GuacamoleClient client = new GuacamoleClient("localhost", 4822);
GuacamoleSession guacSession = new GuacamoleSession(session);
guacSession.setConnection(config.getProtocol(), config.getHostname(), config.getPort()); // TODO: Send "select" and "connect" messages in client connect function (based on config) ... to be implemented.
if (config.getPassword() != null) char[] initMessages = "select:vnc;connect:localhost,5901,potato;".toCharArray();
guacSession.setPassword(config.getPassword()); client.write(initMessages, 0, initMessages.length);
// Return authorized session // Return authorized session
return guacSession; return client;
} }

View File

@@ -28,99 +28,50 @@ 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.Configuration; import net.sourceforge.guacamole.net.Configuration;
import net.sourceforge.guacamole.net.GuacamoleProperties;
public class BasicLogin extends HttpServlet { public class BasicLogin extends HttpServlet {
private Config config; private AuthenticationProvider authProvider;
@Override @Override
public void init() throws ServletException { public void init() throws ServletException {
// Get auth provider instance
try { try {
config = new Config(); String authProviderClassName = GuacamoleProperties.getProperty("auth-provider");
Object obj = Class.forName(authProviderClassName).getConstructor().newInstance();
if (!(obj instanceof AuthenticationProvider))
throw new ServletException("Specified authentication provider class is not a AuthenticationProvider.");
authProvider = (AuthenticationProvider) obj;
} }
catch (GuacamoleException e) { catch (GuacamoleException e) {
throw new ServletException(e); throw new ServletException(e);
} }
} catch (ClassNotFoundException e) {
throw new ServletException("Authentication provider class not found", e);
private class Config extends Configuration {
private AuthenticationProvider authProvider;
public Config() throws GuacamoleException {
// Get auth provider instance
try {
String authProviderClassName = readParameter("auth-provider");
Object obj = Class.forName(authProviderClassName).getConstructor().newInstance();
if (!(obj instanceof AuthenticationProvider))
throw new GuacamoleException("Specified session provider class is not a GuacamoleSessionProvider");
authProvider = (AuthenticationProvider) obj;
}
catch (ClassNotFoundException e) {
throw new GuacamoleException("Session provider class not found", e);
}
catch (NoSuchMethodException e) {
throw new GuacamoleException("Default constructor for session provider not present", e);
}
catch (SecurityException e) {
throw new GuacamoleException("Creation of session provider disallowed; check your security settings", e);
}
catch (InstantiationException e) {
throw new GuacamoleException("Unable to instantiate session provider", e);
}
catch (IllegalAccessException e) {
throw new GuacamoleException("Unable to access default constructor of session provider", e);
}
catch (InvocationTargetException e) {
throw new GuacamoleException("Internal error in constructor of session provider", e.getTargetException());
}
} }
catch (NoSuchMethodException e) {
public AuthenticationProvider getAuthenticationProvider() { throw new ServletException("Default constructor for authentication provider not present", e);
return authProvider; }
catch (SecurityException e) {
throw new ServletException("Creation of authentication provider disallowed; check your security settings", e);
}
catch (InstantiationException e) {
throw new ServletException("Unable to instantiate authentication provider", e);
}
catch (IllegalAccessException e) {
throw new ServletException("Unable to access default constructor of authentication provider", e);
}
catch (InvocationTargetException e) {
throw new ServletException("Internal error in constructor of authentication provider", e.getTargetException());
} }
} }
public static interface AuthenticationProvider { public static interface AuthenticationProvider {
public AuthorizedConfiguration getAuthorizedConfiguration(String username, String password) throws GuacamoleException; public Configuration getAuthorizedConfiguration(String username, String password) throws GuacamoleException;
}
// Added to session when session validated
public static class AuthorizedConfiguration {
private String protocol;
private String hostname;
private int port;
private String password;
public AuthorizedConfiguration(String protocol, String hostname, int port, String password) {
this.protocol = protocol;
this.hostname = hostname;
this.port = port;
this.password = password;
}
public String getHostname() {
return hostname;
}
public String getPassword() {
return password;
}
public int getPort() {
return port;
}
public String getProtocol() {
return protocol;
}
} }
@Override @Override
@@ -133,14 +84,14 @@ public class BasicLogin extends HttpServlet {
// Validate username and password // Validate username and password
try { try {
AuthorizedConfiguration info = config.getAuthenticationProvider().getAuthorizedConfiguration(username, password); Configuration config = authProvider.getAuthorizedConfiguration(username, password);
if (info != null) { if (config != null) {
// Store authorized configuration // Store authorized configuration
HttpSession session = req.getSession(true); HttpSession session = req.getSession(true);
session.setAttribute( session.setAttribute(
"BASIC-LOGIN-AUTH", "BASIC-LOGIN-AUTH",
info config
); );
// Success // Success