1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.security.impl;
18
19 import javax.security.auth.callback.*;
20
21 /***
22 * <p>PassiveCallbackHandler has constructor that takes
23 * a username and password so its handle() method does
24 * not have to prompt the user for input.</p>
25 * <p>Useful for server-side applications.</p>
26 *
27 * <p>This code was inspired from an article from:<p>
28 * <ul>
29 * <li><a href="http://www.javaworld.com/javaworld/jw-09-2002/jw-0913-jaas.html">
30 * All that JAAS</a></li>
31 * </ul> *
32 * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
33 */
34
35 public class PassiveCallbackHandler implements CallbackHandler
36 {
37
38 private String username;
39 char[] password;
40
41 /***
42 * <p>Creates a callback handler with the give username
43 * and password.</p>
44 * @param username The username.
45 * @param password The password.
46 */
47 public PassiveCallbackHandler(String username, String password)
48 {
49 this.username = username;
50 this.password = password.toCharArray();
51 }
52
53 /***
54 * <p>Handles the specified set of Callbacks. Uses the
55 * username and password that were supplied to our
56 * constructor to popluate the Callbacks.</p>
57 * <p>This class supports NameCallback and PasswordCallback.</p>
58 *
59 * @param callbacks the callbacks to handle
60 * @throws IOException if an input or output error occurs.
61 * @throws UnsupportedCallbackException if the callback is not an
62 * instance of NameCallback or PasswordCallback
63 */
64 public void handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException
65 {
66 for (int i = 0; i < callbacks.length; i++)
67 {
68 if (callbacks[i] instanceof NameCallback)
69 {
70 ((NameCallback) callbacks[i]).setName(username);
71 }
72 else if (callbacks[i] instanceof PasswordCallback)
73 {
74 ((PasswordCallback) callbacks[i]).setPassword(password);
75 }
76 else
77 {
78 throw new UnsupportedCallbackException(callbacks[i], "Callback class not supported");
79 }
80 }
81 }
82
83 /***
84 * <p>Clears out password state.</p>
85 */
86 public void clearPassword()
87 {
88 if (password != null)
89 {
90 for (int i = 0; i < password.length; i++)
91 {
92 password[i] = ' ';
93 }
94 password = null;
95 }
96 }
97
98 }