Migrated to improved API.
authorMichael Jumper <zhangmaike@users.sourceforge.net>
Sun, 2 Jan 2011 10:37:09 +0000 (02:37 -0800)
committerMichael Jumper <zhangmaike@users.sourceforge.net>
Sun, 2 Jan 2011 10:37:09 +0000 (02:37 -0800)
src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicFileAuthenticationProvider.java
src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicGuacamoleClientProvider.java [new file with mode: 0644]
src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicGuacamoleSessionProvider.java [deleted file]
src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicLogin.java

index 83fcfd3..e0f24f3 100644 (file)
@@ -27,6 +27,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import net.sourceforge.guacamole.GuacamoleException;
+import net.sourceforge.guacamole.net.Configuration;
 import net.sourceforge.guacamole.net.GuacamoleProperties;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
@@ -80,7 +81,7 @@ public class BasicFileAuthenticationProvider implements BasicLogin.Authenticatio
     }
 
     @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
         File userMappingFile = getUserMappingFile();
@@ -95,13 +96,19 @@ public class BasicFileAuthenticationProvider implements BasicLogin.Authenticatio
         }
 
         AuthInfo info = mapping.get(username);
-        if (info != null && info.validate(username, password))
-            return new BasicLogin.AuthorizedConfiguration(
-                info.getProtocol(),
-                info.getHostname(),
-                info.getPort(),
-                info.getPassword()
-            );
+        if (info != null && info.validate(username, password)) {
+
+            Configuration config = new Configuration();
+
+            // TODO: Migrate user-mapping to general form
+            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;
 
diff --git a/src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicGuacamoleClientProvider.java b/src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicGuacamoleClientProvider.java
new file mode 100644 (file)
index 0000000..bbef84b
--- /dev/null
@@ -0,0 +1,51 @@
+
+package net.sourceforge.guacamole.net.authentication.basic;
+
+import javax.servlet.http.HttpSession;
+import net.sourceforge.guacamole.GuacamoleClient;
+import net.sourceforge.guacamole.GuacamoleException;
+import net.sourceforge.guacamole.net.Configuration;
+import net.sourceforge.guacamole.net.GuacamoleSession;
+import net.sourceforge.guacamole.net.authentication.GuacamoleClientProvider;
+
+/*
+ *  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/>.
+ */
+
+public class BasicGuacamoleClientProvider implements GuacamoleClientProvider {
+
+    public GuacamoleClient createClient(HttpSession session) throws GuacamoleException {
+
+        // Retrieve authorized config data from session
+        Configuration config = (Configuration) session.getAttribute("BASIC-LOGIN-AUTH");
+
+        // If no data, not authorized
+        if (config == null)
+            throw new GuacamoleException("Unauthorized");
+
+        GuacamoleClient client = new GuacamoleClient("localhost", 4822);
+
+        // TODO: Send "select" and "connect" messages in client connect function (based on config) ... to be implemented.
+        char[] initMessages = "select:vnc;connect:localhost,5901,potato;".toCharArray();
+        client.write(initMessages, 0, initMessages.length);
+
+        // Return authorized session
+        return client;
+
+    }
+
+}
diff --git a/src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicGuacamoleSessionProvider.java b/src/main/java/net/sourceforge/guacamole/net/authentication/basic/BasicGuacamoleSessionProvider.java
deleted file mode 100644 (file)
index 959bf0e..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-
-package net.sourceforge.guacamole.net.authentication.basic;
-
-import javax.servlet.http.HttpSession;
-import net.sourceforge.guacamole.GuacamoleException;
-import net.sourceforge.guacamole.net.GuacamoleSession;
-import net.sourceforge.guacamole.net.authentication.GuacamoleSessionProvider;
-
-/*
- *  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/>.
- */
-
-public class BasicGuacamoleSessionProvider implements GuacamoleSessionProvider {
-
-    public GuacamoleSession createSession(HttpSession session) throws GuacamoleException {
-
-        // Retrieve authorized config data from session
-        BasicLogin.AuthorizedConfiguration config = (BasicLogin.AuthorizedConfiguration)
-                session.getAttribute("BASIC-LOGIN-AUTH");
-
-        // If no data, not authorized
-        if (config == null)
-            throw new GuacamoleException("Unauthorized");
-
-        // Configure session from authorized config info
-        GuacamoleSession guacSession = new GuacamoleSession(session);
-        guacSession.setConnection(config.getProtocol(), config.getHostname(), config.getPort());
-        if (config.getPassword() != null)
-            guacSession.setPassword(config.getPassword());
-
-        // Return authorized session
-        return guacSession;
-
-    }
-
-}
index 43e8721..8347202 100644 (file)
@@ -28,99 +28,50 @@ import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import net.sourceforge.guacamole.GuacamoleException;
 import net.sourceforge.guacamole.net.Configuration;
+import net.sourceforge.guacamole.net.GuacamoleProperties;
 
 public class BasicLogin extends HttpServlet {
 
-    private Config config;
+    private AuthenticationProvider authProvider;
 
     @Override
     public void init() throws ServletException {
+
+        // Get auth provider instance
         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) {
             throw new ServletException(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 (ClassNotFoundException e) {
+            throw new ServletException("Authentication provider class not found", e);
         }
-
-        public AuthenticationProvider getAuthenticationProvider() {
-            return authProvider;
+        catch (NoSuchMethodException e) {
+            throw new ServletException("Default constructor for authentication provider not present", e);
         }
-
-    }
-
-    public static interface AuthenticationProvider {
-        public AuthorizedConfiguration 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;
+        catch (SecurityException e) {
+            throw new ServletException("Creation of authentication provider disallowed; check your security settings", e);
         }
-
-        public String getHostname() {
-            return hostname;
+        catch (InstantiationException e) {
+            throw new ServletException("Unable to instantiate authentication provider", e);
         }
-
-        public String getPassword() {
-            return password;
+        catch (IllegalAccessException e) {
+            throw new ServletException("Unable to access default constructor of authentication provider", e);
         }
-
-        public int getPort() {
-            return port;
+        catch (InvocationTargetException e) {
+            throw new ServletException("Internal error in constructor of authentication provider", e.getTargetException());
         }
 
-        public String getProtocol() {
-            return protocol;
-        }
+    }
 
+    public static interface AuthenticationProvider {
+        public Configuration getAuthorizedConfiguration(String username, String password) throws GuacamoleException;
     }
 
     @Override
@@ -133,14 +84,14 @@ public class BasicLogin extends HttpServlet {
         // Validate username and password
         try {
 
-            AuthorizedConfiguration info = config.getAuthenticationProvider().getAuthorizedConfiguration(username, password);
-            if (info != null) {
+            Configuration config = authProvider.getAuthorizedConfiguration(username, password);
+            if (config != null) {
 
                 // Store authorized configuration
                 HttpSession session = req.getSession(true);
                 session.setAttribute(
                     "BASIC-LOGIN-AUTH",
-                    info
+                    config
                 );
 
                 // Success