View Javadoc

1   package no.feide.mellon.jaas.callbackhandlers;
2   
3   import java.io.*;
4   
5   import javax.security.auth.callback.*;
6   
7   /***
8    * @author Rikke Amilde Løvlid
9    *
10   */
11  public class PassiveCallbackHandler implements CallbackHandler {
12  
13      private String username;
14      char[] password;
15  
16      /***
17       * Creates a callback handler with the given username and password.
18       */
19      public PassiveCallbackHandler(String username, String password) {
20          this.username = username;
21          this.password = password.toCharArray();
22      }
23  
24      /***
25       * Handles the specified set of Callbacks. Uses the
26       * username and password that were given to the constructor
27       *
28       * This class supports NameCallback and PasswordCallback.
29       *
30       * @param  	callbacks the callbacks to handle
31       * @throws  IOException if an input or output error occurs.
32       * @throws  UnsupportedCallbackException if the callback is not an
33       * 			instance of NameCallback or PasswordCallback
34       */
35      public void handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException
36      {
37          for (int i = 0; i < callbacks.length; i++) {
38              if (callbacks[i] instanceof NameCallback) {
39                  ((NameCallback)callbacks[i]).setName(username);
40              } 
41              else if (callbacks[i] instanceof PasswordCallback) {
42                  ((PasswordCallback)callbacks[i]).setPassword(password);
43              } 
44              else {
45                  throw new UnsupportedCallbackException(
46                              callbacks[i], "Callback class not supported");
47              }
48          }
49      }
50  
51      /***
52       * Clears out the password state.
53       */
54      public void clearPassword() {
55          if (password != null) {
56              for (int i = 0; i < password.length; i++)
57                  password[i] = ' ';
58              password = null;
59          }
60      }
61  
62  }
63