author | weijun |
Tue, 29 Oct 2019 09:34:21 +0800 | |
changeset 58830 | fa0b9f9c597a |
parent 57838 | 78844dceede6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
20742
diff
changeset
|
2 |
* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.net.httpserver; |
|
27 |
||
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
28 |
import java.nio.charset.Charset; |
15264
3baf7f9d320d
8005406: HTTP server implementation should use Base64 API
chegar
parents:
10596
diff
changeset
|
29 |
import java.util.Base64; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
30 |
import java.util.Objects; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
31 |
|
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
32 |
import static java.nio.charset.StandardCharsets.UTF_8; |
15264
3baf7f9d320d
8005406: HTTP server implementation should use Base64 API
chegar
parents:
10596
diff
changeset
|
33 |
|
2 | 34 |
/** |
35 |
* BasicAuthenticator provides an implementation of HTTP Basic |
|
36 |
* authentication. It is an abstract class and must be extended |
|
37 |
* to provide an implementation of {@link #checkCredentials(String,String)} |
|
38 |
* which is called to verify each incoming request. |
|
39 |
*/ |
|
40 |
public abstract class BasicAuthenticator extends Authenticator { |
|
41 |
||
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
42 |
protected final String realm; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
43 |
protected final Charset charset; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
44 |
private final boolean isUTF8; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
45 |
|
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
46 |
/** |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
47 |
* Creates a BasicAuthenticator for the given HTTP realm. |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
48 |
* The Basic authentication credentials (username and password) are decoded |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
49 |
* using the platform's {@link Charset#defaultCharset() default character set}. |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
50 |
* |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
51 |
* @param realm The HTTP Basic authentication realm |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
52 |
* @throws NullPointerException if realm is {@code null} |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
53 |
* @throws IllegalArgumentException if realm is an empty string |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
54 |
*/ |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
55 |
public BasicAuthenticator (String realm) { |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
56 |
this(realm, Charset.defaultCharset()); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
57 |
} |
2 | 58 |
|
59 |
/** |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
60 |
* Creates a BasicAuthenticator for the given HTTP realm and using the |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
61 |
* given {@link Charset} to decode the Basic authentication credentials |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
62 |
* (username and password). |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
63 |
* |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
64 |
* @apiNote {@code UTF-8} is the recommended charset because its usage is |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
65 |
* communicated to the client, and therefore more likely to be used also |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
66 |
* by the client. |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
67 |
* |
2 | 68 |
* @param realm The HTTP Basic authentication realm |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
69 |
* @param charset The Charset to decode incoming credentials from the client |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
70 |
* @throws NullPointerException if realm or charset are {@code null} |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
71 |
* @throws IllegalArgumentException if realm is an empty string |
2 | 72 |
*/ |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
73 |
public BasicAuthenticator (String realm, Charset charset) { |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
74 |
Objects.requireNonNull(charset); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
75 |
if (realm.isEmpty()) // implicit NPE check |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
76 |
throw new IllegalArgumentException("realm must not be empty"); |
2 | 77 |
this.realm = realm; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
78 |
this.charset = charset; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
79 |
this.isUTF8 = charset.equals(UTF_8); |
2 | 80 |
} |
81 |
||
82 |
/** |
|
83 |
* returns the realm this BasicAuthenticator was created with |
|
84 |
* @return the authenticator's realm string. |
|
85 |
*/ |
|
86 |
public String getRealm () { |
|
87 |
return realm; |
|
88 |
} |
|
89 |
||
90 |
public Result authenticate (HttpExchange t) |
|
91 |
{ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
7668
diff
changeset
|
92 |
Headers rmap = t.getRequestHeaders(); |
2 | 93 |
/* |
94 |
* look for auth token |
|
95 |
*/ |
|
96 |
String auth = rmap.getFirst ("Authorization"); |
|
97 |
if (auth == null) { |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
7668
diff
changeset
|
98 |
Headers map = t.getResponseHeaders(); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
99 |
var authString = "Basic realm=" + "\"" + realm + "\"" + |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
100 |
(isUTF8 ? " charset=\"UTF-8\"" : ""); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
101 |
map.set ("WWW-Authenticate", authString); |
2 | 102 |
return new Authenticator.Retry (401); |
103 |
} |
|
104 |
int sp = auth.indexOf (' '); |
|
105 |
if (sp == -1 || !auth.substring(0, sp).equals ("Basic")) { |
|
106 |
return new Authenticator.Failure (401); |
|
107 |
} |
|
15264
3baf7f9d320d
8005406: HTTP server implementation should use Base64 API
chegar
parents:
10596
diff
changeset
|
108 |
byte[] b = Base64.getDecoder().decode(auth.substring(sp+1)); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
109 |
String userpass = new String (b, charset); |
2 | 110 |
int colon = userpass.indexOf (':'); |
111 |
String uname = userpass.substring (0, colon); |
|
112 |
String pass = userpass.substring (colon+1); |
|
113 |
||
114 |
if (checkCredentials (uname, pass)) { |
|
115 |
return new Authenticator.Success ( |
|
116 |
new HttpPrincipal ( |
|
117 |
uname, realm |
|
118 |
) |
|
119 |
); |
|
120 |
} else { |
|
121 |
/* reject the request again with 401 */ |
|
122 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
7668
diff
changeset
|
123 |
Headers map = t.getResponseHeaders(); |
2 | 124 |
map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\""); |
125 |
return new Authenticator.Failure(401); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* called for each incoming request to verify the |
|
131 |
* given name and password in the context of this |
|
132 |
* Authenticator's realm. Any caching of credentials |
|
133 |
* must be done by the implementation of this method |
|
134 |
* @param username the username from the request |
|
135 |
* @param password the password from the request |
|
136 |
* @return <code>true</code> if the credentials are valid, |
|
137 |
* <code>false</code> otherwise. |
|
138 |
*/ |
|
139 |
public abstract boolean checkCredentials (String username, String password); |
|
140 |
} |
|
141 |