Moved OAuth2GetAccessToken to be run in OperationsService
authorDavid A. Velasco <dvelasco@solidgear.es>
Tue, 1 Apr 2014 11:56:08 +0000 (13:56 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Tue, 1 Apr 2014 11:56:08 +0000 (13:56 +0200)
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/operations/OAuth2GetAccessToken.java
src/com/owncloud/android/services/OperationsService.java

index d9e1bea..84362ba 100644 (file)
@@ -19,6 +19,7 @@
 package com.owncloud.android.authentication;\r
 \r
 import java.security.cert.X509Certificate;\r
+import java.util.Map;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -147,7 +148,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon;\r
     private boolean mServerIsChecked, mServerIsValid, mIsSslConn;\r
     private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN;\r
+\r
     private int mGetServerInfoOpId = -1;\r
+    private int mOauth2GetAccessTokenOpId = -1;\r
 \r
     private int mAuthStatusText, mAuthStatusIcon;    \r
     private TextView mAuthStatusLayout;\r
@@ -602,7 +605,24 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         /// Showing the dialog with instructions for the user.\r
         showDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
 \r
-        /// GET ACCESS TOKEN to the oAuth server \r
+        /// GET ACCESS TOKEN to the oAuth server\r
+        Intent getServerInfoIntent = new Intent();\r
+        getServerInfoIntent.setAction(OperationsService.ACTION_OAUTH2_GET_ACCESS_TOKEN);\r
+        \r
+        getServerInfoIntent.putExtra(\r
+                OperationsService.EXTRA_SERVER_URL, \r
+                mOAuthTokenEndpointText.getText().toString().trim());\r
+        \r
+        getServerInfoIntent.putExtra(\r
+                OperationsService.EXTRA_OAUTH2_QUERY_PARAMETERS, \r
+                queryParameters);\r
+        \r
+        if (mOperationsServiceBinder != null) {\r
+            //Log.wtf(TAG, "getting access token..." );\r
+            mOauth2GetAccessTokenOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
+        }\r
+        \r
+        /*\r
         RemoteOperation operation = new OAuth2GetAccessToken(   getString(R.string.oauth2_client_id), \r
                 getString(R.string.oauth2_redirect_uri),       \r
                 getString(R.string.oauth2_grant_type),\r
@@ -610,6 +630,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         //OwnCloudClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth2_url_endpoint_access)), getApplicationContext());\r
         OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mOAuthTokenEndpointText.getText().toString().trim()), getApplicationContext(), true);\r
         operation.execute(client, this, mHandler);\r
+        */\r
+        \r
     }\r
 \r
 \r
@@ -672,12 +694,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mServerStatusIcon = R.drawable.progress_small;\r
             showServerStatus();\r
             \r
-            /*\r
-            mServerInfoOperation = new GetServerInfoOperation(uri, mAuthTokenType, this);\r
-            OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(uri), this, true);\r
-            mServerInfoOperation.execute(client, this, mHandler);\r
-            */\r
-            \r
             Intent getServerInfoIntent = new Intent();\r
             getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);\r
             getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri);\r
@@ -828,7 +844,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         mAuthStatusText = R.string.oauth_login_connection;\r
         showAuthStatus();\r
 \r
-\r
         // GET AUTHORIZATION request\r
         //Uri uri = Uri.parse(getString(R.string.oauth2_url_endpoint_auth));\r
         Uri uri = Uri.parse(mOAuthAuthEndpointText.getText().toString().trim());\r
