package oracle.sample.jaastester;

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;

/*
 * @(#)PassiveCallbackHandler.java  1.0 March 2005
 *
 * Copyright (c) 2001-2005 by Oracle. All Rights Reserved.
 * 
 * Oracle grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Oracle.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ORACLE AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL ORACLE OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF ORACLE HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 * 
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

/**
 * Passive callback handler that obtains the username and password from a string
 * and array of char passed in as an argument to the constructor. 
 * 
 * <p> An application implements a <code>CallbackHandler</code> and passes
 * it to underlying security services so that they may interact with
 * the application to retrieve specific authentication data,
 * such as usernames and passwords, or to display certain information,
 * such as error and warning messages.
 * 
 * <p> CallbackHandlers are implemented in an application-dependent fashion.
 * For example, implementations for an application with a graphical user 
 * interface (GUI) may pop up windows to prompt for requested information
 * or to display error messages.  An implementation may also choose to obtain
 * requested information from an alternate source without asking the end user.
 *
 * <p> Underlying security services make requests for different types
 * of information by passing individual Callbacks to the
 * <code>CallbackHandler</code>.  The <code>CallbackHandler</code>
 * implementation decides how to retrieve and display information
 * depending on the Callbacks passed to it.  For example,
 * if the underlying service needs a username and password to
 * authenticate a user, it uses a <code>NameCallback</code> and
 * <code>PasswordCallback</code>.  The <code>CallbackHandler</code>
 * can then choose to prompt for a username and password serially,
 * or to prompt for both in a single window.
 * 
 * @author Frank Nimphius
 * @version 1.0
 */
public class PassiveCallbackHandler implements CallbackHandler
{
  private String _username;
  private char[] _password;
  
  public PassiveCallbackHandler(String username, String password)
  {
    _username=username;
    _password=password.toCharArray();
  }
  
  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
  {
    
    for (int i = 0; i < callbacks.length; i++) 
    {
      
      // evaluate the type of callback. This callback handler only works with username and
      // password callbacks
      
      if (callbacks[i] instanceof NameCallback) 
      {
        // handle name callback
        ((NameCallback)callbacks[i]).setName(_username);
        
      } else if(callbacks[i] instanceof PasswordCallback)
      {
        ((PasswordCallback)callbacks[i]).setPassword(_password);
      }
      else
      {
        throw new UnsupportedCallbackException(callbacks[i], callbacks[i].getClass()+" callback not supported in PassiveCallback Handler");
      }
      
    }
    
  
  
    //clean password memory space
    for (int i = 0; i < _password.length; i++) 
    {
      _password[i] = ' ';
    }
    
    // free memory space
    _password=null;
        
  }
}