1 /* 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.security.auth.callback; 27 28 /** 29 * <p> Underlying security services instantiate and pass a 30 * {@code PasswordCallback} to the {@code handle} 31 * method of a {@code CallbackHandler} to retrieve password information. 32 * 33 * @see javax.security.auth.callback.CallbackHandler 34 */ 35 public class PasswordCallback implements Callback, java.io.Serializable { 36 37 private static final long serialVersionUID = 2267422647454909926L; 38 39 /** 40 * @serial 41 * @since 1.4 42 */ 43 private String prompt; 44 /** 45 * @serial 46 * @since 1.4 47 */ 48 private boolean echoOn; 49 /** 50 * @serial 51 * @since 1.4 52 */ 53 private char[] inputPassword; 54 55 /** 56 * Construct a {@code PasswordCallback} with a prompt 57 * and a boolean specifying whether the password should be displayed 58 * as it is being typed. 59 * 60 * <p> 61 * 62 * @param prompt the prompt used to request the password. <p> 63 * 64 * @param echoOn true if the password should be displayed 65 * as it is being typed. 66 * 67 * @exception IllegalArgumentException if {@code prompt} is null or 68 * if {@code prompt} has a length of 0. 69 */ PasswordCallback(String prompt, boolean echoOn)70 public PasswordCallback(String prompt, boolean echoOn) { 71 if (prompt == null || prompt.length() == 0) 72 throw new IllegalArgumentException(); 73 74 this.prompt = prompt; 75 this.echoOn = echoOn; 76 } 77 78 /** 79 * Get the prompt. 80 * 81 * <p> 82 * 83 * @return the prompt. 84 */ getPrompt()85 public String getPrompt() { 86 return prompt; 87 } 88 89 /** 90 * Return whether the password 91 * should be displayed as it is being typed. 92 * 93 * <p> 94 * 95 * @return the whether the password 96 * should be displayed as it is being typed. 97 */ isEchoOn()98 public boolean isEchoOn() { 99 return echoOn; 100 } 101 102 /** 103 * Set the retrieved password. 104 * 105 * <p> This method makes a copy of the input <i>password</i> 106 * before storing it. 107 * 108 * <p> 109 * 110 * @param password the retrieved password, which may be null. 111 * 112 * @see #getPassword 113 */ setPassword(char[] password)114 public void setPassword(char[] password) { 115 this.inputPassword = (password == null ? null : password.clone()); 116 } 117 118 /** 119 * Get the retrieved password. 120 * 121 * <p> This method returns a copy of the retrieved password. 122 * 123 * <p> 124 * 125 * @return the retrieved password, which may be null. 126 * 127 * @see #setPassword 128 */ getPassword()129 public char[] getPassword() { 130 return (inputPassword == null ? null : inputPassword.clone()); 131 } 132 133 /** 134 * Clear the retrieved password. 135 */ clearPassword()136 public void clearPassword() { 137 if (inputPassword != null) { 138 for (int i = 0; i < inputPassword.length; i++) 139 inputPassword[i] = ' '; 140 } 141 } 142 } 143