|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.net.www.protocol.http; |
|
|
|
import java.io.IOException; |
|
import java.io.ObjectInputStream; |
|
import java.net.PasswordAuthentication; |
|
import java.net.URL; |
|
import java.util.HashMap; |
|
import java.util.Objects; |
|
|
|
import sun.net.www.HeaderParser; |
|
|
|
|
|
/** |
|
* AuthenticationInfo: Encapsulate the information needed to |
|
* authenticate a user to a server. |
|
* |
|
* @author Jon Payne |
|
* @author Herb Jellinek |
|
* @author Bill Foote |
|
*/ |
|
// REMIND: It would be nice if this class understood about partial matching. |
|
// If you're authorized for foo.com, chances are high you're also |
|
// authorized for baz.foo.com. |
|
// NB: When this gets implemented, be careful about the uncaching |
|
// policy in HttpURLConnection. A failure on baz.foo.com shouldn't |
|
// uncache foo.com! |
|
|
|
public abstract class AuthenticationInfo extends AuthCacheValue implements Cloneable { |
|
|
|
static final long serialVersionUID = -2588378268010453259L; |
|
|
|
// Constants saying what kind of authroization this is. This determines |
|
|
|
public static final char SERVER_AUTHENTICATION = 's'; |
|
public static final char PROXY_AUTHENTICATION = 'p'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static final boolean serializeAuth; |
|
static { |
|
serializeAuth = java.security.AccessController.doPrivileged( |
|
new sun.security.action.GetBooleanAction( |
|
"http.auth.serializeRequests")).booleanValue(); |
|
} |
|
|
|
/* AuthCacheValue: */ |
|
|
|
protected transient PasswordAuthentication pw; |
|
|
|
public PasswordAuthentication credentials() { |
|
return pw; |
|
} |
|
|
|
public AuthCacheValue.Type getAuthType() { |
|
return type == SERVER_AUTHENTICATION ? |
|
AuthCacheValue.Type.Server: |
|
AuthCacheValue.Type.Proxy; |
|
} |
|
|
|
AuthScheme getAuthScheme() { |
|
return authScheme; |
|
} |
|
|
|
public String getHost() { |
|
return host; |
|
} |
|
public int getPort() { |
|
return port; |
|
} |
|
public String getRealm() { |
|
return realm; |
|
} |
|
public String getPath() { |
|
return path; |
|
} |
|
public String getProtocolScheme() { |
|
return protocol; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected boolean useAuthCache() { |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static HashMap<String,Thread> requests = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
*/ |
|
private static boolean requestIsInProgress (String key) { |
|
if (!serializeAuth) { |
|
|
|
return false; |
|
} |
|
synchronized (requests) { |
|
Thread t, c; |
|
c = Thread.currentThread(); |
|
if ((t = requests.get(key)) == null) { |
|
requests.put (key, c); |
|
return false; |
|
} |
|
if (t == c) { |
|
return false; |
|
} |
|
while (requests.containsKey(key)) { |
|
try { |
|
requests.wait (); |
|
} catch (InterruptedException e) {} |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
|
|
|
|
|
|
*/ |
|
private static void requestCompleted (String key) { |
|
synchronized (requests) { |
|
Thread thread = requests.get(key); |
|
if (thread != null && thread == Thread.currentThread()) { |
|
boolean waspresent = requests.remove(key) != null; |
|
assert waspresent; |
|
} |
|
requests.notifyAll(); |
|
} |
|
} |
|
|
|
//public String toString () { |
|
//return ("{"+type+":"+authScheme+":"+protocol+":"+host+":"+port+":"+realm+":"+path+"}"); |
|
//} |
|
|
|
// REMIND: This cache just grows forever. We should put in a bounded |
|
// cache, or maybe something using WeakRef's. |
|
|
|
|
|
char type; |
|
|
|
|
|
AuthScheme authScheme; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
String protocol; |
|
|
|
|
|
String host; |
|
|
|
|
|
int port; |
|
|
|
|
|
String realm; |
|
|
|
|
|
String path; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
String authenticatorKey; |
|
|
|
|
|
public AuthenticationInfo(char type, AuthScheme authScheme, String host, |
|
int port, String realm, String authenticatorKey) { |
|
this.type = type; |
|
this.authScheme = authScheme; |
|
this.protocol = ""; |
|
this.host = host.toLowerCase(); |
|
this.port = port; |
|
this.realm = realm; |
|
this.path = null; |
|
this.authenticatorKey = Objects.requireNonNull(authenticatorKey); |
|
} |
|
|
|
public Object clone() { |
|
try { |
|
return super.clone (); |
|
} catch (CloneNotSupportedException e) { |
|
|
|
return null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public AuthenticationInfo(char type, AuthScheme authScheme, URL url, String realm, |
|
String authenticatorKey) { |
|
this.type = type; |
|
this.authScheme = authScheme; |
|
this.protocol = url.getProtocol().toLowerCase(); |
|
this.host = url.getHost().toLowerCase(); |
|
this.port = url.getPort(); |
|
if (this.port == -1) { |
|
this.port = url.getDefaultPort(); |
|
} |
|
this.realm = realm; |
|
|
|
String urlPath = url.getPath(); |
|
if (urlPath.length() == 0) |
|
this.path = urlPath; |
|
else { |
|
this.path = reducePath (urlPath); |
|
} |
|
this.authenticatorKey = Objects.requireNonNull(authenticatorKey); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public final String getAuthenticatorKey() { |
|
return authenticatorKey; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static String reducePath (String urlPath) { |
|
int sepIndex = urlPath.lastIndexOf('/'); |
|
int targetSuffixIndex = urlPath.lastIndexOf('.'); |
|
if (sepIndex != -1) |
|
if (sepIndex < targetSuffixIndex) |
|
return urlPath.substring(0, sepIndex+1); |
|
else |
|
return urlPath; |
|
else |
|
return urlPath; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static AuthenticationInfo getServerAuth(URL url, String authenticatorKey) { |
|
int port = url.getPort(); |
|
if (port == -1) { |
|
port = url.getDefaultPort(); |
|
} |
|
String key = SERVER_AUTHENTICATION + ":" + url.getProtocol().toLowerCase() |
|
+ ":" + url.getHost().toLowerCase() + ":" + port |
|
+ ";auth=" + authenticatorKey; |
|
return getAuth(key, url); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static String getServerAuthKey(URL url, String realm, AuthScheme scheme, |
|
String authenticatorKey) { |
|
int port = url.getPort(); |
|
if (port == -1) { |
|
port = url.getDefaultPort(); |
|
} |
|
String key = SERVER_AUTHENTICATION + ":" + scheme + ":" |
|
+ url.getProtocol().toLowerCase() |
|
+ ":" + url.getHost().toLowerCase() |
|
+ ":" + port + ":" + realm |
|
+ ";auth=" + authenticatorKey; |
|
return key; |
|
} |
|
|
|
static AuthenticationInfo getServerAuth(String key) { |
|
AuthenticationInfo cached = getAuth(key, null); |
|
if ((cached == null) && requestIsInProgress (key)) { |
|
|
|
cached = getAuth(key, null); |
|
} |
|
return cached; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static AuthenticationInfo getAuth(String key, URL url) { |
|
if (url == null) { |
|
return (AuthenticationInfo)cache.get (key, null); |
|
} else { |
|
return (AuthenticationInfo)cache.get (key, url.getPath()); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static AuthenticationInfo getProxyAuth(String host, int port, |
|
String authenticatorKey) { |
|
String key = PROXY_AUTHENTICATION + "::" + host.toLowerCase() + ":" + port |
|
+ ";auth=" + authenticatorKey; |
|
AuthenticationInfo result = (AuthenticationInfo) cache.get(key, null); |
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static String getProxyAuthKey(String host, int port, String realm, |
|
AuthScheme scheme, String authenticatorKey) { |
|
String key = PROXY_AUTHENTICATION + ":" + scheme |
|
+ "::" + host.toLowerCase() |
|
+ ":" + port + ":" + realm |
|
+ ";auth=" + authenticatorKey; |
|
return key; |
|
} |
|
|
|
static AuthenticationInfo getProxyAuth(String key) { |
|
AuthenticationInfo cached = (AuthenticationInfo) cache.get(key, null); |
|
if ((cached == null) && requestIsInProgress (key)) { |
|
|
|
cached = (AuthenticationInfo) cache.get(key, null); |
|
} |
|
return cached; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
void addToCache() { |
|
String key = cacheKey(true); |
|
if (useAuthCache()) { |
|
cache.put(key, this); |
|
if (supportsPreemptiveAuthorization()) { |
|
cache.put(cacheKey(false), this); |
|
} |
|
} |
|
endAuthRequest(key); |
|
} |
|
|
|
static void endAuthRequest (String key) { |
|
if (!serializeAuth) { |
|
return; |
|
} |
|
synchronized (requests) { |
|
requestCompleted(key); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
void removeFromCache() { |
|
cache.remove(cacheKey(true), this); |
|
if (supportsPreemptiveAuthorization()) { |
|
cache.remove(cacheKey(false), this); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public abstract boolean supportsPreemptiveAuthorization(); |
|
|
|
|
|
|
|
|
|
*/ |
|
public String getHeaderName() { |
|
if (type == SERVER_AUTHENTICATION) { |
|
return "Authorization"; |
|
} else { |
|
return "Proxy-authorization"; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public abstract String getHeaderValue(URL url, String method); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public abstract boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public abstract boolean isAuthorizationStale (String header); |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
String cacheKey(boolean includeRealm) { |
|
// This must be kept in sync with the getXXXAuth() methods in this |
|
|
|
String authenticatorKey = getAuthenticatorKey(); |
|
if (includeRealm) { |
|
return type + ":" + authScheme + ":" + protocol + ":" |
|
+ host + ":" + port + ":" + realm |
|
+ ";auth=" + authenticatorKey; |
|
} else { |
|
return type + ":" + protocol + ":" + host + ":" + port |
|
+ ";auth=" + authenticatorKey; |
|
} |
|
} |
|
|
|
String s1, s2; /* used for serialization of pw */ |
|
|
|
private synchronized void readObject(ObjectInputStream s) |
|
throws IOException, ClassNotFoundException |
|
{ |
|
s.defaultReadObject (); |
|
pw = new PasswordAuthentication (s1, s2.toCharArray()); |
|
s1 = null; s2= null; |
|
if (authenticatorKey == null) { |
|
authenticatorKey = AuthenticatorKeys.DEFAULT; |
|
} |
|
} |
|
|
|
private synchronized void writeObject(java.io.ObjectOutputStream s) |
|
throws IOException |
|
{ |
|
Objects.requireNonNull(authenticatorKey); |
|
s1 = pw.getUserName(); |
|
s2 = new String (pw.getPassword()); |
|
s.defaultWriteObject (); |
|
} |
|
} |