From db7ea2b7e1321d5906ef218cd6959b62b7cc55f7 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 12 Dec 2006 23:58:25 +0000 Subject: [PATCH] httpclient patch for iis proxy compatibility (use basic instead of ntlm auth) git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@4 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../httpclient/auth/HttpAuthenticator.java | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java diff --git a/src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java b/src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java new file mode 100644 index 00000000..bbaaa866 --- /dev/null +++ b/src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java @@ -0,0 +1,169 @@ +// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov. +// Jad home page: http://kpdus.tripod.com/jad.html +// Decompiler options: packimports(3) +// Source File Name: HttpAuthenticator.java + +package org.apache.commons.httpclient.auth; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.httpclient.*; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +// Referenced classes of package org.apache.commons.httpclient.auth: +// NTLMScheme, DigestScheme, BasicScheme, AuthenticationException, +// MalformedChallengeException, AuthChallengeParser, AuthScheme + +public final class HttpAuthenticator { + + public HttpAuthenticator() { + } + + public static AuthScheme selectAuthScheme(Header challenges[]) + throws MalformedChallengeException { + LOG.trace("enter HttpAuthenticator.selectAuthScheme(Header[])"); + if (challenges == null) + throw new IllegalArgumentException("Array of challenges may not be null"); + if (challenges.length == 0) + throw new IllegalArgumentException("Array of challenges may not be empty"); + String challenge = null; + Map challengemap = new HashMap(challenges.length); + for (int i = 0; i < challenges.length; i++) { + challenge = challenges[i].getValue(); + String s = AuthChallengeParser.extractScheme(challenge); + challengemap.put(s, challenge); + } + + challenge = (String) challengemap.get("digest"); + if (challenge != null) + return new DigestScheme(challenge); + challenge = (String) challengemap.get("basic"); + if (challenge != null) + return new BasicScheme(challenge); + // patch ntlm last priority + challenge = (String) challengemap.get("ntlm"); + if (challenge != null) + return new NTLMScheme(challenge); + else + throw new UnsupportedOperationException("Authentication scheme(s) not supported: " + challengemap.toString()); + } + + private static boolean doAuthenticateDefault(HttpMethod method, HttpConnection conn, HttpState state, boolean proxy) + throws AuthenticationException { + if (method == null) + throw new IllegalArgumentException("HTTP method may not be null"); + if (state == null) + throw new IllegalArgumentException("HTTP state may not be null"); + String host = null; + if (conn != null) + host = proxy ? conn.getProxyHost() : conn.getHost(); + org.apache.commons.httpclient.Credentials credentials = proxy ? state.getProxyCredentials(null, host) : state.getCredentials(null, host); + if (credentials == null) { + if (LOG.isWarnEnabled()) + LOG.warn("Default credentials for " + host + " not available"); + return false; + } + if (!(credentials instanceof UsernamePasswordCredentials)) + throw new AuthenticationException("Credentials cannot be used for basic authentication: " + credentials.toString()); + String auth = BasicScheme.authenticate((UsernamePasswordCredentials) credentials); + if (auth != null) { + String s = proxy ? "Proxy-Authorization" : "Authorization"; + method.setRequestHeader(s, auth); + return true; + } else { + return false; + } + } + + public static boolean authenticateDefault(HttpMethod method, HttpConnection conn, HttpState state) + throws AuthenticationException { + LOG.trace("enter HttpAuthenticator.authenticateDefault(HttpMethod, HttpConnection, HttpState)"); + return doAuthenticateDefault(method, conn, state, false); + } + + public static boolean authenticateProxyDefault(HttpMethod method, HttpConnection conn, HttpState state) + throws AuthenticationException { + LOG.trace("enter HttpAuthenticator.authenticateProxyDefault(HttpMethod, HttpState)"); + return doAuthenticateDefault(method, conn, state, true); + } + + private static boolean doAuthenticate(AuthScheme authscheme, HttpMethod method, HttpConnection conn, HttpState state, boolean proxy) + throws AuthenticationException { + if (authscheme == null) + throw new IllegalArgumentException("Authentication scheme may not be null"); + if (method == null) + throw new IllegalArgumentException("HTTP method may not be null"); + if (state == null) + throw new IllegalArgumentException("HTTP state may not be null"); + String host = null; + if (conn != null) + if (proxy) { + host = conn.getProxyHost(); + } else { + host = conn.getVirtualHost(); + if (host == null) + host = conn.getHost(); + } + String realm = authscheme.getRealm(); + if (LOG.isDebugEnabled()) { + StringBuffer buffer = new StringBuffer(); + buffer.append("Authenticating with the "); + if (realm == null) { + buffer.append("default"); + } else { + buffer.append('\''); + buffer.append(realm); + buffer.append('\''); + } + buffer.append(" authentication realm at "); + buffer.append(host); + LOG.debug(buffer.toString()); + } + if (realm == null) + realm = host; + org.apache.commons.httpclient.Credentials credentials = proxy ? state.getProxyCredentials(realm, host) : state.getCredentials(realm, host); + if (credentials == null) + throw new AuthenticationException("No credentials available for the '" + authscheme.getRealm() + "' authentication realm at " + host); + String auth = authscheme.authenticate(credentials, method.getName(), method.getPath()); + if (auth != null) { + String s = proxy ? "Proxy-Authorization" : "Authorization"; + method.setRequestHeader(s, auth); + return true; + } else { + return false; + } + } + + public static boolean authenticate(AuthScheme authscheme, HttpMethod method, HttpConnection conn, HttpState state) + throws AuthenticationException { + LOG.trace("enter HttpAuthenticator.authenticate(AuthScheme, HttpMethod, HttpConnection, HttpState)"); + return doAuthenticate(authscheme, method, conn, state, false); + } + + public static boolean authenticateProxy(AuthScheme authscheme, HttpMethod method, HttpConnection conn, HttpState state) + throws AuthenticationException { + LOG.trace("enter HttpAuthenticator.authenticateProxy(AuthScheme, HttpMethod, HttpState)"); + return doAuthenticate(authscheme, method, conn, state, true); + } + + static Class _mthclass$(String x0) { + try { + return Class.forName(x0); + } + catch (ClassNotFoundException x1) { + throw new NoClassDefFoundError(x1.getMessage()); + } + } + + private static final Log LOG; + public static final String WWW_AUTH = "WWW-Authenticate"; + public static final String WWW_AUTH_RESP = "Authorization"; + public static final String PROXY_AUTH = "Proxy-Authenticate"; + public static final String PROXY_AUTH_RESP = "Proxy-Authorization"; + + static { + LOG = LogFactory.getLog(org.apache.commons.httpclient.auth.HttpAuthenticator.class); + } +}