View Javadoc
1   /**
2    * Originally contributed by eMation (www.emation.pt)
3    */
4   package org.itracker.services.authentication.adsson;
5   
6   import javax.security.auth.callback.*;
7   
8   /**
9    * Callback class for Active Directory authentication Gets username and password
10   * from class constructor
11   *
12   * @author ricardo
13   */
14  public class SimpleCallbackHandler implements CallbackHandler {
15  
16      private String username;
17  
18      private String password;
19  
20      public SimpleCallbackHandler(String username, String password) {
21          super();
22          this.username = username;
23          this.password = password;
24      }
25  
26      public void handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException {
27  
28          for (int i = 0; i < callbacks.length; i++) {
29              if (callbacks[i] instanceof NameCallback) {
30                  NameCallback cb = (NameCallback) callbacks[i];
31                  cb.setName(username);
32              } else if (callbacks[i] instanceof PasswordCallback) {
33                  PasswordCallback cb = (PasswordCallback) callbacks[i];
34                  cb.setPassword(password.toCharArray());
35              } else {
36                  throw new UnsupportedCallbackException(callbacks[i]);
37              }
38          }
39  
40      }
41  }