src/share/classes/sun/net/www/protocol/http/AuthenticationInfo.java

Print this page




   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.io.*;
  29 import java.net.*;


  30 import java.util.Hashtable;
  31 import java.util.LinkedList;
  32 import java.util.ListIterator;
  33 import java.util.Enumeration;
  34 import java.util.HashMap;
  35 
  36 import sun.net.www.HeaderParser;
  37 
  38 
  39 /**
  40  * AuthenticationInfo: Encapsulate the information needed to
  41  * authenticate a user to a server.
  42  *
  43  * @author Jon Payne
  44  * @author Herb Jellinek
  45  * @author Bill Foote
  46  */
  47 // REMIND:  It would be nice if this class understood about partial matching.
  48 //      If you're authorized for foo.com, chances are high you're also
  49 //      authorized for baz.foo.com.
  50 // NB:  When this gets implemented, be careful about the uncaching
  51 //      policy in HttpURLConnection.  A failure on baz.foo.com shouldn't
  52 //      uncache foo.com!
  53 
  54 abstract class AuthenticationInfo extends AuthCacheValue implements Cloneable {
  55 
  56     // Constants saying what kind of authroization this is.  This determines
  57     // the namespace in the hash table lookup.
  58     static final char SERVER_AUTHENTICATION = 's';
  59     static final char PROXY_AUTHENTICATION = 'p';
  60 
  61     /**
  62      * If true, then simultaneous authentication requests to the same realm/proxy
  63      * are serialized, in order to avoid a user having to type the same username/passwords
  64      * repeatedly, via the Authenticator. Default is false, which means that this
  65      * behavior is switched off.
  66      */
  67     static boolean serializeAuth;
  68 
  69     static {
  70         serializeAuth = java.security.AccessController.doPrivileged(
  71             new sun.security.action.GetBooleanAction(
  72                 "http.auth.serializeRequests")).booleanValue();
  73     }
  74 
  75     /* AuthCacheValue: */
  76 
  77     transient protected PasswordAuthentication pw;
  78 
  79     public PasswordAuthentication credentials() {


 171     /** The protocol/scheme (i.e. http or https ). Need to keep the caches
 172      *  logically separate for the two protocols. This field is only used
 173      *  when constructed with a URL (the normal case for server authentication)
 174      *  For proxy authentication the protocol is not relevant.
 175      */
 176     String protocol;
 177 
 178     /** The host we're authenticating against. */
 179     String host;
 180 
 181     /** The port on the host we're authenticating against. */
 182     int port;
 183 
 184     /** The realm we're authenticating against. */
 185     String realm;
 186 
 187     /** The shortest path from the URL we authenticated against. */
 188     String path;
 189 
 190     /** Use this constructor only for proxy entries */
 191     AuthenticationInfo(char type, AuthScheme authScheme, String host, int port, String realm) {
 192         this.type = type;
 193         this.authScheme = authScheme;
 194         this.protocol = "";
 195         this.host = host.toLowerCase();
 196         this.port = port;
 197         this.realm = realm;
 198         this.path = null;
 199     }
 200 
 201     public Object clone() {
 202         try {
 203             return super.clone ();
 204         } catch (CloneNotSupportedException e) {
 205             // Cannot happen because Cloneable implemented by AuthenticationInfo
 206             return null;
 207         }
 208     }
 209 
 210     /*
 211      * Constructor used to limit the authorization to the path within
 212      * the URL. Use this constructor for origin server entries.
 213      */
 214     AuthenticationInfo(char type, AuthScheme authScheme, URL url, String realm) {
 215         this.type = type;
 216         this.authScheme = authScheme;
 217         this.protocol = url.getProtocol().toLowerCase();
 218         this.host = url.getHost().toLowerCase();
 219         this.port = url.getPort();
 220         if (this.port == -1) {
 221             this.port = url.getDefaultPort();
 222         }
 223         this.realm = realm;
 224 
 225         String urlPath = url.getPath();
 226         if (urlPath.length() == 0)
 227             this.path = urlPath;
 228         else {
 229             this.path = reducePath (urlPath);
 230         }
 231 
 232     }
 233 
 234     /*


 341             return;
 342         }
 343         synchronized (requests) {
 344             requestCompleted (cacheKey(true));
 345         }
 346     }
 347 
 348     /**
 349      * Remove this authentication from the cache
 350      */
 351     void removeFromCache() {
 352         cache.remove(cacheKey(true), this);
 353         if (supportsPreemptiveAuthorization()) {
 354             cache.remove(cacheKey(false), this);
 355         }
 356     }
 357 
 358     /**
 359      * @return true if this authentication supports preemptive authorization
 360      */
 361     abstract boolean supportsPreemptiveAuthorization();
 362 
 363     /**
 364      * @return the name of the HTTP header this authentication wants set.
 365      *          This is used for preemptive authorization.
 366      */
 367     abstract String getHeaderName();






 368 
 369     /**
 370      * Calculates and returns the authentication header value based
 371      * on the stored authentication parameters. If the calculation does not depend
 372      * on the URL or the request method then these parameters are ignored.
 373      * @param url The URL
 374      * @param method The request method
 375      * @return the value of the HTTP header this authentication wants set.
 376      *          Used for preemptive authorization.
 377      */
 378     abstract String getHeaderValue(URL url, String method);
 379 
 380     /**
 381      * Set header(s) on the given connection.  Subclasses must override
 382      * This will only be called for
 383      * definitive (i.e. non-preemptive) authorization.
 384      * @param conn The connection to apply the header(s) to
 385      * @param p A source of header values for this connection, if needed.
 386      * @param raw The raw header field (if needed)
 387      * @return true if all goes well, false if no headers were set.
 388      */
 389     abstract boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw);
 390 
 391     /**
 392      * Check if the header indicates that the current auth. parameters are stale.
 393      * If so, then replace the relevant field with the new value
 394      * and return true. Otherwise return false.
 395      * returning true means the request can be retried with the same userid/password
 396      * returning false means we have to go back to the user to ask for a new
 397      * username password.
 398      */
 399     abstract boolean isAuthorizationStale (String header);
 400 
 401     /**
 402      * Give a key for hash table lookups.
 403      * @param includeRealm if you want the realm considered.  Preemptively
 404      *          setting an authorization is done before the realm is known.
 405      */
 406     String cacheKey(boolean includeRealm) {
 407         // This must be kept in sync with the getXXXAuth() methods in this
 408         // class.
 409         if (includeRealm) {
 410             return type + ":" + authScheme + ":" + protocol + ":"
 411                         + host + ":" + port + ":" + realm;
 412         } else {
 413             return type + ":" + protocol + ":" + host + ":" + port;
 414         }
 415     }
 416 
 417     String s1, s2;  /* used for serialization of pw */
 418 
 419     private void readObject(ObjectInputStream s)


   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.io.IOException;
  29 import java.io.ObjectInputStream;
  30 import java.net.PasswordAuthentication;
  31 import java.net.URL;
  32 import java.util.Hashtable;
  33 import java.util.LinkedList;
  34 import java.util.ListIterator;
  35 import java.util.Enumeration;
  36 import java.util.HashMap;
  37 
  38 import sun.net.www.HeaderParser;
  39 
  40 
  41 /**
  42  * AuthenticationInfo: Encapsulate the information needed to
  43  * authenticate a user to a server.
  44  *
  45  * @author Jon Payne
  46  * @author Herb Jellinek
  47  * @author Bill Foote
  48  */
  49 // REMIND:  It would be nice if this class understood about partial matching.
  50 //      If you're authorized for foo.com, chances are high you're also
  51 //      authorized for baz.foo.com.
  52 // NB:  When this gets implemented, be careful about the uncaching
  53 //      policy in HttpURLConnection.  A failure on baz.foo.com shouldn't
  54 //      uncache foo.com!
  55 
  56 public abstract class AuthenticationInfo extends AuthCacheValue implements Cloneable {
  57 
  58     // Constants saying what kind of authroization this is.  This determines
  59     // the namespace in the hash table lookup.
  60     public static final char SERVER_AUTHENTICATION = 's';
  61     public static final char PROXY_AUTHENTICATION = 'p';
  62 
  63     /**
  64      * If true, then simultaneous authentication requests to the same realm/proxy
  65      * are serialized, in order to avoid a user having to type the same username/passwords
  66      * repeatedly, via the Authenticator. Default is false, which means that this
  67      * behavior is switched off.
  68      */
  69     static boolean serializeAuth;
  70 
  71     static {
  72         serializeAuth = java.security.AccessController.doPrivileged(
  73             new sun.security.action.GetBooleanAction(
  74                 "http.auth.serializeRequests")).booleanValue();
  75     }
  76 
  77     /* AuthCacheValue: */
  78 
  79     transient protected PasswordAuthentication pw;
  80 
  81     public PasswordAuthentication credentials() {


 173     /** The protocol/scheme (i.e. http or https ). Need to keep the caches
 174      *  logically separate for the two protocols. This field is only used
 175      *  when constructed with a URL (the normal case for server authentication)
 176      *  For proxy authentication the protocol is not relevant.
 177      */
 178     String protocol;
 179 
 180     /** The host we're authenticating against. */
 181     String host;
 182 
 183     /** The port on the host we're authenticating against. */
 184     int port;
 185 
 186     /** The realm we're authenticating against. */
 187     String realm;
 188 
 189     /** The shortest path from the URL we authenticated against. */
 190     String path;
 191 
 192     /** Use this constructor only for proxy entries */
 193     public AuthenticationInfo(char type, AuthScheme authScheme, String host, int port, String realm) {
 194         this.type = type;
 195         this.authScheme = authScheme;
 196         this.protocol = "";
 197         this.host = host.toLowerCase();
 198         this.port = port;
 199         this.realm = realm;
 200         this.path = null;
 201     }
 202 
 203     public Object clone() {
 204         try {
 205             return super.clone ();
 206         } catch (CloneNotSupportedException e) {
 207             // Cannot happen because Cloneable implemented by AuthenticationInfo
 208             return null;
 209         }
 210     }
 211 
 212     /*
 213      * Constructor used to limit the authorization to the path within
 214      * the URL. Use this constructor for origin server entries.
 215      */
 216     public AuthenticationInfo(char type, AuthScheme authScheme, URL url, String realm) {
 217         this.type = type;
 218         this.authScheme = authScheme;
 219         this.protocol = url.getProtocol().toLowerCase();
 220         this.host = url.getHost().toLowerCase();
 221         this.port = url.getPort();
 222         if (this.port == -1) {
 223             this.port = url.getDefaultPort();
 224         }
 225         this.realm = realm;
 226 
 227         String urlPath = url.getPath();
 228         if (urlPath.length() == 0)
 229             this.path = urlPath;
 230         else {
 231             this.path = reducePath (urlPath);
 232         }
 233 
 234     }
 235 
 236     /*


 343             return;
 344         }
 345         synchronized (requests) {
 346             requestCompleted (cacheKey(true));
 347         }
 348     }
 349 
 350     /**
 351      * Remove this authentication from the cache
 352      */
 353     void removeFromCache() {
 354         cache.remove(cacheKey(true), this);
 355         if (supportsPreemptiveAuthorization()) {
 356             cache.remove(cacheKey(false), this);
 357         }
 358     }
 359 
 360     /**
 361      * @return true if this authentication supports preemptive authorization
 362      */
 363     public abstract boolean supportsPreemptiveAuthorization();
 364 
 365     /**
 366      * @return the name of the HTTP header this authentication wants set.
 367      *          This is used for preemptive authorization.
 368      */
 369     public String getHeaderName() {
 370         if (type == SERVER_AUTHENTICATION) {
 371             return "Authorization";
 372         } else {
 373             return "Proxy-authorization";
 374         }
 375     }
 376 
 377     /**
 378      * Calculates and returns the authentication header value based
 379      * on the stored authentication parameters. If the calculation does not depend
 380      * on the URL or the request method then these parameters are ignored.
 381      * @param url The URL
 382      * @param method The request method
 383      * @return the value of the HTTP header this authentication wants set.
 384      *          Used for preemptive authorization.
 385      */
 386     public abstract String getHeaderValue(URL url, String method);
 387 
 388     /**
 389      * Set header(s) on the given connection.  Subclasses must override
 390      * This will only be called for
 391      * definitive (i.e. non-preemptive) authorization.
 392      * @param conn The connection to apply the header(s) to
 393      * @param p A source of header values for this connection, if needed.
 394      * @param raw The raw header field (if needed)
 395      * @return true if all goes well, false if no headers were set.
 396      */
 397     public abstract boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw);
 398 
 399     /**
 400      * Check if the header indicates that the current auth. parameters are stale.
 401      * If so, then replace the relevant field with the new value
 402      * and return true. Otherwise return false.
 403      * returning true means the request can be retried with the same userid/password
 404      * returning false means we have to go back to the user to ask for a new
 405      * username password.
 406      */
 407     public abstract boolean isAuthorizationStale (String header);
 408 
 409     /**
 410      * Give a key for hash table lookups.
 411      * @param includeRealm if you want the realm considered.  Preemptively
 412      *          setting an authorization is done before the realm is known.
 413      */
 414     String cacheKey(boolean includeRealm) {
 415         // This must be kept in sync with the getXXXAuth() methods in this
 416         // class.
 417         if (includeRealm) {
 418             return type + ":" + authScheme + ":" + protocol + ":"
 419                         + host + ":" + port + ":" + realm;
 420         } else {
 421             return type + ":" + protocol + ":" + host + ":" + port;
 422         }
 423     }
 424 
 425     String s1, s2;  /* used for serialization of pw */
 426 
 427     private void readObject(ObjectInputStream s)