1 /*
   2  * Copyright (c) 1999, 2015, 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  * @since 1.4
  34  * @see javax.security.auth.callback.CallbackHandler
  35  */
  36 public class PasswordCallback implements Callback, java.io.Serializable {
  37 
  38     private static final long serialVersionUID = 2267422647454909926L;
  39 
  40     /**
  41      * @serial
  42      * @since 1.4
  43      */
  44     private String prompt;
  45     /**
  46      * @serial
  47      * @since 1.4
  48      */
  49     private boolean echoOn;
  50     /**
  51      * @serial
  52      * @since 1.4
  53      */
  54     private char[] inputPassword;
  55 
  56     /**
  57      * Construct a {@code PasswordCallback} with a prompt
  58      * and a boolean specifying whether the password should be displayed
  59      * as it is being typed.
  60      *
  61      * @param prompt the prompt used to request the password.
  62      *
  63      * @param echoOn true if the password should be displayed
  64      *                  as it is being typed.
  65      *
  66      * @exception IllegalArgumentException if {@code prompt} is null or
  67      *                  if {@code prompt} has a length of 0.
  68      */
  69     public PasswordCallback(String prompt, boolean echoOn) {
  70         if (prompt == null || prompt.isEmpty())
  71             throw new IllegalArgumentException();
  72 
  73         this.prompt = prompt;
  74         this.echoOn = echoOn;
  75     }
  76 
  77     /**
  78      * Get the prompt.
  79      *
  80      * @return the prompt.
  81      */
  82     public String getPrompt() {
  83         return prompt;
  84     }
  85 
  86     /**
  87      * Return whether the password
  88      * should be displayed as it is being typed.
  89      *
  90      * @return the whether the password
  91      *          should be displayed as it is being typed.
  92      */
  93     public boolean isEchoOn() {
  94         return echoOn;
  95     }
  96 
  97     /**
  98      * Set the retrieved password.
  99      *
 100      * <p> This method makes a copy of the input {@code password}
 101      * before storing it.
 102      *
 103      * @param password the retrieved password, which may be null.
 104      *
 105      * @see #getPassword
 106      */
 107     public void setPassword(char[] password) {
 108         this.inputPassword = (password == null ? null : password.clone());
 109     }
 110 
 111     /**
 112      * Get the retrieved password.
 113      *
 114      * <p> This method returns a copy of the retrieved password.
 115      *
 116      * @return the retrieved password, which may be null.
 117      *
 118      * @see #setPassword
 119      */
 120     public char[] getPassword() {
 121         return (inputPassword == null ? null : inputPassword.clone());
 122     }
 123 
 124     /**
 125      * Clear the retrieved password.
 126      */
 127     public void clearPassword() {
 128         if (inputPassword != null) {
 129             for (int i = 0; i < inputPassword.length; i++)
 130                 inputPassword[i] = ' ';
 131         }
 132     }
 133 }