Refresh authorization token on failed synchronizations when notification error is...
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / AccountAuthenticator.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.authenticator;
20
21 import com.owncloud.android.ui.activity.AuthenticatorActivity;
22
23 import android.accounts.*;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.util.Log;
28
29 public class AccountAuthenticator extends AbstractAccountAuthenticator {
30 /**
31 * Is used by android system to assign accounts to authenticators. Should be
32 * used by application and all extensions.
33 */
34 public static final String ACCOUNT_TYPE = "owncloud";
35 public static final String AUTHORITY = "org.owncloud";
36 public static final String AUTH_TOKEN_TYPE = "org.owncloud";
37 public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";
38 public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";
39 public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";
40
41 public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
42 public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
43 public static final String KEY_LOGIN_OPTIONS = "loginOptions";
44 public static final String KEY_ACCOUNT = "account";
45 /**
46 * Value under this key should handle path to webdav php script. Will be
47 * removed and usage should be replaced by combining
48 * {@link com.owncloud.android.authenticator.AuthenticatorActivity.KEY_OC_BASE_URL} and
49 * {@link com.owncloud.android.utils.OwnCloudVersion}
50 *
51 * @deprecated
52 */
53 public static final String KEY_OC_URL = "oc_url";
54 /**
55 * Version should be 3 numbers separated by dot so it can be parsed by
56 * {@link com.owncloud.android.utils.OwnCloudVersion}
57 */
58 public static final String KEY_OC_VERSION = "oc_version";
59 /**
60 * Base url should point to owncloud installation without trailing / ie:
61 * http://server/path or https://owncloud.server
62 */
63 public static final String KEY_OC_BASE_URL = "oc_base_url";
64 /**
65 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
66 */
67 public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";
68
69 private static final String TAG = AccountAuthenticator.class.getSimpleName();
70
71 private Context mContext;
72
73 public AccountAuthenticator(Context context) {
74 super(context);
75 mContext = context;
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public Bundle addAccount(AccountAuthenticatorResponse response,
83 String accountType, String authTokenType,
84 String[] requiredFeatures, Bundle options)
85 throws NetworkErrorException {
86 Log.i(TAG, "Adding account with type " + accountType
87 + " and auth token " + authTokenType);
88 try {
89 validateAccountType(accountType);
90 } catch (AuthenticatorException e) {
91 Log.e(TAG, "Failed to validate account type " + accountType + ": "
92 + e.getMessage());
93 e.printStackTrace();
94 return e.getFailureBundle();
95 }
96 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
97 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
98 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
99 intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
100 intent.putExtra(KEY_LOGIN_OPTIONS, options);
101 intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_CREATE);
102
103 setIntentFlags(intent);
104
105 final Bundle bundle = new Bundle();
106 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
107 return bundle;
108 }
109
110 /**
111 * {@inheritDoc}
112 */
113 @Override
114 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
115 Account account, Bundle options) throws NetworkErrorException {
116 try {
117 validateAccountType(account.type);
118 } catch (AuthenticatorException e) {
119 Log.e(TAG, "Failed to validate account type " + account.type + ": "
120 + e.getMessage());
121 e.printStackTrace();
122 return e.getFailureBundle();
123 }
124 Intent intent = new Intent(mContext, AuthenticatorActivity.class);
125 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
126 response);
127 intent.putExtra(KEY_ACCOUNT, account);
128 intent.putExtra(KEY_LOGIN_OPTIONS, options);
129
130 setIntentFlags(intent);
131
132 Bundle resultBundle = new Bundle();
133 resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);
134 return resultBundle;
135 }
136
137 @Override
138 public Bundle editProperties(AccountAuthenticatorResponse response,
139 String accountType) {
140 return null;
141 }
142
143 /**
144 * {@inheritDoc}
145 */
146 @Override
147 public Bundle getAuthToken(AccountAuthenticatorResponse response,
148 Account account, String authTokenType, Bundle options)
149 throws NetworkErrorException {
150 /// validate parameters
151 try {
152 validateAccountType(account.type);
153 validateAuthTokenType(authTokenType);
154 } catch (AuthenticatorException e) {
155 Log.e(TAG, "Failed to validate account type " + account.type + ": "
156 + e.getMessage());
157 e.printStackTrace();
158 return e.getFailureBundle();
159 }
160
161 /// check if required token is stored
162 final AccountManager am = AccountManager.get(mContext);
163 String accessToken;
164 if (authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD)) {
165 accessToken = am.getPassword(account);
166 } else {
167 accessToken = am.peekAuthToken(account, authTokenType);
168 }
169 if (accessToken != null) {
170 final Bundle result = new Bundle();
171 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
172 result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
173 result.putString(AccountManager.KEY_AUTHTOKEN, accessToken);
174 return result;
175 }
176
177 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
178 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
179 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
180 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
181 intent.putExtra(KEY_LOGIN_OPTIONS, options);
182 intent.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
183 intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
184
185
186 final Bundle bundle = new Bundle();
187 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
188 return bundle;
189 }
190
191 @Override
192 public String getAuthTokenLabel(String authTokenType) {
193 return null;
194 }
195
196 @Override
197 public Bundle hasFeatures(AccountAuthenticatorResponse response,
198 Account account, String[] features) throws NetworkErrorException {
199 final Bundle result = new Bundle();
200 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
201 return result;
202 }
203
204 @Override
205 public Bundle updateCredentials(AccountAuthenticatorResponse response,
206 Account account, String authTokenType, Bundle options)
207 throws NetworkErrorException {
208 final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
209 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
210 response);
211 intent.putExtra(KEY_ACCOUNT, account);
212 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
213 intent.putExtra(KEY_LOGIN_OPTIONS, options);
214 setIntentFlags(intent);
215
216 final Bundle bundle = new Bundle();
217 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
218 return bundle;
219 }
220
221 @Override
222 public Bundle getAccountRemovalAllowed(
223 AccountAuthenticatorResponse response, Account account)
224 throws NetworkErrorException {
225 return super.getAccountRemovalAllowed(response, account);
226 }
227
228 private void setIntentFlags(Intent intent) {
229 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
230 //intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
231 //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); // incompatible with the authorization code grant in OAuth
232 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
233 intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
234 }
235
236 private void validateAccountType(String type)
237 throws UnsupportedAccountTypeException {
238 if (!type.equals(ACCOUNT_TYPE)) {
239 throw new UnsupportedAccountTypeException();
240 }
241 }
242
243 private void validateAuthTokenType(String authTokenType)
244 throws UnsupportedAuthTokenTypeException {
245 if (!authTokenType.equals(AUTH_TOKEN_TYPE) &&
246 !authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD) &&
247 !authTokenType.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN) &&
248 !authTokenType.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN) ) {
249 throw new UnsupportedAuthTokenTypeException();
250 }
251 }
252
253 public static class AuthenticatorException extends Exception {
254 private static final long serialVersionUID = 1L;
255 private Bundle mFailureBundle;
256
257 public AuthenticatorException(int code, String errorMsg) {
258 mFailureBundle = new Bundle();
259 mFailureBundle.putInt(AccountManager.KEY_ERROR_CODE, code);
260 mFailureBundle
261 .putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);
262 }
263
264 public Bundle getFailureBundle() {
265 return mFailureBundle;
266 }
267 }
268
269 public static class UnsupportedAccountTypeException extends
270 AuthenticatorException {
271 private static final long serialVersionUID = 1L;
272
273 public UnsupportedAccountTypeException() {
274 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
275 "Unsupported account type");
276 }
277 }
278
279 public static class UnsupportedAuthTokenTypeException extends
280 AuthenticatorException {
281 private static final long serialVersionUID = 1L;
282
283 public UnsupportedAuthTokenTypeException() {
284 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
285 "Unsupported auth token type");
286 }
287 }
288
289 public static class UnsupportedFeaturesException extends
290 AuthenticatorException {
291 public static final long serialVersionUID = 1L;
292
293 public UnsupportedFeaturesException() {
294 super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
295 "Unsupported features");
296 }
297 }
298
299 public static class AccessDeniedException extends AuthenticatorException {
300 public AccessDeniedException(int code, String errorMsg) {
301 super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");
302 }
303
304 private static final long serialVersionUID = 1L;
305
306 }
307 }