package org.jboss.net.axis.server;
import org.jboss.axis.AxisFault;
import org.jboss.axis.MessageContext;
import org.jboss.axis.handlers.BasicHandler;
import org.jboss.security.AnybodyPrincipal;
import org.jboss.security.NobodyPrincipal;
import org.jboss.security.RealmMapping;
import org.jboss.security.SimplePrincipal;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.security.auth.Subject;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
public class JBossAuthorizationHandler extends BasicHandler
{
protected RealmMapping realmMapping;
final protected Set rolesAllowed = new java.util.HashSet();
final protected Set rolesDenied = new java.util.HashSet();
protected boolean isInitialised;
public JBossAuthorizationHandler()
{
}
protected void initialise() throws AxisFault
{
isInitialised = true;
realmMapping = null;
String securityDomain = (String)getOption(Constants.SECURITY_DOMAIN_OPTION);
if (securityDomain != null)
{
try
{
realmMapping =
(RealmMapping)new InitialContext().lookup(securityDomain);
}
catch (NamingException e)
{
throw new AxisFault("Could not lookup security domain " + securityDomain, e);
}
}
String allowedRoles = (String)getOption(Constants.ALLOWED_ROLES_OPTION);
if (allowedRoles == null)
{
allowedRoles = "*";
}
StringTokenizer tokenizer = new StringTokenizer(allowedRoles, ",");
while (tokenizer.hasMoreTokens())
{
rolesAllowed.add(getPrincipal(tokenizer.nextToken()));
}
String deniedRoles = (String)getOption(Constants.DENIED_ROLES_OPTION);
if (deniedRoles != null)
{
tokenizer = new StringTokenizer(deniedRoles, ",");
while (tokenizer.hasMoreTokens())
{
rolesDenied.add(getPrincipal(tokenizer.nextToken()));
}
}
}
protected Principal getPrincipal(String userName)
{
if (userName.equals("*"))
{
return AnybodyPrincipal.ANYBODY_PRINCIPAL;
}
else
{
return new SimplePrincipal(userName);
}
}
protected Collection getAssociatedPrincipals(MessageContext msgContext)
{
Subject activeSubject =
(Subject)msgContext.getProperty(MessageContext.AUTHUSER);
if (activeSubject == null)
{
return Collections.singleton(NobodyPrincipal.NOBODY_PRINCIPAL);
}
else
{
return activeSubject.getPrincipals();
}
}
protected boolean doesUserHaveRole(Principal principal, Set roles)
{
return realmMapping.doesUserHaveRole(principal, roles);
}
public void invoke(MessageContext msgContext) throws AxisFault
{
if (!isInitialised)
{
synchronized (this)
{
if (!isInitialised)
{
initialise();
}
}
}
if (realmMapping == null)
{
throw new AxisFault("No security domain associated.");
}
Iterator allPrincipals = getAssociatedPrincipals(msgContext).iterator();
boolean accessAllowed = false;
while (allPrincipals.hasNext())
{
Principal nextPrincipal = (Principal)allPrincipals.next();
if (doesUserHaveRole(nextPrincipal, rolesDenied))
{
accessAllowed = false;
break;
}
else if (!accessAllowed && doesUserHaveRole(nextPrincipal, rolesAllowed))
{
accessAllowed = true;
}
}
if (!accessAllowed)
{
throw new AxisFault("Access denied.");
}
}
}