User: starksm 
  Date: 01/04/04 14:46:03

  Added:       src/main/org/jboss/security/auth ObjectCallback.java
                        SecurityAssociationCallback.java
                        SecurityAssociationHandler.java
                        UsernamePasswordHandler.java
  Log:
  Reorganize the packaging a bit.
  
  Revision  Changes    Path
  1.1                  jbosssx/src/main/org/jboss/security/auth/ObjectCallback.java
  
  Index: ObjectCallback.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.security.auth;
  
  import javax.security.auth.callback.Callback;
  
  /** An implementation of Callback that simply obtains an Object to be used
  as the authentication credential. Interpretation of the Object is up to
  the LoginModules that validate the credential.
  
  @author  [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class ObjectCallback implements Callback
  {
      private transient String prompt;
      private transient Object credential;
  
      /** Initialize the SecurityAssociationCallback
      */
      public ObjectCallback(String propmpt)
      {
          this.prompt = prompt;
      }
  
      public String getPrompt()
      {
          return prompt;
      }
      public Object getCredential()
      {
          return credential;
      }
      public void setCredential(Object credential)
      {
          this.credential = credential;
      }
      public void clearCredential()
      {
          this.credential = null;
      }
  }
  
  
  
  
  1.1                  
jbosssx/src/main/org/jboss/security/auth/SecurityAssociationCallback.java
  
  Index: SecurityAssociationCallback.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.security.auth;
  
  import java.security.Principal;
  import javax.security.auth.callback.Callback;
  
  
  /** An implementation of Callback useful on the server side for
  propagating the request Principal and credentials to LoginModules.
  
  @author  [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class SecurityAssociationCallback implements Callback
  {
      private transient Principal principal;
      private transient Object credential;
  
      /** Initialize the SecurityAssociationCallback
      */
      public SecurityAssociationCallback()
      {
      }
  
      public Principal getPrincipal()
      {
          return principal;
      }
      public void setPrincipal(Principal principal)
      {
          this.principal = principal;
      }
  
      public Object getCredential()
      {
          return credential;
      }
      public void setCredential(Object credential)
      {
          this.credential = credential;
      }
      public void clearCredential()
      {
          this.credential = null;
      }
  }
  
  
  
  
  1.1                  
