2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2012 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.authentication
;
24 import com
.owncloud
.android
.MainApp
;
25 import com
.owncloud
.android
.R
;
27 import android
.accounts
.*;
28 import android
.content
.Context
;
29 import android
.content
.Intent
;
30 import android
.os
.Bundle
;
31 import android
.os
.Handler
;
32 import android
.widget
.Toast
;
34 import com
.owncloud
.android
.lib
.common
.accounts
.AccountTypeUtils
;
35 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
39 * Authenticator for ownCloud accounts.
41 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
43 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
44 * TODO - review completeness
46 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
49 * Is used by android system to assign accounts to authenticators. Should be
50 * used by application and all extensions.
52 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
53 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
54 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
55 public static final String KEY_ACCOUNT
= "account";
57 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
59 private Context mContext
;
61 private Handler mHandler
;
63 public AccountAuthenticator(Context context
) {
66 mHandler
= new Handler();
73 public Bundle
addAccount(AccountAuthenticatorResponse response
,
74 String accountType
, String authTokenType
,
75 String
[] requiredFeatures
, Bundle options
)
76 throws NetworkErrorException
{
77 Log_OC
.i(TAG
, "Adding account with type " + accountType
78 + " and auth token " + authTokenType
);
80 final Bundle bundle
= new Bundle();
82 AccountManager accountManager
= AccountManager
.get(mContext
);
83 Account
[] accounts
= accountManager
.getAccountsByType(MainApp
.getAccountType());
85 if (mContext
.getResources().getBoolean(R
.bool
.multiaccount_support
) || accounts
.length
< 1) {
87 validateAccountType(accountType
);
88 } catch (AuthenticatorException e
) {
89 Log_OC
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
92 return e
.getFailureBundle();
95 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
96 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
97 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
98 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
99 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
100 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
102 setIntentFlags(intent
);
104 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
109 bundle
.putInt(AccountManager
.KEY_ERROR_CODE
, AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
);
110 final String message
= String
.format(mContext
.getString(R
.string
.auth_unsupported_multiaccount
), mContext
.getString(R
.string
.app_name
));
111 bundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, message
);
113 mHandler
.post(new Runnable() {
117 Toast
.makeText(mContext
, message
, Toast
.LENGTH_SHORT
).show();
130 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
131 Account account
, Bundle options
) throws NetworkErrorException
{
133 validateAccountType(account
.type
);
134 } catch (AuthenticatorException e
) {
135 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
138 return e
.getFailureBundle();
140 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
141 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
143 intent
.putExtra(KEY_ACCOUNT
, account
);
144 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
146 setIntentFlags(intent
);
148 Bundle resultBundle
= new Bundle();
149 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
154 public Bundle
editProperties(AccountAuthenticatorResponse response
,
155 String accountType
) {
163 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
164 Account account
, String authTokenType
, Bundle options
)
165 throws NetworkErrorException
{
166 /// validate parameters
168 validateAccountType(account
.type
);
169 validateAuthTokenType(authTokenType
);
170 } catch (AuthenticatorException e
) {
171 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
174 return e
.getFailureBundle();
177 /// check if required token is stored
178 final AccountManager am
= AccountManager
.get(mContext
);
180 if (authTokenType
.equals(AccountTypeUtils
.getAuthTokenTypePass(MainApp
.getAccountType()))) {
181 accessToken
= am
.getPassword(account
);
183 accessToken
= am
.peekAuthToken(account
, authTokenType
);
185 if (accessToken
!= null
) {
186 final Bundle result
= new Bundle();
187 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
188 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, MainApp
.getAccountType());
189 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
193 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
194 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
195 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
196 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
197 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
198 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
199 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
);
202 final Bundle bundle
= new Bundle();
203 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
208 public String
getAuthTokenLabel(String authTokenType
) {
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
);
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
,
227 intent
.putExtra(KEY_ACCOUNT
, account
);
228 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
229 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
230 setIntentFlags(intent
);
232 final Bundle bundle
= new Bundle();
233 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
238 public Bundle
getAccountRemovalAllowed(
239 AccountAuthenticatorResponse response
, Account account
)
240 throws NetworkErrorException
{
241 return super.getAccountRemovalAllowed(response
, account
);
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
);
250 private void validateAccountType(String type
)
251 throws UnsupportedAccountTypeException
{
252 if (!type
.equals(MainApp
.getAccountType())) {
253 throw new UnsupportedAccountTypeException();
257 private void validateAuthTokenType(String authTokenType
)
258 throws UnsupportedAuthTokenTypeException
{
259 if (!authTokenType
.equals(MainApp
.getAuthTokenType()) &&
260 !authTokenType
.equals(AccountTypeUtils
.getAuthTokenTypePass(MainApp
.getAccountType())) &&
261 !authTokenType
.equals(AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType())) &&
262 !authTokenType
.equals(AccountTypeUtils
.getAuthTokenTypeRefreshToken(MainApp
.getAccountType())) &&
263 !authTokenType
.equals(AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()))) {
264 throw new UnsupportedAuthTokenTypeException();
268 public static class AuthenticatorException
extends Exception
{
269 private static final long serialVersionUID
= 1L;
270 private Bundle mFailureBundle
;
272 public AuthenticatorException(int code
, String errorMsg
) {
273 mFailureBundle
= new Bundle();
274 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
276 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
279 public Bundle
getFailureBundle() {
280 return mFailureBundle
;
284 public static class UnsupportedAccountTypeException
extends
285 AuthenticatorException
{
286 private static final long serialVersionUID
= 1L;
288 public UnsupportedAccountTypeException() {
289 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
290 "Unsupported account type");
294 public static class UnsupportedAuthTokenTypeException
extends
295 AuthenticatorException
{
296 private static final long serialVersionUID
= 1L;
298 public UnsupportedAuthTokenTypeException() {
299 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
300 "Unsupported auth token type");
304 public static class UnsupportedFeaturesException
extends
305 AuthenticatorException
{
306 public static final long serialVersionUID
= 1L;
308 public UnsupportedFeaturesException() {
309 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
310 "Unsupported features");
314 public static class AccessDeniedException
extends AuthenticatorException
{
315 public AccessDeniedException(int code
, String errorMsg
) {
316 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
319 private static final long serialVersionUID
= 1L;