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