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