1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Merge pull request #141 from owncloud/maintain_session_with_server

Maintain session with server
This commit is contained in:
David A. Velasco 2016-12-16 10:07:27 +01:00 committed by GitHub
commit 8ced17a930
8 changed files with 503 additions and 365 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -32,41 +32,49 @@ import org.apache.commons.httpclient.auth.AuthScope;
public class OwnCloudBasicCredentials implements OwnCloudCredentials { 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;
}
@Override public OwnCloudBasicCredentials(String username, String password, boolean sessionEnabled) {
public void applyTo(OwnCloudClient client) { mUsername = username != null ? username : "";
mPassword = password != null ? password : "";
mAuthenticationPreemptive = !sessionEnabled;
}
@Override
public void applyTo(OwnCloudClient client) {
List<String> authPrefs = new ArrayList<String>(1); List<String> authPrefs = new ArrayList<String>(1);
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,
new UsernamePasswordCredentials(mUsername, mPassword) new UsernamePasswordCredentials(mUsername, mPassword)
); );
} }
@Override @Override
public String getUsername() { public String getUsername() {
return mUsername; return mUsername;
} }
@Override @Override
public String getAuthToken() { public String getAuthToken() {
return mPassword; return mPassword;
} }
@Override @Override
public boolean authTokenExpires() { public boolean authTokenExpires() {
return false; return false;
} }
} }

View File

@ -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 {
@ -86,35 +91,40 @@ public class OwnCloudClientFactory {
String username = AccountUtils.getUsernameForAccount(account); String username = AccountUtils.getUsernameForAccount(account);
if (isOauth2) { if (isOauth2) {
String accessToken = am.blockingGetAuthToken( String accessToken = am.blockingGetAuthToken(
account, account,
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
false); false);
client.setCredentials( client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(accessToken) OwnCloudCredentialsFactory.newBearerCredentials(accessToken)
); );
} else if (isSamlSso) { // TODO avoid a call to getUserData here } else if (isSamlSso) { // TODO avoid a call to getUserData here
String accessToken = am.blockingGetAuthToken( String accessToken = am.blockingGetAuthToken(
account, account,
AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type),
false); false);
client.setCredentials( client.setCredentials(
OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken) OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken)
); );
} else { } else {
//String password = am.getPassword(account); //String password = am.getPassword(account);
String password = am.blockingGetAuthToken( String password = am.blockingGetAuthToken(
account, account,
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())
)
);
} }
// Restore cookies // Restore cookies
@ -140,35 +150,35 @@ public class OwnCloudClientFactory {
String username = AccountUtils.getUsernameForAccount(account); String username = AccountUtils.getUsernameForAccount(account);
if (isOauth2) { // TODO avoid a call to getUserData here if (isOauth2) { // TODO avoid a call to getUserData here
AccountManagerFuture<Bundle> future = am.getAuthToken( AccountManagerFuture<Bundle> future = am.getAuthToken(
account, account,
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
null, null,
currentActivity, currentActivity,
null, null,
null); null);
Bundle result = future.getResult(); Bundle result = future.getResult();
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN); String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) throw new AuthenticatorException("WTF!"); if (accessToken == null) throw new AuthenticatorException("WTF!");
client.setCredentials( client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(accessToken) OwnCloudCredentialsFactory.newBearerCredentials(accessToken)
); );
} else if (isSamlSso) { // TODO avoid a call to getUserData here } else if (isSamlSso) { // TODO avoid a call to getUserData here
AccountManagerFuture<Bundle> future = am.getAuthToken( AccountManagerFuture<Bundle> future = am.getAuthToken(
account, account,
AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type),
null, null,
currentActivity, currentActivity,
null, null,
null); null);
Bundle result = future.getResult(); Bundle result = future.getResult();
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN); String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) throw new AuthenticatorException("WTF!"); if (accessToken == null) throw new AuthenticatorException("WTF!");
client.setCredentials( client.setCredentials(
OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken) OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken)
); );
} else { } else {
@ -176,18 +186,24 @@ public class OwnCloudClientFactory {
//String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(),
// false); // false);
AccountManagerFuture<Bundle> future = am.getAuthToken( AccountManagerFuture<Bundle> future = am.getAuthToken(
account, account,
AccountTypeUtils.getAuthTokenTypePass(account.type), AccountTypeUtils.getAuthTokenTypePass(account.type),
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())
)
);
} }
// Restore cookies // Restore cookies

