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