jdk/src/share/classes/sun/net/www/protocol/http/NegotiateAuthentication.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2942 37d9baeb7518
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.net.www.HeaderParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.misc.BASE64Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.misc.BASE64Encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.PasswordAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final long serialVersionUID = 100L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String scheme = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final char NEGOTIATE_AUTH = 'S';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final char KERBEROS_AUTH = 'K';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // These maps are used to manage the GSS availability for diffrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // hosts. The key for both maps is the host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // <code>supported</code> is set when isSupported is checked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // if it's true, a cached Negotiator is put into <code>cache</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // the cache can be used only once, so after the first use, it's cleaned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static HashMap <String, Boolean> supported = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static HashMap <String, Negotiator> cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // The HTTP Negotiate Helper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private Negotiator negotiator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    * Constructor used for WWW entries. <code>pw</code> is not used because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    * for GSS there is only one single PasswordAuthentication which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    * independant of host/port/... info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public NegotiateAuthentication(boolean isProxy, URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            PasswordAuthentication pw, String scheme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(isProxy?PROXY_AUTHENTICATION:SERVER_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                NEGOTIATE_AUTH, url, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.scheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    * Constructor used for proxy entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public NegotiateAuthentication(boolean isProxy, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                PasswordAuthentication pw, String scheme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        super(isProxy?PROXY_AUTHENTICATION:SERVER_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                NEGOTIATE_AUTH,host, port, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.scheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return true if this authentication supports preemptive authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    boolean supportsPreemptiveAuthorization() {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Find out if a hostname supports Negotiate protocol. In order to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * out yes or no, an initialization of a Negotiator object against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * hostname and scheme is tried. The generated object will be cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * under the name of hostname at a success try.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * If this method is called for the second time on a hostname, the answer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * already saved in <code>supported</code>, so no need to try again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param hostname hostname to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param scheme scheme to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return true if supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    synchronized public static boolean isSupported(String hostname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            String scheme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (supported == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            supported = new HashMap <String, Boolean>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            cache = new HashMap <String, Negotiator>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        hostname = hostname.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (supported.containsKey(hostname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return supported.get(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Negotiator neg = Negotiator.getSupported(hostname, scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            supported.put(hostname, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // the only place cache.put is called. here we can make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // the object is valid and the oneToken inside is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            cache.put(hostname, neg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            supported.put(hostname, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the name of the HTTP header this authentication wants to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    String getHeaderName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (type == SERVER_AUTHENTICATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return "Authorization";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return "Proxy-Authorization";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Not supported. Must use the setHeaders() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    String getHeaderValue(URL url, String method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        throw new RuntimeException ("getHeaderValue not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Check if the header indicates that the current auth. parameters are stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * If so, then replace the relevant field with the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * and return true. Otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * returning true means the request can be retried with the same userid/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * returning false means we have to go back to the user to ask for a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * username password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    boolean isAuthorizationStale (String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return false; /* should not be called for Negotiate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Set header(s) on the given connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param conn The connection to apply the header(s) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param p A source of header values for this connection, not used because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *          HeaderParser converts the fields to lower case, use raw instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param raw The raw header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return true if all goes well, false if no headers were set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    synchronized boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            String response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            byte[] incoming = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            String[] parts = raw.split("\\s+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (parts.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                incoming = new BASE64Decoder().decodeBuffer(parts[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            response = scheme + " " + new B64Encoder().encode(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        incoming==null?firstToken():nextToken(incoming));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            conn.setAuthenticationProperty(getHeaderName(), response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
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
     * return the first token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @returns the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @throws IOException if <code>Negotiator.getSupported()</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *                     <code>Negotiator.firstToken()</code> failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private byte[] firstToken() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        negotiator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            synchronized(cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                negotiator = cache.get(getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (negotiator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    cache.remove(getHost()); // so that it is only used once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (negotiator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                negotiator = Negotiator.getSupported(getHost(), scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                IOException ioe = new IOException("Cannot initialize Negotiator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return negotiator.firstToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * return more tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param token the token to be fed into <code>negotiator.nextToken()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @returns the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws IOException if <code>negotiator.nextToken()</code> throws Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *  May happen if the input token is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private byte[] nextToken(byte[] token) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return negotiator.nextToken(token);
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
     * no-use for Negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void checkResponse (String header, String method, URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    class B64Encoder extends BASE64Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        protected int bytesPerLine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return 100000;  // as big as it can be, maybe INT_MAX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    // MS will send a final WWW-Authenticate even if the status is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // 200 OK. The token can be fed into initSecContext() again to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // if the server can be trusted. This is not the same concept as Digest's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    // Authentication-Info header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    // Currently we ignore this header.
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
 * This abstract class is a bridge to connect NegotiteAuthentication and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * NegotiatorImpl, so that JAAS and JGSS calls can be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
abstract class Negotiator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static Negotiator getSupported(String hostname, String scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // These lines are equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        //     return new NegotiatorImpl(hostname, scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // The current implementation will make sure NegotiatorImpl is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // directly referenced when compiling, thus smooth the way of building
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // the J2SE platform where HttpURLConnection is a bootstrap class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        Class clazz = Class.forName("sun.net.www.protocol.http.NegotiatorImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        java.lang.reflect.Constructor c = clazz.getConstructor(String.class, String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return (Negotiator) (c.newInstance(hostname, scheme));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    abstract byte[] firstToken() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    abstract byte[] nextToken(byte[] in) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}