Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/util/ConsoleCallbackHandler.java
41159 views
1
/*
2
* Copyright (c) 2014, 2020, 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 sun.security.util;
27
28
import javax.security.auth.callback.Callback;
29
import javax.security.auth.callback.CallbackHandler;
30
import javax.security.auth.callback.ConfirmationCallback;
31
import javax.security.auth.callback.NameCallback;
32
import javax.security.auth.callback.PasswordCallback;
33
import javax.security.auth.callback.TextOutputCallback;
34
import javax.security.auth.callback.UnsupportedCallbackException;
35
36
import java.io.BufferedReader;
37
import java.io.IOException;
38
import java.io.InputStreamReader;
39
40
/**
41
* A {@code CallbackHandler} that prompts and reads from the command line
42
* for answers to authentication questions.
43
*/
44
public class ConsoleCallbackHandler implements CallbackHandler {
45
46
/**
47
* Creates a callback handler that prompts and reads from the
48
* command line for answers to authentication questions.
49
*/
50
public ConsoleCallbackHandler() { }
51
52
/**
53
* Handles the specified set of callbacks.
54
*
55
* @param callbacks the callbacks to handle
56
* @throws IOException if an input or output error occurs.
57
* @throws UnsupportedCallbackException if the callback is not an
58
* instance of NameCallback or PasswordCallback
59
*/
60
public void handle(Callback[] callbacks)
61
throws IOException, UnsupportedCallbackException
62
{
63
ConfirmationCallback confirmation = null;
64
65
for (int i = 0; i < callbacks.length; i++) {
66
if (callbacks[i] instanceof TextOutputCallback) {
67
TextOutputCallback tc = (TextOutputCallback) callbacks[i];
68
69
String text;
70
switch (tc.getMessageType()) {
71
case TextOutputCallback.INFORMATION:
72
text = "";
73
break;
74
case TextOutputCallback.WARNING:
75
text = "Warning: ";
76
break;
77
case TextOutputCallback.ERROR:
78
text = "Error: ";
79
break;
80
default:
81
throw new UnsupportedCallbackException(
82
callbacks[i], "Unrecognized message type");
83
}
84
85
String message = tc.getMessage();
86
if (message != null) {
87
text += message;
88
}
89
if (text != null) {
90
System.err.println(text);
91
}
92
93
} else if (callbacks[i] instanceof NameCallback) {
94
NameCallback nc = (NameCallback) callbacks[i];
95
96
if (nc.getDefaultName() == null) {
97
System.err.print(nc.getPrompt());
98
} else {
99
System.err.print(nc.getPrompt() +
100
" [" + nc.getDefaultName() + "] ");
101
}
102
System.err.flush();
103
104
String result = readLine();
105
if (result.isEmpty()) {
106
result = nc.getDefaultName();
107
}
108
109
nc.setName(result);
110
111
} else if (callbacks[i] instanceof PasswordCallback) {
112
PasswordCallback pc = (PasswordCallback) callbacks[i];
113
114
System.err.print(pc.getPrompt());
115
System.err.flush();
116
117
pc.setPassword(Password.readPassword(System.in, pc.isEchoOn()));
118
119
} else if (callbacks[i] instanceof ConfirmationCallback) {
120
confirmation = (ConfirmationCallback) callbacks[i];
121
122
} else {
123
throw new UnsupportedCallbackException(
124
callbacks[i], "Unrecognized Callback");
125
}
126
}
127
128
/* Do the confirmation callback last. */
129
if (confirmation != null) {
130
doConfirmation(confirmation);
131
}
132
}
133
134
/* Reads a line of input */
135
private String readLine() throws IOException {
136
String result = new BufferedReader
137
(new InputStreamReader(System.in)).readLine();
138
if (result == null) {
139
throw new IOException("Cannot read from System.in");
140
}
141
return result;
142
}
143
144
private void doConfirmation(ConfirmationCallback confirmation)
145
throws IOException, UnsupportedCallbackException
146
{
147
String prefix;
148
int messageType = confirmation.getMessageType();
149
switch (messageType) {
150
case ConfirmationCallback.WARNING:
151
prefix = "Warning: ";
152
break;
153
case ConfirmationCallback.ERROR:
154
prefix = "Error: ";
155
break;
156
case ConfirmationCallback.INFORMATION:
157
prefix = "";
158
break;
159
default:
160
throw new UnsupportedCallbackException(
161
confirmation, "Unrecognized message type: " + messageType);
162
}
163
164
class OptionInfo {
165
String name;
166
int value;
167
OptionInfo(String name, int value) {
168
this.name = name;
169
this.value = value;
170
}
171
}
172
173
OptionInfo[] options;
174
int optionType = confirmation.getOptionType();
175
switch (optionType) {
176
case ConfirmationCallback.YES_NO_OPTION:
177
options = new OptionInfo[] {
178
new OptionInfo("Yes", ConfirmationCallback.YES),
179
new OptionInfo("No", ConfirmationCallback.NO)
180
};
181
break;
182
case ConfirmationCallback.YES_NO_CANCEL_OPTION:
183
options = new OptionInfo[] {
184
new OptionInfo("Yes", ConfirmationCallback.YES),
185
new OptionInfo("No", ConfirmationCallback.NO),
186
new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
187
};
188
break;
189
case ConfirmationCallback.OK_CANCEL_OPTION:
190
options = new OptionInfo[] {
191
new OptionInfo("OK", ConfirmationCallback.OK),
192
new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
193
};
194
break;
195
case ConfirmationCallback.UNSPECIFIED_OPTION:
196
String[] optionStrings = confirmation.getOptions();
197
options = new OptionInfo[optionStrings.length];
198
for (int i = 0; i < options.length; i++) {
199
options[i] = new OptionInfo(optionStrings[i], i);
200
}
201
break;
202
default:
203
throw new UnsupportedCallbackException(
204
confirmation, "Unrecognized option type: " + optionType);
205
}
206
207
int defaultOption = confirmation.getDefaultOption();
208
209
String prompt = confirmation.getPrompt();
210
if (prompt == null) {
211
prompt = "";
212
}
213
prompt = prefix + prompt;
214
if (!prompt.isEmpty()) {
215
System.err.println(prompt);
216
}
217
218
for (int i = 0; i < options.length; i++) {
219
if (optionType == ConfirmationCallback.UNSPECIFIED_OPTION) {
220
// defaultOption is an index into the options array
221
System.err.println(
222
i + ". " + options[i].name +
223
(i == defaultOption ? " [default]" : ""));
224
} else {
225
// defaultOption is an option value
226
System.err.println(
227
i + ". " + options[i].name +
228
(options[i].value == defaultOption ? " [default]" : ""));
229
}
230
}
231
System.err.print("Enter a number: ");
232
System.err.flush();
233
int result;
234
try {
235
result = Integer.parseInt(readLine());
236
if (result < 0 || result > (options.length - 1)) {
237
result = defaultOption;
238
} else {
239
result = options[result].value;
240
}
241
} catch (NumberFormatException e) {
242
result = defaultOption;
243
}
244
245
confirmation.setSelectedIndex(result);
246
}
247
}
248
249