-/* ownCloud Android client application\r
+/**\r
+ * ownCloud Android client application\r
+ *\r
* Copyright (C) 2012 Bartek Przybylski\r
- * Copyright (C) 2012-2013 ownCloud Inc.\r
+ * Copyright (C) 2015 ownCloud Inc.\r
*\r
* This program is free software: you can redistribute it and/or modify\r
* it under the terms of the GNU General Public License version 2,\r
\r
package com.owncloud.android.authentication;\r
\r
-import com.owncloud.android.utils.OwnCloudVersion;\r
+import java.util.Locale;\r
+\r
+import com.owncloud.android.MainApp;\r
+import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
+import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
+import com.owncloud.android.lib.common.utils.Log_OC;\r
+import com.owncloud.android.lib.resources.status.OwnCloudVersion;\r
\r
import android.accounts.Account;\r
import android.accounts.AccountManager;\r
-import android.accounts.AccountsException;\r
import android.content.Context;\r
import android.content.SharedPreferences;\r
+import android.net.Uri;\r
import android.preference.PreferenceManager;\r
\r
public class AccountUtils {\r
- public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";\r
- public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";\r
- public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";\r
+\r
+ private static final String TAG = AccountUtils.class.getSimpleName();\r
+\r
+ public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav";\r
private static final String ODAV_PATH = "/remote.php/odav";\r
- public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";\r
- public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";\r
+ private static final String SAML_SSO_PATH = "/remote.php/webdav";\r
public static final String STATUS_PATH = "/status.php";\r
\r
+ public static final int ACCOUNT_VERSION = 1;\r
+\r
/**\r
* Can be used to get the currently selected ownCloud {@link Account} in the\r
* application preferences.\r
*/\r
public static Account getCurrentOwnCloudAccount(Context context) {\r
Account[] ocAccounts = AccountManager.get(context).getAccountsByType(\r
- AccountAuthenticator.ACCOUNT_TYPE);\r
+ MainApp.getAccountType());\r
Account defaultAccount = null;\r
\r
SharedPreferences appPreferences = PreferenceManager\r
}\r
\r
\r
+ public static boolean exists(Account account, Context context) {\r
+ Account[] ocAccounts = AccountManager.get(context).getAccountsByType(\r
+ MainApp.getAccountType());\r
\r
- /**\r
- * Checks, whether or not there are any ownCloud accounts setup.\r
- * \r
- * @return true, if there is at least one account.\r
- */\r
- public static boolean accountsAreSetup(Context context) {\r
- AccountManager accMan = AccountManager.get(context);\r
- Account[] accounts = accMan\r
- .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);\r
- return accounts.length > 0;\r
+ if (account != null && account.name != null) {\r
+ int lastAtPos = account.name.lastIndexOf("@");\r
+ String hostAndPort = account.name.substring(lastAtPos + 1);\r
+ String username = account.name.substring(0, lastAtPos);\r
+ String otherHostAndPort, otherUsername;\r
+ Locale currentLocale = context.getResources().getConfiguration().locale;\r
+ for (Account otherAccount : ocAccounts) {\r
+ lastAtPos = otherAccount.name.lastIndexOf("@");\r
+ otherHostAndPort = otherAccount.name.substring(lastAtPos + 1);\r
+ otherUsername = otherAccount.name.substring(0, lastAtPos);\r
+ if (otherHostAndPort.equals(hostAndPort) &&\r
+ otherUsername.toLowerCase(currentLocale).\r
+ equals(username.toLowerCase(currentLocale))) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ return false;\r
}\r
\r
- \r
+\r
public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {\r
boolean result = false;\r
if (accountName != null) {\r
Account[] ocAccounts = AccountManager.get(context).getAccountsByType(\r
- AccountAuthenticator.ACCOUNT_TYPE);\r
+ MainApp.getAccountType());\r
boolean found = false;\r
for (Account account : ocAccounts) {\r
found = (account.name.equals(accountName));\r
}\r
\r
/**\r
+ * Returns the proper URL path to access the WebDAV interface of an ownCloud server,\r
+ * according to its version and the authorization method used.\r
* \r
- * @param version version of owncloud\r
- * @return webdav path for given OC version, null if OC version unknown\r
+ * @param version Version of ownCloud server.\r
+ * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in\r
+ * {@link AccountAuthenticator}.\r
+ * @return WebDAV path for given OC version and authorization method, null if OC version\r
+ * is unknown; versions prior to ownCloud 4 are not supported anymore\r
*/\r
- public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) {\r
+ public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {\r
if (version != null) {\r
- if (supportsOAuth) {\r
+ if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(authTokenType)) {\r
return ODAV_PATH;\r
}\r
- if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)\r
- return WEBDAV_PATH_4_0;\r
- if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0\r
- || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)\r
- return WEBDAV_PATH_2_0;\r
- if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)\r
- return WEBDAV_PATH_1_2;\r
+ if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {\r
+ return SAML_SSO_PATH;\r
+ }\r
+ return WEBDAV_PATH_4_0_AND_LATER;\r
}\r
return null;\r
}\r
- \r
+\r
+\r
/**\r
- * Constructs full url to host and webdav resource basing on host version\r
- * @param context\r
- * @param account\r
- * @return url or null on failure\r
- * @throws AccountNotFoundException When 'account' is unknown for the AccountManager\r
+ * Update the accounts in AccountManager to meet the current version of accounts expected by the app, if needed.\r
+ *\r
+ * Introduced to handle a change in the structure of stored account names needed to allow different OC servers\r
+ * in the same domain, but not in the same path.\r
+ *\r
+ * @param context Used to access the AccountManager.\r
*/\r
- public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {\r
- AccountManager ama = AccountManager.get(context);\r
- String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
- String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
- boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
- OwnCloudVersion ver = new OwnCloudVersion(strver);\r
- String webdavpath = getWebdavPath(ver, supportsOAuth);\r
-\r
- if (baseurl == null || webdavpath == null) \r
- throw new AccountNotFoundException(account, "Account not found", null);\r
- \r
- return baseurl + webdavpath;\r
- }\r
- \r
- \r
- public static class AccountNotFoundException extends AccountsException {\r
- \r
- private static final long serialVersionUID = 4276870654168776992L;\r
- \r
- private Account mFailedAccount; \r
- \r
- public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {\r
- super(message, cause);\r
- mFailedAccount = failedAccount;\r
- }\r
- \r
- public Account getFailedAccount() {\r
- return mFailedAccount;\r
+ public static void updateAccountVersion(Context context) {\r
+ Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(context);\r
+ AccountManager accountMgr = AccountManager.get(context);\r
+\r
+ if ( currentAccount != null ) {\r
+ String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);\r
+\r
+ if (currentAccountVersion == null) {\r
+ Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION);\r
+ Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());\r
+ String serverUrl, username, newAccountName, password;\r
+ Account newAccount;\r
+ for (Account account : ocAccounts) {\r
+ // build new account name\r
+ serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);\r
+ username = account.name.substring(0, account.name.lastIndexOf('@'));\r
+ newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.\r
+ buildAccountName(Uri.parse(serverUrl), username);\r
+\r
+ // migrate to a new account, if needed\r
+ if (!newAccountName.equals(account.name)) {\r
+ Log_OC.d(TAG, "Upgrading " + account.name + " to " + newAccountName);\r
+\r
+ // create the new account\r
+ newAccount = new Account(newAccountName, MainApp.getAccountType());\r
+ password = accountMgr.getPassword(account);\r
+ accountMgr.addAccountExplicitly(newAccount, (password != null) ? password : "", null);\r
+\r
+ // copy base URL\r
+ accountMgr.setUserData(newAccount, Constants.KEY_OC_BASE_URL, serverUrl);\r
+\r
+ // copy server version\r
+ accountMgr.setUserData(\r
+ newAccount,\r
+ Constants.KEY_OC_VERSION,\r
+ accountMgr.getUserData(account, Constants.KEY_OC_VERSION)\r
+ );\r
+\r
+ // copy cookies\r
+ accountMgr.setUserData(\r
+ newAccount,\r
+ Constants.KEY_COOKIES,\r
+ accountMgr.getUserData(account, Constants.KEY_COOKIES)\r
+ );\r
+\r
+ // copy type of authentication\r
+ String isSamlStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO);\r
+ boolean isSaml = "TRUE".equals(isSamlStr);\r
+ if (isSaml) {\r
+ accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");\r
+ }\r
+\r
+ String isOauthStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2);\r
+ boolean isOAuth = "TRUE".equals(isOauthStr);\r
+ if (isOAuth) {\r
+ accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");\r
+ }\r
+ /* TODO - study if it's possible to run this method in a background thread to copy the authToken\r
+ if (isOAuth || isSaml) {\r
+ accountMgr.setAuthToken(newAccount, mAuthTokenType, mAuthToken);\r
+ }\r
+ */\r
+\r
+ // don't forget the account saved in preferences as the current one\r
+ if (currentAccount != null && currentAccount.name.equals(account.name)) {\r
+ AccountUtils.setCurrentOwnCloudAccount(context, newAccountName);\r
+ }\r
+\r
+ // remove the old account\r
+ accountMgr.removeAccount(account, null, null); // will assume it succeeds, not a big deal otherwise\r
+\r
+ } else {\r
+ // servers which base URL is in the root of their domain need no change\r
+ Log_OC.d(TAG, account.name + " needs no upgrade ");\r
+ newAccount = account;\r
+ }\r
+\r
+ // at least, upgrade account version\r
+ Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION + " to " + newAccountName);\r
+ accountMgr.setUserData(newAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION));\r
+\r
+ }\r
+ }\r
}\r
}\r
\r
+\r
}\r