Fix form based re-authentication when logon cookies had expired.

This commit is contained in:
Jesse Vincent 2010-12-16 02:52:08 +00:00
parent 4074b717c0
commit 78bc6e07f8
1 changed files with 23 additions and 11 deletions

View File

@ -56,9 +56,7 @@ import java.util.zip.GZIPInputStream;
/** /**
* <pre> * <pre>
* Uses WebDAV formatted HTTP calls to an MS Exchange server to fetch email * Uses WebDAV formatted HTTP calls to an MS Exchange server to fetch email
* and email information. This has only been tested on an MS Exchange * and email information.
* Server 2003. It uses Form-Based authentication and requires that
* Outlook Web Access be enabled on the server.
* </pre> * </pre>
*/ */
public class WebDavStore extends Store public class WebDavStore extends Store
@ -102,9 +100,8 @@ public class WebDavStore extends Store
private HttpContext mContext = null; private HttpContext mContext = null;
private CookieStore mAuthCookies = null; private CookieStore mAuthCookies = null;
private short mAuthentication = AUTH_TYPE_NONE; private short mAuthentication = AUTH_TYPE_NONE;
private long mLastAuth = -1; private String mCachedLoginUrl;
private long mAuthTimeout = 5 * 60;
private HashMap<String, WebDavFolder> mFolderList = new HashMap<String, WebDavFolder>(); private HashMap<String, WebDavFolder> mFolderList = new HashMap<String, WebDavFolder>();
/** /**
@ -573,7 +570,6 @@ public class WebDavStore extends Store
if (statusCode >= 200 && statusCode < 300) if (statusCode >= 200 && statusCode < 300)
{ {
mAuthentication = AUTH_TYPE_BASIC; mAuthentication = AUTH_TYPE_BASIC;
mLastAuth = System.currentTimeMillis() / 1000;
} }
else if (statusCode == 401) else if (statusCode == 401)
{ {
@ -653,7 +649,7 @@ public class WebDavStore extends Store
// We will handle all 3 situations the same. First we take an educated // We will handle all 3 situations the same. First we take an educated
// guess at where the authorization DLL is located. If this is this // guess at where the authorization DLL is located. If this is this
// doesn't work, then we'll use the redirection URL for OWA login given // doesn't work, then we'll use the redirection URL for OWA login given
// to use by exchange. We can use this to scrape the location of the // to us by exchange. We can use this to scrape the location of the
// authorization URL. // authorization URL.
info.requiredAuthType = AUTH_TYPE_FORM_BASED; info.requiredAuthType = AUTH_TYPE_FORM_BASED;
@ -696,9 +692,26 @@ public class WebDavStore extends Store
*/ */
public void doFBA(ConnectionInfo info) throws IOException, MessagingException public void doFBA(ConnectionInfo info) throws IOException, MessagingException
{ {
// Clear out cookies from any previous authentication.
mAuthCookies.clear();
WebDavHttpClient httpClient = getHttpClient(); WebDavHttpClient httpClient = getHttpClient();
String loginUrl = "";
if (info != null)
{
loginUrl = info.guessedAuthUrl;
}
else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals(""))
{
loginUrl = mCachedLoginUrl;
}
else
{
throw new MessagingException("No valid login URL available for form-based authentication.");
}
HttpGeneric request = new HttpGeneric(info.guessedAuthUrl); HttpGeneric request = new HttpGeneric(loginUrl);
request.setMethod("POST"); request.setMethod("POST");
// Build the POST data. // Build the POST data.
@ -738,7 +751,6 @@ public class WebDavStore extends Store
} }
}); });
String loginUrl = "";
if (info != null) if (info != null)
{ {
loginUrl = info.redirectUrl; loginUrl = info.redirectUrl;
@ -803,7 +815,7 @@ public class WebDavStore extends Store
if (mAuthCookies != null && !mAuthCookies.getCookies().isEmpty()) if (mAuthCookies != null && !mAuthCookies.getCookies().isEmpty())
{ {
mAuthentication = AUTH_TYPE_FORM_BASED; mAuthentication = AUTH_TYPE_FORM_BASED;
mLastAuth = System.currentTimeMillis() / 1000; mCachedLoginUrl = loginUrl;
} }
} }