1 18 package org.apache.activemq.jaas; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Enumeration ; 23 import java.util.HashSet ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.Set ; 27 28 import javax.security.auth.Subject ; 29 import javax.security.auth.callback.Callback ; 30 import javax.security.auth.callback.CallbackHandler ; 31 import javax.security.auth.callback.NameCallback ; 32 import javax.security.auth.callback.PasswordCallback ; 33 import javax.security.auth.callback.UnsupportedCallbackException ; 34 import javax.security.auth.login.LoginException ; 35 import javax.security.auth.login.FailedLoginException ; 36 import javax.security.auth.spi.LoginModule ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 42 45 public class PropertiesLoginModule implements LoginModule { 46 47 private final String USER_FILE = "org.apache.activemq.jaas.properties.user"; 48 private final String GROUP_FILE = "org.apache.activemq.jaas.properties.group"; 49 50 private static final Log log = LogFactory.getLog(PropertiesLoginModule.class); 51 52 private Subject subject; 53 private CallbackHandler callbackHandler; 54 55 private boolean debug; 56 private String usersFile; 57 private String groupsFile; 58 private Properties users = new Properties (); 59 private Properties groups = new Properties (); 60 private String user; 61 private Set principals = new HashSet (); 62 private File baseDir; 63 64 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { 65 this.subject = subject; 66 this.callbackHandler = callbackHandler; 67 68 if( System.getProperty("java.security.auth.login.config")!=null ) { 69 baseDir=new File (System.getProperty("java.security.auth.login.config")).getParentFile(); 70 } else { 71 baseDir = new File ("."); 72 } 73 74 debug = "true".equalsIgnoreCase((String ) options.get("debug")); 75 usersFile = (String ) options.get(USER_FILE)+""; 76 groupsFile = (String ) options.get(GROUP_FILE)+""; 77 78 if (debug) { 79 log.debug("Initialized debug=" + debug + " usersFile=" + usersFile + " groupsFile=" + groupsFile+" basedir="+baseDir); 80 } 81 } 82 83 public boolean login() throws LoginException { 84 File f = new File (baseDir,usersFile); 85 try { 86 users.load(new java.io.FileInputStream (f)); 87 } catch (IOException ioe) { 88 throw new LoginException ("Unable to load user properties file " + f); 89 } 90 f = new File (baseDir, groupsFile); 91 try { 92 groups.load(new java.io.FileInputStream (f)); 93 } catch (IOException ioe) { 94 throw new LoginException ("Unable to load group properties file " + f); 95 } 96 97 Callback [] callbacks = new Callback [2]; 98 99 callbacks[0] = new NameCallback ("Username: "); 100 callbacks[1] = new PasswordCallback ("Password: ", false); 101 try { 102 callbackHandler.handle(callbacks); 103 } catch (IOException ioe) { 104 throw new LoginException (ioe.getMessage()); 105 } catch (UnsupportedCallbackException uce) { 106 throw new LoginException (uce.getMessage() + " not available to obtain information from user"); 107 } 108 user = ((NameCallback ) callbacks[0]).getName(); 109 char[] tmpPassword = ((PasswordCallback ) callbacks[1]).getPassword(); 110 if (tmpPassword == null) tmpPassword = new char[0]; 111 112 String password = users.getProperty(user); 113 114 if (password == null) throw new FailedLoginException ("User does exist"); 115 if (!password.equals(new String (tmpPassword))) throw new FailedLoginException ("Password does not match"); 116 117 users.clear(); 118 119 if (debug) { 120 log.debug("login " + user); 121 } 122 return true; 123 } 124 125 public boolean commit() throws LoginException { 126 principals.add(new UserPrincipal(user)); 127 128 for (Enumeration enumeration = groups.keys(); enumeration.hasMoreElements();) { 129 String name = (String ) enumeration.nextElement(); 130 String [] userList = ((String ) groups.getProperty(name) + "").split(","); 131 for (int i = 0; i < userList.length; i++) { 132 if (user.equals(userList[i])) { 133 principals.add(new GroupPrincipal(name)); 134 break; 135 } 136 } 137 } 138 139 subject.getPrincipals().addAll(principals); 140 141 clear(); 142 143 if (debug) { 144 log.debug("commit"); 145 } 146 return true; 147 } 148 149 public boolean abort() throws LoginException { 150 clear(); 151 152 if (debug) { 153 log.debug("abort"); 154 } 155 return true; 156 } 157 158 public boolean logout() throws LoginException { 159 subject.getPrincipals().removeAll(principals); 160 principals.clear(); 161 162 if (debug) { 163 log.debug("logout"); 164 } 165 return true; 166 } 167 168 private void clear() { 169 groups.clear(); 170 user = null; 171 } 172 } 173 | Popular Tags |