/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.security.plugins.authorization;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Map;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import org.jboss.logging.Logger;
import org.jboss.security.SecurityConstants;
import org.jboss.security.authorization.AuthorizationContext;
import org.jboss.security.authorization.AuthorizationException;
import org.jboss.security.authorization.AuthorizationModule;
import org.jboss.security.authorization.Resource;
import org.jboss.security.authorization.ResourceKeys;
import org.jboss.security.authorization.ResourceType;
import org.jboss.security.authorization.config.AuthorizationModuleEntry;
import org.jboss.security.authorization.modules.DelegatingAuthorizationModule;
import org.jboss.security.config.ApplicationPolicy;
import org.jboss.security.config.AuthorizationInfo;
import org.jboss.security.config.ControlFlag;
import org.jboss.security.config.SecurityConfiguration;
import org.jboss.security.identity.Role;
import org.jboss.security.identity.RoleGroup;
//$Id: JBossAuthorizationContext.java 62954 2007-05-10 04:12:18Z anil.saldhana@jboss.com $
/**
* JBAS-3374: Authorization Framework for Policy Decision Modules
* For information on the behavior of the Authorization Modules,
* For Authorization Modules behavior(Required, Requisite, Sufficient and Optional)
* please refer to the javadoc for @see javax.security.auth.login.Configuration
*
* The AuthorizationContext derives the AuthorizationInfo(configuration for the modules)
* in the following way:
* a) If there has been an injection of ApplicationPolicy, then it will be used.
* b) Util.getApplicationPolicy will be used(which relies on SecurityConfiguration static class).
* c) Flag an error that there is no available Application Policy
*
* @author Anil Saldhana
* @since Jun 11, 2006
* @version $Revision: 62954 $
*/
public class JBossAuthorizationContext extends AuthorizationContext
{
private static Logger log = Logger.getLogger(JBossAuthorizationContext.class);
private boolean trace = log.isTraceEnabled();
private final String EJB = SecurityConstants.DEFAULT_EJB_APPLICATION_POLICY;
private final String WEB = SecurityConstants.DEFAULT_WEB_APPLICATION_POLICY;
private Subject authenticatedSubject = null;
//Application Policy can be injected
private ApplicationPolicy applicationPolicy = null;
public JBossAuthorizationContext(String name)
{
this.securityDomainName = name;
}
public JBossAuthorizationContext(String name, CallbackHandler handler)
{
this(name);
this.callbackHandler = handler;
}
public JBossAuthorizationContext(String name, Subject subject, CallbackHandler handler)
{
this(name,handler);
this.authenticatedSubject = subject;
}
/**
* Inject an ApplicationPolicy that contains AuthorizationInfo
* @param aPolicy
* @throws IllegalArgumentException if ApplicationPolicy is null or
* does not contain AuthorizationInfo or domain name does not match
*/
public void setApplicationPolicy(ApplicationPolicy aPolicy)
{
if(aPolicy == null)
throw new IllegalArgumentException("Application Policy is null:domain="+this.securityDomainName);
AuthorizationInfo authzInfo = aPolicy.getAuthorizationInfo();
if( authzInfo == null)
throw new IllegalArgumentException("Application Policy has no AuthorizationInfo");
if(!authzInfo.getName().equals(securityDomainName))
throw new IllegalArgumentException("Application Policy ->AuthorizationInfo:" + authzInfo.getName()
+ " does not match required domain name=" + this.securityDomainName);
this.applicationPolicy = aPolicy;
}
/**
* Authorize the Resource
* @param resource
* @return AuthorizationContext.PERMIT or AuthorizationContext.DENY
* @throws AuthorizationException
*/
public int authorize(final Resource resource) throws AuthorizationException
{
return this.authorize(resource, this.authenticatedSubject,
(RoleGroup)resource.getMap().get(ResourceKeys.SECURITY_CONTEXT_ROLES));
}
/**
* @see AuthorizationContext#authorize(Resource, Role)
*/
public int authorize(final Resource resource,
final Subject subject,
final RoleGroup callerRoles) throws AuthorizationException
{
try
{
try
{
this.authenticatedSubject = subject;
initializeModules(resource, callerRoles);
}
catch (PrivilegedActionException e1)
{
throw new RuntimeException(e1);
}
//Do a PrivilegedAction
try
{
AccessController.doPrivileged(new PrivilegedExceptionAction