author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57956 | e0b8b019d2f5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
57838
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, 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 sun.net.www.protocol.http; |
|
27 |
||
28 |
import java.net.URL; |
|
29 |
import java.net.URI; |
|
30 |
import java.net.URISyntaxException; |
|
31 |
import java.net.PasswordAuthentication; |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
33 |
import java.nio.CharBuffer; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
34 |
import java.nio.charset.Charset; |
5464
8c4deb1b4a45
6947917: Error in basic authentication when user name and password are long
chegar
parents:
4157
diff
changeset
|
35 |
import java.io.IOException; |
8c4deb1b4a45
6947917: Error in basic authentication when user name and password are long
chegar
parents:
4157
diff
changeset
|
36 |
import java.io.OutputStream; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
37 |
import java.util.Arrays; |
15258
dd5001103120
8006153: HTTP protocol handler authenication should use Base64 API
chegar
parents:
7668
diff
changeset
|
38 |
import java.util.Base64; |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
39 |
import java.util.Objects; |
2 | 40 |
import sun.net.www.HeaderParser; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
41 |
import static java.nio.charset.StandardCharsets.UTF_8; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
42 |
import static java.nio.charset.StandardCharsets.ISO_8859_1; |
2 | 43 |
|
44 |
/** |
|
45 |
* BasicAuthentication: Encapsulate an http server authentication using |
|
46 |
* the "basic" scheme. |
|
47 |
* |
|
48 |
* @author Bill Foote |
|
49 |
*/ |
|
50 |
||
51 |
||
52 |
class BasicAuthentication extends AuthenticationInfo { |
|
53 |
||
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
57838
diff
changeset
|
54 |
@java.io.Serial |
2 | 55 |
private static final long serialVersionUID = 100L; |
56 |
||
57 |
/** The authentication string for this host, port, and realm. This is |
|
58 |
a simple BASE64 encoding of "login:password". */ |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
59 |
final String auth; |
2 | 60 |
|
61 |
/** |
|
62 |
* Create a BasicAuthentication |
|
63 |
*/ |
|
64 |
public BasicAuthentication(boolean isProxy, String host, int port, |
|
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
65 |
String realm, PasswordAuthentication pw, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
66 |
boolean isUTF8, String authenticatorKey) { |
2 | 67 |
super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
68 |
AuthScheme.BASIC, host, port, realm, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
69 |
Objects.requireNonNull(authenticatorKey)); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
70 |
this.auth = authValueFrom(pw, isUTF8); |
2 | 71 |
this.pw = pw; |
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Create a BasicAuthentication |
|
76 |
*/ |
|
77 |
public BasicAuthentication(boolean isProxy, String host, int port, |
|
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
78 |
String realm, String auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
79 |
String authenticatorKey) { |
2 | 80 |
super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
81 |
AuthScheme.BASIC, host, port, realm, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
82 |
Objects.requireNonNull(authenticatorKey)); |
2 | 83 |
this.auth = "Basic " + auth; |
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Create a BasicAuthentication |
|
88 |
*/ |
|
89 |
public BasicAuthentication(boolean isProxy, URL url, String realm, |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
90 |
PasswordAuthentication pw, boolean isUTF8, |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
91 |
String authenticatorKey) { |
2 | 92 |
super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
93 |
AuthScheme.BASIC, url, realm, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
94 |
Objects.requireNonNull(authenticatorKey)); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
95 |
this.auth = authValueFrom(pw, isUTF8); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
96 |
this.pw = pw; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
97 |
} |
2 | 98 |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
99 |
private static String authValueFrom(PasswordAuthentication pw, boolean isUTF8) { |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
100 |
String plain = pw.getUserName() + ":"; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
101 |
char[] password = pw.getPassword(); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
102 |
CharBuffer cbuf = CharBuffer.allocate(plain.length() + password.length); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
103 |
cbuf.put(plain).put(password).flip(); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
104 |
Charset charset = isUTF8 ? UTF_8 : ISO_8859_1; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
105 |
ByteBuffer buf = charset.encode(cbuf); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
106 |
ByteBuffer enc = Base64.getEncoder().encode(buf); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
107 |
String ret = "Basic " + new String(enc.array(), enc.position(), enc.remaining(), ISO_8859_1); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
108 |
Arrays.fill(buf.array(), (byte) 0); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
109 |
Arrays.fill(enc.array(), (byte) 0); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
110 |
Arrays.fill(cbuf.array(), (char) 0); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
47216
diff
changeset
|
111 |
return ret; |
2 | 112 |
} |
113 |
||
114 |
/** |
|
115 |
* Create a BasicAuthentication |
|
116 |
*/ |
|
117 |
public BasicAuthentication(boolean isProxy, URL url, String realm, |
|
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
118 |
String auth, String authenticatorKey) { |
2 | 119 |
super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
120 |
AuthScheme.BASIC, url, realm, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
25859
diff
changeset
|
121 |
Objects.requireNonNull(authenticatorKey)); |
2 | 122 |
this.auth = "Basic " + auth; |
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* @return true if this authentication supports preemptive authorization |
|
127 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
128 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
129 |
public boolean supportsPreemptiveAuthorization() { |
2 | 130 |
return true; |
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Set header(s) on the given connection. This will only be called for |
|
135 |
* definitive (i.e. non-preemptive) authorization. |
|
136 |
* @param conn The connection to apply the header(s) to |
|
137 |
* @param p A source of header values for this connection, if needed. |
|
138 |
* @param raw The raw header values for this connection, if needed. |
|
139 |
* @return true if all goes well, false if no headers were set. |
|
140 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
141 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
142 |
public boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) { |
2 | 143 |
conn.setAuthenticationProperty(getHeaderName(), getHeaderValue(null,null)); |
144 |
return true; |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* @return the value of the HTTP header this authentication wants set |
|
149 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
150 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
151 |
public String getHeaderValue(URL url, String method) { |
2 | 152 |
/* For Basic the authorization string does not depend on the request URL |
153 |
* or the request method |
|
154 |
*/ |
|
155 |
return auth; |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* For Basic Authentication, the security parameters can never be stale. |
|
160 |
* In other words there is no possibility to reuse the credentials. |
|
161 |
* They are always either valid or invalid. |
|
162 |
*/ |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
163 |
@Override |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
3952
diff
changeset
|
164 |
public boolean isAuthorizationStale (String header) { |
2 | 165 |
return false; |
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* @return the common root path between npath and path. |
|
170 |
* This is used to detect when we have an authentication for two |
|
171 |
* paths and the root of th authentication space is the common root. |
|
172 |
*/ |
|
173 |
||
174 |
static String getRootPath(String npath, String opath) { |
|
175 |
int index = 0; |
|
176 |
int toindex; |
|
177 |
||
178 |
/* Must normalize so we don't get confused by ../ and ./ segments */ |
|
179 |
try { |
|
180 |
npath = new URI (npath).normalize().getPath(); |
|
181 |
opath = new URI (opath).normalize().getPath(); |
|
182 |
} catch (URISyntaxException e) { |
|
183 |
/* ignore error and use the old value */ |
|
184 |
} |
|
185 |
||
186 |
while (index < opath.length()) { |
|
187 |
toindex = opath.indexOf('/', index+1); |
|
188 |
if (toindex != -1 && opath.regionMatches(0, npath, 0, toindex+1)) |
|
189 |
index = toindex; |
|
190 |
else |
|
191 |
return opath.substring(0, index+1); |
|
192 |
} |
|
193 |
/*should not reach here. If we do simply return npath*/ |
|
194 |
return npath; |
|
195 |
} |
|
15258
dd5001103120
8006153: HTTP protocol handler authenication should use Base64 API
chegar
parents:
7668
diff
changeset
|
196 |
} |