Improve initial authentication error handling, detect invalid OWA URL
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@113 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
4ce550d3ff
commit
7f8ee78586
|
@ -210,10 +210,29 @@ public class ExchangeSession {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test authentication mode : form based or basic.
|
||||||
|
*
|
||||||
|
* @param url exchange base URL
|
||||||
|
* @return true if basic authentication detected
|
||||||
|
* @throws java.io.IOException unable to connect to exchange
|
||||||
|
*/
|
||||||
|
protected boolean isBasicAuthentication(String url) throws IOException {
|
||||||
|
// create an HttpClient instance
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
configureClient(httpClient);
|
||||||
|
HttpMethod testMethod = new GetMethod(url);
|
||||||
|
int status = httpClient.executeMethod(testMethod);
|
||||||
|
testMethod.releaseConnection();
|
||||||
|
return status == HttpStatus.SC_UNAUTHORIZED;
|
||||||
|
}
|
||||||
|
|
||||||
public void login(String userName, String password) throws IOException {
|
public void login(String userName, String password) throws IOException {
|
||||||
try {
|
try {
|
||||||
String url = Settings.getProperty("davmail.url");
|
String url = Settings.getProperty("davmail.url");
|
||||||
|
|
||||||
|
boolean isBasicAuthentication = isBasicAuthentication(url);
|
||||||
|
|
||||||
// get proxy configuration from setttings properties
|
// get proxy configuration from setttings properties
|
||||||
URL urlObject = new URL(url);
|
URL urlObject = new URL(url);
|
||||||
// webdavresource is unable to create the correct url type
|
// webdavresource is unable to create the correct url type
|
||||||
|
@ -242,42 +261,44 @@ public class ExchangeSession {
|
||||||
HttpMethod initmethod = new GetMethod(url);
|
HttpMethod initmethod = new GetMethod(url);
|
||||||
wdr.executeHttpRequestMethod(httpClient,
|
wdr.executeHttpRequestMethod(httpClient,
|
||||||
initmethod);
|
initmethod);
|
||||||
if (initmethod.getPath().indexOf("exchweb/bin") > 0) {
|
if (!isBasicAuthentication) {
|
||||||
LOGGER.debug("Form based authentication detected");
|
LOGGER.debug("Form based authentication detected");
|
||||||
|
if (initmethod.getPath().indexOf("exchweb/bin") == -1) {
|
||||||
|
LOGGER.error("DavMail configuration exception: authentication form not found at " + url +
|
||||||
|
" and basic authentication not requested");
|
||||||
|
throw new IOException("DavMail configuration exception: authentication form not found at " + url +
|
||||||
|
" and basic authentication not requested");
|
||||||
|
} else {
|
||||||
|
PostMethod logonMethod = new PostMethod(
|
||||||
|
"/exchweb/bin/auth/owaauth.dll?" +
|
||||||
|
"ForcedBasic=false&Basic=false&Private=true" +
|
||||||
|
"&Language=No_Value"
|
||||||
|
);
|
||||||
|
logonMethod.addParameter("destination", url);
|
||||||
|
logonMethod.addParameter("flags", "4");
|
||||||
|
// logonMethod.addParameter("visusername", userName.substring(userName.lastIndexOf('\\')));
|
||||||
|
logonMethod.addParameter("username", userName);
|
||||||
|
logonMethod.addParameter("password", password);
|
||||||
|
// logonMethod.addParameter("SubmitCreds", "Log On");
|
||||||
|
// logonMethod.addParameter("forcedownlevel", "0");
|
||||||
|
logonMethod.addParameter("trusted", "4");
|
||||||
|
|
||||||
PostMethod logonMethod = new PostMethod(
|
wdr.executeHttpRequestMethod(httpClient, logonMethod);
|
||||||
"/exchweb/bin/auth/owaauth.dll?" +
|
Header locationHeader = logonMethod.getResponseHeader("Location");
|
||||||
"ForcedBasic=false&Basic=false&Private=true" +
|
|
||||||
"&Language=No_Value"
|
|
||||||
);
|
|
||||||
logonMethod.addParameter("destination", url);
|
|
||||||
logonMethod.addParameter("flags", "4");
|
|
||||||
// logonMethod.addParameter("visusername", userName.substring(userName.lastIndexOf('\\')));
|
|
||||||
logonMethod.addParameter("username", userName);
|
|
||||||
logonMethod.addParameter("password", password);
|
|
||||||
// logonMethod.addParameter("SubmitCreds", "Log On");
|
|
||||||
// logonMethod.addParameter("forcedownlevel", "0");
|
|
||||||
logonMethod.addParameter("trusted", "4");
|
|
||||||
|
|
||||||
wdr.executeHttpRequestMethod(wdr.retrieveSessionInstance(),
|
if (logonMethod.getStatusCode() != HttpURLConnection.HTTP_MOVED_TEMP ||
|
||||||
logonMethod);
|
locationHeader == null ||
|
||||||
Header locationHeader = logonMethod.getResponseHeader(
|
!url.equals(locationHeader.getValue())) {
|
||||||
"Location");
|
throw new HttpException("Authentication failed");
|
||||||
|
}
|
||||||
|
|
||||||
if (logonMethod.getStatusCode() != HttpURLConnection.HTTP_MOVED_TEMP ||
|
|
||||||
locationHeader == null ||
|
|
||||||
!url.equals(locationHeader.getValue())) {
|
|
||||||
throw new HttpException("Authentication failed");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// User now authenticated, get various session information
|
// User may be authenticated, get various session information
|
||||||
HttpMethod method = new GetMethod(url);
|
HttpMethod method = new GetMethod(url);
|
||||||
int status = wdr.executeHttpRequestMethod(wdr.
|
int status = wdr.executeHttpRequestMethod(httpClient, method);
|
||||||
retrieveSessionInstance(), method);
|
if (status != HttpStatus.SC_OK) {
|
||||||
if (status != HttpStatus.SC_MULTI_STATUS
|
|
||||||
&& status != HttpStatus.SC_OK) {
|
|
||||||
HttpException ex = new HttpException();
|
HttpException ex = new HttpException();
|
||||||
ex.setReasonCode(status);
|
ex.setReasonCode(status);
|
||||||
ex.setReason(method.getStatusText());
|
ex.setReason(method.getStatusText());
|
||||||
|
@ -285,19 +306,24 @@ public class ExchangeSession {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get user mail URL from html body (multi frame)
|
// get user mail URL from html body (multi frame)
|
||||||
String body = method.getResponseBodyAsString();
|
String mailboxName = method.getResponseBodyAsString();
|
||||||
int beginIndex = body.indexOf(url);
|
int beginIndex = mailboxName.indexOf(url);
|
||||||
if (beginIndex < 0) {
|
if (beginIndex < 0) {
|
||||||
throw new HttpException(url + " not found in body");
|
throw new HttpException(url + " not found in body");
|
||||||
}
|
}
|
||||||
body = body.substring(beginIndex);
|
mailboxName = mailboxName.substring(beginIndex);
|
||||||
int endIndex = body.indexOf('"');
|
int endIndex = mailboxName.indexOf('"');
|
||||||
if (endIndex < 0) {
|
if (endIndex < 0) {
|
||||||
throw new HttpException(url + " not found in body");
|
throw new HttpException(url + " not found in body");
|
||||||
}
|
}
|
||||||
body = body.substring(url.length(), endIndex);
|
mailboxName = mailboxName.substring(url.length(), endIndex);
|
||||||
|
|
||||||
|
// if body is empty : wrong password, not authenticated
|
||||||
|
if (mailboxName.length() == 0) {
|
||||||
|
throw new HttpException("Authentication failed");
|
||||||
|
}
|
||||||
// got base http mailbox http url
|
// got base http mailbox http url
|
||||||
mailPath = "/exchange/" + body;
|
mailPath = "/exchange/" + mailboxName;
|
||||||
wdr.setPath(mailPath);
|
wdr.setPath(mailPath);
|
||||||
|
|
||||||
// Retrieve inbox and trash URLs
|
// Retrieve inbox and trash URLs
|
||||||
|
@ -1022,20 +1048,21 @@ public class ExchangeSession {
|
||||||
// double dot filter : avoid end of message in body
|
// double dot filter : avoid end of message in body
|
||||||
quotedOs = new FilterOutputStream(os) {
|
quotedOs = new FilterOutputStream(os) {
|
||||||
byte state = 0;
|
byte state = 0;
|
||||||
|
|
||||||
public void write(int achar) throws IOException {
|
public void write(int achar) throws IOException {
|
||||||
if (achar == 13 && state != 3) {
|
if (achar == 13 && state != 3) {
|
||||||
state = 1;
|
state = 1;
|
||||||
} else if (achar == 10 && state == 1) {
|
} else if (achar == 10 && state == 1) {
|
||||||
state = 2;
|
state = 2;
|
||||||
} else if (achar == '.' && state == 2) {
|
} else if (achar == '.' && state == 2) {
|
||||||
state = 3;
|
state = 3;
|
||||||
} else if (achar == 13) {
|
} else if (achar == 13) {
|
||||||
state = 0;
|
state = 0;
|
||||||
super.write('.');
|
super.write('.');
|
||||||
} else {
|
} else {
|
||||||
state = 0;
|
state = 0;
|
||||||
}
|
}
|
||||||
super.write(achar);
|
super.write(achar);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
quotedOs = (MimeUtility.encode(quotedOs, mimeHeader.contentTransferEncoding));
|
quotedOs = (MimeUtility.encode(quotedOs, mimeHeader.contentTransferEncoding));
|
||||||
|
@ -1389,13 +1416,13 @@ public class ExchangeSession {
|
||||||
// try to get attachment by index, only if no name found
|
// try to get attachment by index, only if no name found
|
||||||
// or attachment renamed to winmail.dat by Exchange
|
// or attachment renamed to winmail.dat by Exchange
|
||||||
if (attachment == null && (partHeader.name == null || "winmail.dat".equals(partHeader.name))
|
if (attachment == null && (partHeader.name == null || "winmail.dat".equals(partHeader.name))
|
||||||
// avoid out of bounds exception
|
// avoid out of bounds exception
|
||||||
&& attachmentIndex >= 0 && attachmentIndex < attachments.size()) {
|
&& attachmentIndex >= 0 && attachmentIndex < attachments.size()) {
|
||||||
attachment = attachments.get(attachmentIndex);
|
attachment = attachments.get(attachmentIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to get by index if attachment renamed to application
|
// try to get by index if attachment renamed to application
|
||||||
if (attachment == null && partHeader.name != null) {
|
if (attachment == null && partHeader.name != null && attachmentIndex < attachments.size()) {
|
||||||
Attachment currentAttachment = attachments.get(attachmentIndex);
|
Attachment currentAttachment = attachments.get(attachmentIndex);
|
||||||
if (currentAttachment != null && currentAttachment.name.startsWith("application")) {
|
if (currentAttachment != null && currentAttachment.name.startsWith("application")) {
|
||||||
attachment = currentAttachment;
|
attachment = currentAttachment;
|
||||||
|
|
Loading…
Reference in New Issue