mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +00:00
Merge pull request #141 from owncloud/maintain_session_with_server
Maintain session with server
This commit is contained in:
commit
8ced17a930
@ -0,0 +1,66 @@
|
|||||||
|
package com.owncloud.android.lib.common;
|
||||||
|
|
||||||
|
import android.accounts.AuthenticatorException;
|
||||||
|
import android.accounts.OperationCanceledException;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||||
|
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic implementation of {@link OwnCloudClientManager}.
|
||||||
|
*
|
||||||
|
* Wraps instances of {@link SingleSessionManager} and {@link SimpleFactoryManager} and delegates on one
|
||||||
|
* or the other depending on the known version of the server corresponding to the {@link OwnCloudAccount}
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DynamicSessionManager implements OwnCloudClientManager {
|
||||||
|
|
||||||
|
private SimpleFactoryManager mSimpleFactoryManager = new SimpleFactoryManager();
|
||||||
|
|
||||||
|
private SingleSessionManager mSingleSessionManager = new SingleSessionManager();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
|
||||||
|
throws AccountUtils.AccountNotFoundException,
|
||||||
|
OperationCanceledException, AuthenticatorException, IOException {
|
||||||
|
|
||||||
|
OwnCloudVersion ownCloudVersion = null;
|
||||||
|
if (account.getSavedAccount() != null) {
|
||||||
|
ownCloudVersion = AccountUtils.getServerVersionForAccount(
|
||||||
|
account.getSavedAccount(), context
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ownCloudVersion != null && ownCloudVersion.isSessionMonitoringSupported()) {
|
||||||
|
return mSingleSessionManager.getClientFor(account, context);
|
||||||
|
} else {
|
||||||
|
return mSimpleFactoryManager.getClientFor(account, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OwnCloudClient removeClientFor(OwnCloudAccount account) {
|
||||||
|
OwnCloudClient clientRemovedFromFactoryManager = mSimpleFactoryManager.removeClientFor(account);
|
||||||
|
OwnCloudClient clientRemovedFromSingleSessionManager = mSingleSessionManager.removeClientFor(account);
|
||||||
|
if (clientRemovedFromSingleSessionManager != null) {
|
||||||
|
return clientRemovedFromSingleSessionManager;
|
||||||
|
} else {
|
||||||
|
return clientRemovedFromFactoryManager;
|
||||||
|
}
|
||||||
|
// clientRemoved and clientRemoved2 should not be != null at the same time
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveAllClients(Context context, String accountType)
|
||||||
|
throws AccountUtils.AccountNotFoundException,
|
||||||
|
AuthenticatorException, IOException, OperationCanceledException {
|
||||||
|
mSimpleFactoryManager.saveAllClients(context, accountType);
|
||||||
|
mSingleSessionManager.saveAllClients(context, accountType);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -139,6 +139,10 @@ public class OwnCloudAccount {
|
|||||||
return mSavedAccountName;
|
return mSavedAccountName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Account getSavedAccount() {
|
||||||
|
return mSavedAccount;
|
||||||
|
}
|
||||||
|
|
||||||
public String getDisplayName() {
|
public String getDisplayName() {
|
||||||
if (mDisplayName != null && mDisplayName.length() > 0) {
|
if (mDisplayName != null && mDisplayName.length() > 0) {
|
||||||
return mDisplayName;
|
return mDisplayName;
|
||||||
|
@ -34,10 +34,18 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials {
|
|||||||
|
|
||||||
private String mUsername;
|
private String mUsername;
|
||||||
private String mPassword;
|
private String mPassword;
|
||||||
|
private boolean mAuthenticationPreemptive;
|
||||||
|
|
||||||
public OwnCloudBasicCredentials(String username, String password) {
|
public OwnCloudBasicCredentials(String username, String password) {
|
||||||
mUsername = username != null ? username : "";
|
mUsername = username != null ? username : "";
|
||||||
mPassword = password != null ? password : "";
|
mPassword = password != null ? password : "";
|
||||||
|
mAuthenticationPreemptive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OwnCloudBasicCredentials(String username, String password, boolean sessionEnabled) {
|
||||||
|
mUsername = username != null ? username : "";
|
||||||
|
mPassword = password != null ? password : "";
|
||||||
|
mAuthenticationPreemptive = !sessionEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -46,7 +54,7 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials {
|
|||||||
authPrefs.add(AuthPolicy.BASIC);
|
authPrefs.add(AuthPolicy.BASIC);
|
||||||
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
|
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
|
||||||
|
|
||||||
client.getParams().setAuthenticationPreemptive(true);
|
client.getParams().setAuthenticationPreemptive(mAuthenticationPreemptive);
|
||||||
client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
|
client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
|
||||||
client.getState().setCredentials(
|
client.getState().setCredentials(
|
||||||
AuthScope.ANY,
|
AuthScope.ANY,
|
||||||
|
@ -42,6 +42,7 @@ import com.owncloud.android.lib.common.accounts.AccountUtils;
|
|||||||
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
|
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
|
||||||
import com.owncloud.android.lib.common.network.NetworkUtils;
|
import com.owncloud.android.lib.common.network.NetworkUtils;
|
||||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||||
|
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
|
||||||
|
|
||||||
public class OwnCloudClientFactory {
|
public class OwnCloudClientFactory {
|
||||||
|
|
||||||
@ -69,7 +70,11 @@ public class OwnCloudClientFactory {
|
|||||||
* @throws IOException If there was some I/O error while getting the
|
* @throws IOException If there was some I/O error while getting the
|
||||||
* authorization token for the account.
|
* authorization token for the account.
|
||||||
* @throws AccountNotFoundException If 'account' is unknown for the AccountManager
|
* @throws AccountNotFoundException If 'account' is unknown for the AccountManager
|
||||||
|
*
|
||||||
|
* @deprecated : Will be deleted in version 1.0.
|
||||||
|
* Use {@link #createOwnCloudClient(Account, Context, Activity)} instead.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static OwnCloudClient createOwnCloudClient (Account account, Context appContext)
|
public static OwnCloudClient createOwnCloudClient (Account account, Context appContext)
|
||||||
throws OperationCanceledException, AuthenticatorException, IOException,
|
throws OperationCanceledException, AuthenticatorException, IOException,
|
||||||
AccountNotFoundException {
|
AccountNotFoundException {
|
||||||
@ -111,8 +116,13 @@ public class OwnCloudClientFactory {
|
|||||||
AccountTypeUtils.getAuthTokenTypePass(account.type),
|
AccountTypeUtils.getAuthTokenTypePass(account.type),
|
||||||
false);
|
false);
|
||||||
|
|
||||||
|
OwnCloudVersion version = AccountUtils.getServerVersionForAccount(account, appContext);
|
||||||
client.setCredentials(
|
client.setCredentials(
|
||||||
OwnCloudCredentialsFactory.newBasicCredentials(username, password)
|
OwnCloudCredentialsFactory.newBasicCredentials(
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
(version != null && version.isSessionMonitoringSupported())
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -181,12 +191,18 @@ public class OwnCloudClientFactory {
|
|||||||
null,
|
null,
|
||||||
currentActivity,
|
currentActivity,
|
||||||
null,
|
null,
|
||||||
null);
|
null
|
||||||
|
);
|
||||||
|
|
||||||
Bundle result = future.getResult();
|
Bundle result = future.getResult();
|
||||||
String password = result.getString(AccountManager.KEY_AUTHTOKEN);
|
String password = result.getString(AccountManager.KEY_AUTHTOKEN);
|
||||||
|
OwnCloudVersion version = AccountUtils.getServerVersionForAccount(account, appContext);
|
||||||
client.setCredentials(
|
client.setCredentials(
|
||||||
OwnCloudCredentialsFactory.newBasicCredentials(username, password)
|
OwnCloudCredentialsFactory.newBasicCredentials(
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
(version != null && version.isSessionMonitoringSupported())
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,8 @@ public class OwnCloudClientManagerFactory {
|
|||||||
|
|
||||||
public static enum Policy {
|
public static enum Policy {
|
||||||
ALWAYS_NEW_CLIENT,
|
ALWAYS_NEW_CLIENT,
|
||||||
SINGLE_SESSION_PER_ACCOUNT
|
SINGLE_SESSION_PER_ACCOUNT,
|
||||||
|
SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Policy sDefaultPolicy = Policy.ALWAYS_NEW_CLIENT;
|
private static Policy sDefaultPolicy = Policy.ALWAYS_NEW_CLIENT;
|
||||||
@ -48,6 +49,9 @@ public class OwnCloudClientManagerFactory {
|
|||||||
case SINGLE_SESSION_PER_ACCOUNT:
|
case SINGLE_SESSION_PER_ACCOUNT:
|
||||||
return new SingleSessionManager();
|
return new SingleSessionManager();
|
||||||
|
|
||||||
|
case SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING:
|
||||||
|
return new DynamicSessionManager();
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unknown policy");
|
throw new IllegalArgumentException("Unknown policy");
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,12 @@ public class OwnCloudCredentialsFactory {
|
|||||||
return new OwnCloudBasicCredentials(username, password);
|
return new OwnCloudBasicCredentials(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static OwnCloudCredentials newBasicCredentials(
|
||||||
|
String username, String password, boolean sessionEnabled
|
||||||
|
) {
|
||||||
|
return new OwnCloudBasicCredentials(username, password, sessionEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
public static OwnCloudCredentials newBearerCredentials(String authToken) {
|
public static OwnCloudCredentials newBearerCredentials(String authToken) {
|
||||||
return new OwnCloudBearerCredentials(authToken);
|
return new OwnCloudBearerCredentials(authToken);
|
||||||
}
|
}
|
||||||
|
@ -60,10 +60,10 @@ public class AccountUtils {
|
|||||||
* Returns the proper URL path to access the WebDAV interface of an ownCloud server,
|
* Returns the proper URL path to access the WebDAV interface of an ownCloud server,
|
||||||
* according to its version and the authorization method used.
|
* according to its version and the authorization method used.
|
||||||
*
|
*
|
||||||
* @param version Version of ownCloud server.
|
|
||||||
* @param supportsOAuth If true, access with OAuth 2 authorization is considered.
|
* @param supportsOAuth If true, access with OAuth 2 authorization is considered.
|
||||||
* @param supportsSamlSso If true, and supportsOAuth is false, access with SAML-based single-sign-on is considered.
|
* @param supportsSamlSso If true, and supportsOAuth is false, access with SAML-based single-sign-on is considered.
|
||||||
* @return WebDAV path for given OC version, null if OC version unknown
|
* @return WebDAV path for given OC version, null if OC version unknown
|
||||||
|
* @param version Version of ownCloud server.
|
||||||
*/
|
*/
|
||||||
public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
|
public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
|
||||||
if (version != null) {
|
if (version != null) {
|
||||||
@ -87,12 +87,11 @@ public class AccountUtils {
|
|||||||
/**
|
/**
|
||||||
* Constructs full url to host and webdav resource basing on host version
|
* Constructs full url to host and webdav resource basing on host version
|
||||||
*
|
*
|
||||||
* @deprecated To be removed in release 1.0.
|
|
||||||
*
|
|
||||||
* @param context
|
* @param context
|
||||||
* @param account
|
* @param account
|
||||||
* @return url or null on failure
|
* @return url or null on failure
|
||||||
* @throws AccountNotFoundException When 'account' is unknown for the AccountManager
|
* @throws AccountNotFoundException When 'account' is unknown for the AccountManager
|
||||||
|
* @deprecated To be removed in release 1.0.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
|
public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
|
||||||
@ -113,14 +112,13 @@ public class AccountUtils {
|
|||||||
/**
|
/**
|
||||||
* Extracts url server from the account
|
* Extracts url server from the account
|
||||||
*
|
*
|
||||||
* @deprecated This method will be removed in version 1.0.
|
|
||||||
* Use {@link #getBaseUrlForAccount(Context, Account)}
|
|
||||||
* instead.
|
|
||||||
*
|
|
||||||
* @param context
|
* @param context
|
||||||
* @param account
|
* @param account
|
||||||
* @return url server or null on failure
|
* @return url server or null on failure
|
||||||
* @throws AccountNotFoundException When 'account' is unknown for the AccountManager
|
* @throws AccountNotFoundException When 'account' is unknown for the AccountManager
|
||||||
|
* @deprecated This method will be removed in version 1.0.
|
||||||
|
* Use {@link #getBaseUrlForAccount(Context, Account)}
|
||||||
|
* instead.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static String constructBasicURLForAccount(Context context, Account account)
|
public static String constructBasicURLForAccount(Context context, Account account)
|
||||||
@ -130,6 +128,7 @@ public class AccountUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts url server from the account
|
* Extracts url server from the account
|
||||||
|
*
|
||||||
* @param context
|
* @param context
|
||||||
* @param account
|
* @param account
|
||||||
* @return url server or null on failure
|
* @return url server or null on failure
|
||||||
@ -164,7 +163,26 @@ public class AccountUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get the stored server version corresponding to an OC account.
|
||||||
*
|
*
|
||||||
|
* @param account An OC account
|
||||||
|
* @param context Application context
|
||||||
|
* @return Version of the OC server, according to last check
|
||||||
|
*/
|
||||||
|
public static OwnCloudVersion getServerVersionForAccount(Account account, Context context) {
|
||||||
|
AccountManager ama = AccountManager.get(context);
|
||||||
|
OwnCloudVersion version = null;
|
||||||
|
try {
|
||||||
|
String versionString = ama.getUserData(account, Constants.KEY_OC_VERSION);
|
||||||
|
version = new OwnCloudVersion(versionString);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log_OC.e(TAG, "Couldn't get a the server version for an account", e);
|
||||||
|
}
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws AuthenticatorException
|
* @throws AuthenticatorException
|
||||||
@ -185,6 +203,7 @@ public class AccountUtils {
|
|||||||
AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
|
AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
|
||||||
|
|
||||||
String username = AccountUtils.getUsernameForAccount(account);
|
String username = AccountUtils.getUsernameForAccount(account);
|
||||||
|
OwnCloudVersion version = new OwnCloudVersion(am.getUserData(account, Constants.KEY_OC_VERSION));
|
||||||
|
|
||||||
if (isOauth2) {
|
if (isOauth2) {
|
||||||
String accessToken = am.blockingGetAuthToken(
|
String accessToken = am.blockingGetAuthToken(
|
||||||
@ -208,7 +227,11 @@ public class AccountUtils {
|
|||||||
AccountTypeUtils.getAuthTokenTypePass(account.type),
|
AccountTypeUtils.getAuthTokenTypePass(account.type),
|
||||||
false);
|
false);
|
||||||
|
|
||||||
credentials = OwnCloudCredentialsFactory.newBasicCredentials(username, password);
|
credentials = OwnCloudCredentialsFactory.newBasicCredentials(
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
version.isSessionMonitoringSupported()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return credentials;
|
return credentials;
|
||||||
@ -260,6 +283,7 @@ public class AccountUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Restore the client cookies
|
* Restore the client cookies
|
||||||
|
*
|
||||||
* @param account
|
* @param account
|
||||||
* @param client
|
* @param client
|
||||||
* @param context
|
* @param context
|
||||||
@ -293,6 +317,7 @@ public class AccountUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Restore the client cookies from accountName
|
* Restore the client cookies from accountName
|
||||||
|
*
|
||||||
* @param accountName
|
* @param accountName
|
||||||
* @param client
|
* @param client
|
||||||
* @param context
|
* @param context
|
||||||
@ -321,7 +346,9 @@ public class AccountUtils {
|
|||||||
|
|
||||||
public static class AccountNotFoundException extends AccountsException {
|
public static class AccountNotFoundException extends AccountsException {
|
||||||
|
|
||||||
/** Generated - should be refreshed every time the class changes!! */
|
/**
|
||||||
|
* Generated - should be refreshed every time the class changes!!
|
||||||
|
*/
|
||||||
private static final long serialVersionUID = -1684392454798508693L;
|
private static final long serialVersionUID = -1684392454798508693L;
|
||||||
|
|
||||||
private Account mFailedAccount;
|
private Account mFailedAccount;
|
||||||
@ -367,6 +394,7 @@ public class AccountUtils {
|
|||||||
public static final String KEY_SUPPORTS_SAML_WEB_SSO = "oc_supports_saml_web_sso";
|
public static final String KEY_SUPPORTS_SAML_WEB_SSO = "oc_supports_saml_web_sso";
|
||||||
/**
|
/**
|
||||||
* Flag signaling if the ownCloud server supports Share API"
|
* Flag signaling if the ownCloud server supports Share API"
|
||||||
|
*
|
||||||
* @deprecated
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
public static final String KEY_SUPPORTS_SHARE_API = "oc_supports_share_api";
|
public static final String KEY_SUPPORTS_SHARE_API = "oc_supports_share_api";
|
||||||
|
@ -53,6 +53,8 @@ public class OwnCloudVersion implements Comparable<OwnCloudVersion> {
|
|||||||
|
|
||||||
private static final int MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED = 0x09010000; // 9.1
|
private static final int MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED = 0x09010000; // 9.1
|
||||||
|
|
||||||
|
private static final int MINIMUM_VERSION_WITH_SESSION_MONITORING = 0x09010000; // 9.1
|
||||||
|
|
||||||
private static final int MAX_DOTS = 3;
|
private static final int MAX_DOTS = 3;
|
||||||
|
|
||||||
// format is in version
|
// format is in version
|
||||||
@ -162,4 +164,8 @@ public class OwnCloudVersion implements Comparable<OwnCloudVersion> {
|
|||||||
public boolean isNotReshareableFederatedSupported() {
|
public boolean isNotReshareableFederatedSupported() {
|
||||||
return (mVersion >= MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED);
|
return (mVersion >= MINIMUM_VERSION_WITH_NOT_RESHAREABLE_FEDERATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSessionMonitoringSupported() {
|
||||||
|
return (mVersion >= MINIMUM_VERSION_WITH_SESSION_MONITORING);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user