Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/server/auth/plain/Authenticator.java @ 5834:f507086aa94b
Repaired internal references.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:31:32 +0200 |
parents | flys-client/src/main/java/de/intevation/flys/client/server/auth/plain/Authenticator.java@2e12518ff5b4 |
children | 821a02bbfb4e |
comparison
equal
deleted
inserted
replaced
5833:a2bdc0f524e8 | 5834:f507086aa94b |
---|---|
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 import java.util.ArrayList; | |
13 import java.util.List; | |
14 | |
15 import org.apache.log4j.Logger; | |
16 | |
17 import de.intevation.flys.client.server.features.Features; | |
18 | |
19 /** | |
20 * Authenticator that uses a local file as user backend. | |
21 */ | |
22 public class Authenticator | |
23 implements de.intevation.flys.client.server.auth.Authenticator | |
24 { | |
25 private static final Logger log = | |
26 Logger.getLogger(Authenticator.class); | |
27 | |
28 public static class Authentication | |
29 implements de.intevation.flys.client.server.auth.Authentication | |
30 { | |
31 protected String user; | |
32 protected String password; | |
33 protected List<String> roles; | |
34 protected Features features; | |
35 | |
36 public Authentication( | |
37 String user, | |
38 String password, | |
39 List<String> roles, | |
40 Features features | |
41 ) { | |
42 this.user = user; | |
43 this.password = password; | |
44 this.roles = roles; | |
45 this.features = features; | |
46 } | |
47 | |
48 @Override | |
49 public boolean isSuccess() { | |
50 return user != null; | |
51 } | |
52 | |
53 @Override | |
54 public User getUser() { | |
55 return isSuccess() | |
56 ? new DefaultUser(user, password, false, roles, this.features.getFeatures(roles)) | |
57 : null; | |
58 } | |
59 } // class Authentication | |
60 | |
61 public Authenticator() { | |
62 } | |
63 | |
64 private static File credentialsFile() { | |
65 String env = System.getenv("FLYS_USER_FILE"); | |
66 if (env == null) { | |
67 env = System.getProperty( | |
68 "flys.user.file", | |
69 System.getProperty("user.home", ".") | |
70 + System.getProperty("file.separator") | |
71 + "flys_user_file"); | |
72 } | |
73 log.debug("Using credentials file " + env); | |
74 return new File(env); | |
75 | |
76 } | |
77 | |
78 @Override | |
79 public de.intevation.flys.client.server.auth.Authentication auth( | |
80 String username, | |
81 String password, | |
82 String encoding, | |
83 Features features | |
84 ) | |
85 throws AuthenticationException, IOException | |
86 { | |
87 File file = credentialsFile(); | |
88 if (!file.canRead() || !file.isFile()) { | |
89 log.error("cannot find user file '" + file + "'"); | |
90 return new Authentication(null, null, new ArrayList<String>(0), features); | |
91 } | |
92 | |
93 BufferedReader reader = | |
94 new BufferedReader( | |
95 new FileReader(file)); | |
96 try { | |
97 String line; | |
98 while ((line = reader.readLine()) != null) { | |
99 if ((line = line.trim()).length() == 0 | |
100 || line.startsWith("#")) { | |
101 continue; | |
102 } | |
103 | |
104 String[] parts = line.split("\\s+"); | |
105 if (parts.length < 2) { | |
106 continue; | |
107 } | |
108 | |
109 if (parts[0].equals(username)) { | |
110 log.debug("user '" + username + "' found."); | |
111 if (parts[1].equals(password)) { | |
112 List<String> roles = | |
113 new ArrayList<String>(parts.length - 2); | |
114 | |
115 for (int i = 2; i < parts.length; i++) { | |
116 roles.add(parts[i]); | |
117 } | |
118 | |
119 log.debug("success"); | |
120 return new Authentication(username, password, roles, features); | |
121 } | |
122 // Stop: user found, wrong password | |
123 break; | |
124 } | |
125 } | |
126 } | |
127 finally { | |
128 reader.close(); | |
129 } | |
130 log.debug("failed"); | |
131 return null; | |
132 } | |
133 } | |
134 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |