jdk/src/share/classes/sun/net/www/protocol/http/BasicAuthentication.java
author weijun
Tue, 09 Jun 2009 14:17:05 +0800
changeset 2942 37d9baeb7518
parent 2 90ce3da70b43
child 3859 8b82336dedb3
permissions -rw-r--r--
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication() Reviewed-by: chegar, valeriep
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 1997-2003 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.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.PasswordAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.net.www.HeaderParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * BasicAuthentication: Encapsulate an http server authentication using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the "basic" scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class BasicAuthentication extends AuthenticationInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final long serialVersionUID = 100L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final char BASIC_AUTH = 'B';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /** The authentication string for this host, port, and realm.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        a simple BASE64 encoding of "login:password".    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    String auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Create a BasicAuthentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public BasicAuthentication(boolean isProxy, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                               String realm, PasswordAuthentication pw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
              BASIC_AUTH, host, port, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        String plain = pw.getUserName() + ":";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        byte[] nameBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            nameBytes = plain.getBytes("ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // get password bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        char[] passwd = pw.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        byte[] passwdBytes = new byte[passwd.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (int i=0; i<passwd.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            passwdBytes[i] = (byte)passwd[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // concatenate user name and password bytes and encode them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        byte[] concat = new byte[nameBytes.length + passwdBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.arraycopy(passwdBytes, 0, concat, nameBytes.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                         passwdBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.auth = "Basic " + (new sun.misc.BASE64Encoder()).encode(concat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.pw = pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Create a BasicAuthentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public BasicAuthentication(boolean isProxy, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                               String realm, String auth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
              BASIC_AUTH, host, port, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.auth = "Basic " + auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Create a BasicAuthentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public BasicAuthentication(boolean isProxy, URL url, String realm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                   PasswordAuthentication pw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
              BASIC_AUTH, url, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String plain = pw.getUserName() + ":";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        byte[] nameBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            nameBytes = plain.getBytes("ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // get password bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        char[] passwd = pw.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        byte[] passwdBytes = new byte[passwd.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i=0; i<passwd.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            passwdBytes[i] = (byte)passwd[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // concatenate user name and password bytes and encode them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        byte[] concat = new byte[nameBytes.length + passwdBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.arraycopy(passwdBytes, 0, concat, nameBytes.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                         passwdBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.auth = "Basic " + (new sun.misc.BASE64Encoder()).encode(concat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.pw = pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Create a BasicAuthentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public BasicAuthentication(boolean isProxy, URL url, String realm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                   String auth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
              BASIC_AUTH, url, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.auth = "Basic " + auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return true if this authentication supports preemptive authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    boolean supportsPreemptiveAuthorization() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return the name of the HTTP header this authentication wants set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    String getHeaderName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (type == SERVER_AUTHENTICATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return "Authorization";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return "Proxy-authorization";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Set header(s) on the given connection. This will only be called for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * definitive (i.e. non-preemptive) authorization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param conn The connection to apply the header(s) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param p A source of header values for this connection, if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param raw The raw header values for this connection, if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return true if all goes well, false if no headers were set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        conn.setAuthenticationProperty(getHeaderName(), getHeaderValue(null,null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return the value of the HTTP header this authentication wants set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    String getHeaderValue(URL url, String method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /* For Basic the authorization string does not depend on the request URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * or the request method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * For Basic Authentication, the security parameters can never be stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * In other words there is no possibility to reuse the credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * They are always either valid or invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    boolean isAuthorizationStale (String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * For Basic Authentication, there is no security information in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    void checkResponse (String header, String method, URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return the common root path between npath and path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * This is used to detect when we have an authentication for two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * paths and the root of th authentication space is the common root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static String getRootPath(String npath, String opath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int toindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /* Must normalize so we don't get confused by ../ and ./ segments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            npath = new URI (npath).normalize().getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            opath = new URI (opath).normalize().getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            /* ignore error and use the old value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        while (index < opath.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            toindex = opath.indexOf('/', index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (toindex != -1 && opath.regionMatches(0, npath, 0, toindex+1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                index = toindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                return opath.substring(0, index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /*should not reach here. If we do simply return npath*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return npath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}