1 18 19 package org.apache.activemq.jaas; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.security.cert.X509Certificate ; 24 import java.util.Enumeration ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 import java.util.Set ; 29 30 import javax.security.auth.Subject ; 31 import javax.security.auth.callback.CallbackHandler ; 32 import javax.security.auth.login.LoginException ; 33 34 48 public class TextFileCertificateLoginModule extends CertificateLoginModule { 49 50 private final String USER_FILE = "org.apache.activemq.jaas.textfiledn.user"; 51 private final String GROUP_FILE = "org.apache.activemq.jaas.textfiledn.group"; 52 53 private File baseDir; 54 private String usersFilePathname; 55 private String groupsFilePathname; 56 57 62 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { 63 super.initialize(subject, callbackHandler, sharedState, options); 64 if (System.getProperty("java.security.auth.login.config") != null) { 65 baseDir = new File (System.getProperty("java.security.auth.login.config")).getParentFile(); 66 } else { 67 baseDir = new File ("."); 68 } 69 70 usersFilePathname = (String ) options.get(USER_FILE)+""; 71 groupsFilePathname = (String ) options.get(GROUP_FILE)+""; 72 } 73 74 81 protected String getUserNameForCertificates(final X509Certificate [] certs) throws LoginException { 82 if (certs == null) { 83 throw new LoginException ("Client certificates not found. Cannot authenticate."); 84 } 85 86 File usersFile = new File (baseDir,usersFilePathname); 87 88 Properties users = new Properties (); 89 90 try { 91 users.load(new java.io.FileInputStream (usersFile)); 92 } catch (IOException ioe) { 93 throw new LoginException ("Unable to load user properties file " + usersFile); 94 } 95 96 String dn = getDistinguishedName(certs); 97 98 for(Enumeration vals = users.elements(), keys = users.keys(); vals.hasMoreElements(); ) { 99 if ( ((String )vals.nextElement()).equals(dn) ) { 100 return (String )keys.nextElement(); 101 } else { 102 keys.nextElement(); 103 } 104 } 105 106 return null; 107 } 108 109 117 protected Set getUserGroups(String username) throws LoginException { 118 File groupsFile = new File (baseDir, groupsFilePathname); 119 120 Properties groups = new Properties (); 121 try { 122 groups.load(new java.io.FileInputStream (groupsFile)); 123 } catch (IOException ioe) { 124 throw new LoginException ("Unable to load group properties file " + groupsFile); 125 } 126 Set userGroups = new HashSet (); 127 for (Enumeration enumeration = groups.keys(); enumeration.hasMoreElements();) { 128 String groupName = (String ) enumeration.nextElement(); 129 String [] userList = (groups.getProperty(groupName) + "").split(","); 130 for (int i = 0; i < userList.length; i++) { 131 if (username.equals(userList[i])) { 132 userGroups.add(groupName); 133 break; 134 } 135 } 136 } 137 138 return userGroups; 139 } 140 } 141 | Popular Tags |