1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.authenticator
;
21 import com
.owncloud
.android
.ui
.activity
.AuthenticatorActivity
;
23 import android
.accounts
.*;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.os
.Bundle
;
27 import android
.util
.Log
;
29 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
31 * Is used by android system to assign accounts to authenticators. Should be
32 * used by application and all extensions.
34 public static final String ACCOUNT_TYPE
= "owncloud";
35 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
37 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
38 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
39 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
40 public static final String KEY_ACCOUNT
= "account";
42 * Value under this key should handle path to webdav php script. Will be
43 * removed and usage should be replaced by combining
44 * {@link com.owncloud.android.authenticator.AuthenticatorActivity.KEY_OC_BASE_URL} and
45 * {@link com.owncloud.android.utils.OwnCloudVersion}
49 public static final String KEY_OC_URL
= "oc_url";
51 * Version should be 3 numbers separated by dot so it can be parsed by
52 * {@link com.owncloud.android.utils.OwnCloudVersion}
54 public static final String KEY_OC_VERSION
= "oc_version";
56 * Base url should point to owncloud installation without trailing / ie:
57 * http://server/path or https://owncloud.server
59 public static final String KEY_OC_BASE_URL
= "oc_base_url";
61 private static final String TAG
= "AccountAuthenticator";
62 private Context mContext
;
64 public AccountAuthenticator(Context context
) {
73 public Bundle
addAccount(AccountAuthenticatorResponse response
,
74 String accountType
, String authTokenType
,
75 String
[] requiredFeatures
, Bundle options
)
76 throws NetworkErrorException
{
77 Log
.i(TAG
, "Adding account with type " + accountType
78 + " and auth token " + authTokenType
);
80 validateAccountType(accountType
);
81 } catch (AuthenticatorException e
) {
82 Log
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
85 return e
.getFailureBundle();
87 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
88 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
90 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
91 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
92 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
94 setIntentFlags(intent
);
95 final Bundle bundle
= new Bundle();
96 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
104 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
105 Account account
, Bundle options
) throws NetworkErrorException
{
107 validateAccountType(account
.type
);
108 } catch (AuthenticatorException e
) {
109 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
112 return e
.getFailureBundle();
114 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
115 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
117 intent
.putExtra(KEY_ACCOUNT
, account
);
118 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
120 setIntentFlags(intent
);
122 Bundle resultBundle
= new Bundle();
123 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
128 public Bundle
editProperties(AccountAuthenticatorResponse response
,
129 String accountType
) {
134 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
135 Account account
, String authTokenType
, Bundle options
)
136 throws NetworkErrorException
{
138 validateAccountType(account
.type
);
139 validateAuthTokenType(authTokenType
);
140 } catch (AuthenticatorException e
) {
141 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
144 return e
.getFailureBundle();
146 final AccountManager am
= AccountManager
.get(mContext
);
147 final String password
= am
.getPassword(account
);
148 if (password
!= null
) {
149 final Bundle result
= new Bundle();
150 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
151 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
152 result
.putString(AccountManager
.KEY_AUTHTOKEN
, password
);
156 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
157 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
159 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
160 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
161 intent
.putExtra(AuthenticatorActivity
.PARAM_USERNAME
, account
.name
);
163 final Bundle bundle
= new Bundle();
164 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
169 public String
getAuthTokenLabel(String authTokenType
) {
174 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
175 Account account
, String
[] features
) throws NetworkErrorException
{
176 final Bundle result
= new Bundle();
177 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, false
);
182 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
183 Account account
, String authTokenType
, Bundle options
)
184 throws NetworkErrorException
{
185 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
186 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
188 intent
.putExtra(KEY_ACCOUNT
, account
);
189 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
190 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
191 setIntentFlags(intent
);
193 final Bundle bundle
= new Bundle();
194 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
199 public Bundle
getAccountRemovalAllowed(
200 AccountAuthenticatorResponse response
, Account account
)
201 throws NetworkErrorException
{
202 return super.getAccountRemovalAllowed(response
, account
);
205 private void setIntentFlags(Intent intent
) {
206 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
207 intent
.addFlags(Intent
.FLAG_ACTIVITY_MULTIPLE_TASK
);
208 intent
.addFlags(Intent
.FLAG_ACTIVITY_NO_HISTORY
);
209 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
210 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
213 private void validateAccountType(String type
)
214 throws UnsupportedAccountTypeException
{
215 if (!type
.equals(ACCOUNT_TYPE
)) {
216 throw new UnsupportedAccountTypeException();
220 private void validateAuthTokenType(String authTokenType
)
221 throws UnsupportedAuthTokenTypeException
{
222 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
)) {
223 throw new UnsupportedAuthTokenTypeException();
227 public static class AuthenticatorException
extends Exception
{
228 private static final long serialVersionUID
= 1L;
229 private Bundle mFailureBundle
;
231 public AuthenticatorException(int code
, String errorMsg
) {
232 mFailureBundle
= new Bundle();
233 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
235 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
238 public Bundle
getFailureBundle() {
239 return mFailureBundle
;
243 public static class UnsupportedAccountTypeException
extends
244 AuthenticatorException
{
245 private static final long serialVersionUID
= 1L;
247 public UnsupportedAccountTypeException() {
248 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
249 "Unsupported account type");
253 public static class UnsupportedAuthTokenTypeException
extends
254 AuthenticatorException
{
255 private static final long serialVersionUID
= 1L;
257 public UnsupportedAuthTokenTypeException() {
258 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
259 "Unsupported auth token type");
263 public static class UnsupportedFeaturesException
extends
264 AuthenticatorException
{
265 public static final long serialVersionUID
= 1L;
267 public UnsupportedFeaturesException() {
268 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
269 "Unsupported features");
273 public static class AccessDeniedException
extends AuthenticatorException
{
274 public AccessDeniedException(int code
, String errorMsg
) {
275 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
278 private static final long serialVersionUID
= 1L;