Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/net/Authenticator.java
41152 views
1
/*
2
* Copyright (c) 1997, 2021, 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 java.net;
27
28
import sun.net.www.protocol.http.AuthenticatorKeys;
29
30
/**
31
* The class Authenticator represents an object that knows how to obtain
32
* authentication for a network connection. Usually, it will do this
33
* by prompting the user for information.
34
* <p>
35
* Applications use this class by overriding {@link
36
* #getPasswordAuthentication()} in a sub-class. This method will
37
* typically use the various getXXX() accessor methods to get information
38
* about the entity requesting authentication. It must then acquire a
39
* username and password either by interacting with the user or through
40
* some other non-interactive means. The credentials are then returned
41
* as a {@link PasswordAuthentication} return value.
42
* <p>
43
* An instance of this concrete sub-class is then registered
44
* with the system by calling {@link #setDefault(Authenticator)}.
45
* When authentication is required, the system will invoke one of the
46
* requestPasswordAuthentication() methods which in turn will call the
47
* getPasswordAuthentication() method of the registered object.
48
* <p>
49
* All methods that request authentication have a default implementation
50
* that fails.
51
*
52
* @see java.net.Authenticator#setDefault(java.net.Authenticator)
53
* @see java.net.Authenticator#getPasswordAuthentication()
54
*
55
* @author Bill Foote
56
* @since 1.2
57
*/
58
59
// There are no abstract methods, but to be useful the user must
60
// subclass.
61
public abstract
62
class Authenticator {
63
64
// The system-wide authenticator object. See setDefault().
65
private static volatile Authenticator theAuthenticator;
66
67
private String requestingHost;
68
private InetAddress requestingSite;
69
private int requestingPort;
70
private String requestingProtocol;
71
private String requestingPrompt;
72
private String requestingScheme;
73
private URL requestingURL;
74
private RequestorType requestingAuthType;
75
private final String key = AuthenticatorKeys.computeKey(this);
76
77
/**
78
* Constructor for subclasses to call.
79
*/
80
public Authenticator() {}
81
82
/**
83
* The type of the entity requesting authentication.
84
*
85
* @since 1.5
86
*/
87
public enum RequestorType {
88
/**
89
* Entity requesting authentication is a HTTP proxy server.
90
*/
91
PROXY,
92
/**
93
* Entity requesting authentication is a HTTP origin server.
94
*/
95
SERVER
96
}
97
98
private void reset() {
99
requestingHost = null;
100
requestingSite = null;
101
requestingPort = -1;
102
requestingProtocol = null;
103
requestingPrompt = null;
104
requestingScheme = null;
105
requestingURL = null;
106
requestingAuthType = RequestorType.SERVER;
107
}
108
109
110
/**
111
* Sets the authenticator that will be used by the networking code
112
* when a proxy or an HTTP server asks for authentication.
113
* <p>
114
* First, if there is a security manager, its {@code checkPermission}
115
* method is called with a
116
* {@code NetPermission("setDefaultAuthenticator")} permission.
117
* This may result in a java.lang.SecurityException.
118
*
119
* @param a The authenticator to be set. If a is {@code null} then
120
* any previously set authenticator is removed.
121
*
122
* @throws SecurityException
123
* if a security manager exists and its
124
* {@code checkPermission} method doesn't allow
125
* setting the default authenticator.
126
*
127
* @see SecurityManager#checkPermission
128
* @see java.net.NetPermission
129
*/
130
public static synchronized void setDefault(Authenticator a) {
131
@SuppressWarnings("removal")
132
SecurityManager sm = System.getSecurityManager();
133
if (sm != null) {
134
NetPermission setDefaultPermission
135
= new NetPermission("setDefaultAuthenticator");
136
sm.checkPermission(setDefaultPermission);
137
}
138
139
theAuthenticator = a;
140
}
141
142
/**
143
* Gets the default authenticator.
144
* First, if there is a security manager, its {@code checkPermission}
145
* method is called with a
146
* {@code NetPermission("requestPasswordAuthentication")} permission.
147
* This may result in a java.lang.SecurityException.
148
* Then the default authenticator, if set, is returned.
149
* Otherwise, {@code null} is returned.
150
*
151
* @return The default authenticator, if set, {@code null} otherwise.
152
*
153
* @throws SecurityException
154
* if a security manager exists and its
155
* {@code checkPermission} method doesn't allow
156
* requesting password authentication.
157
* @since 9
158
* @see SecurityManager#checkPermission
159
* @see java.net.NetPermission
160
*/
161
public static Authenticator getDefault() {
162
@SuppressWarnings("removal")
163
SecurityManager sm = System.getSecurityManager();
164
if (sm != null) {
165
NetPermission requestPermission
166
= new NetPermission("requestPasswordAuthentication");
167
sm.checkPermission(requestPermission);
168
}
169
return theAuthenticator;
170
}
171
172
/**
173
* Ask the authenticator that has been registered with the system
174
* for a password.
175
* <p>
176
* First, if there is a security manager, its {@code checkPermission}
177
* method is called with a
178
* {@code NetPermission("requestPasswordAuthentication")} permission.
179
* This may result in a java.lang.SecurityException.
180
*
181
* @param addr The InetAddress of the site requesting authorization,
182
* or null if not known.
183
* @param port the port for the requested connection
184
* @param protocol The protocol that's requesting the connection
185
* ({@link java.net.Authenticator#getRequestingProtocol()})
186
* @param prompt A prompt string for the user
187
* @param scheme The authentication scheme
188
*
189
* @return The username/password, or null if one can't be gotten.
190
*
191
* @throws SecurityException
192
* if a security manager exists and its
193
* {@code checkPermission} method doesn't allow
194
* the password authentication request.
195
*
196
* @see SecurityManager#checkPermission
197
* @see java.net.NetPermission
198
*/
199
public static PasswordAuthentication requestPasswordAuthentication(
200
InetAddress addr,
201
int port,
202
String protocol,
203
String prompt,
204
String scheme) {
205
206
@SuppressWarnings("removal")
207
SecurityManager sm = System.getSecurityManager();
208
if (sm != null) {
209
NetPermission requestPermission
210
= new NetPermission("requestPasswordAuthentication");
211
sm.checkPermission(requestPermission);
212
}
213
214
Authenticator a = theAuthenticator;
215
if (a == null) {
216
return null;
217
} else {
218
synchronized(a) {
219
a.reset();
220
a.requestingSite = addr;
221
a.requestingPort = port;
222
a.requestingProtocol = protocol;
223
a.requestingPrompt = prompt;
224
a.requestingScheme = scheme;
225
return a.getPasswordAuthentication();
226
}
227
}
228
}
229
230
/**
231
* Ask the authenticator that has been registered with the system
232
* for a password. This is the preferred method for requesting a password
233
* because the hostname can be provided in cases where the InetAddress
234
* is not available.
235
* <p>
236
* First, if there is a security manager, its {@code checkPermission}
237
* method is called with a
238
* {@code NetPermission("requestPasswordAuthentication")} permission.
239
* This may result in a java.lang.SecurityException.
240
*
241
* @param host The hostname of the site requesting authentication.
242
* @param addr The InetAddress of the site requesting authentication,
243
* or null if not known.
244
* @param port the port for the requested connection.
245
* @param protocol The protocol that's requesting the connection
246
* ({@link java.net.Authenticator#getRequestingProtocol()})
247
* @param prompt A prompt string for the user which identifies the authentication realm.
248
* @param scheme The authentication scheme
249
*
250
* @return The username/password, or null if one can't be gotten.
251
*
252
* @throws SecurityException
253
* if a security manager exists and its
254
* {@code checkPermission} method doesn't allow
255
* the password authentication request.
256
*
257
* @see SecurityManager#checkPermission
258
* @see java.net.NetPermission
259
* @since 1.4
260
*/
261
public static PasswordAuthentication requestPasswordAuthentication(
262
String host,
263
InetAddress addr,
264
int port,
265
String protocol,
266
String prompt,
267
String scheme) {
268
269
@SuppressWarnings("removal")
270
SecurityManager sm = System.getSecurityManager();
271
if (sm != null) {
272
NetPermission requestPermission
273
= new NetPermission("requestPasswordAuthentication");
274
sm.checkPermission(requestPermission);
275
}
276
277
Authenticator a = theAuthenticator;
278
if (a == null) {
279
return null;
280
} else {
281
synchronized(a) {
282
a.reset();
283
a.requestingHost = host;
284
a.requestingSite = addr;
285
a.requestingPort = port;
286
a.requestingProtocol = protocol;
287
a.requestingPrompt = prompt;
288
a.requestingScheme = scheme;
289
return a.getPasswordAuthentication();
290
}
291
}
292
}
293
294
/**
295
* Ask the authenticator that has been registered with the system
296
* for a password.
297
* <p>
298
* First, if there is a security manager, its {@code checkPermission}
299
* method is called with a
300
* {@code NetPermission("requestPasswordAuthentication")} permission.
301
* This may result in a java.lang.SecurityException.
302
*
303
* @param host The hostname of the site requesting authentication.
304
* @param addr The InetAddress of the site requesting authorization,
305
* or null if not known.
306
* @param port the port for the requested connection
307
* @param protocol The protocol that's requesting the connection
308
* ({@link java.net.Authenticator#getRequestingProtocol()})
309
* @param prompt A prompt string for the user
310
* @param scheme The authentication scheme
311
* @param url The requesting URL that caused the authentication
312
* @param reqType The type (server or proxy) of the entity requesting
313
* authentication.
314
*
315
* @return The username/password, or null if one can't be gotten.
316
*
317
* @throws SecurityException
318
* if a security manager exists and its
319
* {@code checkPermission} method doesn't allow
320
* the password authentication request.
321
*
322
* @see SecurityManager#checkPermission
323
* @see java.net.NetPermission
324
*
325
* @since 1.5
326
*/
327
public static PasswordAuthentication requestPasswordAuthentication(
328
String host,
329
InetAddress addr,
330
int port,
331
String protocol,
332
String prompt,
333
String scheme,
334
URL url,
335
RequestorType reqType) {
336
337
@SuppressWarnings("removal")
338
SecurityManager sm = System.getSecurityManager();
339
if (sm != null) {
340
NetPermission requestPermission
341
= new NetPermission("requestPasswordAuthentication");
342
sm.checkPermission(requestPermission);
343
}
344
345
Authenticator a = theAuthenticator;
346
if (a == null) {
347
return null;
348
} else {
349
synchronized(a) {
350
a.reset();
351
a.requestingHost = host;
352
a.requestingSite = addr;
353
a.requestingPort = port;
354
a.requestingProtocol = protocol;
355
a.requestingPrompt = prompt;
356
a.requestingScheme = scheme;
357
a.requestingURL = url;
358
a.requestingAuthType = reqType;
359
return a.getPasswordAuthentication();
360
}
361
}
362
}
363
364
/**
365
* Ask the given {@code authenticator} for a password. If the given
366
* {@code authenticator} is null, the authenticator, if any, that has been
367
* registered with the system using {@link #setDefault(java.net.Authenticator)
368
* setDefault} is used.
369
* <p>
370
* First, if there is a security manager, its {@code checkPermission}
371
* method is called with a
372
* {@code NetPermission("requestPasswordAuthentication")} permission.
373
* This may result in a java.lang.SecurityException.
374
*
375
* @param authenticator the authenticator, or {@code null}.
376
* @param host The hostname of the site requesting authentication.
377
* @param addr The InetAddress of the site requesting authorization,
378
* or null if not known.
379
* @param port the port for the requested connection
380
* @param protocol The protocol that's requesting the connection
381
* ({@link java.net.Authenticator#getRequestingProtocol()})
382
* @param prompt A prompt string for the user
383
* @param scheme The authentication scheme
384
* @param url The requesting URL that caused the authentication
385
* @param reqType The type (server or proxy) of the entity requesting
386
* authentication.
387
*
388
* @return The username/password, or {@code null} if one can't be gotten.
389
*
390
* @throws SecurityException
391
* if a security manager exists and its
392
* {@code checkPermission} method doesn't allow
393
* the password authentication request.
394
*
395
* @see SecurityManager#checkPermission
396
* @see java.net.NetPermission
397
*
398
* @since 9
399
*/
400
public static PasswordAuthentication requestPasswordAuthentication(
401
Authenticator authenticator,
402
String host,
403
InetAddress addr,
404
int port,
405
String protocol,
406
String prompt,
407
String scheme,
408
URL url,
409
RequestorType reqType) {
410
411
@SuppressWarnings("removal")
412
SecurityManager sm = System.getSecurityManager();
413
if (sm != null) {
414
NetPermission requestPermission
415
= new NetPermission("requestPasswordAuthentication");
416
sm.checkPermission(requestPermission);
417
}
418
419
Authenticator a = authenticator == null ? theAuthenticator : authenticator;
420
if (a == null) {
421
return null;
422
} else {
423
return a.requestPasswordAuthenticationInstance(host,
424
addr,
425
port,
426
protocol,
427
prompt,
428
scheme,
429
url,
430
reqType);
431
}
432
}
433
434
/**
435
* Ask this authenticator for a password.
436
*
437
* @param host The hostname of the site requesting authentication.
438
* @param addr The InetAddress of the site requesting authorization,
439
* or null if not known.
440
* @param port the port for the requested connection
441
* @param protocol The protocol that's requesting the connection
442
* ({@link java.net.Authenticator#getRequestingProtocol()})
443
* @param prompt A prompt string for the user
444
* @param scheme The authentication scheme
445
* @param url The requesting URL that caused the authentication
446
* @param reqType The type (server or proxy) of the entity requesting
447
* authentication.
448
*
449
* @return The username/password, or null if one can't be gotten
450
*
451
* @since 9
452
*/
453
public PasswordAuthentication
454
requestPasswordAuthenticationInstance(String host,
455
InetAddress addr,
456
int port,
457
String protocol,
458
String prompt,
459
String scheme,
460
URL url,
461
RequestorType reqType) {
462
synchronized (this) {
463
this.reset();
464
this.requestingHost = host;
465
this.requestingSite = addr;
466
this.requestingPort = port;
467
this.requestingProtocol = protocol;
468
this.requestingPrompt = prompt;
469
this.requestingScheme = scheme;
470
this.requestingURL = url;
471
this.requestingAuthType = reqType;
472
return this.getPasswordAuthentication();
473
}
474
}
475
476
/**
477
* Gets the {@code hostname} of the
478
* site or proxy requesting authentication, or {@code null}
479
* if not available.
480
*
481
* @return the hostname of the connection requiring authentication, or null
482
* if it's not available.
483
* @since 1.4
484
*/
485
protected final String getRequestingHost() {
486
return requestingHost;
487
}
488
489
/**
490
* Gets the {@code InetAddress} of the
491
* site requesting authorization, or {@code null}
492
* if not available.
493
*
494
* @return the InetAddress of the site requesting authorization, or null
495
* if it's not available.
496
*/
497
protected final InetAddress getRequestingSite() {
498
return requestingSite;
499
}
500
501
/**
502
* Gets the port number for the requested connection.
503
* @return an {@code int} indicating the
504
* port for the requested connection.
505
*/
506
protected final int getRequestingPort() {
507
return requestingPort;
508
}
509
510
/**
511
* Give the protocol that's requesting the connection. Often this
512
* will be based on a URL, but in a future JDK it could be, for
513
* example, "SOCKS" for a password-protected SOCKS5 firewall.
514
*
515
* @return the protocol, optionally followed by "/version", where
516
* version is a version number.
517
*
518
* @see java.net.URL#getProtocol()
519
*/
520
protected final String getRequestingProtocol() {
521
return requestingProtocol;
522
}
523
524
/**
525
* Gets the prompt string given by the requestor.
526
*
527
* @return the prompt string given by the requestor (realm for
528
* http requests)
529
*/
530
protected final String getRequestingPrompt() {
531
return requestingPrompt;
532
}
533
534
/**
535
* Gets the scheme of the requestor (the HTTP scheme
536
* for an HTTP firewall, for example).
537
*
538
* @return the scheme of the requestor
539
*
540
*/
541
protected final String getRequestingScheme() {
542
return requestingScheme;
543
}
544
545
/**
546
* Called when password authorization is needed. Subclasses should
547
* override the default implementation, which returns null.
548
* @return The PasswordAuthentication collected from the
549
* user, or null if none is provided.
550
*/
551
protected PasswordAuthentication getPasswordAuthentication() {
552
return null;
553
}
554
555
/**
556
* Returns the URL that resulted in this
557
* request for authentication.
558
*
559
* @since 1.5
560
*
561
* @return the requesting URL
562
*
563
*/
564
protected URL getRequestingURL () {
565
return requestingURL;
566
}
567
568
/**
569
* Returns whether the requestor is a Proxy or a Server.
570
*
571
* @since 1.5
572
*
573
* @return the authentication type of the requestor
574
*
575
*/
576
protected RequestorType getRequestorType () {
577
return requestingAuthType;
578
}
579
580
static String getKey(Authenticator a) {
581
return a.key;
582
}
583
static {
584
AuthenticatorKeys.setAuthenticatorKeyAccess(Authenticator::getKey);
585
}
586
}
587
588