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