1 31 32 package org.apache.commons.httpclient.auth; 33 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import org.apache.commons.httpclient.Header; 37 import org.apache.commons.httpclient.HttpConnection; 38 import org.apache.commons.httpclient.HttpMethod; 39 import org.apache.commons.httpclient.Credentials; 40 import org.apache.commons.httpclient.HttpState; 41 import org.apache.commons.httpclient.UsernamePasswordCredentials; 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 71 public final class HttpAuthenticator { 72 73 74 private static final Log LOG = LogFactory.getLog(HttpAuthenticator.class); 75 76 79 public static final String WWW_AUTH = "WWW-Authenticate"; 80 81 82 85 public static final String WWW_AUTH_RESP = "Authorization"; 86 87 88 91 public static final String PROXY_AUTH = "Proxy-Authenticate"; 92 93 94 97 public static final String PROXY_AUTH_RESP = "Proxy-Authorization"; 98 99 117 public static AuthScheme selectAuthScheme(final Header[] challenges) 118 throws MalformedChallengeException { 119 LOG.trace("enter HttpAuthenticator.selectAuthScheme(Header[])"); 120 if (challenges == null) { 121 throw new IllegalArgumentException ("Array of challenges may not be null"); 122 } 123 if (challenges.length == 0) { 124 throw new IllegalArgumentException ("Array of challenges may not be empty"); 125 } 126 String challenge = null; 127 Map challengemap = new HashMap (challenges.length); 128 for (int i = 0; i < challenges.length; i++) { 129 challenge = challenges[i].getValue(); 130 String s = AuthChallengeParser.extractScheme(challenge); 131 challengemap.put(s, challenge); 132 } 133 challenge = (String ) challengemap.get("ntlm"); 134 if (challenge != null) { 135 return new NTLMScheme(challenge); 136 } 137 challenge = (String ) challengemap.get("digest"); 138 if (challenge != null) { 139 return new DigestScheme(challenge); 140 } 141 challenge = (String ) challengemap.get("basic"); 142 if (challenge != null) { 143 return new BasicScheme(challenge); 144 } 145 throw new UnsupportedOperationException ( 146 "Authentication scheme(s) not supported: " + challengemap.toString()); 147 } 148 149 150 private static boolean doAuthenticateDefault( 151 HttpMethod method, 152 HttpConnection conn, 153 HttpState state, 154 boolean proxy) 155 throws AuthenticationException { 156 if (method == null) { 157 throw new IllegalArgumentException ("HTTP method may not be null"); 158 } 159 if (state == null) { 160 throw new IllegalArgumentException ("HTTP state may not be null"); 161 } 162 String host = null; 163 if (conn != null) { 164 host = proxy ? conn.getProxyHost() : conn.getHost(); 165 } 166 Credentials credentials = proxy 167 ? state.getProxyCredentials(null, host) : state.getCredentials(null, host); 168 if (credentials == null) { 169 if (LOG.isWarnEnabled()) { 170 LOG.warn("Default credentials for " + host + " not available"); 171 } 172 return false; 173 } 174 if (!(credentials instanceof UsernamePasswordCredentials)) { 175 throw new AuthenticationException( 176 "Credentials cannot be used for basic authentication: " 177 + credentials.toString()); 178 } 179 String auth = BasicScheme.authenticate((UsernamePasswordCredentials) credentials); 180 if (auth != null) { 181 String s = proxy ? PROXY_AUTH_RESP : WWW_AUTH_RESP; 182 method.setRequestHeader(s, auth); 183 return true; 184 } else { 185 return false; 186 } 187 } 188 189 190 208 public static boolean authenticateDefault( 209 HttpMethod method, 210 HttpConnection conn, 211 HttpState state) 212 throws AuthenticationException { 213 LOG.trace( 214 "enter HttpAuthenticator.authenticateDefault(HttpMethod, HttpConnection, HttpState)"); 215 return doAuthenticateDefault(method, conn, state, false); 216 } 217 218 219 237 public static boolean authenticateProxyDefault( 238 HttpMethod method, 239 HttpConnection conn, 240 HttpState state) 241 throws AuthenticationException { 242 LOG.trace("enter HttpAuthenticator.authenticateProxyDefault(HttpMethod, HttpState)"); 243 return doAuthenticateDefault(method, conn, state, true); 244 } 245 246 247 private static boolean doAuthenticate( 248 AuthScheme authscheme, 249 HttpMethod method, 250 HttpConnection conn, 251 HttpState state, 252 boolean proxy) 253 throws AuthenticationException { 254 if (authscheme == null) { 255 throw new IllegalArgumentException ("Authentication scheme may not be null"); 256 } 257 if (method == null) { 258 throw new IllegalArgumentException ("HTTP method may not be null"); 259 } 260 if (state == null) { 261 throw new IllegalArgumentException ("HTTP state may not be null"); 262 } 263 String host = null; 264 if (conn != null) { 265 if (proxy) { 266 host = conn.getProxyHost(); 267 } else { 268 host = conn.getVirtualHost(); 269 if (host == null) { 270 host = conn.getHost(); 271 } 272 } 273 } 274 String realm = authscheme.getRealm(); 275 if (LOG.isDebugEnabled()) { 276 StringBuffer buffer = new StringBuffer (); 277 buffer.append("Authenticating with the "); 278 if (realm == null) { 279 buffer.append("default"); 280 } else { 281 buffer.append('\''); 282 buffer.append(realm); 283 buffer.append('\''); 284 } 285 buffer.append(" authentication realm at "); 286 buffer.append(host); 287 LOG.debug(buffer.toString()); 288 } 289 if (realm == null) { 291 realm = host; 292 } 293 Credentials credentials = proxy 294 ? state.getProxyCredentials(realm, host) 295 : state.getCredentials(realm, host); 296 if (credentials == null) { 297 throw new AuthenticationException( 298 "No credentials available for the '" + authscheme.getRealm() 299 + "' authentication realm at " + host); 300 } 301 String auth = authscheme.authenticate(credentials, method.getName(), method.getPath()); 302 if (auth != null) { 303 String s = proxy ? PROXY_AUTH_RESP : WWW_AUTH_RESP; 304 method.setRequestHeader(s, auth); 305 return true; 306 } else { 307 return false; 308 } 309 } 310 311 329 public static boolean authenticate( 330 AuthScheme authscheme, 331 HttpMethod method, 332 HttpConnection conn, 333 HttpState state) 334 throws AuthenticationException { 335 LOG.trace( 336 "enter HttpAuthenticator.authenticate(AuthScheme, HttpMethod, HttpConnection, " 337 + "HttpState)"); 338 return doAuthenticate(authscheme, method, conn, state, false); 339 } 340 341 342 361 public static boolean authenticateProxy( 362 AuthScheme authscheme, 363 HttpMethod method, 364 HttpConnection conn, 365 HttpState state 366 ) throws AuthenticationException { 367 LOG.trace("enter HttpAuthenticator.authenticateProxy(AuthScheme, HttpMethod, HttpState)"); 368 return doAuthenticate(authscheme, method, conn, state, true); 369 } 370 } 371 | Popular Tags |