blob: bd142fccf00627c7bd91f02df4d04cfc234cae0d [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/*
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 org.apache.harmony.javax.security.auth.callback;
19
20import java.io.Serializable;
21import java.util.Arrays;
22
23
24
25/**
26 * Is used in conjunction with a {@link CallbackHandler} to retrieve a password
27 * when needed.
28 */
29public class PasswordCallback implements Callback, Serializable {
30
31 private static final long serialVersionUID = 2267422647454909926L;
32
33 private String prompt;
34
35 boolean echoOn;
36
37 private char[] inputPassword;
38
39 private void setPrompt(String prompt) throws IllegalArgumentException {
40 if (prompt == null || prompt.length() == 0) {
41 throw new IllegalArgumentException("auth.14"); //$NON-NLS-1$
42 }
43 this.prompt = prompt;
44 }
45
46 /**
47 * Creates a new {@code PasswordCallback} instance.
48 *
49 * @param prompt
50 * the message that should be displayed to the user
51 * @param echoOn
52 * determines whether the user input should be echoed
53 */
54 public PasswordCallback(String prompt, boolean echoOn) {
55 super();
56 setPrompt(prompt);
57 this.echoOn = echoOn;
58 }
59
60 /**
61 * Returns the prompt that was specified when creating this {@code
62 * PasswordCallback}
63 *
64 * @return the prompt
65 */
66 public String getPrompt() {
67 return prompt;
68 }
69
70 /**
71 * Queries whether this {@code PasswordCallback} expects user input to be
72 * echoed, which is specified during the creation of the object.
73 *
74 * @return {@code true} if (and only if) user input should be echoed
75 */
76 public boolean isEchoOn() {
77 return echoOn;
78 }
79
80 /**
81 * Sets the password. The {@link CallbackHandler} that performs the actual
82 * provisioning or input of the password needs to call this method to hand
83 * back the password to the security service that requested it.
84 *
85 * @param password
86 * the password. A copy of this is stored, so subsequent changes
87 * to the input array do not affect the {@code PasswordCallback}.
88 */
89 public void setPassword(char[] password) {
90 if (password == null) {
91 this.inputPassword = password;
92 } else {
93 inputPassword = new char[password.length];
94 System.arraycopy(password, 0, inputPassword, 0, inputPassword.length);
95 }
96 }
97
98 /**
99 * Returns the password. The security service that needs the password
100 * usually calls this method once the {@link CallbackHandler} has finished
101 * its work.
102 *
103 * @return the password. A copy of the internal password is created and
104 * returned, so subsequent changes to the internal password do not
105 * affect the result.
106 */
107 public char[] getPassword() {
108 if (inputPassword != null) {
109 char[] tmp = new char[inputPassword.length];
110 System.arraycopy(inputPassword, 0, tmp, 0, tmp.length);
111 return tmp;
112 }
113 return null;
114 }
115
116 /**
117 * Clears the password stored in this {@code PasswordCallback}.
118 */
119 public void clearPassword() {
120 if (inputPassword != null) {
121 Arrays.fill(inputPassword, '\u0000');
122 }
123 }
124}