1 package eu
.alefzero
.owncloud
.authenticator
;
3 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
4 import android
.accounts
.AbstractAccountAuthenticator
;
5 import android
.accounts
.Account
;
6 import android
.accounts
.AccountAuthenticatorResponse
;
7 import android
.accounts
.AccountManager
;
8 import android
.accounts
.NetworkErrorException
;
9 import android
.content
.ContentResolver
;
10 import android
.content
.Context
;
11 import android
.content
.Intent
;
12 import android
.os
.Bundle
;
13 import android
.util
.Log
;
15 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
16 public static final String OPTIONS_USERNAME
= "username";
17 public static final String OPTIONS_PASSWORD
= "password";
18 public static final String OPTIONS_FILE_LIST_SYNC_ENABLED
= "filelist";
19 public static final String OPTIONS_PINNED_FILE_SYNC_ENABLED
= "pinned";
21 public static final String ACCOUNT_TYPE
= "owncloud";
22 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
24 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
25 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
26 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
27 public static final String KEY_ACCOUNT
= "account";
28 public static final String KEY_OC_URL
= "oc_url";
30 private Context mContext
;
32 public AccountAuthenticator(Context context
) {
41 public Bundle
addAccount(AccountAuthenticatorResponse response
,
42 String accountType
, String authTokenType
, String
[] requiredFeatures
,
43 Bundle options
) throws NetworkErrorException
{
44 Log
.i(getClass().getName(), "Adding account with type " + accountType
+
45 " and auth token " + authTokenType
);
47 validateAccountType(accountType
);
48 //validateAuthTokenType(authTokenType);
49 validateRequiredFeatures(requiredFeatures
);
50 } catch (AuthenticatorException e
) {
52 return e
.getFailureBundle();
54 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
55 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
56 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
57 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
58 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
60 setIntentFlags(intent
);
61 Log
.i(getClass().getName(), intent
.toString());
62 final Bundle bundle
= new Bundle();
63 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
71 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
72 Account account
, Bundle options
) throws NetworkErrorException
{
74 validateAccountType(account
.type
);
75 } catch (AuthenticatorException e
) {
76 return e
.getFailureBundle();
78 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
79 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
80 intent
.putExtra(KEY_ACCOUNT
, account
);
81 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
83 setIntentFlags(intent
);
85 Bundle resultBundle
= new Bundle();
86 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
91 public Bundle
editProperties(AccountAuthenticatorResponse response
,
93 throw new UnsupportedOperationException();
97 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
98 Account account
, String authTokenType
, Bundle options
)
99 throws NetworkErrorException
{
100 Log
.i(getClass().getName(), "Getting authToken");
102 validateAccountType(account
.type
);
103 validateAuthTokenType(authTokenType
);
104 } catch (AuthenticatorException e
) {
105 Log
.w(getClass().getName(), "Validating failded in getAuthToken");
106 return e
.getFailureBundle();
108 final AccountManager am
= AccountManager
.get(mContext
);
109 final String password
= am
.getPassword(account
);
110 if (password
!= null
) {
111 final Bundle result
= new Bundle();
112 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
113 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
114 result
.putString(AccountManager
.KEY_AUTHTOKEN
, password
);
118 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
119 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
120 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
121 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
122 intent
.putExtra(AuthenticatorActivity
.PARAM_USERNAME
, account
.name
);
124 final Bundle bundle
= new Bundle();
125 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
130 public String
getAuthTokenLabel(String authTokenType
) {
135 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
136 Account account
, String
[] features
) throws NetworkErrorException
{
137 final Bundle result
= new Bundle();
138 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, false
);
143 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
144 Account account
, String authTokenType
, Bundle options
)
145 throws NetworkErrorException
{
146 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
147 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
148 intent
.putExtra(KEY_ACCOUNT
, account
);
149 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
150 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
151 setIntentFlags(intent
);
153 final Bundle bundle
= new Bundle();
154 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
159 public Bundle
getAccountRemovalAllowed(AccountAuthenticatorResponse response
,
160 Account account
) throws NetworkErrorException
{
161 return super.getAccountRemovalAllowed(response
, account
);
164 private void setIntentFlags(Intent intent
) {
165 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
166 intent
.addFlags(Intent
.FLAG_ACTIVITY_MULTIPLE_TASK
);
167 intent
.addFlags(Intent
.FLAG_ACTIVITY_NO_HISTORY
);
168 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
169 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
172 private void validateAccountType(String type
) throws UnsupportedAccountTypeException
{
173 if (!type
.equals(ACCOUNT_TYPE
)) {
174 throw new UnsupportedAccountTypeException();
178 private void validateAuthTokenType(String authTokenType
) throws UnsupportedAuthTokenTypeException
{
179 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
)) {
180 throw new UnsupportedAuthTokenTypeException();
184 private void validateRequiredFeatures(String
[] requiredFeatures
) throws UnsupportedFeaturesException
{
188 private void validateCreaditials(String username
, String password
, String path
) throws AccessDeniedException
{
192 public static class AuthenticatorException
extends Exception
{
193 private static final long serialVersionUID
= 1L;
194 private Bundle mFailureBundle
;
195 public AuthenticatorException(int code
, String errorMsg
) {
196 mFailureBundle
= new Bundle();
197 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
198 mFailureBundle
.putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
201 public Bundle
getFailureBundle() {
202 return mFailureBundle
;
206 public static class UnsupportedAccountTypeException
extends AuthenticatorException
{
207 private static final long serialVersionUID
= 1L;
209 public UnsupportedAccountTypeException() {
210 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported account type");
214 public static class UnsupportedAuthTokenTypeException
extends AuthenticatorException
{
215 private static final long serialVersionUID
= 1L;
217 public UnsupportedAuthTokenTypeException() {
218 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported auth token type");
222 public static class UnsupportedFeaturesException
extends AuthenticatorException
{
223 public static final long serialVersionUID
= 1L;
225 public UnsupportedFeaturesException() {
226 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
, "Unsupported features");
230 public static class AccessDeniedException
extends AuthenticatorException
{
231 public AccessDeniedException(int code
, String errorMsg
) {
232 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
235 private static final long serialVersionUID
= 1L;