jdk/src/java.base/share/classes/sun/net/www/protocol/http/AuthenticationInfo.java
author dfuchs
Thu, 20 Oct 2016 15:10:52 +0100
changeset 44753 37d4270a2a8d
parent 42351 85ed90be0ae1
permissions -rw-r--r--
8163520: Reuse cache entries Reviewed-by: chegar, michaelm, weijun, aefimov, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
     2
 * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www.protocol.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    28
import java.io.IOException;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    29
import java.io.ObjectInputStream;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    30
import java.net.PasswordAuthentication;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    31
import java.net.URL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
    33
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.www.HeaderParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * AuthenticationInfo: Encapsulate the information needed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * authenticate a user to a server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jon Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Herb Jellinek
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
// REMIND:  It would be nice if this class understood about partial matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
//      If you're authorized for foo.com, chances are high you're also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
//      authorized for baz.foo.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
// NB:  When this gets implemented, be careful about the uncaching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
//      policy in HttpURLConnection.  A failure on baz.foo.com shouldn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
//      uncache foo.com!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    53
public abstract class AuthenticationInfo extends AuthCacheValue implements Cloneable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
19219
0d4231d25a29 8022478: Fix Warnings In sun.net.www.protocol.http Package
dxu
parents: 7668
diff changeset
    55
    static final long serialVersionUID = -2588378268010453259L;
