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