package org.jboss.resource.security;
import java.security.acl.Group;
import java.security.Principal;
import java.util.Map;
import java.util.Set;
import javax.resource.spi.security.PasswordCredential;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginException;
import org.jboss.security.SimplePrincipal;
import org.jboss.security.RunAsIdentity;
import org.jboss.logging.Logger;
public class CallerIdentityLoginModule
extends AbstractPasswordCredentialLoginModule
{
private static final Logger log = Logger.getLogger(CallerIdentityLoginModule.class);
private String userName;
private char[] password;
private boolean addRunAsRoles;
private Set runAsRoles;
public CallerIdentityLoginModule()
{
}
public void initialize(Subject subject, CallbackHandler handler,
Map sharedState, Map options)
{
super.initialize(subject, handler, sharedState, options);
userName = (String) options.get("userName");
if (userName == null)
{
log.debug("No default username supplied.");
}
String pass = (String) options.get("password");
if (pass == null)
{
log.debug("No default password supplied.");
}
else
{
password = pass.toCharArray();
}
String flag = (String) options.get("addRunAsRoles");
addRunAsRoles = Boolean.valueOf(flag).booleanValue();
log.debug("got default principal: " + userName + ", username: "
+ userName + ", password: " + (password == null ? "null" : "****")
+ " addRunAsRoles: "+addRunAsRoles);
}
public boolean login() throws LoginException
{
log.trace("Caller Association login called");
String username = userName;
try
{
Principal user = GetPrincipalInfoAction.getPrincipal();
char[] userPassword = GetPrincipalInfoAction.getCredential();
if( userPassword != null )
{
password = userPassword;
}
if (user != null)
{
username = user.getName();
if (log.isTraceEnabled())
{
log.trace("Current Calling principal is: " + username
+ " ThreadName: " + Thread.currentThread().getName());
}
if( user instanceof RunAsIdentity )
{
RunAsIdentity runAs = (RunAsIdentity) user;
runAsRoles = runAs.getRunAsRoles();
}
}
}
catch (Throwable e)
{
throw new LoginException("Unable to get the calling principal or its credentials for resource association");
}
userName = username;
if (super.login() == true)
{
return true;
}
sharedState.put("javax.security.auth.login.name", username);
super.loginOk = true;
return true;
}
public boolean commit() throws LoginException
{
sharedState.put("javax.security.auth.login.name", userName);
if( addRunAsRoles && runAsRoles != null )
{
SubjectActions.addRoles(subject, runAsRoles);
}
PasswordCredential cred = new PasswordCredential(userName, password);
cred.setManagedConnectionFactory(getMcf());
SubjectActions.addCredentials(subject, cred);
return super.commit();
}
protected Principal getIdentity()
{
log.trace("getIdentity called");
Principal principal = new SimplePrincipal(userName);
return principal;
}
protected Group[] getRoleSets() throws LoginException
{
log.trace("getRoleSets called");
return new Group[]{};
}
}