-package eu.alefzero.owncloud.authenticator;
-
-import android.accounts.*;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Bundle;
-import android.util.Log;
-
-public class AccountAuthenticator extends AbstractAccountAuthenticator {
- public static final String OPTIONS_USERNAME = "username";
- public static final String OPTIONS_PASSWORD = "password";
- public static final String OPTIONS_FILE_LIST_SYNC_ENABLED = "filelist";
- public static final String OPTIONS_PINNED_FILE_SYNC_ENABLED = "pinned";
-
- public static final String ACCOUNT_TYPE = "owncloud";
- public static final String AUTH_TOKEN_TYPE = "org.owncloud";
-
- public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
- public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
- public static final String KEY_LOGIN_OPTIONS = "loginOptions";
- public static final String KEY_ACCOUNT = "account";
- public static final String KEY_OC_URL = "oc_url";
- public static final String KEY_CONTACT_URL = "oc_contact_url";
-
- private Context mContext;
-
- public AccountAuthenticator(Context context) {
- super(context);
- mContext = context;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Bundle addAccount(AccountAuthenticatorResponse response,
- String accountType, String authTokenType, String[] requiredFeatures,
- Bundle options) throws NetworkErrorException {
- Log.i(getClass().getName(), "Adding account with type " + accountType +
- " and auth token " + authTokenType);
- try {
- validateAccountType(accountType);
- //validateAuthTokenType(authTokenType);
- validateRequiredFeatures(requiredFeatures);
- } catch (AuthenticatorException e) {
- e.printStackTrace();
- return e.getFailureBundle();
- }
- final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
- intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
- intent.putExtra(KEY_LOGIN_OPTIONS, options);
-
- setIntentFlags(intent);
- Log.i(getClass().getName(), intent.toString());
- final Bundle bundle = new Bundle();
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return bundle;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Bundle confirmCredentials(AccountAuthenticatorResponse response,
- Account account, Bundle options) throws NetworkErrorException {
- try {
- validateAccountType(account.type);
- } catch (AuthenticatorException e) {
- return e.getFailureBundle();
- }
- Intent intent = new Intent(mContext, AuthenticatorActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- intent.putExtra(KEY_ACCOUNT, account);
- intent.putExtra(KEY_LOGIN_OPTIONS, options);
-
- setIntentFlags(intent);
-
- Bundle resultBundle = new Bundle();
- resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return resultBundle;
- }
-
- @Override
- public Bundle editProperties(AccountAuthenticatorResponse response,
- String accountType) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Bundle getAuthToken(AccountAuthenticatorResponse response,
- Account account, String authTokenType, Bundle options)
- throws NetworkErrorException {
- Log.i(getClass().getName(), "Getting authToken");
- try {
- validateAccountType(account.type);
- validateAuthTokenType(authTokenType);
- } catch (AuthenticatorException e) {
- Log.w(getClass().getName(), "Validating failded in getAuthToken");
- return e.getFailureBundle();
- }
- final AccountManager am = AccountManager.get(mContext);
- final String password = am.getPassword(account);
- if (password != null) {
- final Bundle result = new Bundle();
- result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
- result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
- result.putString(AccountManager.KEY_AUTHTOKEN, password);
- return result;
- }
-
- final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
- intent.putExtra(KEY_LOGIN_OPTIONS, options);
- intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);
-
- final Bundle bundle = new Bundle();
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return bundle;
- }
-
- @Override
- public String getAuthTokenLabel(String authTokenType) {
- return null;
- }
-
- @Override
- public Bundle hasFeatures(AccountAuthenticatorResponse response,
- Account account, String[] features) throws NetworkErrorException {
- final Bundle result = new Bundle();
- result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
- return result;
- }
-
- @Override
- public Bundle updateCredentials(AccountAuthenticatorResponse response,
- Account account, String authTokenType, Bundle options)
- throws NetworkErrorException {
- final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- intent.putExtra(KEY_ACCOUNT, account);
- intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
- intent.putExtra(KEY_LOGIN_OPTIONS, options);
- setIntentFlags(intent);
-
- final Bundle bundle = new Bundle();
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return bundle;
- }
-
- @Override
- public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response,
- Account account) throws NetworkErrorException {
- return super.getAccountRemovalAllowed(response, account);
- }
-
- private void setIntentFlags(Intent intent) {
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
- intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
- intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
- intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
- }
-
- private void validateAccountType(String type) throws UnsupportedAccountTypeException {
- if (!type.equals(ACCOUNT_TYPE)) {
- throw new UnsupportedAccountTypeException();
- }
- }
-
- private void validateAuthTokenType(String authTokenType) throws UnsupportedAuthTokenTypeException {
- if (!authTokenType.equals(AUTH_TOKEN_TYPE)) {
- throw new UnsupportedAuthTokenTypeException();
- }
- }
-
- private void validateRequiredFeatures(String[] requiredFeatures) throws UnsupportedFeaturesException {
- // TODO
- }
-
- private void validateCreaditials(String username, String password, String path) throws AccessDeniedException {
-
- }
-
- public static class AuthenticatorException extends Exception {
- private static final long serialVersionUID = 1L;
- private Bundle mFailureBundle;
-
- public AuthenticatorException(int code, String errorMsg) {
- mFailureBundle = new Bundle();
- mFailureBundle.putInt(AccountManager.KEY_ERROR_CODE, code);
- mFailureBundle.putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);
- }
-
- public Bundle getFailureBundle() {
- return mFailureBundle;
- }
- }
-
- public static class UnsupportedAccountTypeException extends AuthenticatorException {
- private static final long serialVersionUID = 1L;
-
- public UnsupportedAccountTypeException() {
- super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported account type");
- }
- }
-
- public static class UnsupportedAuthTokenTypeException extends AuthenticatorException {
- private static final long serialVersionUID = 1L;
-
- public UnsupportedAuthTokenTypeException() {
- super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported auth token type");
- }
- }
-
- public static class UnsupportedFeaturesException extends AuthenticatorException {
- public static final long serialVersionUID = 1L;
-
- public UnsupportedFeaturesException() {
- super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported features");
- }
- }
-
- public static class AccessDeniedException extends AuthenticatorException {
- public AccessDeniedException(int code, String errorMsg) {
- super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");
- }
-
- private static final long serialVersionUID = 1L;
-
- }
-}
+package eu.alefzero.owncloud.authenticator;\r
+\r
+import eu.alefzero.owncloud.ui.activity.AuthenticatorActivity;\r
+import android.accounts.*;\r
+import android.content.Context;\r
+import android.content.Intent;\r
+import android.os.Bundle;\r
+import android.util.Log;\r
+\r
+public class AccountAuthenticator extends AbstractAccountAuthenticator {\r
+ public static final String OPTIONS_USERNAME = "username";\r
+ public static final String OPTIONS_PASSWORD = "password";\r
+ public static final String OPTIONS_FILE_LIST_SYNC_ENABLED = "filelist";\r
+ public static final String OPTIONS_PINNED_FILE_SYNC_ENABLED = "pinned";\r
+\r
+ public static final String ACCOUNT_TYPE = "owncloud";\r
+ public static final String AUTH_TOKEN_TYPE = "org.owncloud";\r
+\r
+ public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";\r
+ public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";\r
+ public static final String KEY_LOGIN_OPTIONS = "loginOptions";\r
+ public static final String KEY_ACCOUNT = "account";\r
+ public static final String KEY_OC_URL = "oc_url";\r
+ public static final String KEY_CONTACT_URL = "oc_contact_url";\r
+\r
+ private Context mContext;\r
+\r
+ public AccountAuthenticator(Context context) {\r
+ super(context);\r
+ mContext = context;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ */\r
+ @Override\r
+ public Bundle addAccount(AccountAuthenticatorResponse response,\r
+ String accountType, String authTokenType, String[] requiredFeatures,\r
+ Bundle options) throws NetworkErrorException {\r
+ Log.i(getClass().getName(), "Adding account with type " + accountType +\r
+ " and auth token " + authTokenType);\r
+ try {\r
+ validateAccountType(accountType);\r
+ //validateAuthTokenType(authTokenType);\r
+ validateRequiredFeatures(requiredFeatures);\r
+ } catch (AuthenticatorException e) {\r
+ e.printStackTrace();\r
+ return e.getFailureBundle();\r
+ }\r
+ final Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);\r
+ intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);\r
+ intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);\r
+ intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
+\r
+ setIntentFlags(intent);\r
+ Log.i(getClass().getName(), intent.toString());\r
+ final Bundle bundle = new Bundle();\r
+ bundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
+ return bundle;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ */\r
+ @Override\r
+ public Bundle confirmCredentials(AccountAuthenticatorResponse response,\r
+ Account account, Bundle options) throws NetworkErrorException {\r
+ try {\r
+ validateAccountType(account.type);\r
+ } catch (AuthenticatorException e) {\r
+ return e.getFailureBundle();\r
+ }\r
+ Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);\r
+ intent.putExtra(KEY_ACCOUNT, account);\r
+ intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
+\r
+ setIntentFlags(intent);\r
+\r
+ Bundle resultBundle = new Bundle();\r
+ resultBundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
+ return resultBundle;\r
+ }\r
+\r
+ @Override\r
+ public Bundle editProperties(AccountAuthenticatorResponse response,\r
+ String accountType) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public Bundle getAuthToken(AccountAuthenticatorResponse response,\r
+ Account account, String authTokenType, Bundle options)\r
+ throws NetworkErrorException {\r
+ Log.i(getClass().getName(), "Getting authToken");\r
+ try {\r
+ validateAccountType(account.type);\r
+ validateAuthTokenType(authTokenType);\r
+ } catch (AuthenticatorException e) {\r
+ Log.w(getClass().getName(), "Validating failded in getAuthToken");\r
+ return e.getFailureBundle();\r
+ }\r
+ final AccountManager am = AccountManager.get(mContext);\r
+ final String password = am.getPassword(account);\r
+ if (password != null) {\r
+ final Bundle result = new Bundle();\r
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);\r
+ result.putString(AccountManager.KEY_AUTHTOKEN, password);\r
+ return result;\r
+ }\r
+\r
+ final Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);\r
+ intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);\r
+ intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
+ intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);\r
+\r
+ final Bundle bundle = new Bundle();\r
+ bundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
+ return bundle;\r
+ }\r
+\r
+ @Override\r
+ public String getAuthTokenLabel(String authTokenType) {\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public Bundle hasFeatures(AccountAuthenticatorResponse response,\r
+ Account account, String[] features) throws NetworkErrorException {\r
+ final Bundle result = new Bundle();\r
+ result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public Bundle updateCredentials(AccountAuthenticatorResponse response,\r
+ Account account, String authTokenType, Bundle options)\r
+ throws NetworkErrorException {\r
+ final Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
+ intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);\r
+ intent.putExtra(KEY_ACCOUNT, account);\r
+ intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);\r
+ intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
+ setIntentFlags(intent);\r
+\r
+ final Bundle bundle = new Bundle();\r
+ bundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
+ return bundle;\r
+ }\r
+\r
+ @Override\r
+ public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response,\r
+ Account account) throws NetworkErrorException {\r
+ return super.getAccountRemovalAllowed(response, account);\r
+ }\r
+\r
+ private void setIntentFlags(Intent intent) {\r
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\r
+ intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);\r
+ intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);\r
+ intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);\r
+ intent.addFlags(Intent.FLAG_FROM_BACKGROUND);\r
+ }\r
+\r
+ private void validateAccountType(String type) throws UnsupportedAccountTypeException {\r
+ if (!type.equals(ACCOUNT_TYPE)) {\r
+ throw new UnsupportedAccountTypeException();\r
+ }\r
+ }\r
+\r
+ private void validateAuthTokenType(String authTokenType) throws UnsupportedAuthTokenTypeException {\r
+ if (!authTokenType.equals(AUTH_TOKEN_TYPE)) {\r
+ throw new UnsupportedAuthTokenTypeException();\r
+ }\r
+ }\r
+\r
+ private void validateRequiredFeatures(String[] requiredFeatures) throws UnsupportedFeaturesException {\r
+ }\r
+\r
+ private void validateCreaditials(String username, String password, String path) throws AccessDeniedException {\r
+\r
+ }\r
+\r
+ public static class AuthenticatorException extends Exception {\r
+ private static final long serialVersionUID = 1L;\r
+ private Bundle mFailureBundle;\r
+\r
+ public AuthenticatorException(int code, String errorMsg) {\r
+ mFailureBundle = new Bundle();\r
+ mFailureBundle.putInt(AccountManager.KEY_ERROR_CODE, code);\r
+ mFailureBundle.putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);\r
+ }\r
+\r
+ public Bundle getFailureBundle() {\r
+ return mFailureBundle;\r
+ }\r
+ }\r
+\r
+ public static class UnsupportedAccountTypeException extends AuthenticatorException {\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ public UnsupportedAccountTypeException() {\r
+ super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported account type");\r
+ }\r
+ }\r
+\r
+ public static class UnsupportedAuthTokenTypeException extends AuthenticatorException {\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ public UnsupportedAuthTokenTypeException() {\r
+ super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported auth token type");\r
+ }\r
+ }\r
+\r
+ public static class UnsupportedFeaturesException extends AuthenticatorException {\r
+ public static final long serialVersionUID = 1L;\r
+\r
+ public UnsupportedFeaturesException() {\r
+ super(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION, "Unsupported features");\r
+ }\r
+ }\r
+\r
+ public static class AccessDeniedException extends AuthenticatorException {\r
+ public AccessDeniedException(int code, String errorMsg) {\r
+ super(AccountManager.ERROR_CODE_INVALID_RESPONSE, "Access Denied");\r
+ }\r
+\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ }\r
+}\r