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