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