KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > realm > ArgumentsRealm


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone.realm;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Arrays JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import winstone.AuthenticationPrincipal;
19 import winstone.AuthenticationRealm;
20 import winstone.Logger;
21 import winstone.WinstoneResourceBundle;
22
23 /**
24  * Base class for authentication realms. Subclasses provide the source of
25  * authentication roles, usernames, passwords, etc, and when asked for
26  * validation respond with a role if valid, or null otherwise.
27  *
28  * @author mailto: <a HREF="rick_knowles@hotmail.com">Rick Knowles</a>
29  * @version $Id: ArgumentsRealm.java,v 1.3 2006/02/28 07:32:50 rickknowles Exp $
30  */

31 public class ArgumentsRealm implements AuthenticationRealm {
32     private static final WinstoneResourceBundle REALM_RESOURCES = new WinstoneResourceBundle("winstone.realm.LocalStrings");
33     
34     static final String JavaDoc PASSWORD_PREFIX = "argumentsRealm.passwd.";
35     static final String JavaDoc ROLES_PREFIX = "argumentsRealm.roles.";
36     private Map JavaDoc passwords;
37     private Map JavaDoc roles;
38
39     /**
40      * Constructor - this sets up an authentication realm, using the arguments
41      * supplied on the command line as a source of userNames/passwords/roles.
42      */

43     public ArgumentsRealm(Set JavaDoc rolesAllowed, Map JavaDoc args) {
44         this.passwords = new Hashtable JavaDoc();
45         this.roles = new Hashtable JavaDoc();
46
47         for (Iterator JavaDoc i = args.keySet().iterator(); i.hasNext();) {
48             String JavaDoc key = (String JavaDoc) i.next();
49             if (key.startsWith(PASSWORD_PREFIX)) {
50                 String JavaDoc userName = key.substring(PASSWORD_PREFIX.length());
51                 String JavaDoc password = (String JavaDoc) args.get(key);
52
53                 String JavaDoc roleList = (String JavaDoc) args.get(ROLES_PREFIX + userName);
54                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(roleList, ",");
55                 List JavaDoc rl = new ArrayList JavaDoc();
56                 for (; st.hasMoreTokens();) {
57                     String JavaDoc currentRole = st.nextToken();
58                     if (rolesAllowed.contains(currentRole))
59                         rl.add(currentRole);
60                 }
61                 Object JavaDoc roleArray[] = rl.toArray();
62                 Arrays.sort(roleArray);
63                 this.passwords.put(userName, password);
64                 this.roles.put(userName, Arrays.asList(roleArray));
65             }
66         }
67
68         Logger.log(Logger.DEBUG, REALM_RESOURCES, "ArgumentsRealm.Initialised",
69                 "" + this.passwords.size());
70     }
71
72     /**
73      * Authenticate the user - do we know them ? Return a principal once we know
74      * them
75      */

76     public AuthenticationPrincipal authenticateByUsernamePassword(
77             String JavaDoc userName, String JavaDoc password) {
78         if ((userName == null) || (password == null))
79             return null;
80
81         String JavaDoc realPassword = (String JavaDoc) this.passwords.get(userName);
82         if (realPassword == null)
83             return null;
84         else if (!realPassword.equals(password))
85             return null;
86         else
87             return new AuthenticationPrincipal(userName, password,
88                     (List JavaDoc) this.roles.get(userName));
89     }
90
91     /**
92      * Retrieve an authenticated user
93      */

94     public AuthenticationPrincipal retrieveUser(String JavaDoc userName) {
95         if (userName == null)
96             return null;
97         else
98             return new AuthenticationPrincipal(userName,
99                     (String JavaDoc) this.passwords.get(userName), (List JavaDoc) this.roles
100                             .get(userName));
101     }
102 }
Popular Tags