5fb6505f7aa15445f4a8004bd9ef0d4528e28280
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / authentication / AccountUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.oc_framework.authentication;
20
21
22 import com.owncloud.android.oc_framework.MainApp;
23 import com.owncloud.android.oc_framework.OwnCloudVersion;
24
25 import android.accounts.Account;
26 import android.accounts.AccountManager;
27 import android.accounts.AccountsException;
28 import android.content.Context;
29 import android.content.SharedPreferences;
30 import android.preference.PreferenceManager;
31
32 public class AccountUtils {
33 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
34 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
35 public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
36 private static final String ODAV_PATH = "/remote.php/odav";
37 private static final String SAML_SSO_PATH = "/remote.php/webdav";
38 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
39 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
40 public static final String STATUS_PATH = "/status.php";
41
42 /**
43 * Can be used to get the currently selected ownCloud {@link Account} in the
44 * application preferences.
45 *
46 * @param context The current application {@link Context}
47 * @return The ownCloud {@link Account} currently saved in preferences, or the first
48 * {@link Account} available, if valid (still registered in the system as ownCloud
49 * account). If none is available and valid, returns null.
50 */
51 public static Account getCurrentOwnCloudAccount(Context context) {
52 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
53 MainApp.getAccountType());
54 Account defaultAccount = null;
55
56 SharedPreferences appPreferences = PreferenceManager
57 .getDefaultSharedPreferences(context);
58 String accountName = appPreferences
59 .getString("select_oc_account", null);
60
61 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
62 if (accountName != null) {
63 for (Account account : ocAccounts) {
64 if (account.name.equals(accountName)) {
65 defaultAccount = account;
66 break;
67 }
68 }
69 }
70
71 if (defaultAccount == null && ocAccounts.length != 0) {
72 // take first account as fallback
73 defaultAccount = ocAccounts[0];
74 }
75
76 return defaultAccount;
77 }
78
79
80 public static boolean exists(Account account, Context context) {
81 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
82 MainApp.getAccountType());
83
84 if (account != null && account.name != null) {
85 for (Account ac : ocAccounts) {
86 if (ac.name.equals(account.name)) {
87 return true;
88 }
89 }
90 }
91 return false;
92 }
93
94
95 /**
96 * Checks, whether or not there are any ownCloud accounts setup.
97 *
98 * @return true, if there is at least one account.
99 */
100 public static boolean accountsAreSetup(Context context) {
101 AccountManager accMan = AccountManager.get(context);
102 Account[] accounts = accMan
103 .getAccountsByType(MainApp.getAccountType());
104 return accounts.length > 0;
105 }
106
107
108 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
109 boolean result = false;
110 if (accountName != null) {
111 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
112 MainApp.getAccountType());
113 boolean found = false;
114 for (Account account : ocAccounts) {
115 found = (account.name.equals(accountName));
116 if (found) {
117 SharedPreferences.Editor appPrefs = PreferenceManager
118 .getDefaultSharedPreferences(context).edit();
119 appPrefs.putString("select_oc_account", accountName);
120
121 appPrefs.commit();
122 result = true;
123 break;
124 }
125 }
126 }
127 return result;
128 }
129
130 /**
131 *
132 * @param version version of owncloud
133 * @return webdav path for given OC version, null if OC version unknown
134 */
135 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
136 if (version != null) {
137 if (supportsOAuth) {
138 return ODAV_PATH;
139 }
140 if (supportsSamlSso) {
141 return SAML_SSO_PATH;
142 }
143 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
144 return WEBDAV_PATH_4_0;
145 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
146 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
147 return WEBDAV_PATH_2_0;
148 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
149 return WEBDAV_PATH_1_2;
150 }
151 return null;
152 }
153
154 /**
155 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
156 * according to its version and the authorization method used.
157 *
158 * @param version Version of ownCloud server.
159 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
160 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
161 */
162 public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
163 if (version != null) {
164 if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
165 return ODAV_PATH;
166 }
167 if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
168 return SAML_SSO_PATH;
169 }
170 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
171 return WEBDAV_PATH_4_0;
172 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
173 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
174 return WEBDAV_PATH_2_0;
175 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
176 return WEBDAV_PATH_1_2;
177 }
178 return null;
179 }
180
181 /**
182 * Constructs full url to host and webdav resource basing on host version
183 * @param context
184 * @param account
185 * @return url or null on failure
186 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
187 */
188 public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
189 AccountManager ama = AccountManager.get(context);
190 String baseurl = ama.getUserData(account, AccountAuthenticatorConstants.KEY_OC_BASE_URL);
191 String strver = ama.getUserData(account, AccountAuthenticatorConstants.KEY_OC_VERSION);
192 boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_OAUTH2) != null);
193 boolean supportsSamlSso = (ama.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_SAML_WEB_SSO) != null);
194 OwnCloudVersion ver = new OwnCloudVersion(strver);
195 String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);
196
197 if (baseurl == null || webdavpath == null)
198 throw new AccountNotFoundException(account, "Account not found", null);
199
200 return baseurl + webdavpath;
201 }
202
203
204 public static class AccountNotFoundException extends AccountsException {
205
206 /** Generated - should be refreshed every time the class changes!! */
207 private static final long serialVersionUID = -9013287181793186830L;
208
209 private Account mFailedAccount;
210
211 public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
212 super(message, cause);
213 mFailedAccount = failedAccount;
214 }
215
216 public Account getFailedAccount() {
217 return mFailedAccount;
218 }
219 }
220
221 }