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 com
.owncloud
.android
.Log_OC
;
22 import com
.owncloud
.android
.MainApp
;
23 import com
.owncloud
.android
.R
;
25 import android
.accounts
.*;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
28 import android
.os
.Bundle
;
29 import android
.os
.Handler
;
30 import android
.widget
.Toast
;
36 * Authenticator for ownCloud accounts.
38 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
40 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
41 * TODO - review completeness
43 * @author David A. Velasco
45 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
48 * Is used by android system to assign accounts to authenticators. Should be
49 * used by application and all extensions.
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";
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}
64 public static final String KEY_OC_URL
= "oc_url";
66 * Version should be 3 numbers separated by dot so it can be parsed by
67 * {@link com.owncloud.android.utils.OwnCloudVersion}
69 public static final String KEY_OC_VERSION
= "oc_version";
71 * Base url should point to owncloud installation without trailing / ie:
72 * http://server/path or https://owncloud.server
74 public static final String KEY_OC_BASE_URL
= "oc_base_url";
76 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
78 public static final String KEY_SUPPORTS_OAUTH2
= "oc_supports_oauth2";
80 * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
82 public static final String KEY_SUPPORTS_SAML_WEB_SSO
= "oc_supports_saml_web_sso";
84 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
86 private Context mContext
;
88 private Handler mHandler
;
90 public AccountAuthenticator(Context context
) {
93 mHandler
= new Handler();
100 public Bundle
addAccount(AccountAuthenticatorResponse response
,
101 String accountType
, String authTokenType
,
102 String
[] requiredFeatures
, Bundle options
)
103 throws NetworkErrorException
{
104 Log_OC
.i(TAG
, "Adding account with type " + accountType
105 + " and auth token " + authTokenType
);
107 final Bundle bundle
= new Bundle();
109 AccountManager accountManager
= AccountManager
.get(mContext
);
110 Account
[] accounts
= accountManager
.getAccountsByType(MainApp
.getAccountType());
112 if (mContext
.getResources().getBoolean(R
.bool
.multiaccount_support
) || accounts
.length
< 1) {
114 validateAccountType(accountType
);
115 } catch (AuthenticatorException e
) {
116 Log_OC
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
119 return e
.getFailureBundle();
122 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
123 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
124 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
125 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
126 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
127 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
129 setIntentFlags(intent
);
131 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
136 bundle
.putInt(AccountManager
.KEY_ERROR_CODE
, AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
);
137 final String message
= String
.format(mContext
.getString(R
.string
.auth_unsupported_multiaccount
), mContext
.getString(R
.string
.app_name
));
138 bundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, message
);
140 mHandler
.post(new Runnable() {
144 Toast
.makeText(mContext
, message
, Toast
.LENGTH_SHORT
).show();
157 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
158 Account account
, Bundle options
) throws NetworkErrorException
{
160 validateAccountType(account
.type
);
161 } catch (AuthenticatorException e
) {
162 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
165 return e
.getFailureBundle();
167 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
168 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
170 intent
.putExtra(KEY_ACCOUNT
, account
);
171 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
173 setIntentFlags(intent
);
175 Bundle resultBundle
= new Bundle();
176 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
181 public Bundle
editProperties(AccountAuthenticatorResponse response
,
182 String accountType
) {
190 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
191 Account account
, String authTokenType
, Bundle options
)
192 throws NetworkErrorException
{
193 /// validate parameters
195 validateAccountType(account
.type
);
196 validateAuthTokenType(authTokenType
);
197 } catch (AuthenticatorException e
) {
198 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
201 return e
.getFailureBundle();
204 /// check if required token is stored
205 final AccountManager am
= AccountManager
.get(mContext
);
207 if (authTokenType
.equals(MainApp
.getAuthTokenTypePass())) {
208 accessToken
= am
.getPassword(account
);
210 accessToken
= am
.peekAuthToken(account
, authTokenType
);
212 if (accessToken
!= null
) {
213 final Bundle result
= new Bundle();
214 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
215 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, MainApp
.getAccountType());
216 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
220 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
221 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
222 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
223 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
224 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
225 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
226 intent
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
227 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
230 final Bundle bundle
= new Bundle();
231 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
236 public String
getAuthTokenLabel(String authTokenType
) {
241 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
242 Account account
, String
[] features
) throws NetworkErrorException
{
243 final Bundle result
= new Bundle();
244 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
249 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
250 Account account
, String authTokenType
, Bundle options
)
251 throws NetworkErrorException
{
252 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
253 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
255 intent
.putExtra(KEY_ACCOUNT
, account
);
256 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
257 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
258 setIntentFlags(intent
);
260 final Bundle bundle
= new Bundle();
261 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
266 public Bundle
getAccountRemovalAllowed(
267 AccountAuthenticatorResponse response
, Account account
)
268 throws NetworkErrorException
{
269 return super.getAccountRemovalAllowed(response
, account
);
272 private void setIntentFlags(Intent intent
) {
273 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
274 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
275 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
278 private void validateAccountType(String type
)
279 throws UnsupportedAccountTypeException
{
280 if (!type
.equals(MainApp
.getAccountType())) {
281 throw new UnsupportedAccountTypeException();
285 private void validateAuthTokenType(String authTokenType
)
286 throws UnsupportedAuthTokenTypeException
{
287 if (!authTokenType
.equals(MainApp
.getAuthTokenType()) &&
288 !authTokenType
.equals(MainApp
.getAuthTokenTypePass()) &&
289 !authTokenType
.equals(MainApp
.getAuthTokenTypeAccessToken()) &&
290 !authTokenType
.equals(MainApp
.getAuthTokenTypeRefreshToken()) &&
291 !authTokenType
.equals(MainApp
.getAuthTokenTypeSamlSessionCookie())) {
292 throw new UnsupportedAuthTokenTypeException();
296 public static class AuthenticatorException
extends Exception
{
297 private static final long serialVersionUID
= 1L;
298 private Bundle mFailureBundle
;
300 public AuthenticatorException(int code
, String errorMsg
) {
301 mFailureBundle
= new Bundle();
302 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
304 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
307 public Bundle
getFailureBundle() {
308 return mFailureBundle
;
312 public static class UnsupportedAccountTypeException
extends
313 AuthenticatorException
{
314 private static final long serialVersionUID
= 1L;
316 public UnsupportedAccountTypeException() {
317 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
318 "Unsupported account type");
322 public static class UnsupportedAuthTokenTypeException
extends
323 AuthenticatorException
{
324 private static final long serialVersionUID
= 1L;
326 public UnsupportedAuthTokenTypeException() {
327 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
328 "Unsupported auth token type");
332 public static class UnsupportedFeaturesException
extends
333 AuthenticatorException
{
334 public static final long serialVersionUID
= 1L;
336 public UnsupportedFeaturesException() {
337 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
338 "Unsupported features");
342 public static class AccessDeniedException
extends AuthenticatorException
{
343 public AccessDeniedException(int code
, String errorMsg
) {
344 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
347 private static final long serialVersionUID
= 1L;