package org.jboss.mq.sm.file;
import java.security.acl.Group;
import java.util.Map;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginException;
import org.jboss.security.SimpleGroup;
import org.jboss.security.SimplePrincipal;
import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
public class DynamicLoginModule extends UsernamePasswordLoginModule
{
static final String DEFAULT_SM_NAME = "jboss.mq:service=StateManager";
DynamicStateManager sm = null;
public DynamicLoginModule()
{
}
public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options)
{
super.initialize(subject, callbackHandler, sharedState, options);
try
{
String smName = (String) options.get("sm.objectname");
if (smName == null)
smName = DEFAULT_SM_NAME;
javax.management.ObjectName smObjectName = new javax.management.ObjectName(smName);
javax.management.MBeanServer server = org.jboss.mx.util.MBeanServerLocator.locateJBoss();
sm = (DynamicStateManager) server.getAttribute(smObjectName, "Instance");
}
catch (Exception ex)
{
super.log.error("Failed to load DynamicSecurityManager", ex);
}
}
public boolean login() throws LoginException
{
if (sm == null)
throw new LoginException("StateManager is null");
return super.login();
}
protected String getUsersPassword() throws LoginException
{
return "";
}
protected boolean validatePassword(String inputPassword, String expectedPassword)
{
boolean valid = false;
try
{
valid = sm.validatePassword(getUsername(), inputPassword);
}
catch (Exception ex)
{
super.log.debug("Could not validate password for user " + getUsername(), ex);
}
return valid;
}
protected Group[] getRoleSets() throws LoginException
{
SimpleGroup userRoles = new SimpleGroup("Roles");
String[] roles = null;
try
{
roles = sm.getRoles(getUsername());
}
catch (Exception ex)
{
super.log.error("Could not get roleSets for user " + getUsername(), ex);
throw new LoginException("Could not get roleSets for user");
}
if (roles != null)
{
for (int i = 0; i < roles.length; i++)
{
userRoles.addMember(new SimplePrincipal(roles[i]));
}
}
Group[] roleSets = {userRoles};
return roleSets;
}
}