@@ -881,7 +896,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 // multiple can be started if the user amends a URL quickly\r
 \r
         } else if (operation instanceof OAuth2GetAccessToken) {\r
-            onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
+            onGetOAuthAccessTokenFinish(result);\r
 \r
         } else if (operation instanceof ExistenceCheckRemoteOperation)  {\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
@@ -1238,10 +1253,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * Processes the result of the request for and access token send \r
      * to an OAuth authorization server.\r
      * \r
-     * @param operation     Operation performed requesting the access token.\r
      * @param result        Result of the operation.\r
      */\r
-    private void onGetOAuthAccessTokenFinish(OAuth2GetAccessToken operation, RemoteOperationResult result) {\r
+    private void onGetOAuthAccessTokenFinish(RemoteOperationResult result) {\r
+        mOauth2GetAccessTokenOpId = -1;\r
         try {\r
             dismissDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
@@ -1254,7 +1269,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             showDialog(DIALOG_LOGIN_PROGRESS);\r
 \r
             /// time to test the retrieved access token on the ownCloud server\r
-            mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
+            @SuppressWarnings("unchecked")\r
+            Map<String, String> tokens = (Map<String, String>)(result.getData().get(0));\r
+            mAuthToken = tokens.get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
+            //mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);\r
             mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false);\r
             OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
@@ -1862,6 +1880,15 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 //Log.wtf(TAG, "found result of operation finished while rotating");\r
                 onGetServerInfoFinish(result);\r
             }\r
+            \r
+        } else if (mOauth2GetAccessTokenOpId != -1) {\r
+            RemoteOperationResult result = \r
+                    mOperationsServiceBinder.getOperationResultIfFinished(\r
+                            mOauth2GetAccessTokenOpId);\r
+            if (result != null) {\r
+                //Log.wtf(TAG, "found result of operation finished while rotating");\r
+                onGetOAuthAccessTokenFinish(result);\r
+            }\r
         }\r
     }\r
     \r
index e2d72c5..5f6a085 100644 (file)
@@ -1,5 +1,6 @@
 package com.owncloud.android.operations;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -37,15 +38,12 @@ public class OAuth2GetAccessToken extends RemoteOperation {
         mOAuth2ParsedAuthorizationResponse = new HashMap<String, String>();
         mResultTokenMap = null;
     }
-    
-    
-    public Map<String, String> getOauth2AutorizationResponse() {
-        return mOAuth2ParsedAuthorizationResponse;
-    }
 
+    /*
     public Map<String, String> getResultTokenMap() {
         return mResultTokenMap;
     }
+    */
     
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
@@ -83,6 +81,9 @@ public class OAuth2GetAccessToken extends RemoteOperation {
                     
                     } else {
                         result = new RemoteOperationResult(true, status, postMethod.getResponseHeaders());
+                        ArrayList<Object> data = new ArrayList<Object>();
+                        data.add(mResultTokenMap);
+                        result.setData(data);
                     }
                     
                 } else {
index 518f2af..42b46e8 100644 (file)
@@ -23,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ConcurrentMap;
 
+import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.lib.common.OwnCloudClientFactory;
 import com.owncloud.android.lib.common.OwnCloudClient;
@@ -32,8 +33,8 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.operations.CreateShareOperation;
-import com.owncloud.android.operations.DetectAuthenticationMethodOperation;
 import com.owncloud.android.operations.GetServerInfoOperation;
+import com.owncloud.android.operations.OAuth2GetAccessToken;
 import com.owncloud.android.operations.UnshareLinkOperation;
 import com.owncloud.android.utils.Log_OC;
 
@@ -58,14 +59,15 @@ public class OperationsService extends Service {
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
     public static final String EXTRA_SERVER_URL = "SERVER_URL";
     public static final String EXTRA_AUTH_TOKEN_TYPE = "AUTH_TOKEN_TYPE";
+    public static final String EXTRA_OAUTH2_QUERY_PARAMETERS = "OAUTH2_QUERY_PARAMETERS";
     public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
     public static final String EXTRA_SEND_INTENT = "SEND_INTENT";
     public static final String EXTRA_RESULT = "RESULT";
     
     public static final String ACTION_CREATE_SHARE = "CREATE_SHARE";
     public static final String ACTION_UNSHARE = "UNSHARE";
-    //public static final String ACTION_DETECT_AUTHENTICATION_METHOD = "DETECT_AUTHENTICATION_METHOD";
     public static final String ACTION_GET_SERVER_INFO = "GET_SERVER_INFO";
+    public static final String ACTION_OAUTH2_GET_ACCESS_TOKEN = "OAUTH2_GET_ACCESS_TOKEN";
     
     public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
     public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
@@ -256,6 +258,16 @@ public class OperationsService extends Service {
                                 operationIntent.getStringExtra(EXTRA_AUTH_TOKEN_TYPE);
                         operation = new GetServerInfoOperation(
                                 serverUrl, authTokenType, OperationsService.this);
+                        
+                    } else if (action.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN)) {
+                        /// GET ACCESS TOKEN to the OAuth server
+                        String oauth2QueryParameters =
+                                operationIntent.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS);
+                        operation = new OAuth2GetAccessToken(
+                                getString(R.string.oauth2_client_id), 
+                                getString(R.string.oauth2_redirect_uri),       
+                                getString(R.string.oauth2_grant_type),
+                                oauth2QueryParameters);
                     }
                 }