X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/6b765c7ad23e8b602c5e60f0b05bfba0288e0489..ac07e35d8ab68bf94d5cd8b45680ea69247fcc9f:/src/com/owncloud/android/authenticator/AccountAuthenticator.java diff --git a/src/com/owncloud/android/authenticator/AccountAuthenticator.java b/src/com/owncloud/android/authenticator/AccountAuthenticator.java index 58919ec2..c6ad9e7b 100644 --- a/src/com/owncloud/android/authenticator/AccountAuthenticator.java +++ b/src/com/owncloud/android/authenticator/AccountAuthenticator.java @@ -32,7 +32,11 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { * used by application and all extensions. */ public static final String ACCOUNT_TYPE = "owncloud"; + public static final String AUTHORITY = "org.owncloud"; public static final String AUTH_TOKEN_TYPE = "org.owncloud"; + public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password"; + public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token"; + public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token"; public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType"; public static final String KEY_REQUIRED_FEATURES = "requiredFeatures"; @@ -57,8 +61,13 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { * http://server/path or https://owncloud.server */ public static final String KEY_OC_BASE_URL = "oc_base_url"; - - private static final String TAG = "AccountAuthenticator"; + /** + * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens. + */ + public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2"; + + private static final String TAG = AccountAuthenticator.class.getSimpleName(); + private Context mContext; public AccountAuthenticator(Context context) { @@ -85,13 +94,13 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { return e.getFailureBundle(); } final Intent intent = new Intent(mContext, AuthenticatorActivity.class); - intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, - response); + 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); + final Bundle bundle = new Bundle(); bundle.putParcelable(AccountManager.KEY_INTENT, intent); return bundle; @@ -130,10 +139,14 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { return null; } + /** + * {@inheritDoc} + */ @Override public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { + /// validate parameters try { validateAccountType(account.type); validateAuthTokenType(authTokenType); @@ -143,22 +156,29 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { e.printStackTrace(); return e.getFailureBundle(); } + + /// check if required token is stored final AccountManager am = AccountManager.get(mContext); - final String password = am.getPassword(account); - if (password != null) { + String accessToken; + if (authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD)) { + accessToken = am.getPassword(account); + } else { + accessToken = am.peekAuthToken(account, authTokenType); + } + if (accessToken != 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); + result.putString(AccountManager.KEY_AUTHTOKEN, accessToken); return result; } - + + /// if not stored, return Intent to access the AuthenticatorActivity final Intent intent = new Intent(mContext, AuthenticatorActivity.class); - intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, - response); + 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); + intent.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account); final Bundle bundle = new Bundle(); bundle.putParcelable(AccountManager.KEY_INTENT, intent); @@ -205,7 +225,7 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { 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_NO_HISTORY); // incompatible with the authorization code grant in OAuth intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); intent.addFlags(Intent.FLAG_FROM_BACKGROUND); } @@ -219,7 +239,10 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator { private void validateAuthTokenType(String authTokenType) throws UnsupportedAuthTokenTypeException { - if (!authTokenType.equals(AUTH_TOKEN_TYPE)) { + if (!authTokenType.equals(AUTH_TOKEN_TYPE) && + !authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD) && + !authTokenType.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN) && + !authTokenType.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN) ) { throw new UnsupportedAuthTokenTypeException(); } }