1 7 package winstone.realm; 8 9 import java.util.ArrayList ; 10 import java.util.Arrays ; 11 import java.util.Hashtable ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Map ; 15 import java.util.Set ; 16 import java.util.StringTokenizer ; 17 18 import winstone.AuthenticationPrincipal; 19 import winstone.AuthenticationRealm; 20 import winstone.Logger; 21 import winstone.WinstoneResourceBundle; 22 23 31 public class ArgumentsRealm implements AuthenticationRealm { 32 private static final WinstoneResourceBundle REALM_RESOURCES = new WinstoneResourceBundle("winstone.realm.LocalStrings"); 33 34 static final String PASSWORD_PREFIX = "argumentsRealm.passwd."; 35 static final String ROLES_PREFIX = "argumentsRealm.roles."; 36 private Map passwords; 37 private Map roles; 38 39 43 public ArgumentsRealm(Set rolesAllowed, Map args) { 44 this.passwords = new Hashtable (); 45 this.roles = new Hashtable (); 46 47 for (Iterator i = args.keySet().iterator(); i.hasNext();) { 48 String key = (String ) i.next(); 49 if (key.startsWith(PASSWORD_PREFIX)) { 50 String userName = key.substring(PASSWORD_PREFIX.length()); 51 String password = (String ) args.get(key); 52 53 String roleList = (String ) args.get(ROLES_PREFIX + userName); 54 StringTokenizer st = new StringTokenizer (roleList, ","); 55 List rl = new ArrayList (); 56 for (; st.hasMoreTokens();) { 57 String currentRole = st.nextToken(); 58 if (rolesAllowed.contains(currentRole)) 59 rl.add(currentRole); 60 } 61 Object 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 76 public AuthenticationPrincipal authenticateByUsernamePassword( 77 String userName, String password) { 78 if ((userName == null) || (password == null)) 79 return null; 80 81 String realPassword = (String ) 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 ) this.roles.get(userName)); 89 } 90 91 94 public AuthenticationPrincipal retrieveUser(String userName) { 95 if (userName == null) 96 return null; 97 else 98 return new AuthenticationPrincipal(userName, 99 (String ) this.passwords.get(userName), (List ) this.roles 100 .get(userName)); 101 } 102 } | Popular Tags |