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