KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > activedirectory > UserPasswordCallbackHandler


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.activedirectory;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.security.auth.callback.Callback JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.callback.NameCallback JavaDoc;
27 import javax.security.auth.callback.PasswordCallback JavaDoc;
28 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Implementation of {@link javax.security.auth.callback.CallbackHandler} that
35  * takes a <i>User name</i> and <i>Password</i>. The only known use of this
36  * class is in the
37  * {@link com.sslexplorer.activedirectory.ActiveDirectoryUserDatabase}.
38  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
39  */

40 public class UserPasswordCallbackHandler implements CallbackHandler JavaDoc {
41     private static final Log log = LogFactory.getLog(UserPasswordCallbackHandler.class);
42     private String JavaDoc username;
43     private char[] password;
44
45     /* (non-Javadoc)
46      * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
47      */

48     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
49         for (int i = 0; i < callbacks.length; i++) {
50             if (callbacks[i] instanceof NameCallback JavaDoc) {
51                 if (log.isDebugEnabled()) {
52                     log.debug("Handling username callback");
53                 }
54                 NameCallback JavaDoc cb = (NameCallback JavaDoc) callbacks[i];
55                 cb.setName(username);
56
57             } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
58                 if (log.isDebugEnabled()) {
59                     log.debug("Handling password callback");
60                 }
61                 PasswordCallback JavaDoc cb = (PasswordCallback JavaDoc) callbacks[i];
62                 cb.setPassword(password);
63
64             } else {
65                 throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
66             }
67         }
68     }
69
70     /**
71      * Set the user name
72      * @param username user name
73      */

74     public void setUserId(String JavaDoc username) {
75         this.username = username;
76     }
77
78     /**
79      * Set the password
80      * @param password password
81      */

82     public void setPassword(String JavaDoc password) {
83         this.password = password.toCharArray();
84     }
85 }
Popular Tags