Package org.jboss.security.identitytrust

Source Code of org.jboss.security.identitytrust.JBossIdentityTrustContext

/*
  * JBoss, Home of Professional Open Source
  * Copyright 2007, 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.identitytrust;

import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Map;

import org.jboss.logging.Logger;
import org.jboss.security.SecurityContext;
import org.jboss.security.config.ApplicationPolicy;
import org.jboss.security.config.ControlFlag;
import org.jboss.security.config.IdentityTrustInfo;
import org.jboss.security.config.SecurityConfiguration;
import org.jboss.security.identitytrust.IdentityTrustManager.TrustDecision;
import org.jboss.security.identitytrust.config.IdentityTrustModuleEntry;
import org.jboss.security.plugins.authorization.SecurityActions;

//$Id$

/**
@author Anil.Saldhana@redhat.com
@since  Aug 2, 2007
@version $Revision$
*/
public class JBossIdentityTrustContext extends IdentityTrustContext
{
   protected Logger log = Logger.getLogger(JBossIdentityTrustContext.class);

   //Control Flag behavior
   private boolean encounteredRequiredDeny = false;
   private boolean encounteredRequiredNotApplicable = false;
   private boolean encounteredOptionalError = false;
   private IdentityTrustException moduleException = null;
   private TrustDecision overallDecision = TrustDecision.NotApplicable;

   private boolean encounteredRequiredPermit;
  
   public JBossIdentityTrustContext(SecurityContext sc)
   {
      this.securityContext = sc;
   }
  
   @Override
   public TrustDecision isTrusted() throws IdentityTrustException
   {
      TrustDecision decision = NOTAPPLICABLE;
        
      try
      {
         initializeModules();
      }
      catch (Exception e)
      {
         throw new IdentityTrustException(e);
      }
      //Do a PrivilegedAction
      try
      {
         decision = (TrustDecision) AccessController.doPrivileged(new PrivilegedExceptionAction()
         {
            public Object run() throws IdentityTrustException
            {
               TrustDecision result = invokeTrusted();
               if(result == PERMIT)
                  invokeCommit();
               if(result == DENY || result == NOTAPPLICABLE)
               {
                  invokeAbort()
               }
               return result;
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         Exception exc = e.getException();
         log.trace("Error in isAuthorize:", exc);
         invokeAbort();
         throw ((IdentityTrustException)exc);
      }
      return decision;
   }
  
   private void initializeModules() throws Exception
   {
      String securityDomain = this.securityContext.getSecurityDomain();
      //Get the Configuration
      ApplicationPolicy aPolicy = SecurityConfiguration.getApplicationPolicy( securityDomain);
      if(aPolicy == null)
         throw new IllegalStateException("ApplicationPolicy not found for "+ securityDomain);
     
      IdentityTrustInfo iti = aPolicy.getIdentityTrustInfo();
      IdentityTrustModuleEntry[] itmearr = iti.getIdentityTrustModuleEntry();
      for(IdentityTrustModuleEntry itme: itmearr)
      {
         ControlFlag cf = itme.getControlFlag();
         if(cf == null)
            cf = ControlFlag.REQUIRED;
        
         this.controlFlags.add(cf);
         modules.add(instantiateModule(itme.getName(), itme.getOptions()));
      }
   }
  
   private IdentityTrustModule instantiateModule(String name, Map map) throws Exception
   {
      IdentityTrustModule im = null;
      ClassLoader tcl = SecurityActions.getContextClassLoader();
      try
      {
         Class clazz = tcl.loadClass(name);
         im = (IdentityTrustModule)clazz.newInstance();
      }
      catch ( Exception e)
      {
         log.debug("Error instantiating IdentityTrustModule:",e);
      }
      if(im == null)
         throw new IllegalStateException("IdentityTrustModule has not " +
               "been instantiated");
      im.initialize(this.securityContext, this.callbackHandler, this.sharedState,map);
      return im;
   }
  
   private TrustDecision invokeTrusted()
   throws IdentityTrustException
   {
      int length = modules.size();
      for(int i = 0; i < length; i++)
      {
         IdentityTrustModule module = (IdentityTrustModule)modules.get(i);
         ControlFlag flag = (ControlFlag)this.controlFlags.get(i);
         TrustDecision decision = NOTAPPLICABLE;
         try
         {
            decision = module.isTrusted();
         }
         catch(Exception ae)
         {
            decision = NOTAPPLICABLE;
            if(this.moduleException == null)
               this.moduleException = new IdentityTrustException(ae);
         }
        
         if(decision == PERMIT)
         {
            this.overallDecision =  PERMIT;
            if(flag == ControlFlag.REQUIRED)
               this.encounteredRequiredPermit = true;
            //SUFFICIENT case
            if(flag == ControlFlag.SUFFICIENT && this.encounteredRequiredDeny == false)
               return PERMIT;
            continue; //Continue with the other modules
         }
        
         if(decision == NOTAPPLICABLE && flag == ControlFlag.REQUIRED)
         { 
            this.encounteredRequiredNotApplicable = true;
            continue; //Continue with the other modules
         }
         //Go through the failure cases
         //REQUISITE case
         if(flag == ControlFlag.REQUISITE)
         {
            log.trace("REQUISITE failed for " + module);
            if(this.moduleException == null)
               this.moduleException = new IdentityTrustException("Authorization failed");
            else
               throw this.moduleException;
         }
         //REQUIRED Case
         if(flag == ControlFlag.REQUIRED)
         {
            log.trace("REQUIRED failed for " + module);
            this.encounteredRequiredDeny = true;
         }
         if(flag == ControlFlag.OPTIONAL)
            this.encounteredOptionalError = true;
      }
     
      //All the authorization modules have been visited.
      if(this.encounteredRequiredDeny)
         return DENY;
      if(this.overallDecision == DENY && this.encounteredOptionalError)
         return DENY;
      if(this.overallDecision == DENY)
         return DENY;
     
      if(this.encounteredRequiredNotApplicable && !encounteredRequiredPermit)
         return NOTAPPLICABLE;
      return PERMIT;
   }
  
   private void invokeCommit()
   throws IdentityTrustException
   {
      int length = modules.size();
      for(int i = 0; i < length; i++)
      {
         IdentityTrustModule module = (IdentityTrustModule)modules.get(i);
         boolean bool = module.commit();
         if(!bool)
            throw new IdentityTrustException("commit on modules failed");
      }
   }
  
   private void invokeAbort()
   throws IdentityTrustException
   {
      int length = modules.size();
      for(int i = 0; i < length; i++)
      {
         IdentityTrustModule module = (IdentityTrustModule)modules.get(i);
         boolean bool = module.abort();
         if(!bool)
            throw new IdentityTrustException("abort on modules failed");
      }
   }
}
TOP

Related Classes of org.jboss.security.identitytrust.JBossIdentityTrustContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.