AccountUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2012 Bartek Przybylski
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.authentication;
  21. import android.accounts.Account;
  22. import android.accounts.AccountManager;
  23. import android.content.Context;
  24. import android.content.SharedPreferences;
  25. import android.net.Uri;
  26. import android.preference.PreferenceManager;
  27. import com.owncloud.android.MainApp;
  28. import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
  29. import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
  30. import com.owncloud.android.lib.common.utils.Log_OC;
  31. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  32. import java.util.Locale;
  33. public class AccountUtils {
  34. private static final String TAG = AccountUtils.class.getSimpleName();
  35. public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav";
  36. public static final String DAV_PATH = "/remote.php/dav";
  37. private static final String ODAV_PATH = "/remote.php/odav";
  38. private static final String SAML_SSO_PATH = "/remote.php/webdav";
  39. public static final String STATUS_PATH = "/status.php";
  40. public static final int ACCOUNT_VERSION = 1;
  41. /**
  42. * Can be used to get the currently selected ownCloud {@link Account} in the
  43. * application preferences.
  44. *
  45. * @param context The current application {@link Context}
  46. * @return The ownCloud {@link Account} currently saved in preferences, or the first
  47. * {@link Account} available, if valid (still registered in the system as ownCloud
  48. * account). If none is available and valid, returns null.
  49. */
  50. public static Account getCurrentOwnCloudAccount(Context context) {
  51. Account[] ocAccounts = getAccounts(context);
  52. Account defaultAccount = null;
  53. SharedPreferences appPreferences = PreferenceManager
  54. .getDefaultSharedPreferences(context);
  55. String accountName = appPreferences
  56. .getString("select_oc_account", null);
  57. // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
  58. if (accountName != null) {
  59. for (Account account : ocAccounts) {
  60. if (account.name.equals(accountName)) {
  61. defaultAccount = account;
  62. break;
  63. }
  64. }
  65. }
  66. if (defaultAccount == null && ocAccounts.length != 0) {
  67. // take first account as fallback
  68. defaultAccount = ocAccounts[0];
  69. }
  70. return defaultAccount;
  71. }
  72. public static Account[] getAccounts(Context context) {
  73. AccountManager accountManager = AccountManager.get(context);
  74. return accountManager.getAccountsByType(MainApp.getAccountType());
  75. }
  76. public static boolean exists(Account account, Context context) {
  77. Account[] ocAccounts = getAccounts(context);
  78. if (account != null && account.name != null) {
  79. int lastAtPos = account.name.lastIndexOf("@");
  80. String hostAndPort = account.name.substring(lastAtPos + 1);
  81. String username = account.name.substring(0, lastAtPos);
  82. String otherHostAndPort, otherUsername;
  83. Locale currentLocale = context.getResources().getConfiguration().locale;
  84. for (Account otherAccount : ocAccounts) {
  85. lastAtPos = otherAccount.name.lastIndexOf("@");
  86. otherHostAndPort = otherAccount.name.substring(lastAtPos + 1);
  87. otherUsername = otherAccount.name.substring(0, lastAtPos);
  88. if (otherHostAndPort.equals(hostAndPort) &&
  89. otherUsername.toLowerCase(currentLocale).
  90. equals(username.toLowerCase(currentLocale))) {
  91. return true;
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. /**
  98. * returns the user's name based on the account name.
  99. *
  100. * @param accountName the account name
  101. * @return the user's name
  102. */
  103. public static String getAccountUsername(String accountName) {
  104. if (accountName != null) {
  105. return accountName.substring(0, accountName.lastIndexOf('@'));
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * Returns owncloud account identified by accountName or null if it does not exist.
  112. * @param context
  113. * @param accountName name of account to be returned
  114. * @return owncloud account named accountName
  115. */
  116. public static Account getOwnCloudAccountByName(Context context, String accountName) {
  117. Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
  118. MainApp.getAccountType());
  119. for (Account account : ocAccounts) {
  120. if(account.name.equals(accountName)) {
  121. return account;
  122. }
  123. }
  124. return null;
  125. }
  126. public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
  127. boolean result = false;
  128. if (accountName != null) {
  129. boolean found;
  130. for (Account account : getAccounts(context)) {
  131. found = (account.name.equals(accountName));
  132. if (found) {
  133. SharedPreferences.Editor appPrefs = PreferenceManager
  134. .getDefaultSharedPreferences(context).edit();
  135. appPrefs.putString("select_oc_account", accountName);
  136. appPrefs.commit();
  137. result = true;
  138. break;
  139. }
  140. }
  141. }
  142. return result;
  143. }
  144. /**
  145. * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
  146. * according to its version and the authorization method used.
  147. *
  148. * @param version Version of ownCloud server.
  149. * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in
  150. * {@link AccountAuthenticator}.
  151. * @return WebDAV path for given OC version and authorization method, null if OC version
  152. * is unknown; versions prior to ownCloud 4 are not supported anymore
  153. */
  154. public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
  155. if (version != null) {
  156. if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(authTokenType)) {
  157. return ODAV_PATH;
  158. }
  159. if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {
  160. return SAML_SSO_PATH;
  161. }
  162. return WEBDAV_PATH_4_0_AND_LATER;
  163. }
  164. return null;
  165. }
  166. /**
  167. * Update the accounts in AccountManager to meet the current version of accounts expected by the app, if needed.
  168. *
  169. * Introduced to handle a change in the structure of stored account names needed to allow different OC servers
  170. * in the same domain, but not in the same path.
  171. *
  172. * @param context Used to access the AccountManager.
  173. */
  174. public static void updateAccountVersion(Context context) {
  175. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(context);
  176. AccountManager accountMgr = AccountManager.get(context);
  177. if ( currentAccount != null ) {
  178. String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);
  179. if (currentAccountVersion == null) {
  180. Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION);
  181. Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());
  182. String serverUrl, username, newAccountName, password;
  183. Account newAccount;
  184. for (Account account : ocAccounts) {
  185. // build new account name
  186. serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);
  187. username = com.owncloud.android.lib.common.accounts.AccountUtils.
  188. getUsernameForAccount(account);
  189. newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.
  190. buildAccountName(Uri.parse(serverUrl), username);
  191. // migrate to a new account, if needed
  192. if (!newAccountName.equals(account.name)) {
  193. Log_OC.d(TAG, "Upgrading " + account.name + " to " + newAccountName);
  194. // create the new account
  195. newAccount = new Account(newAccountName, MainApp.getAccountType());
  196. password = accountMgr.getPassword(account);
  197. accountMgr.addAccountExplicitly(newAccount, (password != null) ? password : "", null);
  198. // copy base URL
  199. accountMgr.setUserData(newAccount, Constants.KEY_OC_BASE_URL, serverUrl);
  200. // copy server version
  201. accountMgr.setUserData(
  202. newAccount,
  203. Constants.KEY_OC_VERSION,
  204. accountMgr.getUserData(account, Constants.KEY_OC_VERSION)
  205. );
  206. // copy cookies
  207. accountMgr.setUserData(
  208. newAccount,
  209. Constants.KEY_COOKIES,
  210. accountMgr.getUserData(account, Constants.KEY_COOKIES)
  211. );
  212. // copy type of authentication
  213. final String isSamlStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO);
  214. if (Boolean.parseBoolean(isSamlStr)) {
  215. accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
  216. }
  217. final String isOauthStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2);
  218. if (Boolean.parseBoolean(isOauthStr)) {
  219. accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");
  220. }
  221. /* TODO - study if it's possible to run this method in a background thread to copy the authToken
  222. if (isOAuth || isSaml) {
  223. accountMgr.setAuthToken(newAccount, mAuthTokenType, mAuthToken);
  224. }
  225. */
  226. // don't forget the account saved in preferences as the current one
  227. if (currentAccount.name.equals(account.name)) {
  228. AccountUtils.setCurrentOwnCloudAccount(context, newAccountName);
  229. }
  230. // remove the old account
  231. accountMgr.removeAccount(account, null, null);
  232. // will assume it succeeds, not a big deal otherwise
  233. } else {
  234. // servers which base URL is in the root of their domain need no change
  235. Log_OC.d(TAG, account.name + " needs no upgrade ");
  236. newAccount = account;
  237. }
  238. // at least, upgrade account version
  239. Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION + " to " + newAccountName);
  240. accountMgr.setUserData(
  241. newAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION)
  242. );
  243. }
  244. }
  245. }
  246. }
  247. public static String trimWebdavSuffix(String url) {
  248. while(url.endsWith("/")) {
  249. url = url.substring(0, url.length() - 1);
  250. }
  251. int pos = url.lastIndexOf(WEBDAV_PATH_4_0_AND_LATER);
  252. if (pos >= 0) {
  253. url = url.substring(0, pos);
  254. } else {
  255. pos = url.lastIndexOf(ODAV_PATH);
  256. if (pos >= 0) {
  257. url = url.substring(0, pos);
  258. }
  259. }
  260. return url;
  261. }
  262. /**
  263. * Access the version of the OC server corresponding to an account SAVED IN THE ACCOUNTMANAGER
  264. *
  265. * @param account ownCloud account
  266. * @return Version of the OC server corresponding to account, according to the data saved
  267. * in the system AccountManager
  268. */
  269. public static OwnCloudVersion getServerVersion(Account account) {
  270. OwnCloudVersion serverVersion = null;
  271. if (account != null) {
  272. AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
  273. String serverVersionStr = accountMgr.getUserData(account, Constants.KEY_OC_VERSION);
  274. if (serverVersionStr != null) {
  275. serverVersion = new OwnCloudVersion(serverVersionStr);
  276. }
  277. }
  278. return serverVersion;
  279. }
  280. private static OwnCloudVersion getOwnCloudVersion(Account account) {
  281. OwnCloudVersion serverVersion = null;
  282. if (account != null) {
  283. AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
  284. String serverVersionStr = accountMgr.getUserData(account, Constants.KEY_OC_VERSION);
  285. if (serverVersionStr != null) {
  286. serverVersion = new OwnCloudVersion(serverVersionStr);
  287. }
  288. }
  289. return serverVersion;
  290. }
  291. public static boolean hasSearchUsersSupport(Account account){
  292. OwnCloudVersion serverVersion = getOwnCloudVersion(account);
  293. return (serverVersion != null && serverVersion.isSearchUsersSupported());
  294. }
  295. public static boolean hasSearchSupport(Account account) {
  296. OwnCloudVersion serverVersion = getOwnCloudVersion(account);
  297. return (serverVersion != null && serverVersion.isSearchSupported());
  298. }
  299. }