From 38fbc96da3bff3f335231ce7d68a4608970c080f Mon Sep 17 00:00:00 2001 From: mguessan Date: Wed, 15 Jan 2014 22:10:43 +0000 Subject: [PATCH] Fix reauthentication issue: separate domain from username in credentials git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2210 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../http/DavGatewayHttpClientFacade.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/java/davmail/http/DavGatewayHttpClientFacade.java b/src/java/davmail/http/DavGatewayHttpClientFacade.java index eafb38f7..0b88aeec 100644 --- a/src/java/davmail/http/DavGatewayHttpClientFacade.java +++ b/src/java/davmail/http/DavGatewayHttpClientFacade.java @@ -123,7 +123,15 @@ public final class DavGatewayHttpClientFacade { public static void setCredentials(HttpClient httpClient, String userName, String password) { // some Exchange servers redirect to a different host for freebusy, use wide auth scope AuthScope authScope = new AuthScope(null, -1); - httpClient.getState().setCredentials(authScope, new NTCredentials(userName, password, "UNKNOWN", "")); + int backSlashIndex = userName.indexOf('\\'); + if (needNTLM && backSlashIndex >= 0) { + // separate domain from username in credentials + String domain = userName.substring(0, backSlashIndex); + userName = userName.substring(backSlashIndex + 1); + httpClient.getState().setCredentials(authScope, new NTCredentials(userName, password, "UNKNOWN", domain)); + } else { + httpClient.getState().setCredentials(authScope, new NTCredentials(userName, password, "UNKNOWN", "")); + } } /** @@ -190,7 +198,7 @@ public final class DavGatewayHttpClientFacade { try { java.net.URI uri = new java.net.URI(url); if (isNoProxyFor(uri)) { - LOGGER.debug("no proxy for "+uri.getHost()); + LOGGER.debug("no proxy for " + uri.getHost()); } else if (useSystemProxies) { // get proxy for url from system settings System.setProperty("java.net.useSystemProxies", "true"); @@ -205,10 +213,10 @@ public final class DavGatewayHttpClientFacade { proxyPassword = Settings.getProperty("davmail.proxyPassword"); } } else if (enableProxy) { - proxyHost = Settings.getProperty("davmail.proxyHost"); - proxyPort = Settings.getIntProperty("davmail.proxyPort"); - proxyUser = Settings.getProperty("davmail.proxyUser"); - proxyPassword = Settings.getProperty("davmail.proxyPassword"); + proxyHost = Settings.getProperty("davmail.proxyHost"); + proxyPort = Settings.getIntProperty("davmail.proxyPort"); + proxyUser = Settings.getProperty("davmail.proxyUser"); + proxyPassword = Settings.getProperty("davmail.proxyPassword"); } } catch (URISyntaxException e) { throw new DavMailException("LOG_INVALID_URL", url); @@ -598,20 +606,13 @@ public final class DavGatewayHttpClientFacade { authPrefs.add(AuthPolicy.BASIC); httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); + // make sure NTLM is always active + needNTLM = true; + // separate domain from username in credentials AuthScope authScope = new AuthScope(null, -1); NTCredentials credentials = (NTCredentials) httpClient.getState().getCredentials(authScope); - String userName = credentials.getUserName(); - int backSlashIndex = userName.indexOf('\\'); - if (backSlashIndex >= 0) { - String domain = userName.substring(0, backSlashIndex); - userName = userName.substring(backSlashIndex + 1); - credentials = new NTCredentials(userName, credentials.getPassword(), "UNKNOWN", domain); - httpClient.getState().setCredentials(authScope, credentials); - } - - // make sure NTLM is always active - needNTLM = true; + setCredentials(httpClient, credentials.getUserName(), credentials.getPassword()); } /**