KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > core > security > PropertiesLoginModuleManager


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.core.security;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.net.UnknownServiceException JavaDoc;
28 import java.security.MessageDigest JavaDoc;
29 import java.security.NoSuchAlgorithmException JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import org.apache.geronimo.common.GeronimoSecurityException;
37 import org.apache.geronimo.gbean.GBeanInfo;
38 import org.apache.geronimo.gbean.GBeanInfoBuilder;
39 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
40 import org.apache.geronimo.security.jaas.LoginModuleSettings;
41 import org.apache.geronimo.system.serverinfo.ServerInfo;
42 import org.apache.geronimo.util.encoders.HexTranslator;
43
44 /**
45  * @version $Rev: 480049 $ $Date: 2006-11-28 09:06:46 -0500 (Tue, 28 Nov 2006) $
46  */

47 public class PropertiesLoginModuleManager {
48
49     private ServerInfo serverInfo;
50
51     private LoginModuleSettings loginModule;
52
53     private Properties JavaDoc users = new Properties JavaDoc();
54
55     private Properties JavaDoc groups = new Properties JavaDoc();
56
57     private static final String JavaDoc usersKey = "usersURI";
58
59     private static final String JavaDoc groupsKey = "groupsURI";
60
61     private static final String JavaDoc digestKey = "digest";
62
63     public PropertiesLoginModuleManager(ServerInfo serverInfo, LoginModuleSettings loginModule) {
64         this.serverInfo = serverInfo;
65         this.loginModule = loginModule;
66     }
67
68     private void refreshUsers() {
69         users.clear();
70         InputStream JavaDoc in = null;
71         try {
72             in = serverInfo.resolve(getUsersURI()).toURL().openStream();
73             users.load(in);
74         } catch (Exception JavaDoc e) {
75             throw new GeronimoSecurityException(e);
76         } finally {
77             if (in != null) {
78                 try {
79                     in.close();
80                 } catch (IOException JavaDoc ignored) {
81                     // ignored
82
}
83             }
84         }
85     }
86
87     private void refreshGroups() throws GeronimoSecurityException {
88         groups.clear();
89         InputStream JavaDoc in = null;
90         try {
91             in = serverInfo.resolve(getGroupsURI()).toURL().openStream();
92             groups.load(in);
93         } catch (Exception JavaDoc e) {
94             throw new GeronimoSecurityException(e);
95         } finally {
96             if (in != null) {
97                 try {
98                     in.close();
99                 } catch (IOException JavaDoc ignored) {
100                     // ignored
101
}
102             }
103         }
104     }
105
106     public String JavaDoc[] getUsers() throws GeronimoSecurityException {
107         users.clear();
108         InputStream JavaDoc in = null;
109         try {
110             in = serverInfo.resolve(getUsersURI()).toURL().openStream();
111             users.load(in);
112         } catch (Exception JavaDoc e) {
113             throw new GeronimoSecurityException(e);
114         } finally {
115             if (in != null) {
116                 try {
117                     in.close();
118                 } catch (IOException JavaDoc ignored) {
119                     // ignored
120
}
121             }
122         }
123         return (String JavaDoc[]) users.keySet().toArray(new String JavaDoc[0]);
124     }
125
126     public String JavaDoc[] getGroups() throws GeronimoSecurityException {
127         groups.clear();
128         InputStream JavaDoc in = null;
129         try {
130             in = serverInfo.resolve(getGroupsURI()).toURL().openStream();
131             groups.load(in);
132         } catch (Exception JavaDoc e) {
133             throw new GeronimoSecurityException(e);
134         } finally {
135             if (in != null) {
136                 try {
137                     in.close();
138                 } catch (IOException JavaDoc ignored) {
139                     // ignored
140
}
141             }
142         }
143         return (String JavaDoc[]) groups.keySet().toArray(new String JavaDoc[0]);
144     }
145
146     public void addUserPrincipal(Hashtable JavaDoc properties)
147             throws GeronimoSecurityException {
148         if (users.getProperty((String JavaDoc) properties.get("UserName")) != null) {
149             throw new GeronimoSecurityException("User principal "
150                     + properties.get("UserName") + " already exists.");
151         }
152         try {
153             refreshUsers();
154             String JavaDoc digest = getDigest();
155             String JavaDoc user = (String JavaDoc) properties.get("UserName");
156             String JavaDoc password = (String JavaDoc) properties.get("Password");
157             if(digest != null && !digest.equals("")) {
158                 password = digestPassword(password, digest);
159             }
160             users.setProperty(user, password);
161             store(users, serverInfo.resolve(getUsersURI()).toURL());
162         } catch (Exception JavaDoc e) {
163             throw new GeronimoSecurityException("Cannot add user principal: "
164                     + e.getMessage());
165         }
166     }
167
168     public void removeUserPrincipal(String JavaDoc userPrincipal)
169             throws GeronimoSecurityException {
170         try {
171             refreshUsers();
172             users.remove(userPrincipal);
173             store(users, serverInfo.resolve(getUsersURI()).toURL());
174         } catch (Exception JavaDoc e) {
175             throw new GeronimoSecurityException("Cannot remove user principal "
176                     + userPrincipal + ": " + e.getMessage());
177         }
178     }
179
180     public void updateUserPrincipal(Hashtable JavaDoc properties)
181             throws GeronimoSecurityException {
182         //same as add principal overriding the property
183
try {
184             refreshUsers();
185             String JavaDoc digest = getDigest();
186             String JavaDoc user = (String JavaDoc) properties.get("UserName");
187             String JavaDoc password = (String JavaDoc) properties.get("Password");
188             if(digest != null && !digest.equals("")) {
189                 password = digestPassword(password, digest);
190             }
191             users.setProperty(user, password);
192             store(users, serverInfo.resolve(getUsersURI()).toURL());
193         } catch (Exception JavaDoc e) {
194             throw new GeronimoSecurityException("Cannot add user principal: "
195                     + e.getMessage());
196         }
197     }
198
199     public void addGroupPrincipal(Hashtable JavaDoc properties)
200             throws GeronimoSecurityException {
201         refreshGroups();
202         if (groups.getProperty((String JavaDoc) properties.get("GroupName")) != null) {
203             throw new GeronimoSecurityException("Group "
204                     + properties.get("GroupName") + " already exists.");
205         }
206         try {
207             groups.setProperty((String JavaDoc) properties.get("GroupName"),
208                     (String JavaDoc) properties.get("Members"));
209             store(groups, serverInfo.resolve(getGroupsURI()).toURL());
210         } catch (Exception JavaDoc e) {
211             throw new GeronimoSecurityException("Cannot add group principal: "
212                     + e.getMessage());
213         }
214     }
215
216     public void removeGroupPrincipal(String JavaDoc groupPrincipal)
217             throws GeronimoSecurityException {
218         refreshGroups();
219         try {
220             groups.remove(groupPrincipal);
221             store(groups, serverInfo.resolve(getGroupsURI()).toURL());
222         } catch (Exception JavaDoc e) {
223             throw new GeronimoSecurityException(
224                     "Cannot remove group principal: " + e.getMessage());
225         }
226     }
227
228     public void updateGroupPrincipal(Hashtable JavaDoc properties)
229             throws GeronimoSecurityException {
230         //same as add group principal
231
refreshGroups();
232         try {
233             groups.setProperty((String JavaDoc) properties.get("GroupName"),
234                     (String JavaDoc) properties.get("Members"));
235             store(groups, serverInfo.resolve(getGroupsURI()).toURL());
236         } catch (Exception JavaDoc e) {
237             throw new GeronimoSecurityException("Cannot add group principal: "
238                     + e.getMessage());
239         }
240     }
241
242     public void addToGroup(String JavaDoc userPrincipal, String JavaDoc groupPrincipal)
243             throws GeronimoSecurityException {
244         throw new GeronimoSecurityException(
245                 "Not implemented for properties file security realm...");
246     }
247
248     public void removeFromGroup(String JavaDoc userPrincipal, String JavaDoc groupPrincipal)
249             throws GeronimoSecurityException {
250         throw new GeronimoSecurityException(
251                 "Not implemented for properties file security realm...");
252     }
253
254     public String JavaDoc getPassword(String JavaDoc userPrincipal)
255             throws GeronimoSecurityException {
256         refreshUsers();
257         return users.getProperty(userPrincipal);
258     }
259
260     public Set JavaDoc getGroupMembers(String JavaDoc groupPrincipal)
261             throws GeronimoSecurityException {
262         Set JavaDoc memberSet = new HashSet JavaDoc();
263         groups.clear();
264         refreshGroups();
265         if (groups.getProperty(groupPrincipal) == null) {
266             return memberSet;
267         }
268         String JavaDoc[] members = groups.getProperty(groupPrincipal)
269                 .split(",");
270
271         memberSet.addAll(Arrays.asList(members));
272         return memberSet;
273     }
274
275     private String JavaDoc getUsersURI() {
276         return loginModule.getOptions().getProperty(usersKey);
277     }
278
279     private String JavaDoc getGroupsURI() {
280         return loginModule.getOptions().getProperty(groupsKey);
281     }
282
283     private String JavaDoc getDigest() {
284         return loginModule.getOptions().getProperty(digestKey);
285     }
286
287     private void store(Properties JavaDoc props, URL JavaDoc url) throws Exception JavaDoc {
288         OutputStream JavaDoc out = null;
289         try {
290             try {
291                 URLConnection JavaDoc con = url.openConnection();
292                 con.setDoOutput(true);
293                 out = con.getOutputStream();
294             } catch (Exception JavaDoc e) {
295                 if ("file".equalsIgnoreCase(url.getProtocol()) && e instanceof UnknownServiceException JavaDoc) {
296                     out = new FileOutputStream JavaDoc(new File JavaDoc(url.getFile()));
297                 } else {
298                     throw e;
299                 }
300             }
301             props.store(out, null);
302         } finally {
303             if (out != null) {
304                 try {
305                     out.close();
306                 } catch (IOException JavaDoc ignored) {
307                     // ignored
308
}
309             }
310         }
311     }
312
313     /**
314      * This method returns the message digest of a specified string.
315      * @param password The string that is to be digested
316      * @param algorithm Name of the Message Digest algorithm
317      * @return Hex encoding of the digest bytes
318      * @throws NoSuchAlgorithmException if the Message Digest algorithm is not available
319      */

320     private String JavaDoc digestPassword(String JavaDoc password, String JavaDoc algorithm) throws NoSuchAlgorithmException JavaDoc {
321         MessageDigest JavaDoc md = MessageDigest.getInstance(algorithm);
322         byte[] data = md.digest(password.getBytes());
323         // Convert bytes to hex digits
324
byte[] hexData = new byte[data.length * 2];
325         HexTranslator ht = new HexTranslator();
326         ht.encode(data, 0, data.length, hexData, 0);
327         return new String JavaDoc(hexData);
328     }
329
330     public static final GBeanInfo GBEAN_INFO;
331
332     static {
333         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("PropertiesLoginModuleManager", PropertiesLoginModuleManager.class);
334
335         infoFactory.addOperation("addUserPrincipal", new Class JavaDoc[]{Hashtable JavaDoc.class});
336         infoFactory.addOperation("removeUserPrincipal", new Class JavaDoc[]{String JavaDoc.class});
337         infoFactory.addOperation("updateUserPrincipal", new Class JavaDoc[]{Hashtable JavaDoc.class});
338         infoFactory.addOperation("getGroups");
339         infoFactory.addOperation("getUsers");
340
341         infoFactory.addOperation("updateUserPrincipal", new Class JavaDoc[]{Hashtable JavaDoc.class});
342
343         infoFactory.addOperation("getPassword", new Class JavaDoc[]{String JavaDoc.class});
344         infoFactory.addOperation("getGroupMembers", new Class JavaDoc[]{String JavaDoc.class});
345         infoFactory.addOperation("addGroupPrincipal", new Class JavaDoc[]{Hashtable JavaDoc.class});
346         infoFactory.addOperation("removeGroupPrincipal", new Class JavaDoc[]{String JavaDoc.class});
347         infoFactory.addOperation("updateGroupPrincipal", new Class JavaDoc[]{Hashtable JavaDoc.class});
348         infoFactory.addOperation("addToGroup", new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class});
349         infoFactory.addOperation("removeFromGroup", new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class});
350
351         infoFactory.addReference("ServerInfo", ServerInfo.class, NameFactory.GERONIMO_SERVICE);
352         infoFactory.addReference("LoginModule", LoginModuleSettings.class, NameFactory.LOGIN_MODULE);
353
354         infoFactory.setConstructor(new String JavaDoc[]{"ServerInfo", "LoginModule"});
355
356         GBEAN_INFO = infoFactory.getBeanInfo();
357     }
358
359     public static GBeanInfo getGBeanInfo() {
360         return GBEAN_INFO;
361     }
362
363 }
364
Popular Tags