1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.authentication
;
21 import android
.accounts
.*;
22 import android
.content
.Context
;
23 import android
.content
.Intent
;
24 import android
.os
.Bundle
;
25 import android
.os
.Handler
;
26 import android
.widget
.Toast
;
28 import com
.owncloud
.android
.Log_OC
;
29 import com
.owncloud
.android
.MainApp
;
31 import com
.owncloud
.android
.R
;
35 * Authenticator for ownCloud accounts.
37 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
39 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
40 * TODO - review completeness
42 * @author David A. Velasco
44 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
47 * Is used by android system to assign accounts to authenticators. Should be
48 * used by application and all extensions.
50 /* These constants are now in MainApp
51 public static final String ACCOUNT_TYPE = "owncloud";
52 public static final String AUTHORITY = "org.owncloud";
53 public static final String AUTH_TOKEN_TYPE = "org.owncloud";
54 public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";
55 public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";
56 public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";
57 public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE = "owncloud.saml.web_sso.session_cookie";
59 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
60 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
61 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
62 public static final String KEY_ACCOUNT
= "account";
65 * Value under this key should handle path to webdav php script. Will be
66 * removed and usage should be replaced by combining
67 * {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and
68 * {@link com.owncloud.android.utils.OwnCloudVersion}
72 public static final String KEY_OC_URL
= "oc_url";
74 * Version should be 3 numbers separated by dot so it can be parsed by
75 * {@link com.owncloud.android.utils.OwnCloudVersion}
77 public static final String KEY_OC_VERSION
= "oc_version";
79 * Base url should point to owncloud installation without trailing / ie:
80 * http://server/path or https://owncloud.server
82 public static final String KEY_OC_BASE_URL
= "oc_base_url";
84 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
86 public static final String KEY_SUPPORTS_OAUTH2
= "oc_supports_oauth2";
88 * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
90 public static final String KEY_SUPPORTS_SAML_WEB_SSO
= "oc_supports_saml_web_sso";
92 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
94 private Context mContext
;
96 private Handler mHandler
;
98 public AccountAuthenticator(Context context
) {
101 mHandler
= new Handler();
108 public Bundle
addAccount(AccountAuthenticatorResponse response
,
109 String accountType
, String authTokenType
,
110 String
[] requiredFeatures
, Bundle options
)
111 throws NetworkErrorException
{
112 Log_OC
.i(TAG
, "Adding account with type " + accountType
113 + " and auth token " + authTokenType
);
115 final Bundle bundle
= new Bundle();
117 AccountManager accountManager
= AccountManager
.get(mContext
);
118 Account
[] accounts
= accountManager
.getAccountsByType(MainApp
.getAccountType());
120 if (mContext
.getResources().getBoolean(R
.bool
.multiaccount_support
) || accounts
.length
< 1) {
122 validateAccountType(accountType
);
123 } catch (AuthenticatorException e
) {
124 Log_OC
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
127 return e
.getFailureBundle();
130 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
131 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
132 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
133 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
134 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
135 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
137 setIntentFlags(intent
);
139 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
144 bundle
.putInt(AccountManager
.KEY_ERROR_CODE
, AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
);
145 final String message
= String
.format(mContext
.getString(R
.string
.auth_unsupported_multiaccount
), mContext
.getString(R
.string
.app_name
));
146 bundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, message
);
148 mHandler
.post(new Runnable() {
152 Toast
.makeText(mContext
, message
, Toast
.LENGTH_SHORT
).show();
165 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
166 Account account
, Bundle options
) throws NetworkErrorException
{
168 validateAccountType(account
.type
);
169 } catch (AuthenticatorException e
) {
170 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
173 return e
.getFailureBundle();
175 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
176 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
178 intent
.putExtra(KEY_ACCOUNT
, account
);
179 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
181 setIntentFlags(intent
);
183 Bundle resultBundle
= new Bundle();
184 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
189 public Bundle
editProperties(AccountAuthenticatorResponse response
,
190 String accountType
) {
198 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
199 Account account
, String authTokenType
, Bundle options
)
200 throws NetworkErrorException
{
201 /// validate parameters
203 validateAccountType(account
.type
);
204 validateAuthTokenType(authTokenType
);
205 } catch (AuthenticatorException e
) {
206 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
209 return e
.getFailureBundle();
212 /// check if required token is stored
213 final AccountManager am
= AccountManager
.get(mContext
);
215 if (authTokenType
.equals(MainApp
.getAuthTokenTypePass())) {
216 accessToken
= am
.getPassword(account
);
218 accessToken
= am
.peekAuthToken(account
, authTokenType
);
220 if (accessToken
!= null
) {
221 final Bundle result
= new Bundle();
222 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
223 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, MainApp
.getAccountType());
224 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
228 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
229 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
230 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
231 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
232 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
233 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
234 intent
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
235 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
238 final Bundle bundle
= new Bundle();
239 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
244 public String
getAuthTokenLabel(String authTokenType
) {
249 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
250 Account account
, String
[] features
) throws NetworkErrorException
{
251 final Bundle result
= new Bundle();
252 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
257 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
258 Account account
, String authTokenType
, Bundle options
)
259 throws NetworkErrorException
{
260 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
261 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
263 intent
.putExtra(KEY_ACCOUNT
, account
);
264 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
265 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
266 setIntentFlags(intent
);
268 final Bundle bundle
= new Bundle();
269 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
274 public Bundle
getAccountRemovalAllowed(
275 AccountAuthenticatorResponse response
, Account account
)
276 throws NetworkErrorException
{
277 return super.getAccountRemovalAllowed(response
, account
);
280 private void setIntentFlags(Intent intent
) {
281 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
282 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
283 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
286 private void validateAccountType(String type
)
287 throws UnsupportedAccountTypeException
{
288 if (!type
.equals(MainApp
.getAccountType())) {
289 throw new UnsupportedAccountTypeException();
293 private void validateAuthTokenType(String authTokenType
)
294 throws UnsupportedAuthTokenTypeException
{
295 if (!authTokenType
.equals(MainApp
.getAuthTokenType()) &&
296 !authTokenType
.equals(MainApp
.getAuthTokenTypePass()) &&
297 !authTokenType
.equals(MainApp
.getAuthTokenTypeAccessToken()) &&
298 !authTokenType
.equals(MainApp
.getAuthTokenTypeRefreshToken()) &&
299 !authTokenType
.equals(MainApp
.getAuthTokenTypeSamlSessionCookie())) {
300 throw new UnsupportedAuthTokenTypeException();
304 public static class AuthenticatorException
extends Exception
{
305 private static final long serialVersionUID
= 1L;
306 private Bundle mFailureBundle
;
308 public AuthenticatorException(int code
, String errorMsg
) {
309 mFailureBundle
= new Bundle();
310 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
312 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
315 public Bundle
getFailureBundle() {
316 return mFailureBundle
;
320 public static class UnsupportedAccountTypeException
extends
321 AuthenticatorException
{
322 private static final long serialVersionUID
= 1L;
324 public UnsupportedAccountTypeException() {
325 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
326 "Unsupported account type");
330 public static class UnsupportedAuthTokenTypeException
extends
331 AuthenticatorException
{
332 private static final long serialVersionUID
= 1L;
334 public UnsupportedAuthTokenTypeException() {
335 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
336 "Unsupported auth token type");
340 public static class UnsupportedFeaturesException
extends
341 AuthenticatorException
{
342 public static final long serialVersionUID
= 1L;
344 public UnsupportedFeaturesException() {
345 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
346 "Unsupported features");
350 public static class AccessDeniedException
extends AuthenticatorException
{
351 public AccessDeniedException(int code
, String errorMsg
) {
352 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
355 private static final long serialVersionUID
= 1L;