1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

EWS: implement failover on OWA authentication failure (e.g. with outlook.com)

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1521 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-10-30 19:13:46 +00:00
parent 18584e259d
commit c0abc93ac5
2 changed files with 47 additions and 15 deletions

View File

@ -119,7 +119,7 @@ public abstract class ExchangeSession {
protected String currentMailboxPath;
protected final HttpClient httpClient;
private final String userName;
protected final String userName;
protected String serverVersion;
@ -429,15 +429,7 @@ public abstract class ExchangeSession {
return logonMethod;
}
protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException {
LOGGER.debug("Form based authentication detected");
HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod);
if (logonMethod == null) {
LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", trying default url");
logonMethod = new PostMethod("/owa/auth/owaauth.dll");
}
protected HttpMethod postLogonMethod(HttpClient httpClient, HttpMethod logonMethod, String userName, String password) throws IOException {
// make sure username and password fields are empty
((PostMethod) logonMethod).removeParameter(userNameInput);
((PostMethod) logonMethod).removeParameter(passwordInput);
@ -480,6 +472,18 @@ public abstract class ExchangeSession {
// need to submit form
logonMethod = submitLanguageSelectionForm(logonMethod);
}
return logonMethod;
}
protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException {
LOGGER.debug("Form based authentication detected");
HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod);
if (logonMethod == null) {
LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", trying default url");
logonMethod = new PostMethod("/owa/auth/owaauth.dll");
}
logonMethod = postLogonMethod(httpClient, logonMethod, userName, password);
return logonMethod;
}

View File

@ -79,6 +79,21 @@ public class EwsExchangeSession extends ExchangeSession {
super(url, userName, password);
}
@Override
protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password) throws IOException {
LOGGER.debug("Form based authentication detected");
HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod);
if (logonMethod == null) {
LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", will try direct EWS access");
} else {
logonMethod = postLogonMethod(httpClient, logonMethod, userName, password);
}
return logonMethod;
}
/**
* Check endpoint url.
*
@ -90,7 +105,9 @@ public class EwsExchangeSession extends ExchangeSession {
getMethod.setFollowRedirects(false);
try {
int status = DavGatewayHttpClientFacade.executeNoRedirect(httpClient, getMethod);
if (status != HttpStatus.SC_MOVED_TEMPORARILY) {
if (status == HttpStatus.SC_UNAUTHORIZED) {
throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
} else if (status != HttpStatus.SC_MOVED_TEMPORARILY) {
throw DavGatewayHttpClientFacade.buildHttpException(getMethod);
}
// check Location
@ -109,10 +126,19 @@ public class EwsExchangeSession extends ExchangeSession {
@Override
protected void buildSessionInfo(HttpMethod method) throws DavMailException {
// no need to check logon method body
method.releaseConnection();
if (method != null) {
method.releaseConnection();
// need to retrieve email and alias
getEmailAndAliasFromOptions();
} else {
// OWA authentication failed, get email address from login
if (userName.indexOf('@') >= 0) {
// userName is email address
email = userName;
alias = userName.substring(0, userName.indexOf('@'));
}
}
// also need to retrieve email and alias
getEmailAndAliasFromOptions();
if (email == null || alias == null) {
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
}
@ -122,6 +148,8 @@ public class EwsExchangeSession extends ExchangeSession {
// check EWS access
try {
checkEndPointUrl("/ews/exchange.asmx");
} catch (DavMailAuthenticationException e) {
throw e;
} catch (IOException e) {
try {
// failover, try to retrieve EWS url from autodiscover
@ -1273,7 +1301,7 @@ public class EwsExchangeSession extends ExchangeSession {
event.getEventContent();
events.add(event);
} catch (HttpException e) {
LOGGER.warn("Ignore invalid event "+event.getHref());
LOGGER.warn("Ignore invalid event " + event.getHref());
}
} else {
events.add(event);