/** * Copyright (c) 1996-2005 Cafesoft, LLC. All Rights Reserved. * * This software is the confidential and proprietary information of * Cafesoft, LLC. ("Confidential Information"). You shall not disclose * such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Cafesoft. * * CAFESOFT MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. CAFESOFT SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ package com.cafesoft.security.engine.auth.callback; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.NameCallback; import javax.security.auth.callback.PasswordCallback; import javax.security.auth.callback.UnsupportedCallbackException; import java.io.IOException; /** * CallBack interface for JAAS LoginModules that must prompt for input. Provides * the specified username and password when prompted by the Callback. * * @version $Revision: 1.2 $ $Date: 2005/04/28 23:29:18 $ $Author: gary $ * @since 11/8/01 * @author Norbert K. Kuhnert * @author Derek G. Stainer * @author Kelly Ray Jensen */ public class NamePasswordCallbackHandler implements CallbackHandler { /** * The username to be provided when prompted. */ private String username; /** * The password to be provided when prompted. */ private String password; /** * Create a new NamePasswordCallbackHandler. */ public NamePasswordCallbackHandler() { this.username = null; this.password = null; } /** * Create a new NamePasswordCallbackHandler. * * @param username the username to provide when prompted. * @param password the password to provide when prompted. */ public NamePasswordCallbackHandler(String username, String password) { setUsername(username); setPassword(password); } /** * Set the username. * * @param username the username to be provided to a NameCallback. */ public void setUsername(String username) { if (username == null) { this.username = ""; return; } this.username = username; } /** * Set the password. * * @param password the password to be provided to a PasswordCallback. */ public void setPassword(String password) { if (password == null) { this.password = ""; return; } this.password = password; } /** * Handle Callbacks. * * @param callbacks an array of Callbacks to be handled. */ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { // Loop over all Callbacks. for (int i = 0; i < callbacks.length; i++) { Callback cb = callbacks[i]; if (cb instanceof NameCallback) { handleNameCallback((NameCallback)cb); } else if (cb instanceof PasswordCallback) { handlePasswordCallback((PasswordCallback)cb); } else { throw new UnsupportedCallbackException( cb, "Unrecognized Callback"); } } } /** * Handle a NameCallback. * * @param nc the NameCallback. */ private void handleNameCallback(NameCallback nc) { nc.setName(username); } /** * Handle a PasswordCallback. * * @param pc the PasswordCallback. */ private void handlePasswordCallback(PasswordCallback pc) { // JAAS specifies that the password is a char[] rather than a String. int length = password.length(); char[] password_chars = new char[length]; for (int i = 0; i < length; i++) { password_chars[i] = password.charAt(i); } pc.setPassword(password_chars); } } // End of class: NamePasswordCallbackHandler