changeset 2959:5ba0a6efdf3b

Auth: added simple file based authentication. flys-client/trunk@4939 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 11 Jul 2012 14:53:33 +0000
parents 973d4a968f6a
children 4c944c0193e4
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/auth/AuthenticationFactory.java flys-client/src/main/java/de/intevation/flys/client/server/auth/Authenticator.java flys-client/src/main/java/de/intevation/flys/client/server/auth/DefaultUser.java flys-client/src/main/java/de/intevation/flys/client/server/auth/User.java flys-client/src/main/java/de/intevation/flys/client/server/auth/plain/Authenticator.java flys-client/src/main/java/de/intevation/flys/client/server/auth/was/Response.java flys-client/src/main/java/de/intevation/flys/client/server/auth/was/User.java
diffstat 8 files changed, 180 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Wed Jul 11 13:47:19 2012 +0000
+++ b/flys-client/ChangeLog	Wed Jul 11 14:53:33 2012 +0000
@@ -1,3 +1,27 @@
+2012-07-11	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/server/auth/plain/Authenticator.java:
+	  Authenticates against simple text file. Specified by env variable FLYS_USER_FILE
+	  or system property flys.user.file . 
+
+	* src/main/java/de/intevation/flys/client/server/auth/DefaultUser.java:
+	  Simple user implementation.
+
+	* src/main/java/de/intevation/flys/client/server/auth/AuthenticationFactory.java:
+	  Check for 'plain' as authentication method.
+
+	* src/main/java/de/intevation/flys/client/server/auth/User.java:
+	  Expires return boolean instead of boolean, now.
+
+	* src/main/java/de/intevation/flys/client/server/auth/was/User.java:
+	  Adjusted
+
+	* src/main/java/de/intevation/flys/client/server/auth/was/Response.java:
+	  Added override annotation.
+	  
+	* src/main/java/de/intevation/flys/client/server/auth/Authenticator.java:
+	  Removed same package imports.
+	
 2012-07-11	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/FLYSConstants.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/server/auth/AuthenticationFactory.java	Wed Jul 11 13:47:19 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/AuthenticationFactory.java	Wed Jul 11 14:53:33 2012 +0000
@@ -6,9 +6,15 @@
 
     public static Authenticator getInstance(String name)
         throws IllegalArgumentException {
-        if (name == null || name.equalsIgnoreCase("was") ||
-                name.equalsIgnoreCase("ggina")) {
-            return new de.intevation.flys.client.server.auth.was.Authenticator();
+        if (name == null
+        || name.equalsIgnoreCase("was")
+        || name.equalsIgnoreCase("ggina")) {
+            return
+                new de.intevation.flys.client.server.auth.was.Authenticator();
+        }
+        if (name.equalsIgnoreCase("plain")) {
+            return
+                new de.intevation.flys.client.server.auth.plain.Authenticator();
         }
         throw new IllegalArgumentException("Unkown Authentication " + name);
     }
--- a/flys-client/src/main/java/de/intevation/flys/client/server/auth/Authenticator.java	Wed Jul 11 13:47:19 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/Authenticator.java	Wed Jul 11 14:53:33 2012 +0000
@@ -2,13 +2,9 @@
 
 import java.io.IOException;
 
-import de.intevation.flys.client.server.auth.Authentication;
-import de.intevation.flys.client.server.auth.AuthenticationException;
-
 public interface Authenticator {
 
     public Authentication auth(String username, String password, String encoding)
         throws AuthenticationException, IOException;
 
 }
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/DefaultUser.java	Wed Jul 11 14:53:33 2012 +0000
@@ -0,0 +1,46 @@
+package de.intevation.flys.client.server.auth;
+
+public class DefaultUser
+implements   User
+{
+    protected String  name;
+    protected String  password;
+    protected boolean expired;
+
+    public DefaultUser() {
+    }
+
+    public DefaultUser(String name, String password, boolean expired) {
+        this.name     = name;
+        this.password = password;
+        this.expired  = expired;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    @Override
+    public boolean hasExpired() {
+        return expired;
+    }
+
+    public void setExpired(boolean expired) {
+        this.expired = expired;
+    }
+}
+// vim:set ts=4 sw=4 si et fenc=utf8 tw=80:
--- a/flys-client/src/main/java/de/intevation/flys/client/server/auth/User.java	Wed Jul 11 13:47:19 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/User.java	Wed Jul 11 14:53:33 2012 +0000
@@ -6,6 +6,6 @@
 
     public String getPassword();
 
-    public Boolean hasExpired();
+    public boolean hasExpired();
 }
 // vim:set ts=4 sw=4 si et fenc=utf8 tw=80:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/plain/Authenticator.java	Wed Jul 11 14:53:33 2012 +0000
@@ -0,0 +1,98 @@
+package de.intevation.flys.client.server.auth.plain;
+
+import de.intevation.flys.client.server.auth.AuthenticationException;
+import de.intevation.flys.client.server.auth.DefaultUser;
+import de.intevation.flys.client.server.auth.User;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+public class Authenticator
+implements   de.intevation.flys.client.server.auth.Authenticator
+{
+    public static class Authentication
+    implements          de.intevation.flys.client.server.auth.Authentication
+    {
+        protected String user;
+        protected String password;
+
+        public Authentication(String user, String password) {
+            this.user     = user;
+            this.password = password;
+        }
+
+        @Override
+        public boolean isSuccess() {
+            return user != null;
+        }
+
+        @Override
+        public User getUser() {
+            return isSuccess()
+                ? new DefaultUser(user, password, false)
+                : null;
+        }
+    } // class Authentication
+
+    public Authenticator() {
+    }
+
+    private static File credentialsFile() {
+        String env = System.getenv("FLYS_USER_FILE");
+        if (env == null) {
+            env = System.getProperty(
+                "flys.user.file",
+                System.getProperty("user.home", ".")
+                + System.getProperty("file.separator")
+                + "flys_user_file");
+        }
+        return new File(env);
+
+    }
+
+    @Override
+    public de.intevation.flys.client.server.auth.Authentication auth(
+        String username,
+        String password,
+        String encoding
+    )
+    throws AuthenticationException, IOException
+    {
+        File file = credentialsFile();
+        if (!file.canRead() || !file.isFile()) {
+            return new Authentication(null, null);
+        }
+
+        BufferedReader reader =
+            new BufferedReader(
+            new FileReader(file));
+        try {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                if ((line = line.trim()).length() == 0
+                || line.startsWith("#")) {
+                    continue;
+                }
+                String [] parts = line.split("\\s+");
+                // TODO: role?
+                if (parts.length < 2) {
+                    continue;
+                }
+                if (parts[0].equals(username)) {
+                    if (parts[1].equals(password)) {
+                        return new Authentication(username, password);
+                    }
+                    // Stop: user found, wrong password
+                    break;
+                }
+            }
+        }
+        finally {
+            reader.close();
+        }
+        return new Authentication(null, null);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/server/auth/was/Response.java	Wed Jul 11 13:47:19 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/was/Response.java	Wed Jul 11 14:53:33 2012 +0000
@@ -98,6 +98,7 @@
         return this.assertion;
     }
 
+    @Override
     public User getUser() {
         return new User(this.username, this.password);
     }
--- a/flys-client/src/main/java/de/intevation/flys/client/server/auth/was/User.java	Wed Jul 11 13:47:19 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/was/User.java	Wed Jul 11 14:53:33 2012 +0000
@@ -15,7 +15,7 @@
         return this.password;
     }
 
-    public Boolean hasExpired() {
+    public boolean hasExpired() {
         //TODO
         return false;
     }

http://dive4elements.wald.intevation.org