1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.security.impl;
1819import javax.security.auth.callback.*;
2021/***22 * <p>PassiveCallbackHandler has constructor that takes23 * a username and password so its handle() method does24 * 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 */3435publicclassPassiveCallbackHandler implements CallbackHandler
36 {
3738private String username;
39char[] password;
4041/***42 * <p>Creates a callback handler with the give username43 * and password.</p>44 * @param username The username.45 * @param password The password.46 */47publicPassiveCallbackHandler(String username, String password)
48 {
49this.username = username;
50this.password = password.toCharArray();
51 }
5253/***54 * <p>Handles the specified set of Callbacks. Uses the55 * username and password that were supplied to our56 * constructor to popluate the Callbacks.</p>57 * <p>This class supports NameCallback and PasswordCallback.</p>58 *59 * @param callbacks the callbacks to handle60 * @throws IOException if an input or output error occurs.61 * @throws UnsupportedCallbackException if the callback is not an62 * instance of NameCallback or PasswordCallback63 */64publicvoid handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException
65 {
66for (int i = 0; i < callbacks.length; i++)
67 {
68if (callbacks[i] instanceof NameCallback)
69 {
70 ((NameCallback) callbacks[i]).setName(username);
71 }
72elseif (callbacks[i] instanceof PasswordCallback)
73 {
74 ((PasswordCallback) callbacks[i]).setPassword(password);
75 }
76else77 {
78thrownew UnsupportedCallbackException(callbacks[i], "Callback class not supported");
79 }
80 }
81 }
8283/***84 * <p>Clears out password state.</p>85 */86publicvoid clearPassword()
87 {
88if (password != null)
89 {
90for (int i = 0; i < password.length; i++)
91 {
92 password[i] = ' ';
93 }
94 password = null;
95 }
96 }
9798 }