1 7 package org.jboss.security.auth.spi; 8 9 11 import org.jboss.security.SimpleGroup; 12 13 import javax.security.auth.Subject ; 14 import javax.security.auth.callback.CallbackHandler ; 15 import javax.security.auth.login.LoginException ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.net.URL ; 19 import java.security.acl.Group ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 41 public class UsersLoginModule extends UsernamePasswordLoginModule 42 { 43 44 private String usersRsrcName = "users.properties"; 45 46 private Properties users; 47 48 54 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) 55 { 56 super.initialize(subject, callbackHandler, sharedState, options); 57 try 58 { 59 String option = (String ) options.get("usersProperties"); 61 if (option != null) 62 usersRsrcName = option; 63 64 loadUsers(); 66 } 67 catch (Exception e) 68 { 69 super.log.error("Failed to load users/passwords/role files", e); 72 } 73 } 74 75 83 public boolean login() throws LoginException 84 { 85 if (users == null) 86 throw new LoginException ("Missing users.properties file."); 87 88 return super.login(); 89 } 90 91 96 protected Group[] getRoleSets() throws LoginException 97 { 98 return new Group[0]; 99 } 100 101 protected String getUsersPassword() 102 { 103 String username = getUsername(); 104 String password = null; 105 if (username != null) 106 password = users.getProperty(username, null); 107 return password; 108 } 109 110 private void loadUsers() throws IOException 111 { 112 users = loadProperties(usersRsrcName); 113 } 114 115 120 private Properties loadProperties(String propertiesName) throws IOException 121 { 122 Properties bundle = null; 123 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 124 URL url = loader.getResource(propertiesName); 125 if (url == null) 126 throw new IOException ("Properties file " + propertiesName + " not found"); 127 128 super.log.trace("Properties file=" + url); 129 130 InputStream is = url.openStream(); 131 if (is != null) 132 { 133 bundle = new Properties (); 134 bundle.load(is); 135 } 136 else 137 { 138 throw new IOException ("Properties file " + propertiesName + " not avilable"); 139 } 140 return bundle; 141 } 142 } 143 | Popular Tags |