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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.authenticator
;
22 import com
.owncloud
.android
.Log_OC
;
23 import com
.owncloud
.android
.ui
.activity
.AuthenticatorActivity
;
25 import android
.accounts
.*;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
28 import android
.os
.Bundle
;
29 import android
.util
.Log
;
31 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
33 * Is used by android system to assign accounts to authenticators. Should be
34 * used by application and all extensions.
36 public static final String ACCOUNT_TYPE
= "owncloud";
37 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
39 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
40 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
41 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
42 public static final String KEY_ACCOUNT
= "account";
44 * Value under this key should handle path to webdav php script. Will be
45 * removed and usage should be replaced by combining
46 * {@link com.owncloud.android.authenticator.AuthenticatorActivity.KEY_OC_BASE_URL} and
47 * {@link com.owncloud.android.utils.OwnCloudVersion}
51 public static final String KEY_OC_URL
= "oc_url";
53 * Version should be 3 numbers separated by dot so it can be parsed by
54 * {@link com.owncloud.android.utils.OwnCloudVersion}
56 public static final String KEY_OC_VERSION
= "oc_version";
58 * Base url should point to owncloud installation without trailing / ie:
59 * http://server/path or https://owncloud.server
61 public static final String KEY_OC_BASE_URL
= "oc_base_url";
63 private static final String TAG
= "AccountAuthenticator";
64 private Context mContext
;
66 public AccountAuthenticator(Context context
) {
75 public Bundle
addAccount(AccountAuthenticatorResponse response
,
76 String accountType
, String authTokenType
,
77 String
[] requiredFeatures
, Bundle options
)
78 throws NetworkErrorException
{
79 Log_OC
.i(TAG
, "Adding account with type " + accountType
80 + " and auth token " + authTokenType
);
82 validateAccountType(accountType
);
83 } catch (AuthenticatorException e
) {
84 Log_OC
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
87 return e
.getFailureBundle();
89 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
90 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
92 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
93 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
94 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
96 setIntentFlags(intent
);
97 final Bundle bundle
= new Bundle();
98 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
106 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
107 Account account
, Bundle options
) throws NetworkErrorException
{
109 validateAccountType(account
.type
);
110 } catch (AuthenticatorException e
) {
111 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
114 return e
.getFailureBundle();
116 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
117 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
119 intent
.putExtra(KEY_ACCOUNT
, account
);
120 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
122 setIntentFlags(intent
);
124 Bundle resultBundle
= new Bundle();
125 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
130 public Bundle
editProperties(AccountAuthenticatorResponse response
,
131 String accountType
) {
136 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
137 Account account
, String authTokenType
, Bundle options
)
138 throws NetworkErrorException
{
140 validateAccountType(account
.type
);
141 validateAuthTokenType(authTokenType
);
142 } catch (AuthenticatorException e
) {
143 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
146 return e
.getFailureBundle();
148 final AccountManager am
= AccountManager
.get(mContext
);
149 final String password
= am
.getPassword(account
);
150 if (password
!= null
) {
151 final Bundle result
= new Bundle();
152 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
153 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
154 result
.putString(AccountManager
.KEY_AUTHTOKEN
, password
);
158 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
159 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
161 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
162 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
163 intent
.putExtra(AuthenticatorActivity
.PARAM_USERNAME
, account
.name
);
165 final Bundle bundle
= new Bundle();
166 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
171 public String
getAuthTokenLabel(String authTokenType
) {
176 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
177 Account account
, String
[] features
) throws NetworkErrorException
{
178 final Bundle result
= new Bundle();
179 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
184 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
185 Account account
, String authTokenType
, Bundle options
)
186 throws NetworkErrorException
{
187 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
188 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
190 intent
.putExtra(KEY_ACCOUNT
, account
);
191 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
192 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
193 setIntentFlags(intent
);
195 final Bundle bundle
= new Bundle();
196 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
201 public Bundle
getAccountRemovalAllowed(
202 AccountAuthenticatorResponse response
, Account account
)
203 throws NetworkErrorException
{
204 return super.getAccountRemovalAllowed(response
, account
);
207 private void setIntentFlags(Intent intent
) {
208 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
209 intent
.addFlags(Intent
.FLAG_ACTIVITY_MULTIPLE_TASK
);
210 intent
.addFlags(Intent
.FLAG_ACTIVITY_NO_HISTORY
);
211 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
212 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
215 private void validateAccountType(String type
)
216 throws UnsupportedAccountTypeException
{
217 if (!type
.equals(ACCOUNT_TYPE
)) {
218 throw new UnsupportedAccountTypeException();
222 private void validateAuthTokenType(String authTokenType
)
223 throws UnsupportedAuthTokenTypeException
{
224 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
)) {
225 throw new UnsupportedAuthTokenTypeException();
229 public static class AuthenticatorException
extends Exception
{
230 private static final long serialVersionUID
= 1L;
231 private Bundle mFailureBundle
;
233 public AuthenticatorException(int code
, String errorMsg
) {
234 mFailureBundle
= new Bundle();
235 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
237 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
240 public Bundle
getFailureBundle() {
241 return mFailureBundle
;
245 public static class UnsupportedAccountTypeException
extends
246 AuthenticatorException
{
247 private static final long serialVersionUID
= 1L;
249 public UnsupportedAccountTypeException() {
250 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
251 "Unsupported account type");
255 public static class UnsupportedAuthTokenTypeException
extends
256 AuthenticatorException
{
257 private static final long serialVersionUID
= 1L;
259 public UnsupportedAuthTokenTypeException() {
260 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
261 "Unsupported auth token type");
265 public static class UnsupportedFeaturesException
extends
266 AuthenticatorException
{
267 public static final long serialVersionUID
= 1L;
269 public UnsupportedFeaturesException() {
270 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
271 "Unsupported features");
275 public static class AccessDeniedException
extends AuthenticatorException
{
276 public AccessDeniedException(int code
, String errorMsg
) {
277 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
280 private static final long serialVersionUID
= 1L;