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 3 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
.ui
.activity
.AuthenticatorActivity
;
24 import android
.accounts
.*;
25 import android
.content
.Context
;
26 import android
.content
.Intent
;
27 import android
.os
.Bundle
;
28 import android
.util
.Log
;
30 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
32 * Is used by android system to assign accounts to authenticators. Should be
33 * used by application and all extensions.
35 public static final String ACCOUNT_TYPE
= "owncloud";
36 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
38 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
39 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
40 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
41 public static final String KEY_ACCOUNT
= "account";
43 * Value under this key should handle path to webdav php script. Will be
44 * removed and usage should be replaced by combining
45 * {@link com.owncloud.android.authenticator.AuthenticatorActivity.KEY_OC_BASE_URL} and
46 * {@link com.owncloud.android.utils.OwnCloudVersion}
50 public static final String KEY_OC_URL
= "oc_url";
52 * Version should be 3 numbers separated by dot so it can be parsed by
53 * {@link com.owncloud.android.utils.OwnCloudVersion}
55 public static final String KEY_OC_VERSION
= "oc_version";
57 * Base url should point to owncloud installation without trailing / ie:
58 * http://server/path or https://owncloud.server
60 public static final String KEY_OC_BASE_URL
= "oc_base_url";
62 private static final String TAG
= "AccountAuthenticator";
63 private Context mContext
;
65 public AccountAuthenticator(Context context
) {
74 public Bundle
addAccount(AccountAuthenticatorResponse response
,
75 String accountType
, String authTokenType
,
76 String
[] requiredFeatures
, Bundle options
)
77 throws NetworkErrorException
{
78 Log
.i(TAG
, "Adding account with type " + accountType
79 + " and auth token " + authTokenType
);
81 validateAccountType(accountType
);
82 } catch (AuthenticatorException e
) {
83 Log
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
86 return e
.getFailureBundle();
88 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
89 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
91 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
92 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
93 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
95 setIntentFlags(intent
);
96 final Bundle bundle
= new Bundle();
97 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
105 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
106 Account account
, Bundle options
) throws NetworkErrorException
{
108 validateAccountType(account
.type
);
109 } catch (AuthenticatorException e
) {
110 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
113 return e
.getFailureBundle();
115 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
116 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
118 intent
.putExtra(KEY_ACCOUNT
, account
);
119 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
121 setIntentFlags(intent
);
123 Bundle resultBundle
= new Bundle();
124 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
129 public Bundle
editProperties(AccountAuthenticatorResponse response
,
130 String accountType
) {
135 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
136 Account account
, String authTokenType
, Bundle options
)
137 throws NetworkErrorException
{
139 validateAccountType(account
.type
);
140 validateAuthTokenType(authTokenType
);
141 } catch (AuthenticatorException e
) {
142 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
145 return e
.getFailureBundle();
147 final AccountManager am
= AccountManager
.get(mContext
);
148 final String password
= am
.getPassword(account
);
149 if (password
!= null
) {
150 final Bundle result
= new Bundle();
151 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
152 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
153 result
.putString(AccountManager
.KEY_AUTHTOKEN
, password
);
157 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
158 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
160 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
161 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
162 intent
.putExtra(AuthenticatorActivity
.PARAM_USERNAME
, account
.name
);
164 final Bundle bundle
= new Bundle();
165 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
170 public String
getAuthTokenLabel(String authTokenType
) {
175 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
176 Account account
, String
[] features
) throws NetworkErrorException
{
177 final Bundle result
= new Bundle();
178 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
183 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
184 Account account
, String authTokenType
, Bundle options
)
185 throws NetworkErrorException
{
186 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
187 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
189 intent
.putExtra(KEY_ACCOUNT
, account
);
190 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
191 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
192 setIntentFlags(intent
);
194 final Bundle bundle
= new Bundle();
195 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
200 public Bundle
getAccountRemovalAllowed(
201 AccountAuthenticatorResponse response
, Account account
)
202 throws NetworkErrorException
{
203 return super.getAccountRemovalAllowed(response
, account
);
206 private void setIntentFlags(Intent intent
) {
207 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
208 intent
.addFlags(Intent
.FLAG_ACTIVITY_MULTIPLE_TASK
);
209 intent
.addFlags(Intent
.FLAG_ACTIVITY_NO_HISTORY
);
210 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
211 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
214 private void validateAccountType(String type
)
215 throws UnsupportedAccountTypeException
{
216 if (!type
.equals(ACCOUNT_TYPE
)) {
217 throw new UnsupportedAccountTypeException();
221 private void validateAuthTokenType(String authTokenType
)
222 throws UnsupportedAuthTokenTypeException
{
223 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
)) {
224 throw new UnsupportedAuthTokenTypeException();
228 public static class AuthenticatorException
extends Exception
{
229 private static final long serialVersionUID
= 1L;
230 private Bundle mFailureBundle
;
232 public AuthenticatorException(int code
, String errorMsg
) {
233 mFailureBundle
= new Bundle();
234 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
236 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
239 public Bundle
getFailureBundle() {
240 return mFailureBundle
;
244 public static class UnsupportedAccountTypeException
extends
245 AuthenticatorException
{
246 private static final long serialVersionUID
= 1L;
248 public UnsupportedAccountTypeException() {
249 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
250 "Unsupported account type");
254 public static class UnsupportedAuthTokenTypeException
extends
255 AuthenticatorException
{
256 private static final long serialVersionUID
= 1L;
258 public UnsupportedAuthTokenTypeException() {
259 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
260 "Unsupported auth token type");
264 public static class UnsupportedFeaturesException
extends
265 AuthenticatorException
{
266 public static final long serialVersionUID
= 1L;
268 public UnsupportedFeaturesException() {
269 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
270 "Unsupported features");
274 public static class AccessDeniedException
extends AuthenticatorException
{
275 public AccessDeniedException(int code
, String errorMsg
) {
276 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
279 private static final long serialVersionUID
= 1L;