package org.jboss.test.security.ejb;
import java.security.acl.Group;
import java.security.Principal;
import javax.security.auth.login.LoginException;
import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
import org.jboss.security.SimpleGroup;
import org.jboss.security.SimplePrincipal;
public class CustomPrincipalLoginModule extends UsernamePasswordLoginModule
{
private CustomPrincipalImpl caller;
public boolean login() throws LoginException
{
if (super.login())
{
caller = new CustomPrincipalImpl(getUsername());
return true;
}
return false;
}
protected Principal getIdentity()
{
Principal identity = caller;
if( identity == null )
identity = super.getIdentity();
return identity;
}
protected Group[] getRoleSets() throws LoginException
{
try
{
Group roles = new SimpleGroup("Roles");
Group callerPrincipal = new SimpleGroup("CallerPrincipal");
Group[] groups = {roles, callerPrincipal};
log.info("Getting roles for user=" + getUsername());
roles.addMember(new SimplePrincipal("Echo"));
callerPrincipal.addMember(caller);
return groups;
}
catch (Exception e)
{
log.error("Failed to obtain groups for user=" + getUsername(), e);
throw new LoginException(e.toString());
}
}
protected String getUsersPassword()
{
return "theduke";
}
}