Showing the changes to java/pkg/sun/net/www/protocol/http/BasicAuthentication.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,212 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.net.www.protocol.http; | ||
| 27 | |||
| 28 | import java.net.URL; | ||
| 29 | import java.net.URI; | ||
| 30 | import java.net.URISyntaxException; | ||
| 31 | import java.net.PasswordAuthentication; | ||
| 32 | import java.io.IOException; | ||
| 33 | import java.io.OutputStream; | ||
| 34 | import sun.net.www.HeaderParser; | ||
| 35 | import sun.misc.BASE64Encoder; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * BasicAuthentication: Encapsulate an http server authentication using | ||
| 39 | * the "basic" scheme. | ||
| 40 | * | ||
| 41 | * @author Bill Foote | ||
| 42 | */ | ||
| 43 | |||
| 44 | |||
| 45 | class BasicAuthentication extends AuthenticationInfo { | ||
| 46 | |||
| 47 | private static final long serialVersionUID = 100L; | ||
| 48 | |||
| 49 | /** The authentication string for this host, port, and realm. This is | ||
| 50 | a simple BASE64 encoding of "login:password". */ | ||
| 51 | String auth; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Create a BasicAuthentication | ||
| 55 | */ | ||
| 56 | public BasicAuthentication(boolean isProxy, String host, int port, | ||
| 57 | String realm, PasswordAuthentication pw) { | ||
| 58 | super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, | ||
| 59 | AuthScheme.BASIC, host, port, realm); | ||
| 60 | String plain = pw.getUserName() + ":"; | ||
| 61 | byte[] nameBytes = null; | ||
| 62 | try { | ||
| 63 | nameBytes = plain.getBytes("ISO-8859-1"); | ||
| 64 | } catch (java.io.UnsupportedEncodingException uee) { | ||
| 65 | assert false; | ||
| 66 | } | ||
| 67 | |||
| 68 | // get password bytes | ||
| 69 | char[] passwd = pw.getPassword(); | ||
| 70 | byte[] passwdBytes = new byte[passwd.length]; | ||
| 71 | for (int i=0; i<passwd.length; i++) | ||
| 72 | passwdBytes[i] = (byte)passwd[i]; | ||
| 73 | |||
| 74 | // concatenate user name and password bytes and encode them | ||
| 75 | byte[] concat = new byte[nameBytes.length + passwdBytes.length]; | ||
| 76 | System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length); | ||
| 77 | System.arraycopy(passwdBytes, 0, concat, nameBytes.length, | ||
| 78 | passwdBytes.length); | ||
| 79 | this.auth = "Basic " + (new BasicBASE64Encoder()).encode(concat); | ||
| 80 | this.pw = pw; | ||
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Create a BasicAuthentication | ||
| 85 | */ | ||
| 86 | public BasicAuthentication(boolean isProxy, String host, int port, | ||
| 87 | String realm, String auth) { | ||
| 88 | super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, | ||
| 89 | AuthScheme.BASIC, host, port, realm); | ||
| 90 | this.auth = "Basic " + auth; | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Create a BasicAuthentication | ||
| 95 | */ | ||
| 96 | public BasicAuthentication(boolean isProxy, URL url, String realm, | ||
| 97 | PasswordAuthentication pw) { | ||
| 98 | super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, | ||
| 99 | AuthScheme.BASIC, url, realm); | ||
| 100 | String plain = pw.getUserName() + ":"; | ||
| 101 | byte[] nameBytes = null; | ||
| 102 | try { | ||
| 103 | nameBytes = plain.getBytes("ISO-8859-1"); | ||
| 104 | } catch (java.io.UnsupportedEncodingException uee) { | ||
| 105 | assert false; | ||
| 106 | } | ||
| 107 | |||
| 108 | // get password bytes | ||
| 109 | char[] passwd = pw.getPassword(); | ||
| 110 | byte[] passwdBytes = new byte[passwd.length]; | ||
| 111 | for (int i=0; i<passwd.length; i++) | ||
| 112 | passwdBytes[i] = (byte)passwd[i]; | ||
| 113 | |||
| 114 | // concatenate user name and password bytes and encode them | ||
| 115 | byte[] concat = new byte[nameBytes.length + passwdBytes.length]; | ||
| 116 | System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length); | ||
| 117 | System.arraycopy(passwdBytes, 0, concat, nameBytes.length, | ||
| 118 | passwdBytes.length); | ||
| 119 | this.auth = "Basic " + (new BasicBASE64Encoder()).encode(concat); | ||
| 120 | this.pw = pw; | ||
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * Create a BasicAuthentication | ||
| 125 | */ | ||
| 126 | public BasicAuthentication(boolean isProxy, URL url, String realm, | ||
| 127 | String auth) { | ||
| 128 | super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, | ||
| 129 | AuthScheme.BASIC, url, realm); | ||
| 130 | this.auth = "Basic " + auth; | ||
| 131 | } | ||
| 132 | |||
| 133 | /** | ||
| 134 | * @return true if this authentication supports preemptive authorization | ||
| 135 | */ | ||
| 136 | @Override | ||
| 137 | public boolean supportsPreemptiveAuthorization() { | ||
| 138 | return true; | ||
| 139 | } | ||
| 140 | |||
| 141 | /** | ||
| 142 | * Set header(s) on the given connection. This will only be called for | ||
| 143 | * definitive (i.e. non-preemptive) authorization. | ||
| 144 | * @param conn The connection to apply the header(s) to | ||
| 145 | * @param p A source of header values for this connection, if needed. | ||
| 146 | * @param raw The raw header values for this connection, if needed. | ||
| 147 | * @return true if all goes well, false if no headers were set. | ||
| 148 | */ | ||
| 149 | @Override | ||
| 150 | public boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) { | ||
| 151 | conn.setAuthenticationProperty(getHeaderName(), getHeaderValue(null,null)); | ||
| 152 | return true; | ||
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * @return the value of the HTTP header this authentication wants set | ||
| 157 | */ | ||
| 158 | @Override | ||
| 159 | public String getHeaderValue(URL url, String method) { | ||
| 160 | /* For Basic the authorization string does not depend on the request URL | ||
| 161 | * or the request method | ||
| 162 | */ | ||
| 163 | return auth; | ||
| 164 | } | ||
| 165 | |||
| 166 | /** | ||
| 167 | * For Basic Authentication, the security parameters can never be stale. | ||
| 168 | * In other words there is no possibility to reuse the credentials. | ||
| 169 | * They are always either valid or invalid. | ||
| 170 | */ | ||
| 171 | @Override | ||
| 172 | public boolean isAuthorizationStale (String header) { | ||
| 173 | return false; | ||
| 174 | } | ||
| 175 | |||
| 176 | /** | ||
| 177 | * @return the common root path between npath and path. | ||
| 178 | * This is used to detect when we have an authentication for two | ||
| 179 | * paths and the root of th authentication space is the common root. | ||
| 180 | */ | ||
| 181 | |||
| 182 | static String getRootPath(String npath, String opath) { | ||
| 183 | int index = 0; | ||
| 184 | int toindex; | ||
| 185 | |||
| 186 | /* Must normalize so we don't get confused by ../ and ./ segments */ | ||
| 187 | try { | ||
| 188 | npath = new URI (npath).normalize().getPath(); | ||
| 189 | opath = new URI (opath).normalize().getPath(); | ||
| 190 | } catch (URISyntaxException e) { | ||
| 191 | /* ignore error and use the old value */ | ||
| 192 | } | ||
| 193 | |||
| 194 | while (index < opath.length()) { | ||
| 195 | toindex = opath.indexOf('/', index+1); | ||
| 196 | if (toindex != -1 && opath.regionMatches(0, npath, 0, toindex+1)) | ||
| 197 | index = toindex; | ||
| 198 | else | ||
| 199 | return opath.substring(0, index+1); | ||
| 200 | } | ||
| 201 | /*should not reach here. If we do simply return npath*/ | ||
| 202 | return npath; | ||
| 203 | } | ||
| 204 | |||
| 205 | /* It is never expected that the header value will exceed the bytesPerLine */ | ||
| 206 | private class BasicBASE64Encoder extends BASE64Encoder { | ||
| 207 | @Override | ||
| 208 | protected int bytesPerLine() { | ||
| 209 | return (10000); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||