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