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 bundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, mContext
.getString(R
.string
.auth_unsupported_multiaccount
));
147 mHandler
.post(new Runnable() {
151 Toast
.makeText(mContext
, R
.string
.auth_unsupported_multiaccount
, Toast
.LENGTH_SHORT
).show();
164 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
165 Account account
, Bundle options
) throws NetworkErrorException
{
167 validateAccountType(account
.type
);
168 } catch (AuthenticatorException e
) {
169 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
172 return e
.getFailureBundle();
174 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
175 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
177 intent
.putExtra(KEY_ACCOUNT
, account
);
178 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
180 setIntentFlags(intent
);
182 Bundle resultBundle
= new Bundle();
183 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
188 public Bundle
editProperties(AccountAuthenticatorResponse response
,
189 String accountType
) {
197 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
198 Account account
, String authTokenType
, Bundle options
)
199 throws NetworkErrorException
{
200 /// validate parameters
202 validateAccountType(account
.type
);
203 validateAuthTokenType(authTokenType
);
204 } catch (AuthenticatorException e
) {
205 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
208 return e
.getFailureBundle();
211 /// check if required token is stored
212 final AccountManager am
= AccountManager
.get(mContext
);
214 if (authTokenType
.equals(MainApp
.getAuthTokenTypePass())) {
215 accessToken
= am
.getPassword(account
);
217 accessToken
= am
.peekAuthToken(account
, authTokenType
);
219 if (accessToken
!= null
) {
220 final Bundle result
= new Bundle();
221 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
222 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, MainApp
.getAccountType());
223 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
227 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
228 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
229 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
230 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
231 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
232 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
233 intent
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
234 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
237 final Bundle bundle
= new Bundle();
238 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
243 public String
getAuthTokenLabel(String authTokenType
) {
248 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
249 Account account
, String
[] features
) throws NetworkErrorException
{
250 final Bundle result
= new Bundle();
251 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
256 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
257 Account account
, String authTokenType
, Bundle options
)
258 throws NetworkErrorException
{
259 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
260 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
262 intent
.putExtra(KEY_ACCOUNT
, account
);
263 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
264 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
265 setIntentFlags(intent
);
267 final Bundle bundle
= new Bundle();
268 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
273 public Bundle
getAccountRemovalAllowed(
274 AccountAuthenticatorResponse response
, Account account
)
275 throws NetworkErrorException
{
276 return super.getAccountRemovalAllowed(response
, account
);
279 private void setIntentFlags(Intent intent
) {
280 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
281 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
282 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
285 private void validateAccountType(String type
)
286 throws UnsupportedAccountTypeException
{
287 if (!type
.equals(MainApp
.getAccountType())) {
288 throw new UnsupportedAccountTypeException();
292 private void validateAuthTokenType(String authTokenType
)
293 throws UnsupportedAuthTokenTypeException
{
294 if (!authTokenType
.equals(MainApp
.getAuthTokenType()) &&
295 !authTokenType
.equals(MainApp
.getAuthTokenTypePass()) &&
296 !authTokenType
.equals(MainApp
.getAuthTokenTypeAccessToken()) &&
297 !authTokenType
.equals(MainApp
.getAuthTokenTypeRefreshToken()) &&
298 !authTokenType
.equals(MainApp
.getAuthTokenTypeSamlSessionCookie())) {
299 throw new UnsupportedAuthTokenTypeException();
303 public static class AuthenticatorException
extends Exception
{
304 private static final long serialVersionUID
= 1L;
305 private Bundle mFailureBundle
;
307 public AuthenticatorException(int code
, String errorMsg
) {
308 mFailureBundle
= new Bundle();
309 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
311 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
314 public Bundle
getFailureBundle() {
315 return mFailureBundle
;
319 public static class UnsupportedAccountTypeException
extends
320 AuthenticatorException
{
321 private static final long serialVersionUID
= 1L;
323 public UnsupportedAccountTypeException() {
324 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
325 "Unsupported account type");
329 public static class UnsupportedAuthTokenTypeException
extends
330 AuthenticatorException
{
331 private static final long serialVersionUID
= 1L;
333 public UnsupportedAuthTokenTypeException() {
334 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
335 "Unsupported auth token type");
339 public static class UnsupportedFeaturesException
extends
340 AuthenticatorException
{
341 public static final long serialVersionUID
= 1L;
343 public UnsupportedFeaturesException() {
344 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
345 "Unsupported features");
349 public static class AccessDeniedException
extends AuthenticatorException
{
350 public AccessDeniedException(int code
, String errorMsg
) {
351 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
354 private static final long serialVersionUID
= 1L;