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