View File

@ -24,57 +24,61 @@
package com.owncloud.android.lib.common; package com.owncloud.android.lib.common;
public class OwnCloudClientManagerFactory { 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;
private static OwnCloudClientManager sDefaultSingleton;
private static OwnCloudClientManager sDefaultSingleton;
private static String sUserAgent; private static String sUserAgent;
public static OwnCloudClientManager newDefaultOwnCloudClientManager() { public static OwnCloudClientManager newDefaultOwnCloudClientManager() {
return newOwnCloudClientManager(sDefaultPolicy); return newOwnCloudClientManager(sDefaultPolicy);
}
public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) {
switch (policy) {
case ALWAYS_NEW_CLIENT:
return new SimpleFactoryManager();
case SINGLE_SESSION_PER_ACCOUNT:
return new SingleSessionManager();
default:
throw new IllegalArgumentException("Unknown policy");
}
}
public static OwnCloudClientManager getDefaultSingleton() {
if (sDefaultSingleton == null) {
sDefaultSingleton = newDefaultOwnCloudClientManager();
}
return sDefaultSingleton;
} }
public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) {
switch (policy) {
case ALWAYS_NEW_CLIENT:
return new SimpleFactoryManager();
case SINGLE_SESSION_PER_ACCOUNT:
return new SingleSessionManager();
case SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING:
return new DynamicSessionManager();
default:
throw new IllegalArgumentException("Unknown policy");
}
}
public static OwnCloudClientManager getDefaultSingleton() {
if (sDefaultSingleton == null) {
sDefaultSingleton = newDefaultOwnCloudClientManager();
}
return sDefaultSingleton;
}
public static Policy getDefaultPolicy() { public static Policy getDefaultPolicy() {
return sDefaultPolicy; return sDefaultPolicy;
} }
public static void setDefaultPolicy(Policy policy) { public static void setDefaultPolicy(Policy policy) {
if (policy == null) { if (policy == null) {
throw new IllegalArgumentException("Default policy cannot be NULL"); throw new IllegalArgumentException("Default policy cannot be NULL");
} }
if (defaultSingletonMustBeUpdated(policy)) { if (defaultSingletonMustBeUpdated(policy)) {
sDefaultSingleton = null; sDefaultSingleton = null;
} }
sDefaultPolicy = policy; sDefaultPolicy = policy;
} }
public static void setUserAgent(String userAgent){ public static void setUserAgent(String userAgent) {
sUserAgent = userAgent; sUserAgent = userAgent;
} }
@ -82,19 +86,19 @@ public class OwnCloudClientManagerFactory {
return sUserAgent; return sUserAgent;
} }
private static boolean defaultSingletonMustBeUpdated(Policy policy) { private static boolean defaultSingletonMustBeUpdated(Policy policy) {
if (sDefaultSingleton == null) { if (sDefaultSingleton == null) {
return false; return false;
} }
if (policy == Policy.ALWAYS_NEW_CLIENT && if (policy == Policy.ALWAYS_NEW_CLIENT &&
!(sDefaultSingleton instanceof SimpleFactoryManager)) { !(sDefaultSingleton instanceof SimpleFactoryManager)) {
return true; return true;
} }
if (policy == Policy.SINGLE_SESSION_PER_ACCOUNT && if (policy == Policy.SINGLE_SESSION_PER_ACCOUNT &&
!(sDefaultSingleton instanceof SingleSessionManager)) { !(sDefaultSingleton instanceof SingleSessionManager)) {
return true; return true;
} }
return false; return false;
} }
} }

View File

