Disable extra logs
[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 com.owncloud.android.MainApp;
22 import com.owncloud.android.R;
23
24 import android.accounts.*;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.widget.Toast;
30
31 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
32 import com.owncloud.android.utils.Log_OC;
33
34
35 /**
36 * Authenticator for ownCloud accounts.
37 *
38 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
39 *
40 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
41 * TODO - review completeness
42 *
43 * @author David A. Velasco
44 */
45 public class AccountAuthenticator extends AbstractAccountAuthenticator {
46
47 /**
48 * Is used by android system to assign accounts to authenticators. Should be
49 * used by application and all extensions.
50 */
51 public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
52 public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
53 public static final String KEY_LOGIN_OPTIONS = "loginOptions";
54 public static final String KEY_ACCOUNT = "account";
55
56 private static final String TAG = AccountAuthenticator.class.getSimpleName();
57
58 private Context mContext;
59
60 private Handler mHandler;
61
62 public AccountAuthenticator(Context context) {
63 super(context);
64 mContext = context;
65 mHandler = new Handler();
66 }
67
68 /**
69 * {@inheritDoc}
70 */
71 @Override
72 public Bundle addAccount(AccountAuthenticatorResponse response,
73 String accountType, String authTokenType,
74 String[] requiredFeatures, Bundle options)
75 throws NetworkErrorException {
76 Log_OC.i(TAG, "Adding account with type " + accountType
77 + " and auth token " + authTokenType);
78
79 final Bundle bundle = new Bundle();
80
81 AccountManager accountManager = AccountManager.get(mContext);
82 Account[] accounts = accountManager.getAccountsByType(MainApp.getAccountType());
83
84 if (mContext.getResources().getBoolean(R.bool.multiaccount_support) || accounts.length < 1) {
85 try {
86 validateAccountType(accountType);
87 } catch (AuthenticatorException e) {
88 Log_OC.e(TAG, "Failed to validate account type " + accountType + ": "
89 + e.getMessage());
90 e.printStackTrace();
91 return e.getFailureBundle();
92 }
93
94 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
95 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
96 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
97 intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
98 intent.putExtra(KEY_LOGIN_OPTIONS, options);
99 intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_CREATE);
100
101 setIntentFlags(intent);
102
103 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
104
105 } else {
106
107 // Return an error
108 bundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION);
109 final String message = String.format(mContext.getString(R.string.auth_unsupported_multiaccount), mContext.getString(R.string.app_name));
110 bundle.putString(AccountManager.KEY_ERROR_MESSAGE, message);
111
112 mHandler.post(new Runnable() {
113
114 @Override
115 public void run() {
116 Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
117 }
118 });
119
120 }
121
122 return bundle;
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 @Override
129 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
130 Account account, Bundle options) throws NetworkErrorException {
131 try {
132 validateAccountType(account.type);
133 } catch (AuthenticatorException e) {
134 Log_OC.e(TAG, "Failed to validate account type " + account.type + ": "
135 + e.getMessage());
136 e.printStackTrace();
137 return e.getFailureBundle();
138 }
139 Intent intent = new Intent(mContext, AuthenticatorActivity.class);
140 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
141 response);
142 intent.putExtra(KEY_ACCOUNT, account);
143 intent.putExtra(KEY_LOGIN_OPTIONS, options);
144
145 setIntentFlags(intent);
146
147 Bundle resultBundle = new Bundle();
148 resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);
149 return resultBundle;
150 }
151
152 @Override
153 public Bundle editProperties(AccountAuthenticatorResponse response,
154 String accountType) {
155 return null;
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 @Override
162 public Bundle getAuthToken(AccountAuthenticatorResponse response,
163 Account account, String authTokenType, Bundle options)
164 throws NetworkErrorException {
165 /// validate parameters
166 try {
167 validateAccountType(account.type);
168 validateAuthTokenType(authTokenType);
169 } catch (AuthenticatorException e) {
170 Log_OC.e(TAG, "Failed to validate account type " + account.type + ": "
171 + e.getMessage());
172 e.printStackTrace();
173 return e.getFailureBundle();
174 }
175
176 /// check if required token is stored
177 final AccountManager am = AccountManager.get(mContext);
178 String accessToken;
179 if (authTokenType.equals(AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()))) {
180 accessToken = am.getPassword(account);
181 } else {
182 accessToken = am.peekAuthToken(account, authTokenType);
183 }
184 if (accessToken != null) {
185 final Bundle result = new Bundle();
186 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
187 result.putString(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType());
188 result.putString(AccountManager.KEY_AUTHTOKEN, accessToken);
189 return result;
190 }
191
192 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
193 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
194 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
195 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
196 intent.putExtra(KEY_LOGIN_OPTIONS, options);
197 intent.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
198 intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
199
200
201 final Bundle bundle = new Bundle();
202 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
203 return bundle;
204 }
205
206 @Override
207 public String getAuthTokenLabel(String authTokenType) {
208 return null;
209 }
210
211 @Override
212 public Bundle hasFeatures(AccountAuthenticatorResponse response,
213 Account account, String[] features) throws NetworkErrorException {
214 final Bundle result = new Bundle();
215 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
216 return result;
217 }
218
219 @Override
220 public Bundle updateCredentials(AccountAuthenticatorResponse response,
221 Account account, String authTokenType, Bundle options)
222 throws NetworkErrorException {
223 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
224 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
225 response);
226 intent.putExtra(KEY_ACCOUNT, account);
227 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
228 intent.putExtra(KEY_LOGIN_OPTIONS, options);
229 setIntentFlags(intent);
230
231 final Bundle bundle = new Bundle();
232 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
233 return bundle;
234 }
235
236 @Override
237 public Bundle getAccountRemovalAllowed(
238 AccountAuthenticatorResponse response, Account account)
239 throws NetworkErrorException {
240 return super.getAccountRemovalAllowed(response, account);
241 }
242
243 private void setIntentFlags(Intent intent) {
244 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
245 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
246 intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
247 }
248
249 private void validateAccountType(String type)
250 throws UnsupportedAccountTypeException {
251 if (!type.equals(MainApp.getAccountType())) {
252 throw new UnsupportedAccountTypeException();
253 }
254 }
255
256 private void validateAuthTokenType(String authTokenType)
257 throws UnsupportedAuthTokenTypeException {
258 if (!authTokenType.equals(MainApp.getAuthTokenType()) &&
259 !authTokenType.equals(AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType())) &&
260 !authTokenType.equals(AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType())) &&
261 !authTokenType.equals(AccountTypeUtils.getAuthTokenTypeRefreshToken(MainApp.getAccountType())) &&
262 !authTokenType.equals(AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()))) {
263 throw new UnsupportedAuthTokenTypeException();
264 }
265 }
266
267 public static class AuthenticatorException extends Exception {
268 private static final long serialVersionUID = 1L;
269 private Bundle mFailureBundle;
270
271 public AuthenticatorException(int code, String errorMsg) {
272 mFailureBundle = new Bundle();
273 mFailureBundle.putInt(AccountManager.KEY_ERROR_CODE, code);
274 mFailureBundle
275 .putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);
276 }
277
278 public Bundle getFailureBundle() {
279 return mFailureBundle;
280 }
281 }
282
283 public static class UnsupportedAccountTypeException extends
284 AuthenticatorException {
285 private static final long serialVersionUID = 1L;
286
287 public UnsupportedAccountTypeException() {
288 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
289 "Unsupported account type");
290 }
291 }
292
293 public static class UnsupportedAuthTokenTypeException extends
294 AuthenticatorException {
295 private static final long serialVersionUID = 1L;
296
297 public UnsupportedAuthTokenTypeException() {
298 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
299 "Unsupported auth token type");
300 }
301 }
302
303 public static class UnsupportedFeaturesException extends
304 AuthenticatorException {
305 public static final long serialVersionUID = 1L;
306
307 public UnsupportedFeaturesException() {
308 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
309 "Unsupported features");
310 }
311 }
312
313 public static class AccessDeniedException extends AuthenticatorException {
314 public AccessDeniedException(int code, String errorMsg) {
315 super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");
316 }
317
318 private static final long serialVersionUID = 1L;
319
320 }
321 }