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
.R
;
32 * Authenticator for ownCloud accounts.
34 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
36 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
37 * TODO - review completeness
39 * @author David A. Velasco
41 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
44 * Is used by android system to assign accounts to authenticators. Should be
45 * used by application and all extensions.
47 public static final String ACCOUNT_TYPE
= "owncloud";
48 public static final String AUTHORITY
= "org.owncloud";
49 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
50 public static final String AUTH_TOKEN_TYPE_PASSWORD
= "owncloud.password";
51 public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN
= "owncloud.oauth2.access_token";
52 public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN
= "owncloud.oauth2.refresh_token";
53 public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE
= "owncloud.saml.web_sso.session_cookie";
55 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
56 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
57 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
58 public static final String KEY_ACCOUNT
= "account";
61 * Value under this key should handle path to webdav php script. Will be
62 * removed and usage should be replaced by combining
63 * {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and
64 * {@link com.owncloud.android.utils.OwnCloudVersion}
68 public static final String KEY_OC_URL
= "oc_url";
70 * Version should be 3 numbers separated by dot so it can be parsed by
71 * {@link com.owncloud.android.utils.OwnCloudVersion}
73 public static final String KEY_OC_VERSION
= "oc_version";
75 * Base url should point to owncloud installation without trailing / ie:
76 * http://server/path or https://owncloud.server
78 public static final String KEY_OC_BASE_URL
= "oc_base_url";
80 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
82 public static final String KEY_SUPPORTS_OAUTH2
= "oc_supports_oauth2";
84 * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
86 public static final String KEY_SUPPORTS_SAML_WEB_SSO
= "oc_supports_saml_web_sso";
88 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
90 private Context mContext
;
92 private Handler mHandler
;
94 public AccountAuthenticator(Context context
) {
97 mHandler
= new Handler();
104 public Bundle
addAccount(AccountAuthenticatorResponse response
,
105 String accountType
, String authTokenType
,
106 String
[] requiredFeatures
, Bundle options
)
107 throws NetworkErrorException
{
108 Log_OC
.i(TAG
, "Adding account with type " + accountType
109 + " and auth token " + authTokenType
);
111 final Bundle bundle
= new Bundle();
113 AccountManager accountManager
= AccountManager
.get(mContext
);
114 Account
[] accounts
= accountManager
.getAccountsByType(ACCOUNT_TYPE
);
116 if (mContext
.getResources().getBoolean(R
.bool
.multiaccount_support
) || accounts
.length
< 1) {
118 validateAccountType(accountType
);
119 } catch (AuthenticatorException e
) {
120 Log_OC
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
123 return e
.getFailureBundle();
126 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
127 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
128 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
129 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
130 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
131 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
133 setIntentFlags(intent
);
135 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
140 bundle
.putInt(AccountManager
.KEY_ERROR_CODE
, AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
);
141 bundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, mContext
.getString(R
.string
.auth_unsupported_multiaccount
));
143 mHandler
.post(new Runnable() {
147 Toast
.makeText(mContext
, R
.string
.auth_unsupported_multiaccount
, Toast
.LENGTH_SHORT
).show();
160 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
161 Account account
, Bundle options
) throws NetworkErrorException
{
163 validateAccountType(account
.type
);
164 } catch (AuthenticatorException e
) {
165 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
168 return e
.getFailureBundle();
170 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
171 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
173 intent
.putExtra(KEY_ACCOUNT
, account
);
174 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
176 setIntentFlags(intent
);
178 Bundle resultBundle
= new Bundle();
179 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
184 public Bundle
editProperties(AccountAuthenticatorResponse response
,
185 String accountType
) {
193 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
194 Account account
, String authTokenType
, Bundle options
)
195 throws NetworkErrorException
{
196 /// validate parameters
198 validateAccountType(account
.type
);
199 validateAuthTokenType(authTokenType
);
200 } catch (AuthenticatorException e
) {
201 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
204 return e
.getFailureBundle();
207 /// check if required token is stored
208 final AccountManager am
= AccountManager
.get(mContext
);
210 if (authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
)) {
211 accessToken
= am
.getPassword(account
);
213 accessToken
= am
.peekAuthToken(account
, authTokenType
);
215 if (accessToken
!= null
) {
216 final Bundle result
= new Bundle();
217 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
218 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
219 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
223 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
224 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
225 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
226 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
227 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
228 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
229 intent
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
230 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
233 final Bundle bundle
= new Bundle();
234 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
239 public String
getAuthTokenLabel(String authTokenType
) {
244 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
245 Account account
, String
[] features
) throws NetworkErrorException
{
246 final Bundle result
= new Bundle();
247 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
252 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
253 Account account
, String authTokenType
, Bundle options
)
254 throws NetworkErrorException
{
255 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
256 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
258 intent
.putExtra(KEY_ACCOUNT
, account
);
259 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
260 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
261 setIntentFlags(intent
);
263 final Bundle bundle
= new Bundle();
264 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
269 public Bundle
getAccountRemovalAllowed(
270 AccountAuthenticatorResponse response
, Account account
)
271 throws NetworkErrorException
{
272 return super.getAccountRemovalAllowed(response
, account
);
275 private void setIntentFlags(Intent intent
) {
276 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
277 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
278 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
281 private void validateAccountType(String type
)
282 throws UnsupportedAccountTypeException
{
283 if (!type
.equals(ACCOUNT_TYPE
)) {
284 throw new UnsupportedAccountTypeException();
288 private void validateAuthTokenType(String authTokenType
)
289 throws UnsupportedAuthTokenTypeException
{
290 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
) &&
291 !authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
) &&
292 !authTokenType
.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN
) &&
293 !authTokenType
.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN
) &&
294 !authTokenType
.equals(AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE
)) {
295 throw new UnsupportedAuthTokenTypeException();
299 public static class AuthenticatorException
extends Exception
{
300 private static final long serialVersionUID
= 1L;
301 private Bundle mFailureBundle
;
303 public AuthenticatorException(int code
, String errorMsg
) {
304 mFailureBundle
= new Bundle();
305 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
307 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
310 public Bundle
getFailureBundle() {
311 return mFailureBundle
;
315 public static class UnsupportedAccountTypeException
extends
316 AuthenticatorException
{
317 private static final long serialVersionUID
= 1L;
319 public UnsupportedAccountTypeException() {
320 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
321 "Unsupported account type");
325 public static class UnsupportedAuthTokenTypeException
extends
326 AuthenticatorException
{
327 private static final long serialVersionUID
= 1L;
329 public UnsupportedAuthTokenTypeException() {
330 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
331 "Unsupported auth token type");
335 public static class UnsupportedFeaturesException
extends
336 AuthenticatorException
{
337 public static final long serialVersionUID
= 1L;
339 public UnsupportedFeaturesException() {
340 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
341 "Unsupported features");
345 public static class AccessDeniedException
extends AuthenticatorException
{
346 public AccessDeniedException(int code
, String errorMsg
) {
347 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
350 private static final long serialVersionUID
= 1L;