@ -26,55 +26,61 @@ package com.owncloud.android.lib.common;
public class OwnCloudCredentialsFactory { public class OwnCloudCredentialsFactory {
public static final String CREDENTIAL_CHARSET = "UTF-8"; public static final String CREDENTIAL_CHARSET = "UTF-8";
private static OwnCloudAnonymousCredentials sAnonymousCredentials; private static OwnCloudAnonymousCredentials sAnonymousCredentials;
public static OwnCloudCredentials newBasicCredentials(String username, String password) { public static OwnCloudCredentials newBasicCredentials(String username, String password) {
return new OwnCloudBasicCredentials(username, password); return new OwnCloudBasicCredentials(username, password);
} }
public static OwnCloudCredentials newBearerCredentials(String authToken) { public static OwnCloudCredentials newBasicCredentials(
String username, String password, boolean sessionEnabled
) {
return new OwnCloudBasicCredentials(username, password, sessionEnabled);
}
public static OwnCloudCredentials newBearerCredentials(String authToken) {
return new OwnCloudBearerCredentials(authToken); return new OwnCloudBearerCredentials(authToken);
} }
public static OwnCloudCredentials newSamlSsoCredentials(String username, String sessionCookie) {
return new OwnCloudSamlSsoCredentials(username, sessionCookie);
}
public static final OwnCloudCredentials getAnonymousCredentials() { public static OwnCloudCredentials newSamlSsoCredentials(String username, String sessionCookie) {
if (sAnonymousCredentials == null) { return new OwnCloudSamlSsoCredentials(username, sessionCookie);
sAnonymousCredentials = new OwnCloudAnonymousCredentials(); }
}
return sAnonymousCredentials;
}
public static final class OwnCloudAnonymousCredentials implements OwnCloudCredentials { public static final OwnCloudCredentials getAnonymousCredentials() {
if (sAnonymousCredentials == null) {
protected OwnCloudAnonymousCredentials() { sAnonymousCredentials = new OwnCloudAnonymousCredentials();
} }
return sAnonymousCredentials;
@Override }
public void applyTo(OwnCloudClient client) {
client.getState().clearCredentials();
client.getState().clearCookies();
}
@Override public static final class OwnCloudAnonymousCredentials implements OwnCloudCredentials {
public String getAuthToken() {
return "";
}
@Override protected OwnCloudAnonymousCredentials() {
public boolean authTokenExpires() { }
return false;
}
@Override @Override
public String getUsername() { public void applyTo(OwnCloudClient client) {
// no user name client.getState().clearCredentials();
return null; client.getState().clearCookies();
} }
}
@Override
public String getAuthToken() {
return "";
}
@Override
public boolean authTokenExpires() {
return false;
}
@Override
public String getUsername() {
// no user name
return null;
}
}
} }

View File

