comparison flys-client/src/main/java/de/intevation/flys/client/server/auth/plain/Authenticator.java @ 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
children 98514ab2c9ba
comparison
equal deleted inserted replaced
2958:973d4a968f6a 2959:5ba0a6efdf3b
1 package de.intevation.flys.client.server.auth.plain;
2
3 import de.intevation.flys.client.server.auth.AuthenticationException;
4 import de.intevation.flys.client.server.auth.DefaultUser;
5 import de.intevation.flys.client.server.auth.User;
6
7 import java.io.BufferedReader;
8 import java.io.File;
9 import java.io.FileReader;
10 import java.io.IOException;
11
12 public class Authenticator
13 implements de.intevation.flys.client.server.auth.Authenticator
14 {
15 public static class Authentication
16 implements de.intevation.flys.client.server.auth.Authentication
17 {
18 protected String user;
19 protected String password;
20
21 public Authentication(String user, String password) {
22 this.user = user;
23 this.password = password;
24 }
25
26 @Override
27 public boolean isSuccess() {
28 return user != null;
29 }
30
31 @Override
32 public User getUser() {
33 return isSuccess()
34 ? new DefaultUser(user, password, false)
35 : null;
36 }
37 } // class Authentication
38
39 public Authenticator() {
40 }
41
42 private static File credentialsFile() {
43 String env = System.getenv("FLYS_USER_FILE");
44 if (env == null) {
45 env = System.getProperty(
46 "flys.user.file",
47 System.getProperty("user.home", ".")
48 + System.getProperty("file.separator")
49 + "flys_user_file");
50 }
51 return new File(env);
52
53 }
54
55 @Override
56 public de.intevation.flys.client.server.auth.Authentication auth(
57 String username,
58 String password,
59 String encoding
60 )
61 throws AuthenticationException, IOException
62 {
63 File file = credentialsFile();
64 if (!file.canRead() || !file.isFile()) {
65 return new Authentication(null, null);
66 }
67
68 BufferedReader reader =
69 new BufferedReader(
70 new FileReader(file));
71 try {
72 String line;
73 while ((line = reader.readLine()) != null) {
74 if ((line = line.trim()).length() == 0
75 || line.startsWith("#")) {
76 continue;
77 }
78 String [] parts = line.split("\\s+");
79 // TODO: role?
80 if (parts.length < 2) {
81 continue;
82 }
83 if (parts[0].equals(username)) {
84 if (parts[1].equals(password)) {
85 return new Authentication(username, password);
86 }
87 // Stop: user found, wrong password
88 break;
89 }
90 }
91 }
92 finally {
93 reader.close();
94 }
95 return new Authentication(null, null);
96 }
97 }
98 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org