src/java.base/share/classes/sun/net/www/protocol/http/NegotiateAuthentication.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
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 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: 4157
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: 4157
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: 4157
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4157
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4157
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.net.URL;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    29
import java.io.IOException;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    30
import java.net.Authenticator.RequestorType;
15258
dd5001103120 8006153: HTTP protocol handler authenication should use Base64 API
chegar
parents: 5506
diff changeset
    31
import java.util.Base64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    33
import java.util.concurrent.locks.ReentrantLock;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.www.HeaderParser;
24135
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
    36
import sun.util.logging.PlatformLogger;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2942
diff changeset
    37
import static sun.net.www.protocol.http.AuthScheme.NEGOTIATE;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2942
diff changeset
    38
import static sun.net.www.protocol.http.AuthScheme.KERBEROS;
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    39
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * NegotiateAuthentication:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author weijun.wang@sun.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
class NegotiateAuthentication extends AuthenticationInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
    50
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final long serialVersionUID = 100L;
24135
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
    52
    private static final PlatformLogger logger = HttpURLConnection.getHttpLogger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32227
diff changeset
    54
    private final HttpCallerInfo hci;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // These maps are used to manage the GSS availability for diffrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // hosts. The key for both maps is the host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // <code>supported</code> is set when isSupported is checked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // if it's true, a cached Negotiator is put into <code>cache</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // the cache can be used only once, so after the first use, it's cleaned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static HashMap <String, Boolean> supported = null;
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    62
    static ThreadLocal <HashMap <String, Negotiator>> cache = null;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    63
    private static final ReentrantLock negotiateLock = new ReentrantLock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
    64
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    65
    /* Whether cache is enabled for Negotiate/Kerberos */
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    66
    private static final boolean cacheSPNEGO;
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    67
    static {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    68
        String spnegoCacheProp =
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    69
            GetPropertyAction.privilegedGetProperty("jdk.spnego.cache", "true");
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    70
        cacheSPNEGO = Boolean.parseBoolean(spnegoCacheProp);
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
    71
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // The HTTP Negotiate Helper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private Negotiator negotiator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
   /**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    77
    * Constructor used for both WWW and proxy entries.
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    78
    * @param hci a schemed object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    80
    public NegotiateAuthentication(HttpCallerInfo hci) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2942
diff changeset
    81
        super(RequestorType.PROXY==hci.authType ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2942
diff changeset
    82
              hci.scheme.equalsIgnoreCase("Negotiate") ? NEGOTIATE : KERBEROS,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 2942
diff changeset
    83
              hci.url,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
    84
              "",
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 32649
diff changeset
    85
              AuthenticatorKeys.getKey(hci.authenticator));
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    86
        this.hci = hci;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return true if this authentication supports preemptive authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    92
    @Override
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
    93
    public boolean supportsPreemptiveAuthorization() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
24135
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
    98
     * Find out if the HttpCallerInfo supports Negotiate protocol.
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
    99
     * @return true if supported
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   100
     */
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   101
    public static boolean isSupported(HttpCallerInfo hci) {
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   102
        ClassLoader loader = null;
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   103
        try {
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   104
            loader = Thread.currentThread().getContextClassLoader();
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   105
        } catch (SecurityException se) {
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   106
            if (logger.isLoggable(PlatformLogger.Level.FINER)) {
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   107
                logger.finer("NegotiateAuthentication: " +
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   108
                    "Attempt to get the context class loader failed - " + se);
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   109
            }
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   110
        }
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   111
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   112
        if (loader != null) {
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   113
            // Lock on the class loader instance to avoid the deadlock engaging
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   114
            // the lock in "ClassLoader.loadClass(String, boolean)" method.
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   115
            synchronized (loader) {
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   116
                return isSupportedImpl(hci);
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   117
            }
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   118
        }
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   119
        return isSupportedImpl(hci);
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   120
    }
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   121
f273b57ed7e1 8032832: Applet/browser deadlocks, when IIS integrated authentication is used
alitvinov
parents: 23010
diff changeset
   122
    /**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   123
     * Find out if the HttpCallerInfo supports Negotiate protocol. In order to
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   124
     * find out yes or no, an initialization of a Negotiator object against it
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   125
     * is tried. The generated object will be cached under the name of ths
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   126
     * hostname at a success try.<br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   128
     * If this method is called for the second time on an HttpCallerInfo with
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   129
     * the same hostname, the answer is retrieved from cache.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return true if supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   133
    private static boolean isSupportedImpl(HttpCallerInfo hci) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   134
        negotiateLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   135
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   136
            if (supported == null) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   137
                supported = new HashMap<>();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   138
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   139
            String hostname = hci.host;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   140
            hostname = hostname.toLowerCase();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   141
            if (supported.containsKey(hostname)) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   142
                return supported.get(hostname);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   143
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   145
            Negotiator neg = Negotiator.getNegotiator(hci);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   146
            if (neg != null) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   147
                supported.put(hostname, true);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   148
                // the only place cache.put is called. here we can make sure
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   149
                // the object is valid and the oneToken inside is not null
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   150
                if (cache == null) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   151
                    cache = new ThreadLocal<>() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   152
                        @Override
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   153
                        protected HashMap<String, Negotiator> initialValue() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   154
                            return new HashMap<>();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   155
                        }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   156
                    };
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   157
                }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   158
                cache.get().put(hostname, neg);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   159
                return true;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   160
            } else {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   161
                supported.put(hostname, false);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   162
                return false;
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   163
            }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   164
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   165
            negotiateLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   169
    private static HashMap<String, Negotiator> getCache() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   170
        negotiateLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   171
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   172
            if (cache == null) return null;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   173
            return cache.get();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   174
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   175
            negotiateLock.unlock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   176
        }
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   177
    }
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   178
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   179
    @Override
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   180
    protected boolean useAuthCache() {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   181
        return super.useAuthCache() && cacheSPNEGO;
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   182
    }
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   183
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Not supported. Must use the setHeaders() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   187
    @Override
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   188
    public String getHeaderValue(URL url, String method) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        throw new RuntimeException ("getHeaderValue not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Check if the header indicates that the current auth. parameters are stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * If so, then replace the relevant field with the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * and return true. Otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * returning true means the request can be retried with the same userid/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * returning false means we have to go back to the user to ask for a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * username password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   200
    @Override
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   201
    public boolean isAuthorizationStale (String header) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return false; /* should not be called for Negotiate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Set header(s) on the given connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param conn The connection to apply the header(s) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param p A source of header values for this connection, not used because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          HeaderParser converts the fields to lower case, use raw instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param raw The raw header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return true if all goes well, false if no headers were set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   213
    @Override
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57956
diff changeset
   214
    public boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            String response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            byte[] incoming = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            String[] parts = raw.split("\\s+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (parts.length > 1) {
15258
dd5001103120 8006153: HTTP protocol handler authenication should use Base64 API
chegar
parents: 5506
diff changeset
   221
                incoming = Base64.getDecoder().decode(parts[1]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
15258
dd5001103120 8006153: HTTP protocol handler authenication should use Base64 API
chegar
parents: 5506
diff changeset
   223
            response = hci.scheme + " " + Base64.getEncoder().encodeToString(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        incoming==null?firstToken():nextToken(incoming));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            conn.setAuthenticationProperty(getHeaderName(), response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * return the first token.
32227
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 25859
diff changeset
   235
     * @return the token
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   236
     * @throws IOException if <code>Negotiator.getNegotiator()</code> or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *                     <code>Negotiator.firstToken()</code> failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private byte[] firstToken() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        negotiator = null;
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   241
        HashMap <String, Negotiator> cachedMap = getCache();
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   242
        if (cachedMap != null) {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   243
            negotiator = cachedMap.get(getHost());
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   244
            if (negotiator != null) {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 42351
diff changeset
   245
                cachedMap.remove(getHost()); // so that it is only used once
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (negotiator == null) {
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   249
            negotiator = Negotiator.getNegotiator(hci);
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3952
diff changeset
   250
            if (negotiator == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                IOException ioe = new IOException("Cannot initialize Negotiator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return negotiator.firstToken();
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
     * return more tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param token the token to be fed into <code>negotiator.nextToken()</code>
32227
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 25859
diff changeset
   262
     * @return the token
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws IOException if <code>negotiator.nextToken()</code> throws Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *  May happen if the input token is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private byte[] nextToken(byte[] token) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return negotiator.nextToken(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // MS will send a final WWW-Authenticate even if the status is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // 200 OK. The token can be fed into initSecContext() again to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    // if the server can be trusted. This is not the same concept as Digest's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    // Authentication-Info header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // Currently we ignore this header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}