Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/CustomLoginModule.java
38860 views
1
/*
2
* Copyright (c) 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.security.Principal;
26
import java.util.Arrays;
27
import java.util.Locale;
28
import java.util.Map;
29
import javax.security.auth.Subject;
30
import javax.security.auth.callback.Callback;
31
import javax.security.auth.callback.CallbackHandler;
32
import javax.security.auth.callback.ChoiceCallback;
33
import javax.security.auth.callback.ConfirmationCallback;
34
import javax.security.auth.callback.LanguageCallback;
35
import javax.security.auth.callback.NameCallback;
36
import javax.security.auth.callback.PasswordCallback;
37
import javax.security.auth.callback.TextInputCallback;
38
import javax.security.auth.callback.TextOutputCallback;
39
import javax.security.auth.callback.UnsupportedCallbackException;
40
import javax.security.auth.login.FailedLoginException;
41
import javax.security.auth.login.LoginException;
42
import javax.security.auth.spi.LoginModule;
43
44
public class CustomLoginModule implements LoginModule {
45
46
static final String HELLO = "Hello";
47
48
private Subject subject;
49
private CallbackHandler callbackHandler;
50
private boolean loginSucceeded = false;
51
private String username;
52
private char[] password;
53
54
/*
55
* Initialize this LoginModule.
56
*/
57
@Override
58
public void initialize(Subject subject, CallbackHandler callbackHandler,
59
Map<String, ?> sharedState, Map<String, ?> options) {
60
this.subject = subject;
61
this.callbackHandler = callbackHandler;
62
63
// check if custom parameter is passed from comfiguration
64
if (options == null) {
65
throw new RuntimeException("options is null");
66
}
67
68
// read username/password from configuration
69
Object o = options.get("username");
70
if (o == null) {
71
throw new RuntimeException("Custom parameter not passed");
72
}
73
if (!(o instanceof String)) {
74
throw new RuntimeException("Password is not a string");
75
}
76
username = (String) o;
77
78
o = options.get("password");
79
if (o == null) {
80
throw new RuntimeException("Custom parameter not passed");
81
}
82
if (!(o instanceof String)) {
83
throw new RuntimeException("Password is not a string");
84
}
85
password = ((String) o).toCharArray();
86
}
87
88
/*
89
* Authenticate the user.
90
*/
91
@Override
92
public boolean login() throws LoginException {
93
// prompt for a user name and password
94
if (callbackHandler == null) {
95
throw new LoginException("No CallbackHandler available");
96
}
97
98
// standard callbacks
99
NameCallback name = new NameCallback("username: ", "default");
100
PasswordCallback passwd = new PasswordCallback("password: ", false);
101
102
LanguageCallback language = new LanguageCallback();
103
104
TextOutputCallback error = new TextOutputCallback(
105
TextOutputCallback.ERROR, "This is an error");
106
TextOutputCallback warning = new TextOutputCallback(
107
TextOutputCallback.WARNING, "This is a warning");
108
TextOutputCallback info = new TextOutputCallback(
109
TextOutputCallback.INFORMATION, "This is a FYI");
110
111
TextInputCallback text = new TextInputCallback("Please type " + HELLO,
112
"Bye");
113
114
ChoiceCallback choice = new ChoiceCallback("Choice: ",
115
new String[] { "pass", "fail" }, 1, true);
116
117
ConfirmationCallback confirmation = new ConfirmationCallback(
118
"confirmation: ", ConfirmationCallback.INFORMATION,
119
ConfirmationCallback.YES_NO_OPTION, ConfirmationCallback.NO);
120
121
CustomCallback custom = new CustomCallback();
122
123
Callback[] callbacks = new Callback[] {
124
choice, info, warning, error, name, passwd, text, language,
125
confirmation, custom
126
};
127
128
boolean uce = false;
129
try {
130
callbackHandler.handle(callbacks);
131
} catch (UnsupportedCallbackException e) {
132
Callback callback = e.getCallback();
133
if (custom.equals(callback)) {
134
uce = true;
135
System.out.println("CustomLoginModule: "
136
+ "custom callback not supported as expected");
137
} else {
138
throw new LoginException("Unsupported callback: " + callback);
139
}
140
} catch (IOException ioe) {
141
throw new LoginException(ioe.toString());
142
}
143
144
if (!uce) {
145
throw new RuntimeException("UnsupportedCallbackException "
146
+ "not thrown");
147
}
148
149
if (!HELLO.equals(text.getText())) {
150
System.out.println("Text: " + text.getText());
151
throw new FailedLoginException("No hello");
152
}
153
154
if (!Locale.GERMANY.equals(language.getLocale())) {
155
System.out.println("Selected locale: " + language.getLocale());
156
throw new FailedLoginException("Achtung bitte");
157
}
158
159
String readUsername = name.getName();
160
char[] readPassword = passwd.getPassword();
161
if (readPassword == null) {
162
// treat a NULL password as an empty password
163
readPassword = new char[0];
164
}
165
passwd.clearPassword();
166
167
// verify the username/password
168
if (!username.equals(readUsername)
169
|| !Arrays.equals(password, readPassword)) {
170
loginSucceeded = false;
171
throw new FailedLoginException("Username/password is not correct");
172
}
173
174
// check chosen option
175
int[] selected = choice.getSelectedIndexes();
176
if (selected == null || selected.length == 0) {
177
throw new FailedLoginException("Nothing selected");
178
}
179
180
if (selected[0] != 0) {
181
throw new FailedLoginException("Wrong choice: " + selected[0]);
182
}
183
184
// check confirmation
185
if (confirmation.getSelectedIndex() != ConfirmationCallback.YES) {
186
throw new FailedLoginException("Not confirmed: "
187
+ confirmation.getSelectedIndex());
188
}
189
190
loginSucceeded = true;
191
System.out.println("CustomLoginModule: authentication succeeded");
192
return true;
193
}
194
195
/*
196
* This method is called if the LoginContext's overall authentication
197
* succeeded.
198
*/
199
@Override
200
public boolean commit() throws LoginException {
201
if (loginSucceeded) {
202
// add a Principal to the Subject
203
Principal principal = new TestPrincipal(username);
204
if (!subject.getPrincipals().contains(principal)) {
205
subject.getPrincipals().add(principal);
206
}
207
return true;
208
}
209
210
return false;
211
}
212
213
/*
214
* This method is called if the LoginContext's overall authentication
215
* failed.
216
*/
217
@Override
218
public boolean abort() throws LoginException {
219
loginSucceeded = false;
220
return true;
221
}
222
223
/*
224
* Logout the user.
225
*/
226
@Override
227
public boolean logout() throws LoginException {
228
loginSucceeded = false;
229
boolean removed = subject.getPrincipals().remove(
230
new TestPrincipal(username));
231
if (!removed) {
232
throw new LoginException("Coundn't remove a principal: "
233
+ username);
234
}
235
return true;
236
}
237
238
static class TestPrincipal implements Principal {
239
240
private final String name;
241
242
public TestPrincipal(String name) {
243
this.name = name;
244
}
245
246
@Override
247
public String getName() {
248
return name;
249
}
250
251
@Override
252
public String toString() {
253
return("TestPrincipal [name =" + name + "]");
254
}
255
256
@Override
257
public int hashCode() {
258
return name.hashCode();
259
}
260
261
@Override
262
public boolean equals(Object o) {
263
if (o == null) {
264
return false;
265
}
266
if (!(o instanceof TestPrincipal)) {
267
return false;
268
}
269
TestPrincipal other = (TestPrincipal) o;
270
return name != null ? name.equals(other.name) : other.name == null;
271
}
272
}
273
274
static class CustomCallback implements Callback {}
275
}
276
277