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