jdk/src/share/classes/sun/net/www/protocol/http/NTLMAuthenticationProxy.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8815 9a3f042d307e
child 16839 d0f2e97b7359
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8815
diff changeset
     2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
     4
 *
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
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
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
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.
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    10
 *
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    15
 * accompanied this code).
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    16
 *
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
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.
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    24
 */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    25
package sun.net.www.protocol.http;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    26
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    27
import java.net.URL;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    28
import java.net.PasswordAuthentication;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    29
import java.lang.reflect.Constructor;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    30
import java.lang.reflect.Method;
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
    31
import sun.util.logging.PlatformLogger;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    32
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    33
/**
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    34
 * Proxy class for loading NTLMAuthentication, so as to remove static
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    35
 * dependancy.
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    36
 */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    37
class NTLMAuthenticationProxy {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    38
    private static Method supportsTA;
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    39
    private static Method isTrustedSite;
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 3866
diff changeset
    40
    private static final String clazzStr = "sun.net.www.protocol.http.ntlm.NTLMAuthentication";
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    41
    private static final String supportsTAStr = "supportsTransparentAuth";
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    42
    private static final String isTrustedSiteStr = "isTrustedSite";
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    43
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    44
    static final NTLMAuthenticationProxy proxy = tryLoadNTLMAuthentication();
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    45
    static final boolean supported = proxy != null ? true : false;
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    46
    static final boolean supportsTransparentAuth = supported ? supportsTransparentAuth() : false;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    47
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    48
    private final Constructor<? extends AuthenticationInfo> threeArgCtr;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    49
    private final Constructor<? extends AuthenticationInfo> fiveArgCtr;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    50
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    51
    private NTLMAuthenticationProxy(Constructor<? extends AuthenticationInfo> threeArgCtr,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    52
                                    Constructor<? extends AuthenticationInfo> fiveArgCtr) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    53
        this.threeArgCtr = threeArgCtr;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    54
        this.fiveArgCtr = fiveArgCtr;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    55
    }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    56
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    57
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    58
    AuthenticationInfo create(boolean isProxy,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    59
                              URL url,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    60
                              PasswordAuthentication pw) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    61
        try {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    62
            return threeArgCtr.newInstance(isProxy, url, pw);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    63
        } catch (ReflectiveOperationException roe) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
    64
            finest(roe);
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    65
        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    66
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    67
        return null;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    68
    }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    69
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    70
    AuthenticationInfo create(boolean isProxy,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    71
                              String host,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    72
                              int port,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    73
                              PasswordAuthentication pw) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    74
        try {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    75
            return fiveArgCtr.newInstance(isProxy, host, port, pw);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    76
        } catch (ReflectiveOperationException roe) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
    77
            finest(roe);
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    78
        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    79
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    80
        return null;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    81
    }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    82
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    83
    /* Returns true if the NTLM implementation supports transparent
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    84
     * authentication (try with the current users credentials before
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    85
     * prompting for username and password, etc).
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    86
     */
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    87
    private static boolean supportsTransparentAuth() {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
    88
        try {
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    89
            return (Boolean)supportsTA.invoke(null);
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    90
        } catch (ReflectiveOperationException roe) {
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    91
            finest(roe);
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    92
        }
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    93
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    94
        return false;
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    95
    }
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    96
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    97
    /* Transparent authentication should only be tried with a trusted
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    98
     * site ( when running in a secure environment ).
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
    99
     */
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
   100
    public static boolean isTrustedSite(URL url) {
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
   101
        try {
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
   102
            return (Boolean)isTrustedSite.invoke(null, url);
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   103
        } catch (ReflectiveOperationException roe) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   104
            finest(roe);
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   105
        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   106
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   107
        return false;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   108
    }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   109
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   110
    /**
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   111
     * Loads the NTLM authentiation implementation through reflection. If
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   112
     * the class is present, then it must have the required constructors and
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   113
     * method. Otherwise, it is considered an error.
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   114
     */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   115
    @SuppressWarnings("unchecked")
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   116
    private static NTLMAuthenticationProxy tryLoadNTLMAuthentication() {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   117
        Class<? extends AuthenticationInfo> cl;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   118
        Constructor<? extends AuthenticationInfo> threeArg, fiveArg;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   119
        try {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   120
            cl = (Class<? extends AuthenticationInfo>)Class.forName(clazzStr, true, null);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   121
            if (cl != null) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   122
                threeArg = cl.getConstructor(boolean.class,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   123
                                             URL.class,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   124
                                             PasswordAuthentication.class);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   125
                fiveArg = cl.getConstructor(boolean.class,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   126
                                            String.class,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   127
                                            int.class,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   128
                                            PasswordAuthentication.class);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   129
                supportsTA = cl.getDeclaredMethod(supportsTAStr);
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 5506
diff changeset
   130
                isTrustedSite = cl.getDeclaredMethod(isTrustedSiteStr, java.net.URL.class);
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   131
                return new NTLMAuthenticationProxy(threeArg,
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   132
                                                   fiveArg);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   133
            }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   134
        } catch (ClassNotFoundException cnfe) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   135
            finest(cnfe);
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   136
        } catch (ReflectiveOperationException roe) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   137
            throw new AssertionError(roe);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   138
        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   139
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   140
        return null;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   141
    }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   142
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   143
    static void finest(Exception e) {
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   144
        PlatformLogger logger = HttpURLConnection.getHttpLogger();
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   145
        logger.finest("NTLMAuthenticationProxy: " + e);
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   146
    }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents:
diff changeset
   147
}