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