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