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