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