1 22 package org.jboss.resource.security; 23 24 import java.security.acl.Group ; 25 import java.security.Principal ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import javax.resource.spi.security.PasswordCredential ; 29 import javax.security.auth.Subject ; 30 import javax.security.auth.callback.CallbackHandler ; 31 import javax.security.auth.login.LoginException ; 32 33 import org.jboss.security.SimplePrincipal; 34 import org.jboss.security.RunAsIdentity; 35 import org.jboss.logging.Logger; 36 37 52 public class CallerIdentityLoginModule 53 extends AbstractPasswordCredentialLoginModule 54 { 55 58 private static final Logger log = Logger.getLogger(CallerIdentityLoginModule.class); 59 60 63 private String userName; 64 65 68 private char[] password; 69 70 private boolean addRunAsRoles; 71 private Set runAsRoles; 72 73 76 public CallerIdentityLoginModule() 77 { 78 } 79 80 91 public void initialize(Subject subject, CallbackHandler handler, 92 Map sharedState, Map options) 93 { 94 super.initialize(subject, handler, sharedState, options); 95 96 userName = (String ) options.get("userName"); 97 if (userName == null) 98 { 99 log.debug("No default username supplied."); 100 } 101 102 String pass = (String ) options.get("password"); 103 if (pass == null) 104 { 105 log.debug("No default password supplied."); 106 } 107 else 108 { 109 password = pass.toCharArray(); 110 } 111 112 String flag = (String ) options.get("addRunAsRoles"); 114 addRunAsRoles = Boolean.valueOf(flag).booleanValue(); 115 116 log.debug("got default principal: " + userName + ", username: " 117 + userName + ", password: " + (password == null ? "null" : "****") 118 + " addRunAsRoles: "+addRunAsRoles); 119 120 } 121 122 130 public boolean login() throws LoginException 131 { 132 log.trace("Caller Association login called"); 133 134 String username = userName; 137 138 try 140 { 141 Principal user = GetPrincipalInfoAction.getPrincipal(); 142 char[] userPassword = GetPrincipalInfoAction.getCredential(); 143 144 if( userPassword != null ) 145 { 146 password = userPassword; 147 } 148 149 if (user != null) 150 { 151 username = user.getName(); 152 if (log.isTraceEnabled()) 153 { 154 log.trace("Current Calling principal is: " + username 155 + " ThreadName: " + Thread.currentThread().getName()); 156 } 157 RunAsIdentity runAs = GetPrincipalInfoAction.peekRunAsIdentity(); 159 if( runAs != null ) 160 { 161 runAsRoles = runAs.getRunAsRoles(); 162 } 163 } 164 } 165 catch (Throwable e) 166 { 167 throw new LoginException ("Unable to get the calling principal or its credentials for resource association"); 168 } 169 170 userName = username; 172 if (super.login() == true) 173 { 174 return true; 175 } 176 177 sharedState.put("javax.security.auth.login.name", username); 179 super.loginOk = true; 180 181 return true; 182 } 183 184 public boolean commit() throws LoginException 185 { 186 sharedState.put("javax.security.auth.login.name", userName); 188 if( addRunAsRoles && runAsRoles != null ) 190 { 191 SubjectActions.addRoles(subject, runAsRoles); 192 } 193 194 PasswordCredential cred = new PasswordCredential (userName, password); 196 cred.setManagedConnectionFactory(getMcf()); 197 SubjectActions.addCredentials(subject, cred); 198 return super.commit(); 199 } 200 201 protected Principal getIdentity() 202 { 203 log.trace("getIdentity called"); 204 Principal principal = new SimplePrincipal(userName); 205 return principal; 206 } 207 208 protected Group [] getRoleSets() throws LoginException 209 { 210 log.trace("getRoleSets called"); 211 return new Group []{}; 212 } 213 } 214 | Popular Tags |