2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.net.www.protocol.http;
|
|
27 |
|
|
28 |
import java.util.Arrays;
|
|
29 |
import java.util.HashMap;
|
|
30 |
import java.util.Map;
|
|
31 |
|
|
32 |
import sun.net.www.HeaderParser;
|
|
33 |
import sun.misc.BASE64Decoder;
|
|
34 |
import sun.misc.BASE64Encoder;
|
|
35 |
|
|
36 |
import java.net.URL;
|
|
37 |
import java.net.PasswordAuthentication;
|
|
38 |
import java.io.IOException;
|
|
39 |
|
|
40 |
|
|
41 |
/**
|
|
42 |
* NegotiateAuthentication:
|
|
43 |
*
|
|
44 |
* @author weijun.wang@sun.com
|
|
45 |
* @since 1.6
|
|
46 |
*/
|
|
47 |
|
|
48 |
class NegotiateAuthentication extends AuthenticationInfo {
|
|
49 |
|
|
50 |
private static final long serialVersionUID = 100L;
|
|
51 |
|
|
52 |
private String scheme = null;
|
|
53 |
|
|
54 |
static final char NEGOTIATE_AUTH = 'S';
|
|
55 |
static final char KERBEROS_AUTH = 'K';
|
|
56 |
|
|
57 |
// These maps are used to manage the GSS availability for diffrent
|
|
58 |
// hosts. The key for both maps is the host name.
|
|
59 |
// <code>supported</code> is set when isSupported is checked,
|
|
60 |
// if it's true, a cached Negotiator is put into <code>cache</code>.
|
|
61 |
// the cache can be used only once, so after the first use, it's cleaned.
|
|
62 |
static HashMap <String, Boolean> supported = null;
|
|
63 |
static HashMap <String, Negotiator> cache = null;
|
|
64 |
|
|
65 |
// The HTTP Negotiate Helper
|
|
66 |
private Negotiator negotiator = null;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Constructor used for WWW entries. <code>pw</code> is not used because
|
|
70 |
* for GSS there is only one single PasswordAuthentication which is
|
|
71 |
* independant of host/port/... info.
|
|
72 |
*/
|
|
73 |
public NegotiateAuthentication(boolean isProxy, URL url,
|
|
74 |
PasswordAuthentication pw, String scheme) {
|
|
75 |
super(isProxy?PROXY_AUTHENTICATION:SERVER_AUTHENTICATION,
|
|
76 |
NEGOTIATE_AUTH, url, "");
|
|
77 |
this.scheme = scheme;
|
|
78 |
}
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Constructor used for proxy entries
|
|
82 |
*/
|
|
83 |
public NegotiateAuthentication(boolean isProxy, String host, int port,
|
|
84 |
PasswordAuthentication pw, String scheme) {
|
|
85 |
super(isProxy?PROXY_AUTHENTICATION:SERVER_AUTHENTICATION,
|
|
86 |
NEGOTIATE_AUTH,host, port, "");
|
|
87 |
this.scheme = scheme;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* @return true if this authentication supports preemptive authorization
|
|
92 |
*/
|
|
93 |
boolean supportsPreemptiveAuthorization() {
|
|
94 |
return false;
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Find out if a hostname supports Negotiate protocol. In order to find
|
|
99 |
* out yes or no, an initialization of a Negotiator object against
|
|
100 |
* hostname and scheme is tried. The generated object will be cached
|
|
101 |
* under the name of hostname at a success try.<br>
|
|
102 |
*
|
|
103 |
* If this method is called for the second time on a hostname, the answer is
|
|
104 |
* already saved in <code>supported</code>, so no need to try again.
|
|
105 |
*
|
|
106 |
* @param hostname hostname to test
|
|
107 |
* @param scheme scheme to test
|
|
108 |
* @return true if supported
|
|
109 |
*/
|
|
110 |
synchronized public static boolean isSupported(String hostname,
|
|
111 |
String scheme) {
|
|
112 |
if (supported == null) {
|
|
113 |
supported = new HashMap <String, Boolean>();
|
|
114 |
cache = new HashMap <String, Negotiator>();
|
|
115 |
}
|
|
116 |
|
|
117 |
hostname = hostname.toLowerCase();
|
|
118 |
if (supported.containsKey(hostname)) {
|
|
119 |
return supported.get(hostname);
|
|
120 |
}
|
|
121 |
|
|
122 |
try {
|
|
123 |
Negotiator neg = Negotiator.getSupported(hostname, scheme);
|
|
124 |
supported.put(hostname, true);
|
|
125 |
// the only place cache.put is called. here we can make sure
|
|
126 |
// the object is valid and the oneToken inside is not null
|
|
127 |
cache.put(hostname, neg);
|
|
128 |
return true;
|
|
129 |
} catch(Exception e) {
|
|
130 |
supported.put(hostname, false);
|
|
131 |
return false;
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* @return the name of the HTTP header this authentication wants to set
|
|
137 |
*/
|
|
138 |
String getHeaderName() {
|
|
139 |
if (type == SERVER_AUTHENTICATION) {
|
|
140 |
return "Authorization";
|
|
141 |
} else {
|
|
142 |
return "Proxy-Authorization";
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Not supported. Must use the setHeaders() method
|
|
148 |
*/
|
|
149 |
String getHeaderValue(URL url, String method) {
|
|
150 |
throw new RuntimeException ("getHeaderValue not supported");
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Check if the header indicates that the current auth. parameters are stale.
|
|
155 |
* If so, then replace the relevant field with the new value
|
|
156 |
* and return true. Otherwise return false.
|
|
157 |
* returning true means the request can be retried with the same userid/password
|
|
158 |
* returning false means we have to go back to the user to ask for a new
|
|
159 |
* username password.
|
|
160 |
*/
|
|
161 |
boolean isAuthorizationStale (String header) {
|
|
162 |
return false; /* should not be called for Negotiate */
|
|
163 |
}
|
|
164 |
|
|
165 |
/**
|
|
166 |
* Set header(s) on the given connection.
|
|
167 |
* @param conn The connection to apply the header(s) to
|
|
168 |
* @param p A source of header values for this connection, not used because
|
|
169 |
* HeaderParser converts the fields to lower case, use raw instead
|
|
170 |
* @param raw The raw header field.
|
|
171 |
* @return true if all goes well, false if no headers were set.
|
|
172 |
*/
|
|
173 |
synchronized boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {
|
|
174 |
|
|
175 |
try {
|
|
176 |
String response;
|
|
177 |
byte[] incoming = null;
|
|
178 |
String[] parts = raw.split("\\s+");
|
|
179 |
if (parts.length > 1) {
|
|
180 |
incoming = new BASE64Decoder().decodeBuffer(parts[1]);
|
|
181 |
}
|
|
182 |
response = scheme + " " + new B64Encoder().encode(
|
|
183 |
incoming==null?firstToken():nextToken(incoming));
|
|
184 |
|
|
185 |
conn.setAuthenticationProperty(getHeaderName(), response);
|
|
186 |
return true;
|
|
187 |
} catch (IOException e) {
|
|
188 |
return false;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* return the first token.
|
|
194 |
* @returns the token
|
|
195 |
* @throws IOException if <code>Negotiator.getSupported()</code> or
|
|
196 |
* <code>Negotiator.firstToken()</code> failed.
|
|
197 |
*/
|
|
198 |
private byte[] firstToken() throws IOException {
|
|
199 |
negotiator = null;
|
|
200 |
if (cache != null) {
|
|
201 |
synchronized(cache) {
|
|
202 |
negotiator = cache.get(getHost());
|
|
203 |
if (negotiator != null) {
|
|
204 |
cache.remove(getHost()); // so that it is only used once
|
|
205 |
}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
if (negotiator == null) {
|
|
209 |
try {
|
|
210 |
negotiator = Negotiator.getSupported(getHost(), scheme);
|
|
211 |
} catch(Exception e) {
|
|
212 |
IOException ioe = new IOException("Cannot initialize Negotiator");
|
|
213 |
ioe.initCause(e);
|
|
214 |
throw ioe;
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
return negotiator.firstToken();
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* return more tokens
|
|
223 |
* @param token the token to be fed into <code>negotiator.nextToken()</code>
|
|
224 |
* @returns the token
|
|
225 |
* @throws IOException if <code>negotiator.nextToken()</code> throws Exception.
|
|
226 |
* May happen if the input token is invalid.
|
|
227 |
*/
|
|
228 |
private byte[] nextToken(byte[] token) throws IOException {
|
|
229 |
return negotiator.nextToken(token);
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
* no-use for Negotiate
|
|
234 |
*/
|
|
235 |
public void checkResponse (String header, String method, URL url) throws IOException {
|
|
236 |
}
|
|
237 |
|
|
238 |
class B64Encoder extends BASE64Encoder {
|
|
239 |
protected int bytesPerLine () {
|
|
240 |
return 100000; // as big as it can be, maybe INT_MAX
|
|
241 |
}
|
|
242 |
}
|
|
243 |
|
|
244 |
// MS will send a final WWW-Authenticate even if the status is already
|
|
245 |
// 200 OK. The token can be fed into initSecContext() again to determine
|
|
246 |
// if the server can be trusted. This is not the same concept as Digest's
|
|
247 |
// Authentication-Info header.
|
|
248 |
//
|
|
249 |
// Currently we ignore this header.
|
|
250 |
|
|
251 |
}
|
|
252 |
|
|
253 |
/**
|
|
254 |
* This abstract class is a bridge to connect NegotiteAuthentication and
|
|
255 |
* NegotiatorImpl, so that JAAS and JGSS calls can be made
|
|
256 |
*/
|
|
257 |
abstract class Negotiator {
|
|
258 |
static Negotiator getSupported(String hostname, String scheme)
|
|
259 |
throws Exception {
|
|
260 |
|
|
261 |
// These lines are equivalent to
|
|
262 |
// return new NegotiatorImpl(hostname, scheme);
|
|
263 |
// The current implementation will make sure NegotiatorImpl is not
|
|
264 |
// directly referenced when compiling, thus smooth the way of building
|
|
265 |
// the J2SE platform where HttpURLConnection is a bootstrap class.
|
|
266 |
|
|
267 |
Class clazz = Class.forName("sun.net.www.protocol.http.NegotiatorImpl");
|
|
268 |
java.lang.reflect.Constructor c = clazz.getConstructor(String.class, String.class);
|
|
269 |
return (Negotiator) (c.newInstance(hostname, scheme));
|
|
270 |
}
|
|
271 |
|
|
272 |
abstract byte[] firstToken() throws IOException;
|
|
273 |
|
|
274 |
abstract byte[] nextToken(byte[] in) throws IOException;
|
|
275 |
}
|