Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file 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.auth.callback;
 
 import java.util.List;
 
 import  javax.security.auth.message.callback.CallerPrincipalCallback;
 import  javax.security.auth.message.callback.GroupPrincipalCallback;
 import  javax.security.auth.message.callback.PasswordValidationCallback;
 
 import  org.jboss.security.SecurityConstants;
 import  org.jboss.security.SecurityContext;
 import  org.jboss.security.SecurityContextUtil;
 import  org.jboss.security.identity.Identity;
 import  org.jboss.security.identity.IdentityFactory;
 import  org.jboss.security.identity.Role;
 import  org.jboss.security.identity.RoleGroup;
 import  org.jboss.security.identity.plugins.SimpleRole;
 import  org.jboss.security.identity.plugins.SimpleRoleGroup;


CallbackHandler with the JASPI callbacks

Author(s):
Anil.Saldhana@redhat.com
Since:
May 12, 2010
 
 public class JASPICallbackHandler extends JBossCallbackHandler
 { 
    private static final long serialVersionUID = 1L;   
    
    public JASPICallbackHandler()
    {
       super(); 
    }
 
 
    public JASPICallbackHandler(Principal principalObject credential)
    {
       super(principalcredential); 
    }
 
 
    @Override
    protected void handleCallBack(Callback callbackthrows UnsupportedCallbackException
    { 
       ifcallback instanceof GroupPrincipalCallback )
       {
          GroupPrincipalCallback groupPrincipalCallback = (GroupPrincipalCallback) callback
          SecurityContext currentSC = SecurityActions.getCurrentSecurityContext();
          ifcurrentSC == null )
             throw new RuntimeException" The security context is null " );
          
          String[] rolesArray = groupPrincipalCallback.getGroups();
          int sizeOfRoles = rolesArray != null ? rolesArray.length : 0;
           
          ifsizeOfRoles > 0 )
          {
            List<Role> rolesList = new ArrayList<Role>();
            forint i = 0; i < sizeOfRoles ; i++ )
            {
               Role role = new SimpleRole( rolesArrayi ] );
               rolesList.add( role );
            }
            RoleGroup roles = new SimpleRoleGroup( SecurityConstants.ROLES_IDENTIFIER, rolesList ); 
            currentSC.getUtil().setRoles( roles );   
          } 
          
          Subject subject = groupPrincipalCallback.getSubject();
          
          ifsubject != null )
          {
             currentSC.getSubjectInfo().setAuthenticatedSubject( subject );
          } 
       }
      else ifcallback instanceof CallerPrincipalCallback )
      {
         CallerPrincipalCallback callerPrincipalCallback = (CallerPrincipalCallback) callback
         SecurityContext currentSC = SecurityActions.getCurrentSecurityContext();
         
         Subject subject = callerPrincipalCallback.getSubject();
         ifcurrentSC == null )
            throw new RuntimeException" The security context is null " );
         
         ifsubject != null )
         {
            currentSC.getSubjectInfo().setAuthenticatedSubject( subject );
         } 
         
         Principal callerPrincipal = callerPrincipalCallback.getPrincipal();
         ifcallerPrincipal != null )
         {
            Identity principalBasedIdentity = IdentityFactory.getIdentity( callerPrincipalnull );
            currentSC.getSubjectInfo().addIdentity( principalBasedIdentity ); 
         } 
      }
      else ifcallback instanceof PasswordValidationCallback )
      {
         PasswordValidationCallback passwordValidationCallback = ( PasswordValidationCallback ) callback;
         SecurityContext currentSC = SecurityActions.getCurrentSecurityContext();
         ifcurrentSC == null )
            throw new RuntimeException" The security context is null " );
         String userName = passwordValidationCallback.getUsername();
         char[] password = passwordValidationCallback.getPassword();
         Subject subject = passwordValidationCallback.getSubject();
         
         SecurityContextUtil util = currentSC.getUtil();
         ifsubject != null )
         {
            util.createSubjectInfo( new SimplePrincipaluserName ), passwordsubject); 
         }  
      }
      else super.handleCallBack(callback);
   }
New to GrepCode? Check out our FAQ X