@ -44,9 +44,9 @@ import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.status.OwnCloudVersion; import com.owncloud.android.lib.resources.status.OwnCloudVersion;
public class AccountUtils { public class AccountUtils {
private static final String TAG = AccountUtils.class.getSimpleName(); private static final String TAG = AccountUtils.class.getSimpleName();
public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
public static final String WEBDAV_PATH_2_0 = "/files/webdav.php"; public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav"; public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
@ -59,11 +59,11 @@ 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) {
@ -76,73 +76,72 @@ public class AccountUtils {
if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0) if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
return WEBDAV_PATH_4_0; return WEBDAV_PATH_4_0;
if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
|| version.compareTo(OwnCloudVersion.owncloud_v2) >= 0) || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
return WEBDAV_PATH_2_0; return WEBDAV_PATH_2_0;
if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0) if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
return WEBDAV_PATH_1_2; return WEBDAV_PATH_1_2;
} }
return null; return null;
} }
/** /**
* 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 {
AccountManager ama = AccountManager.get(context); AccountManager ama = AccountManager.get(context);
String baseurl = ama.getUserData(account, Constants.KEY_OC_BASE_URL); String baseurl = ama.getUserData(account, Constants.KEY_OC_BASE_URL);
String version = ama.getUserData(account, Constants.KEY_OC_VERSION); String version = ama.getUserData(account, Constants.KEY_OC_VERSION);
boolean supportsOAuth = (ama.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2) != null); boolean supportsOAuth = (ama.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2) != null);
boolean supportsSamlSso = (ama.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null); boolean supportsSamlSso = (ama.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null);
OwnCloudVersion ver = new OwnCloudVersion(version); OwnCloudVersion ver = new OwnCloudVersion(version);
String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso); String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);
if (baseurl == null || webdavpath == null) if (baseurl == null || webdavpath == null)
throw new AccountNotFoundException(account, "Account not found", null); throw new AccountNotFoundException(account, "Account not found", null);
return baseurl + webdavpath; return baseurl + webdavpath;
} }
/**
* 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 account
* @return url server or null on failure
* @throws AccountNotFoundException When 'account' is unknown for the AccountManager
*/
@Deprecated
public static String constructBasicURLForAccount(Context context, Account account)
throws AccountNotFoundException {
return getBaseUrlForAccount(context, account);
}
/** /**
* 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
* @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.
*/ */
public static String getBaseUrlForAccount(Context context, Account account) @Deprecated
throws AccountNotFoundException { public static String constructBasicURLForAccount(Context context, Account account)
throws AccountNotFoundException {
return getBaseUrlForAccount(context, account);
}
/**
* Extracts url server from the account
*
* @param context
* @param account
* @return url server or null on failure
* @throws AccountNotFoundException When 'account' is unknown for the AccountManager
*/
public static String getBaseUrlForAccount(Context context, Account account)
throws AccountNotFoundException {
AccountManager ama = AccountManager.get(context.getApplicationContext()); AccountManager ama = AccountManager.get(context.getApplicationContext());
String baseurl = ama.getUserData(account, Constants.KEY_OC_BASE_URL); String baseurl = ama.getUserData(account, Constants.KEY_OC_BASE_URL);
if (baseurl == null ) if (baseurl == null)
throw new AccountNotFoundException(account, "Account not found", null); throw new AccountNotFoundException(account, "Account not found", null);
return baseurl; return baseurl;
} }
@ -150,8 +149,8 @@ public class AccountUtils {
/** /**
* Get the username corresponding to an OC account. * Get the username corresponding to an OC account.
* *
* @param account An OC account * @param account An OC account
* @return Username for the given account, extracted from the account.name * @return Username for the given account, extracted from the account.name
*/ */
public static String getUsernameForAccount(Account account) { public static String getUsernameForAccount(Account account) {
String username = null; String username = null;
@ -164,62 +163,86 @@ public class AccountUtils {
} }
/** /**
* * Get the stored server version corresponding to an OC account.
* @return *
* @throws IOException * @param account An OC account
* @throws AuthenticatorException * @param context Application context
* @throws OperationCanceledException * @return Version of the OC server, according to last check
*/ */
public static OwnCloudCredentials getCredentialsForAccount(Context context, Account account) public static OwnCloudVersion getServerVersionForAccount(Account account, Context context) {
throws OperationCanceledException, AuthenticatorException, IOException { AccountManager ama = AccountManager.get(context);
OwnCloudVersion version = null;
OwnCloudCredentials credentials = 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
* @throws IOException
* @throws AuthenticatorException
* @throws OperationCanceledException
*/
public static OwnCloudCredentials getCredentialsForAccount(Context context, Account account)
throws OperationCanceledException, AuthenticatorException, IOException {
OwnCloudCredentials credentials = null;
AccountManager am = AccountManager.get(context); AccountManager am = AccountManager.get(context);
boolean isOauth2 = am.getUserData( boolean isOauth2 = am.getUserData(
account, account,
AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null;
boolean isSamlSso = am.getUserData( boolean isSamlSso = am.getUserData(
account, account,
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(
account, account,
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
false); false);
credentials = OwnCloudCredentialsFactory.newBearerCredentials(accessToken); credentials = OwnCloudCredentialsFactory.newBearerCredentials(accessToken);
} else if (isSamlSso) { } else if (isSamlSso) {
String accessToken = am.blockingGetAuthToken( String accessToken = am.blockingGetAuthToken(
account, account,
AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type),
false); false);
credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken); credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken);
} else { } else {
String password = am.blockingGetAuthToken( String password = am.blockingGetAuthToken(
account, account,
AccountTypeUtils.getAuthTokenTypePass(account.type), AccountTypeUtils.getAuthTokenTypePass(account.type),
false); false);
credentials = OwnCloudCredentialsFactory.newBasicCredentials(username, password); credentials = OwnCloudCredentialsFactory.newBasicCredentials(
} username,
password,
return credentials; version.isSessionMonitoringSupported()
);
} }
return credentials;
}
public static String buildAccountNameOld(Uri serverBaseUrl, String username) { public static String buildAccountNameOld(Uri serverBaseUrl, String username) {
if (serverBaseUrl.getScheme() == null) { if (serverBaseUrl.getScheme() == null) {
serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString()); serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString());
} }
String accountName = username + "@" + serverBaseUrl.getHost(); String accountName = username + "@" + serverBaseUrl.getHost();
if (serverBaseUrl.getPort() >= 0) { if (serverBaseUrl.getPort() >= 0) {
accountName += ":" + serverBaseUrl.getPort(); accountName += ":" + serverBaseUrl.getPort();
@ -228,9 +251,9 @@ public class AccountUtils {
} }
public static String buildAccountName(Uri serverBaseUrl, String username) { public static String buildAccountName(Uri serverBaseUrl, String username) {
if (serverBaseUrl.getScheme() == null) { if (serverBaseUrl.getScheme() == null) {
serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString()); serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString());
} }
// Remove http:// or https:// // Remove http:// or https://
String url = serverBaseUrl.toString(); String url = serverBaseUrl.toString();
@ -242,148 +265,153 @@ public class AccountUtils {
return accountName; return accountName;
} }
public static void saveClient(OwnCloudClient client, Account savedAccount, Context context) { public static void saveClient(OwnCloudClient client, Account savedAccount, Context context) {
// Account Manager // Account Manager
AccountManager ac = AccountManager.get(context.getApplicationContext()); AccountManager ac = AccountManager.get(context.getApplicationContext());
if (client != null) { if (client != null) {
String cookiesString = client.getCookiesString(); String cookiesString = client.getCookiesString();
if (!"".equals(cookiesString)) { if (!"".equals(cookiesString)) {
ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString); ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString);
// Log_OC.d(TAG, "Saving Cookies: "+ cookiesString ); // Log_OC.d(TAG, "Saving Cookies: "+ cookiesString );
} }
} }
} }
/**
* Restore the client cookies
* @param account
* @param client
* @param context
*/
public static void restoreCookies(Account account, OwnCloudClient client, Context context) {
Log_OC.d(TAG, "Restoring cookies for " + account.name);
// Account Manager /**
AccountManager am = AccountManager.get(context.getApplicationContext()); * Restore the client cookies
*
* @param account
* @param client
* @param context
*/
public static void restoreCookies(Account account, OwnCloudClient client, Context context) {
Uri serverUri = (client.getBaseUri() != null)? client.getBaseUri() : client.getWebdavUri(); Log_OC.d(TAG, "Restoring cookies for " + account.name);
String cookiesString = am.getUserData(account, Constants.KEY_COOKIES); // Account Manager
if (cookiesString !=null) { AccountManager am = AccountManager.get(context.getApplicationContext());
String[] cookies = cookiesString.split(";");
if (cookies.length > 0) {
for (int i=0; i< cookies.length; i++) {
Cookie cookie = new Cookie();
int equalPos = cookies[i].indexOf('=');
cookie.setName(cookies[i].substring(0, equalPos));
cookie.setValue(cookies[i].substring(equalPos + 1));
cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
client.getState().addCookie(cookie); Uri serverUri = (client.getBaseUri() != null) ? client.getBaseUri() : client.getWebdavUri();
}
} String cookiesString = am.getUserData(account, Constants.KEY_COOKIES);
} if (cookiesString != null) {
} String[] cookies = cookiesString.split(";");
if (cookies.length > 0) {
/** for (int i = 0; i < cookies.length; i++) {
* Restore the client cookies from accountName Cookie cookie = new Cookie();
* @param accountName int equalPos = cookies[i].indexOf('=');
* @param client cookie.setName(cookies[i].substring(0, equalPos));
* @param context cookie.setValue(cookies[i].substring(equalPos + 1));
*/ cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
public static void restoreCookies(String accountName, OwnCloudClient client, Context context) { cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
Log_OC.d(TAG, "Restoring cookies for " + accountName);
client.getState().addCookie(cookie);
}
}
}
}
/**
* Restore the client cookies from accountName
*
* @param accountName
* @param client
* @param context
*/
public static void restoreCookies(String accountName, OwnCloudClient client, Context context) {
Log_OC.d(TAG, "Restoring cookies for " + accountName);
// Account Manager
AccountManager am = AccountManager.get(context.getApplicationContext());
// Get account
Account account = null;
Account accounts[] = am.getAccounts();
for (Account a : accounts) {
if (a.name.equals(accountName)) {
account = a;
break;
}
}
// Restoring cookies
if (account != null) {
restoreCookies(account, client, context);
}
}
// Account Manager
AccountManager am = AccountManager.get(context.getApplicationContext());
// Get account
Account account = null;
Account accounts[] = am.getAccounts();
for (Account a : accounts) {
if (a.name.equals(accountName)) {
account = a;
break;
}
}
// Restoring cookies
if (account != null) {
restoreCookies(account, client, context);
}
}
public static class AccountNotFoundException extends AccountsException { public static class AccountNotFoundException extends AccountsException {
/** Generated - should be refreshed every time the class changes!! */ /**
private static final long serialVersionUID = -1684392454798508693L; * Generated - should be refreshed every time the class changes!!
*/
private Account mFailedAccount; private static final long serialVersionUID = -1684392454798508693L;
private Account mFailedAccount;
public AccountNotFoundException(Account failedAccount, String message, Throwable cause) { public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
super(message, cause); super(message, cause);
mFailedAccount = failedAccount; mFailedAccount = failedAccount;
} }
public Account getFailedAccount() { public Account getFailedAccount() {
return mFailedAccount; return mFailedAccount;
} }
} }
public static class Constants { public static class Constants {
/** /**
* Value under this key should handle path to webdav php script. Will be * Value under this key should handle path to webdav php script. Will be
* removed and usage should be replaced by combining * removed and usage should be replaced by combining
* {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and * {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and
* {@link com.owncloud.android.lib.resources.status.OwnCloudVersion} * {@link com.owncloud.android.lib.resources.status.OwnCloudVersion}
* *
* @deprecated * @deprecated
*/ */
public static final String KEY_OC_URL = "oc_url"; public static final String KEY_OC_URL = "oc_url";
/** /**
* Version should be 3 numbers separated by dot so it can be parsed by * Version should be 3 numbers separated by dot so it can be parsed by
* {@link com.owncloud.android.lib.resources.status.OwnCloudVersion} * {@link com.owncloud.android.lib.resources.status.OwnCloudVersion}
*/ */
public static final String KEY_OC_VERSION = "oc_version"; public static final String KEY_OC_VERSION = "oc_version";
/** /**
* Base url should point to owncloud installation without trailing / ie: * Base url should point to owncloud installation without trailing / ie:
* http://server/path or https://owncloud.server * http://server/path or https://owncloud.server
*/ */
public static final String KEY_OC_BASE_URL = "oc_base_url"; public static final String KEY_OC_BASE_URL = "oc_base_url";
/** /**
* Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens. * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
*/ */
public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2"; public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";
/** /**
* Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on. * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
*/ */
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";
* OC account cookies /**
*/ * OC account cookies
public static final String KEY_COOKIES = "oc_account_cookies"; */
public static final String KEY_COOKIES = "oc_account_cookies";
/** /**
* OC account version * OC account version
*/ */
public static final String KEY_OC_ACCOUNT_VERSION = "oc_account_version"; public static final String KEY_OC_ACCOUNT_VERSION = "oc_account_version";
/** /**
* User's display name * User's display name
*/ */
public static final String KEY_DISPLAY_NAME = "oc_display_name"; public static final String KEY_DISPLAY_NAME = "oc_display_name";
} }

View File

@ -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);
}
} }