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