001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone.realm;
008:
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.Hashtable;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Map;
015: import java.util.Set;
016: import java.util.StringTokenizer;
017:
018: import winstone.AuthenticationPrincipal;
019: import winstone.AuthenticationRealm;
020: import winstone.Logger;
021: import winstone.WebAppConfiguration;
022: import winstone.WinstoneResourceBundle;
023:
024: /**
025: * Base class for authentication realms. Subclasses provide the source of
026: * authentication roles, usernames, passwords, etc, and when asked for
027: * validation respond with a role if valid, or null otherwise.
028: *
029: * @author mailto: <a href="rick_knowles@hotmail.com">Rick Knowles</a>
030: * @version $Id: ArgumentsRealm.java,v 1.4 2007/06/01 15:55:41 rickknowles Exp $
031: */
032: public class ArgumentsRealm implements AuthenticationRealm {
033: private static final WinstoneResourceBundle REALM_RESOURCES = new WinstoneResourceBundle(
034: "winstone.realm.LocalStrings");
035:
036: static final String PASSWORD_PREFIX = "argumentsRealm.passwd.";
037: static final String ROLES_PREFIX = "argumentsRealm.roles.";
038: private Map passwords;
039: private Map roles;
040:
041: /**
042: * Constructor - this sets up an authentication realm, using the arguments
043: * supplied on the command line as a source of userNames/passwords/roles.
044: */
045: public ArgumentsRealm(Set rolesAllowed, Map args) {
046: this .passwords = new Hashtable();
047: this .roles = new Hashtable();
048:
049: for (Iterator i = args.keySet().iterator(); i.hasNext();) {
050: String key = (String) i.next();
051: if (key.startsWith(PASSWORD_PREFIX)) {
052: String userName = key.substring(PASSWORD_PREFIX
053: .length());
054: String password = (String) args.get(key);
055:
056: String roleList = WebAppConfiguration.stringArg(args,
057: ROLES_PREFIX + userName, "");
058: if (roleList.equals("")) {
059: Logger.log(Logger.WARNING, REALM_RESOURCES,
060: "ArgumentsRealm.UndeclaredRoles", userName);
061: } else {
062: StringTokenizer st = new StringTokenizer(roleList,
063: ",");
064: List rl = new ArrayList();
065: for (; st.hasMoreTokens();) {
066: String currentRole = st.nextToken();
067: if (rolesAllowed.contains(currentRole))
068: rl.add(currentRole);
069: }
070: Object roleArray[] = rl.toArray();
071: Arrays.sort(roleArray);
072: this .roles.put(userName, Arrays.asList(roleArray));
073: }
074: this .passwords.put(userName, password);
075: }
076: }
077:
078: Logger.log(Logger.DEBUG, REALM_RESOURCES,
079: "ArgumentsRealm.Initialised", ""
080: + this .passwords.size());
081: }
082:
083: /**
084: * Authenticate the user - do we know them ? Return a principal once we know
085: * them
086: */
087: public AuthenticationPrincipal authenticateByUsernamePassword(
088: String userName, String password) {
089: if ((userName == null) || (password == null))
090: return null;
091:
092: String realPassword = (String) this .passwords.get(userName);
093: if (realPassword == null)
094: return null;
095: else if (!realPassword.equals(password))
096: return null;
097: else
098: return new AuthenticationPrincipal(userName, password,
099: (List) this .roles.get(userName));
100: }
101:
102: /**
103: * Retrieve an authenticated user
104: */
105: public AuthenticationPrincipal retrieveUser(String userName) {
106: if (userName == null)
107: return null;
108: else
109: return new AuthenticationPrincipal(userName,
110: (String) this .passwords.get(userName),
111: (List) this.roles.get(userName));
112: }
113: }
|