jbosssx/src/main/org/jboss/security/auth/SecurityAssociationHandler.java
  
  Index: SecurityAssociationHandler.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.security.auth;
  
  import java.io.IOException;
  import java.lang.reflect.Method;
  import java.security.Principal;
  import javax.security.auth.callback.Callback;
  import javax.security.auth.callback.CallbackHandler;
  import javax.security.auth.callback.NameCallback;
  import javax.security.auth.callback.PasswordCallback;
  import javax.security.auth.callback.UnsupportedCallbackException;
  
  /** A simple implementation of CallbackHandler that sets a username and
  password in the handle(Callback[]) method to that passed in to
  the constructor. This is suitable for environments that need non-interactive
  JAAS logins.
  
  @see javax.security.auth.callback.CallbackHandler
  @see #handle(Callback[])
  
  @author  [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class SecurityAssociationHandler implements CallbackHandler
  {
      private transient Principal principal;
      private transient Object credential;
  
      public SecurityAssociationHandler()
      {
      }
      /** Initialize the UsernamePasswordHandler with the principal
          and credentials to use.
      */
      public SecurityAssociationHandler(Principal principal, Object credential)
      {
          this.principal = principal;
          this.credential = credential;
      }
  
      public void setSecurityInfo(Principal principal, Object credential)
      {
          this.principal = principal;
          this.credential = credential;
      }
  
      /** Handles SecurityAssociationCallback, NameCallback and
          PasswordCallback types. A NameCallback name property is set to
          the Prinicpal.getName() value. A PasswordCallback password
        property is set to the getPassword() value. The preferred
          SecurityAssociationCallback has its principal and credential
          properties set to the instance principal and credential.
  
      @see #getPassword()
      @exception UnsupportedCallbackException, thrown if any callback of
          type other than NameCallback or PasswordCallback are seen.
      */
      public void handle(Callback[] callbacks) throws
          UnsupportedCallbackException
      {
          for(int i = 0; i < callbacks.length; i++)
          {
              Callback c = callbacks[i];
              if( c instanceof SecurityAssociationCallback )
              {
                  SecurityAssociationCallback sac = (SecurityAssociationCallback) c;
                  sac.setPrincipal(principal);
                  sac.setCredential(credential);
              }
              else if ( c instanceof NameCallback)
              {
                  NameCallback nc = (NameCallback) c;
                  nc.setName(principal.getName());
              }
              else if( c instanceof PasswordCallback )
              {
                  PasswordCallback pc = (PasswordCallback) c;
                  char[] password = getPassword();
                  pc.setPassword(password);
              }
              else
              {
                  throw new UnsupportedCallbackException(c, "Unrecognized Callback");
              }
          }
      }
  
      /** Try to convert the credential value into a char[] using the
          first of the following attempts which succeeds:
  
          1. Check for instanceof char[]
          2. Check for instanceof String and then use toCharArray()
          3. See if credential has a toCharArray() method and use it
          4. Use toString() followed by toCharArray().
      @return a char[] representation of the credential.
       */
      private char[] getPassword()
      {
          char[] password = null;
          if( credential instanceof char[] )
          {
              password = (char[]) credential;
          }
          else if( credential instanceof String )
          {
              String s = (String) credential;
              password = s.toCharArray();
          }
          else
          {
              try
              {
                  Class[] types = {};
                  Method m = credential.getClass().getMethod("toCharArray", types);
                  Object[] args = {};
                  m.invoke(credential, args);
              }
              catch(Exception e)
              {
                  String s = credential.toString();
                  password = s.toCharArray();
              }
          }
          return password;
      }
  }
  
  
  
  
  1.1                  
jbosssx/src/main/org/jboss/security/auth/UsernamePasswordHandler.java
  
  Index: UsernamePasswordHandler.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.security.auth;
  
  import java.io.IOException;
  import javax.security.auth.callback.Callback;
  import javax.security.auth.callback.CallbackHandler;
  import javax.security.auth.callback.NameCallback;
  import javax.security.auth.callback.PasswordCallback;
  import javax.security.auth.callback.UnsupportedCallbackException;
  
  /** A simple implementation of CallbackHandler that sets a username and
  password in the handle(Callback[]) method to that passed in to
  the constructor. This is suitable for environments that need non-interactive
  JAAS logins.
  
  @see javax.security.auth.callback.CallbackHandler
  @see #handle(Callback[])
  
  @author  [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class UsernamePasswordHandler implements CallbackHandler
  {
      private transient String username;
      private transient char[] password;
  
      /** Initialize the UsernamePasswordHandler with the usernmae
          and password to use.
      */
      public UsernamePasswordHandler(String username, char[] password)
      {
          this.username = username;
          this.password = password;
      }
  
      /** Sets any NameCallback name property to the instance username
        and sets any PasswordCallback password property to the instance
        password.
      @exception UnsupportedCallbackException, thrown if any callback of
          type other than NameCallback or PasswordCallback are seen.
      */
      public void handle(Callback[] callbacks) throws
          UnsupportedCallbackException
      {
          for(int i = 0; i < callbacks.length; i++)
          {
              if (callbacks[i] instanceof NameCallback)
              {
                  NameCallback nc = (NameCallback) callbacks[i];
                  nc.setName(username);
              }
              else if(callbacks[i] instanceof PasswordCallback)
              {
                  PasswordCallback pc = (PasswordCallback) callbacks[i];
                  pc.setPassword(password);
              }
              else
              {
                  throw new UnsupportedCallbackException(callbacks[i], "Unrecognized 
Callback");
              }
          }
      }
  }
  
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to