Delete Override onContentChanged method in AccountSelectActivity
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AccountAuthenticator.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 android.accounts.*;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.widget.Toast;
27
28 import com.owncloud.android.Log_OC;
29 import com.owncloud.android.R;
30
31 /**
32 * Authenticator for ownCloud accounts.
33 *
34 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
35 *
36 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
37 * TODO - review completeness
38 *
39 * @author David A. Velasco
40 */
41 public class AccountAuthenticator extends AbstractAccountAuthenticator {
42
43 /**
44 * Is used by android system to assign accounts to authenticators. Should be
45 * used by application and all extensions.
46 */
47 public static final String ACCOUNT_TYPE = "owncloud";
48 public static final String AUTHORITY = "org.owncloud";
49 public static final String AUTH_TOKEN_TYPE = "org.owncloud";
50 public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";
51 public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";
52 public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";
53 public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE = "owncloud.saml.web_sso.session_cookie";
54
55 public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
56 public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
57 public static final String KEY_LOGIN_OPTIONS = "loginOptions";
58 public static final String KEY_ACCOUNT = "account";
59
60 /**
61 * Value under this key should handle path to webdav php script. Will be
62 * removed and usage should be replaced by combining
63 * {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and
64 * {@link com.owncloud.android.utils.OwnCloudVersion}
65 *
66 * @deprecated
67 */
68 public static final String KEY_OC_URL = "oc_url";
69 /**
70 * Version should be 3 numbers separated by dot so it can be parsed by
71 * {@link com.owncloud.android.utils.OwnCloudVersion}
72 */
73 public static final String KEY_OC_VERSION = "oc_version";
74 /**
75 * Base url should point to owncloud installation without trailing / ie:
76 * http://server/path or https://owncloud.server
77 */
78 public static final String KEY_OC_BASE_URL = "oc_base_url";
79 /**
80 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
81 */
82 public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";
83 /**
84 * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
85 */
86 public static final String KEY_SUPPORTS_SAML_WEB_SSO = "oc_supports_saml_web_sso";
87
88 private static final String TAG = AccountAuthenticator.class.getSimpleName();
89
90 private Context mContext;
91
92 private Handler mHandler;
93
94 public AccountAuthenticator(Context context) {
95 super(context);
96 mContext = context;
97 mHandler = new Handler();
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public Bundle addAccount(AccountAuthenticatorResponse response,
105 String accountType, String authTokenType,
106 String[] requiredFeatures, Bundle options)
107 throws NetworkErrorException {
108 Log_OC.i(TAG, "Adding account with type " + accountType
109 + " and auth token " + authTokenType);
110
111 final Bundle bundle = new Bundle();
112
113 AccountManager accountManager = AccountManager.get(mContext);
114 Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
115
116 if (mContext.getResources().getBoolean(R.bool.multiaccount_support) || accounts.length < 1) {
117 try {
118 validateAccountType(accountType);
119 } catch (AuthenticatorException e) {
120 Log_OC.e(TAG, "Failed to validate account type " + accountType + ": "
121 + e.getMessage());
122 e.printStackTrace();
123 return e.getFailureBundle();
124 }
125
126 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
127 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
128 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
129 intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
130 intent.putExtra(KEY_LOGIN_OPTIONS, options);
131 intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_CREATE);
132
133 setIntentFlags(intent);
134
135 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
136
137 } else {
138
139 // Return an error
140 bundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION);
141 bundle.putString(AccountManager.KEY_ERROR_MESSAGE, mContext.getString(R.string.auth_unsupported_multiaccount));
142
143 mHandler.post(new Runnable() {
144
145 @Override
146 public void run() {
147 Toast.makeText(mContext, R.string.auth_unsupported_multiaccount, Toast.LENGTH_SHORT).show();
148 }
149 });
150
151 }
152
153 return bundle;
154 }
155
156 /**
157 * {@inheritDoc}
158 */
159 @Override
160 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
161 Account account, Bundle options) throws NetworkErrorException {
162 try {
163 validateAccountType(account.type);
164 } catch (AuthenticatorException e) {
165 Log_OC.e(TAG, "Failed to validate account type " + account.type + ": "
166 + e.getMessage());
167 e.printStackTrace();
168 return e.getFailureBundle();
169 }
170 Intent intent = new Intent(mContext, AuthenticatorActivity.class);
171 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
172 response);
173 intent.putExtra(KEY_ACCOUNT, account);
174 intent.putExtra(KEY_LOGIN_OPTIONS, options);
175
176 setIntentFlags(intent);
177
178 Bundle resultBundle = new Bundle();
179 resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);
180 return resultBundle;
181 }
182
183 @Override
184 public Bundle editProperties(AccountAuthenticatorResponse response,
185 String accountType) {
186 return null;
187 }
188
189 /**
190 * {@inheritDoc}
191 */
192 @Override
193 public Bundle getAuthToken(AccountAuthenticatorResponse response,
194 Account account, String authTokenType, Bundle options)
195 throws NetworkErrorException {
196 /// validate parameters
197 try {
198 validateAccountType(account.type);
199 validateAuthTokenType(authTokenType);
200 } catch (AuthenticatorException e) {
201 Log_OC.e(TAG, "Failed to validate account type " + account.type + ": "
202 + e.getMessage());
203 e.printStackTrace();
204 return e.getFailureBundle();
205 }
206
207 /// check if required token is stored
208 final AccountManager am = AccountManager.get(mContext);
209 String accessToken;
210 if (authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD)) {
211 accessToken = am.getPassword(account);
212 } else {
213 accessToken = am.peekAuthToken(account, authTokenType);
214 }
215 if (accessToken != null) {
216 final Bundle result = new Bundle();
217 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
218 result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
219 result.putString(AccountManager.KEY_AUTHTOKEN, accessToken);
220 return result;
221 }
222
223 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
224 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
225 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
226 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
227 intent.putExtra(KEY_LOGIN_OPTIONS, options);
228 intent.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
229 intent.putExtra(AuthenticatorActivity.EXTRA_ENFORCED_UPDATE, true);
230 intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
231
232
233 final Bundle bundle = new Bundle();
234 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
235 return bundle;
236 }
237
238 @Override
239 public String getAuthTokenLabel(String authTokenType) {
240 return null;
241 }
242
243 @Override
244 public Bundle hasFeatures(AccountAuthenticatorResponse response,
245 Account account, String[] features) throws NetworkErrorException {
246 final Bundle result = new Bundle();
247 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
248 return result;
249 }
250
251 @Override
252 public Bundle updateCredentials(AccountAuthenticatorResponse response,
253 Account account, String authTokenType, Bundle options)
254 throws NetworkErrorException {
255 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
256 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
257 response);
258 intent.putExtra(KEY_ACCOUNT, account);
259 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
260 intent.putExtra(KEY_LOGIN_OPTIONS, options);
261 setIntentFlags(intent);
262
263 final Bundle bundle = new Bundle();
264 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
265 return bundle;
266 }
267
268 @Override
269 public Bundle getAccountRemovalAllowed(
270 AccountAuthenticatorResponse response, Account account)
271 throws NetworkErrorException {
272 return super.getAccountRemovalAllowed(response, account);
273 }
274
275 private void setIntentFlags(Intent intent) {
276 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
277 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
278 intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
279 }
280
281 private void validateAccountType(String type)
282 throws UnsupportedAccountTypeException {
283 if (!type.equals(ACCOUNT_TYPE)) {
284 throw new UnsupportedAccountTypeException();
285 }
286 }
287
288 private void validateAuthTokenType(String authTokenType)
289 throws UnsupportedAuthTokenTypeException {
290 if (!authTokenType.equals(AUTH_TOKEN_TYPE) &&
291 !authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD) &&
292 !authTokenType.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN) &&
293 !authTokenType.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN) &&
294 !authTokenType.equals(AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE)) {
295 throw new UnsupportedAuthTokenTypeException();
296 }
297 }
298
299 public static class AuthenticatorException extends Exception {
300 private static final long serialVersionUID = 1L;
301 private Bundle mFailureBundle;
302
303 public AuthenticatorException(int code, String errorMsg) {
304 mFailureBundle = new Bundle();
305 mFailureBundle.putInt(AccountManager.KEY_ERROR_CODE, code);
306 mFailureBundle
307 .putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);
308 }
309
310 public Bundle getFailureBundle() {
311 return mFailureBundle;
312 }
313 }
314
315 public static class UnsupportedAccountTypeException extends
316 AuthenticatorException {
317 private static final long serialVersionUID = 1L;
318
319 public UnsupportedAccountTypeException() {
320 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
321 "Unsupported account type");
322 }
323 }
324
325 public static class UnsupportedAuthTokenTypeException extends
326 AuthenticatorException {
327 private static final long serialVersionUID = 1L;
328
329 public UnsupportedAuthTokenTypeException() {
330 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
331 "Unsupported auth token type");
332 }
333 }
334
335 public static class UnsupportedFeaturesException extends
336 AuthenticatorException {
337 public static final long serialVersionUID = 1L;
338
339 public UnsupportedFeaturesException() {
340 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
341 "Unsupported features");
342 }
343 }
344
345 public static class AccessDeniedException extends AuthenticatorException {
346 public AccessDeniedException(int code, String errorMsg) {
347 super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");
348 }
349
350 private static final long serialVersionUID = 1L;
351
352 }
353 }