1 22 package org.jboss.resource.security; 23 24 import java.security.acl.Group ; 25 import java.security.Principal ; 26 import java.security.PrivilegedExceptionAction ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedActionException ; 29 import java.util.Map ; 30 import javax.resource.spi.security.PasswordCredential ; 31 import javax.security.auth.Subject ; 32 import javax.security.auth.callback.CallbackHandler ; 33 import javax.security.auth.login.LoginException ; 34 import javax.management.ObjectName ; 35 import javax.management.MBeanServer ; 36 37 import org.jboss.security.SimplePrincipal; 38 import org.jboss.logging.Logger; 39 40 79 public class JaasSecurityDomainIdentityLoginModule 80 extends AbstractPasswordCredentialLoginModule 81 { 82 private static final Logger log = Logger.getLogger(JaasSecurityDomainIdentityLoginModule.class); 83 84 private String username; 85 private String password; 86 private ObjectName jaasSecurityDomain; 87 88 public void initialize(Subject subject, CallbackHandler handler, 89 Map sharedState, Map options) 90 { 91 super.initialize(subject, handler, sharedState, options); 92 username = (String ) options.get("username"); 94 if( username == null ) 95 { 96 username = (String ) options.get("userName"); 98 if( username == null ) 99 { 100 throw new IllegalArgumentException ("The user name is a required option"); 101 } 102 } 103 104 password = (String ) options.get("password"); 105 if( password == null ) 106 { 107 throw new IllegalArgumentException ("The password is a required option"); 108 } 109 110 String name = (String ) options.get("jaasSecurityDomain"); 111 if( name == null ) 112 { 113 throw new IllegalArgumentException ("The jaasSecurityDomain is a required option"); 114 } 115 116 try 117 { 118 jaasSecurityDomain = new ObjectName (name); 119 } 120 catch(Exception e) 121 { 122 throw new IllegalArgumentException ("Invalid jaasSecurityDomain: " + e.getMessage()); 123 } 124 } 125 126 public boolean login() throws LoginException 127 { 128 log.trace("login called"); 129 if( super.login() == true ) 130 return true; 131 132 super.loginOk = true; 133 return true; 134 } 135 136 public boolean commit() throws LoginException 137 { 138 Principal principal = new SimplePrincipal(username); 139 SubjectActions.addPrincipals(subject, principal); 140 sharedState.put("javax.security.auth.login.name", username); 141 try 143 { 144 char[] decodedPassword = DecodeAction.decode(password, 145 jaasSecurityDomain, getServer()); 146 PasswordCredential cred = new PasswordCredential (username, decodedPassword); 147 cred.setManagedConnectionFactory(getMcf()); 148 SubjectActions.addCredentials(subject, cred); 149 } 150 catch(Exception e) 151 { 152 log.debug("Failed to decode password", e); 153 throw new LoginException ("Failed to decode password: " + e.getMessage()); 154 } 155 return true; 156 } 157 158 public boolean abort() 159 { 160 username = null; 161 password = null; 162 return true; 163 } 164 165 protected Principal getIdentity() 166 { 167 log.trace("getIdentity called, username=" + username); 168 Principal principal = new SimplePrincipal(username); 169 return principal; 170 } 171 172 protected Group [] getRoleSets() throws LoginException 173 { 174 Group [] empty = new Group [0]; 175 return empty; 176 } 177 178 private static class DecodeAction implements PrivilegedExceptionAction 179 { 180 String password; 181 ObjectName jaasSecurityDomain; 182 MBeanServer server; 183 184 DecodeAction(String password, ObjectName jaasSecurityDomain, 185 MBeanServer server) 186 { 187 this.password = password; 188 this.jaasSecurityDomain = jaasSecurityDomain; 189 this.server = server; 190 } 191 192 197 public Object run() throws Exception 198 { 199 Object [] args = {password}; 201 String [] sig = {String .class.getName()}; 202 byte[] secret = (byte[]) server.invoke(jaasSecurityDomain, 203 "decode64", args, sig); 204 String secretPassword = new String (secret, "UTF-8"); 206 return secretPassword.toCharArray(); 207 } 208 static char[] decode(String password, ObjectName jaasSecurityDomain, 209 MBeanServer server) 210 throws Exception 211 { 212 DecodeAction action = new DecodeAction(password, jaasSecurityDomain, server); 213 try 214 { 215 char[] decode = (char[]) AccessController.doPrivileged(action); 216 return decode; 217 } 218 catch(PrivilegedActionException e) 219 { 220 throw e.getException(); 221 } 222 } 223 } 224 } 225 | Popular Tags |