Fix wording of error message.
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / BasicFileAuthenticationProvider.java
index 91a6b1b..c66b416 100644 (file)
@@ -31,7 +31,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import net.sourceforge.guacamole.GuacamoleException;
-import net.sourceforge.guacamole.net.auth.UsernamePassword;
+import net.sourceforge.guacamole.net.auth.Credentials;
 import net.sourceforge.guacamole.properties.FileGuacamoleProperty;
 import net.sourceforge.guacamole.properties.GuacamoleProperties;
 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
@@ -46,12 +46,14 @@ import org.xml.sax.helpers.XMLReaderFactory;
 
 /**
  * Authenticates users against a static list of username/password pairs.
- * Each username/password may be associated with exactly one configuration.
+ * Each username/password may be associated with multiple configurations.
  * This list is stored in an XML file which is reread if modified.
  * 
- * @author Michael Jumper
+ * This is modified version of BasicFileAuthenticationProvider written by Michael Jumper.
+ * 
+ * @author Michal Kotas
  */
-public class BasicFileAuthenticationProvider implements AuthenticationProvider<UsernamePassword> {
+public class BasicFileAuthenticationProvider implements AuthenticationProvider {
 
     private Logger logger = LoggerFactory.getLogger(BasicFileAuthenticationProvider.class);
     
@@ -113,7 +115,7 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
     }
 
     @Override
-    public Map<String, GuacamoleConfiguration> getAuthorizedConfigurations(UsernamePassword credentials) throws GuacamoleException {
+    public Map<String, GuacamoleConfiguration> getAuthorizedConfigurations(Credentials credentials) throws GuacamoleException {
 
         // Check mapping file mod time
         File userMappingFile = getUserMappingFile();
@@ -133,12 +135,15 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
         if (mapping == null)
             throw new GuacamoleException("User mapping could not be read.");
         
-        Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
-        
         // Validate and return info for given user and pass
         AuthInfo info = mapping.get(credentials.getUsername());
         if (info != null && info.validate(credentials.getUsername(), credentials.getPassword())) {
-            configs.put("DEFAULT", info.getConfiguration());
+            
+            //Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
+            //configs.put("DEFAULT", info.getConfiguration());
+            //return configs;
+            
+            Map<String, GuacamoleConfiguration> configs = info.getConfigurations();          
             return configs;
         }
 
@@ -158,14 +163,14 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
         private String auth_password;
         private Encoding auth_encoding;
 
-        private GuacamoleConfiguration config;
+        private Map<String, GuacamoleConfiguration> configs;
 
         public AuthInfo(String auth_username, String auth_password, Encoding auth_encoding) {
             this.auth_username = auth_username;
             this.auth_password = auth_password;
             this.auth_encoding = auth_encoding;
 
-            config = new GuacamoleConfiguration();
+            configs = new HashMap<String, GuacamoleConfiguration>();
         }
 
         private static final char HEX_CHARS[] = {
@@ -221,13 +226,19 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
 
         }
 
-        public GuacamoleConfiguration getConfiguration() {
-            return config;
+        public GuacamoleConfiguration getConfiguration(String name) {
+            //return configs;
+            return configs.get(name);
+        }
+        public Map<String, GuacamoleConfiguration> getConfigurations() {
+            return configs;
+        }
+        public void addConfiguration(String name) {
+            configs.put(name, new GuacamoleConfiguration());
         }
 
     }
 
-
     private static class BasicUserMappingContentHandler extends DefaultHandler {
 
         private Map<String, AuthInfo> authMapping = new HashMap<String, AuthInfo>();
@@ -239,6 +250,7 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
         private enum State {
             ROOT,
             USER_MAPPING,
+            CONNECTION,
             AUTH_INFO,
             PROTOCOL,
             PARAMETER,
@@ -248,56 +260,66 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
         private State state = State.ROOT;
         private AuthInfo current = null;
         private String currentParameter = null;
+        private String currentConnection = null;
 
         @Override
         public void endElement(String uri, String localName, String qName) throws SAXException {
 
             switch (state)  {
 
-                case USER_MAPPING:
+            case USER_MAPPING:
 
-                    if (localName.equals("user-mapping")) {
-                        state = State.END;
-                        return;
-                    }
+                if (localName.equals("user-mapping")) {
+                    state = State.END;
+                    return;
+                }
 
-                    break;
+                break;
 
-                case AUTH_INFO:
+            case AUTH_INFO:
 
-                    if (localName.equals("authorize")) {
+                if (localName.equals("authorize")) {
 
-                        // Finalize mapping for this user
-                        authMapping.put(
-                            current.auth_username,
-                            current
-                        );
+                    // Finalize mapping for this user
+                    authMapping.put(
+                        current.auth_username,
+                        current
+                    );
 
-                        state = State.USER_MAPPING;
-                        return;
-                    }
+                    state = State.USER_MAPPING;
+                    return;
+                }
 
-                    break;
+                break;
+                
+            case CONNECTION:
 
-                case PROTOCOL:
+                if (localName.equals("connection")) {
+                    state = State.AUTH_INFO;
+                    return;
+                }
 
-                    if (localName.equals("protocol")) {
-                        state = State.AUTH_INFO;
-                        return;
-                    }
+                break;                
 
-                    break;
+            case PROTOCOL:
 
-                case PARAMETER:
+                if (localName.equals("protocol")) {
+                    state = State.CONNECTION;
+                    return;
+                }
 
-                    if (localName.equals("param")) {
-                        state = State.AUTH_INFO;
-                        return;
-                    }
+                break;
 
-                    break;
+            case PARAMETER:
 
-            }
+                if (localName.equals("param")) {
+                    state = State.CONNECTION;
+                    return;
+                }
+
+                break;
+
+        }
 
             throw new SAXException("Tag not yet complete: " + localName);
 
@@ -318,7 +340,6 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
 
                     break;
 
-                // Only <authorize> tags allowed in main document
                 case USER_MAPPING:
 
                     if (localName.equals("authorize")) {
@@ -350,6 +371,23 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
 
                 case AUTH_INFO:
 
+                    if (localName.equals("connection")) {
+
+                        currentConnection = attributes.getValue("name");
+                        if (currentConnection == null)
+                            throw new SAXException("Attribute \"name\" required for connection tag.");
+                        
+                        current.addConfiguration(currentConnection);
+                        
+                        // Next state
+                        state = State.CONNECTION;
+                        return;
+                    }
+
+                    break;
+                    
+                case CONNECTION:
+
                     if (localName.equals("protocol")) {
                         // Next state
                         state = State.PROTOCOL;
@@ -367,7 +405,7 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
                         return;
                     }
 
-                    break;
+                    break;                   
 
             }
 
@@ -379,15 +417,16 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider<U
         public void characters(char[] ch, int start, int length) throws SAXException {
 
             String str = new String(ch, start, length);
+   
             switch (state) {
 
                 case PROTOCOL:
-                    current.getConfiguration()
-                            .setProtocol(str);
+                    current.getConfiguration(currentConnection)
+                        .setProtocol(str);
                     return;
 
                 case PARAMETER:
-                    current.getConfiguration()
+                    current.getConfiguration(currentConnection)
                             .setParameter(currentParameter, str);
                     return;