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