PasswordCallback.java revision 2313047d797e4daece04da8e8ed406d26b589f82
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  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 and
15 *  limitations under the License.
16 */
17
18package javax.security.auth.callback;
19
20import java.io.Serializable;
21import java.util.Arrays;
22
23/**
24 * Used in conjunction with a {@link CallbackHandler} to retrieve a password
25 * when needed.
26 */
27public class PasswordCallback implements Callback, Serializable {
28
29    private static final long serialVersionUID = 2267422647454909926L;
30
31    private String prompt;
32
33    boolean echoOn;
34
35    private char[] inputPassword;
36
37    private void setPrompt(String prompt) throws IllegalArgumentException {
38        if (prompt == null || prompt.length() == 0) {
39            throw new IllegalArgumentException("Invalid prompt");
40        }
41        this.prompt = prompt;
42    }
43
44    /**
45     * Creates a new {@code PasswordCallback} instance.
46     *
47     * @param prompt
48     *            the message that should be displayed to the user
49     * @param echoOn
50     *            determines whether the user input should be echoed
51     */
52    public PasswordCallback(String prompt, boolean echoOn) {
53        super();
54        setPrompt(prompt);
55        this.echoOn = echoOn;
56    }
57
58    /**
59     * Returns the prompt that was specified when creating this {@code
60     * PasswordCallback}
61     *
62     * @return the prompt
63     */
64    public String getPrompt() {
65        return prompt;
66    }
67
68    /**
69     * Queries whether this {@code PasswordCallback} expects user input to be
70     * echoed, which is specified during the creation of the object.
71     *
72     * @return {@code true} if (and only if) user input should be echoed
73     */
74    public boolean isEchoOn() {
75        return echoOn;
76    }
77
78    /**
79     * Sets the password. The {@link CallbackHandler} that performs the actual
80     * provisioning or input of the password needs to call this method to hand
81     * back the password to the security service that requested it.
82     *
83     * @param password
84     *            the password. A copy of this is stored, so subsequent changes
85     *            to the input array do not affect the {@code PasswordCallback}.
86     */
87    public void setPassword(char[] password) {
88        if (password == null) {
89            this.inputPassword = password;
90        } else {
91            inputPassword = new char[password.length];
92            System.arraycopy(password, 0, inputPassword, 0, inputPassword.length);
93        }
94    }
95
96    /**
97     * Returns the password. The security service that needs the password
98     * usually calls this method once the {@link CallbackHandler} has finished
99     * its work.
100     *
101     * @return the password. A copy of the internal password is created and
102     *         returned, so subsequent changes to the internal password do not
103     *         affect the result.
104     */
105    public char[] getPassword() {
106        if (inputPassword != null) {
107            char[] tmp = new char[inputPassword.length];
108            System.arraycopy(inputPassword, 0, tmp, 0, tmp.length);
109            return tmp;
110        }
111        return null;
112    }
113
114    /**
115     * Clears the password stored in this {@code PasswordCallback}.
116     */
117    public void clearPassword() {
118        if (inputPassword != null) {
119            Arrays.fill(inputPassword, '\u0000');
120        }
121    }
122}
123