0d4231d25a29 8022478: Fix Warnings In sun.net.www.protocol.http Package
dxu
parents: 7668
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Constants saying what kind of authroization this is.  This determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // the namespace in the hash table lookup.
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    59
    public static final char SERVER_AUTHENTICATION = 's';
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    60
    public static final char PROXY_AUTHENTICATION = 'p';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * If true, then simultaneous authentication requests to the same realm/proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * are serialized, in order to avoid a user having to type the same username/passwords
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * repeatedly, via the Authenticator. Default is false, which means that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * behavior is switched off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
    68
    static final boolean serializeAuth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        serializeAuth = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            new sun.security.action.GetBooleanAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                "http.auth.serializeRequests")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /* AuthCacheValue: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    77
    protected transient PasswordAuthentication pw;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public PasswordAuthentication credentials() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public AuthCacheValue.Type getAuthType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return type == SERVER_AUTHENTICATION ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            AuthCacheValue.Type.Server:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            AuthCacheValue.Type.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
    88
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
    89
    AuthScheme getAuthScheme() {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
    90
        return authScheme;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
    91
    }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
    92
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public String getRealm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public String getProtocolScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   108
    /**
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   109
     * Whether we should cache this instance in the AuthCache.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   110
     * This method returns {@code true} by default.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   111
     * Subclasses may override this method to add
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   112
     * additional restrictions.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   113
     * @return {@code true} by default.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   114
     */
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   115
    protected boolean useAuthCache() {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   116
        return true;
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   117
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * requests is used to ensure that interaction with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Authenticator for a particular realm is single threaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * ie. if multiple threads need to get credentials from the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * at the same time, then all but the first will block until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the first completes its authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   126
    private static HashMap<String,Thread> requests = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /* check if a request for this destination is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * return false immediately if not. Otherwise block until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * request is finished and return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   132
    private static boolean requestIsInProgress (String key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (!serializeAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            /* behavior is disabled. Revert to concurrent requests */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        synchronized (requests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            Thread t, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            c = Thread.currentThread();
4916
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 4157
diff changeset
   140
            if ((t = requests.get(key)) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                requests.put (key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (t == c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            while (requests.containsKey(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    requests.wait ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /* entry may be in cache now. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /* signal completion of an authentication (whether it succeeded or not)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * so that other threads can continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   160
    private static void requestCompleted (String key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        synchronized (requests) {
4916
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 4157
diff changeset
   162
            Thread thread = requests.get(key);
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 4157
diff changeset
   163
            if (thread != null && thread == Thread.currentThread()) {
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 4157
diff changeset
   164
                boolean waspresent = requests.remove(key) != null;
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 4157
diff changeset
   165
                assert waspresent;
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 4157
diff changeset
   166
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            requests.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    //public String toString () {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
   172
        //return ("{"+type+":"+authScheme+":"+protocol+":"+host+":"+port+":"+realm+":"+path+"}");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // REMIND:  This cache just grows forever.  We should put in a bounded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    //          cache, or maybe something using WeakRef's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /** The type (server/proxy) of authentication this is.  Used for key lookup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    char type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
   181
    /** The authentication scheme (basic/digest). Also used for key lookup */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
   182
    AuthScheme authScheme;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /** The protocol/scheme (i.e. http or https ). Need to keep the caches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *  logically separate for the two protocols. This field is only used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *  when constructed with a URL (the normal case for server authentication)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *  For proxy authentication the protocol is not relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /** The host we're authenticating against. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /** The port on the host we're authenticating against. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /** The realm we're authenticating against. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /** The shortest path from the URL we authenticated against. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   203
    /**
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   204
     * A key identifying the authenticator from which the credentials
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   205
     * were obtained.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   206
     * {@link AuthenticatorKeys#DEFAULT} identifies the {@linkplain
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   207
     * java.net.Authenticator#setDefault(java.net.Authenticator) default}
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   208
     * authenticator.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   209
     */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   210
     String authenticatorKey;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   211
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /** Use this constructor only for proxy entries */
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   213
    public AuthenticationInfo(char type, AuthScheme authScheme, String host,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   214
                              int port, String realm, String authenticatorKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.type = type;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
   216
        this.authScheme = authScheme;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this.protocol = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        this.host = host.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.realm = realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.path = null;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   222
        this.authenticatorKey = Objects.requireNonNull(authenticatorKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return super.clone ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            // Cannot happen because Cloneable implemented by AuthenticationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Constructor used to limit the authorization to the path within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the URL. Use this constructor for origin server entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   238
    public AuthenticationInfo(char type, AuthScheme authScheme, URL url, String realm,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   239
                              String authenticatorKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        this.type = type;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
   241
        this.authScheme = authScheme;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        this.protocol = url.getProtocol().toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        this.host = url.getHost().toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        this.port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (this.port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            this.port = url.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        this.realm = realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        String urlPath = url.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (urlPath.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            this.path = urlPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            this.path = reducePath (urlPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   256
        this.authenticatorKey = Objects.requireNonNull(authenticatorKey);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   257
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   259
    /**
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   260
     * The {@linkplain java.net.Authenticator#getKey(java.net.Authenticator) key}
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   261
     * of the authenticator that was used to obtain the credentials.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   262
     * @return The authenticator's key.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   263
     */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   264
    public final String getAuthenticatorKey() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   265
        return authenticatorKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * reduce the path to the root of where we think the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * authorization begins. This could get shorter as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the url is traversed up following a successful challenge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    static String reducePath (String urlPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int sepIndex = urlPath.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int targetSuffixIndex = urlPath.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (sepIndex != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (sepIndex < targetSuffixIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return urlPath.substring(0, sepIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                return urlPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return urlPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Returns info for the URL, for an HTTP server auth.  Used when we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * don't yet know the realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * (i.e. when we're preemptively setting the auth).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   290
    static AuthenticationInfo getServerAuth(URL url, String authenticatorKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            port = url.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        String key = SERVER_AUTHENTICATION + ":" + url.getProtocol().toLowerCase()
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   296
                + ":" + url.getHost().toLowerCase() + ":" + port
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   297
                + ";auth=" + authenticatorKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return getAuth(key, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Returns info for the URL, for an HTTP server auth.  Used when we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * do know the realm (i.e. when we're responding to a challenge).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * In this case we do not use the path because the protection space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * is identified by the host:port:realm only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   307
    static String getServerAuthKey(URL url, String realm, AuthScheme scheme,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   308
                                   String authenticatorKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            port = url.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   313
        String key = SERVER_AUTHENTICATION + ":" + scheme + ":"
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   314
                     + url.getProtocol().toLowerCase()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   315
                     + ":" + url.getHost().toLowerCase()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   316
                     + ":" + port + ":" + realm
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   317
                     + ";auth=" + authenticatorKey;
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   318
        return key;
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   319
    }
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   320
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   321
    static AuthenticationInfo getServerAuth(String key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        AuthenticationInfo cached = getAuth(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if ((cached == null) && requestIsInProgress (key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            /* check the cache again, it might contain an entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            cached = getAuth(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Return the AuthenticationInfo object from the cache if it's path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * a substring of the supplied URLs path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    static AuthenticationInfo getAuth(String key, URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return (AuthenticationInfo)cache.get (key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return (AuthenticationInfo)cache.get (key, url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Returns a firewall authentication, for the given host/port.  Used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * for preemptive header-setting. Note, the protocol field is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * blank for proxies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   348
    static AuthenticationInfo getProxyAuth(String host, int port,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   349
                                           String authenticatorKey) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   350
        String key = PROXY_AUTHENTICATION + "::" + host.toLowerCase() + ":" + port
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   351
                     + ";auth=" + authenticatorKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        AuthenticationInfo result = (AuthenticationInfo) cache.get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Returns a firewall authentication, for the given host/port and realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Used in response to a challenge. Note, the protocol field is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * blank for proxies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   361
    static String getProxyAuthKey(String host, int port, String realm,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   362
                                  AuthScheme scheme, String authenticatorKey) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   363
        String key = PROXY_AUTHENTICATION + ":" + scheme
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   364
                        + "::" + host.toLowerCase()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   365
                        + ":" + port + ":" + realm
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   366
                        + ";auth=" + authenticatorKey;
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   367
        return key;
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   368
    }
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   369
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   370
    static AuthenticationInfo getProxyAuth(String key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        AuthenticationInfo cached = (AuthenticationInfo) cache.get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if ((cached == null) && requestIsInProgress (key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            /* check the cache again, it might contain an entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            cached = (AuthenticationInfo) cache.get(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Add this authentication to the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    void addToCache() {
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   384
        String key = cacheKey(true);
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   385
        if (useAuthCache()) {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   386
            cache.put(key, this);
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   387
            if (supportsPreemptiveAuthorization()) {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   388
                cache.put(cacheKey(false), this);
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   389
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   391
        endAuthRequest(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   394
    static void endAuthRequest (String key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (!serializeAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        synchronized (requests) {
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 4916
diff changeset
   399
            requestCompleted(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Remove this authentication from the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    void removeFromCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        cache.remove(cacheKey(true), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (supportsPreemptiveAuthorization()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            cache.remove(cacheKey(false), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @return true if this authentication supports preemptive authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   416
    public abstract boolean supportsPreemptiveAuthorization();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return the name of the HTTP header this authentication wants set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *          This is used for preemptive authorization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   422
    public String getHeaderName() {
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   423
        if (type == SERVER_AUTHENTICATION) {
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   424
            return "Authorization";
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   425
        } else {
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   426
            return "Proxy-authorization";
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   427
        }
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   428
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Calculates and returns the authentication header value based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * on the stored authentication parameters. If the calculation does not depend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * on the URL or the request method then these parameters are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param url The URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param method The request method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @return the value of the HTTP header this authentication wants set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *          Used for preemptive authorization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   439
    public abstract String getHeaderValue(URL url, String method);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Set header(s) on the given connection.  Subclasses must override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * This will only be called for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * definitive (i.e. non-preemptive) authorization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param conn The connection to apply the header(s) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param p A source of header values for this connection, if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param raw The raw header field (if needed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @return true if all goes well, false if no headers were set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   450
    public abstract boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Check if the header indicates that the current auth. parameters are stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * If so, then replace the relevant field with the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * and return true. Otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * returning true means the request can be retried with the same userid/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * returning false means we have to go back to the user to ask for a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * username password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   460
    public abstract boolean isAuthorizationStale (String header);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Give a key for hash table lookups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param includeRealm if you want the realm considered.  Preemptively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *          setting an authorization is done before the realm is known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    String cacheKey(boolean includeRealm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        // This must be kept in sync with the getXXXAuth() methods in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // class.
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   470
        String authenticatorKey = getAuthenticatorKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (includeRealm) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2
diff changeset
   472
            return type + ":" + authScheme + ":" + protocol + ":"
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   473
                        + host + ":" + port + ":" + realm
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   474
                     + ";auth=" + authenticatorKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } else {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   476
            return type + ":" + protocol + ":" + host + ":" + port
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   477
                     + ";auth=" + authenticatorKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    String s1, s2;  /* used for serialization of pw */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   483
    private synchronized void readObject(ObjectInputStream s)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        s.defaultReadObject ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        pw = new PasswordAuthentication (s1, s2.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        s1 = null; s2= null;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   489
        if (authenticatorKey == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   490
            authenticatorKey = AuthenticatorKeys.DEFAULT;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   491
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
   497
        Objects.requireNonNull(authenticatorKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        s1 = pw.getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        s2 = new String (pw.getPassword());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        s.defaultWriteObject ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
}