1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 19:22:22 -05:00

Implement Microsoft Forefront Unified Access Gateway logon form compatibility

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1633 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2011-02-28 22:31:42 +00:00
parent b2ff79a8a3
commit d4942165b4
2 changed files with 48 additions and 28 deletions

View File

@ -34,6 +34,7 @@ import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.log4j.Logger;
import org.htmlcleaner.CommentToken;
import org.htmlcleaner.ContentToken;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
@ -69,6 +70,7 @@ public abstract class ExchangeSession {
USER_NAME_FIELDS.add("txtUserName");
USER_NAME_FIELDS.add("userid");
USER_NAME_FIELDS.add("SafeWordUser");
USER_NAME_FIELDS.add("user_name");
}
protected static final Set<String> PASSWORD_FIELDS = new HashSet<String>();
@ -190,8 +192,8 @@ public abstract class ExchangeSession {
}
}
// avoid 401 roundtrips, only if NTLM is disabled
if (!DavGatewayHttpClientFacade.hasNTLM(httpClient)) {
// avoid 401 roundtrips, only if NTLM is disabled and basic authentication enabled
if (isBasicAuthentication && !DavGatewayHttpClientFacade.hasNTLM(httpClient)) {
httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
}
@ -350,9 +352,9 @@ public abstract class ExchangeSession {
* @return logon method
* @throws IOException on error
*/
protected PostMethod buildLogonMethod(HttpClient httpClient, HttpMethod initmethod) throws IOException {
protected HttpMethod buildLogonMethod(HttpClient httpClient, HttpMethod initmethod) throws IOException {
PostMethod logonMethod = null;
HttpMethod logonMethod = null;
// create an instance of HtmlCleaner
HtmlCleaner cleaner = new HtmlCleaner();
@ -376,13 +378,15 @@ public abstract class ExchangeSession {
logonMethod = new PostMethod(getAbsoluteUri(initmethod, logonMethodPath));
List inputList = logonForm.getElementListByName("input", true);
// retrieve lost inputs attached to body
List inputList = node.getElementListByName("input", true);
for (Object input : inputList) {
String type = ((TagNode) input).getAttributeByName("type");
String name = ((TagNode) input).getAttributeByName("name");
String value = ((TagNode) input).getAttributeByName("value");
if ("hidden".equalsIgnoreCase(type) && name != null && value != null) {
logonMethod.addParameter(name, value);
((PostMethod) logonMethod).addParameter(name, value);
}
// custom login form
if (USER_NAME_FIELDS.contains(name)) {
@ -395,7 +399,7 @@ public abstract class ExchangeSession {
logonMethod = buildLogonMethod(httpClient, newInitMethod);
} else if (TOKEN_FIELDS.contains(name)) {
// one time password, ask user
logonMethod.addParameter(name, DavGatewayOTPPrompt.getOneTimePassword());
((PostMethod) logonMethod).addParameter(name, DavGatewayOTPPrompt.getOneTimePassword());
}
}
} else {
@ -427,6 +431,15 @@ public abstract class ExchangeSession {
HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, src);
logonMethod = buildLogonMethod(httpClient, newInitMethod);
}
} else if (content instanceof ContentToken) {
// Microsoft Forefront Unified Access Gateway redirect
String scriptValue = ((ContentToken) content).getContent();
String location = StringUtil.getToken(scriptValue, "window.location.replace(\"", "\"");
if (location != null) {
LOGGER.debug("Post logon redirect to: " + location);
logonMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, location);
}
}
}
}

View File

@ -55,6 +55,7 @@ public class EwsExchangeSession extends ExchangeSession {
/**
* Message types.
*
* @see http://msdn.microsoft.com/en-us/library/aa565652%28v=EXCHG.140%29.aspx
*/
protected static final Set<String> MESSAGE_TYPES = new HashSet<String>();
@ -153,9 +154,6 @@ public class EwsExchangeSession extends ExchangeSession {
if (locationHeader == null || !"/ews/services.wsdl".equalsIgnoreCase(locationHeader.getValue())) {
throw new IOException("Ews endpoint not available at " + getMethod.getURI().toString());
}
} catch (IOException e) {
LOGGER.debug(e.getMessage());
throw e;
} finally {
getMethod.releaseConnection();
}
@ -191,12 +189,20 @@ public class EwsExchangeSession extends ExchangeSession {
// workaround for Exchange bug: send fake request
internalGetFolder("");
} catch (IOException e) {
// first failover: retry with NTLM
DavGatewayHttpClientFacade.addNTLM(httpClient);
try {
checkEndPointUrl("/ews/exchange.asmx");
// workaround for Exchange bug: send fake request
internalGetFolder("");
} catch (IOException e2) {
LOGGER.debug(e2.getMessage());
try {
// failover, try to retrieve EWS url from autodiscover
checkEndPointUrl(getEwsUrlFromAutoDiscover());
// workaround for Exchange bug: send fake request
internalGetFolder("");
} catch (IOException e2) {
} catch (IOException e3) {
// autodiscover failed and initial exception was authentication failure => throw original exception
if (e instanceof DavMailAuthenticationException) {
throw (DavMailAuthenticationException) e;
@ -205,6 +211,7 @@ public class EwsExchangeSession extends ExchangeSession {
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
}
}
}
try {
folderIdMap = new HashMap<String, String>();