Browse Source

Moved OAuth2GetAccessToken to be run in OperationsService

David A. Velasco 11 years ago
parent
commit
0addc90ae5

+ 39 - 12
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -19,6 +19,7 @@
 package com.owncloud.android.authentication;
 
 import java.security.cert.X509Certificate;
+import java.util.Map;
 
 import android.accounts.Account;
 import android.accounts.AccountManager;
@@ -147,7 +148,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon;
     private boolean mServerIsChecked, mServerIsValid, mIsSslConn;
     private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN;
+
     private int mGetServerInfoOpId = -1;
+    private int mOauth2GetAccessTokenOpId = -1;
 
     private int mAuthStatusText, mAuthStatusIcon;    
     private TextView mAuthStatusLayout;
@@ -602,7 +605,24 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         /// Showing the dialog with instructions for the user.
         showDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);
 
-        /// GET ACCESS TOKEN to the oAuth server 
+        /// GET ACCESS TOKEN to the oAuth server
+        Intent getServerInfoIntent = new Intent();
+        getServerInfoIntent.setAction(OperationsService.ACTION_OAUTH2_GET_ACCESS_TOKEN);
+        
+        getServerInfoIntent.putExtra(
+                OperationsService.EXTRA_SERVER_URL, 
+                mOAuthTokenEndpointText.getText().toString().trim());
+        
+        getServerInfoIntent.putExtra(
+                OperationsService.EXTRA_OAUTH2_QUERY_PARAMETERS, 
+                queryParameters);
+        
+        if (mOperationsServiceBinder != null) {
+            //Log.wtf(TAG, "getting access token..." );
+            mOauth2GetAccessTokenOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);
+        }
+        
+        /*
         RemoteOperation operation = new OAuth2GetAccessToken(   getString(R.string.oauth2_client_id), 
                 getString(R.string.oauth2_redirect_uri),       
                 getString(R.string.oauth2_grant_type),
@@ -610,6 +630,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         //OwnCloudClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth2_url_endpoint_access)), getApplicationContext());
         OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mOAuthTokenEndpointText.getText().toString().trim()), getApplicationContext(), true);
         operation.execute(client, this, mHandler);
+        */
+        
     }
 
 
@@ -672,12 +694,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mServerStatusIcon = R.drawable.progress_small;
             showServerStatus();
             
-            /*
-            mServerInfoOperation = new GetServerInfoOperation(uri, mAuthTokenType, this);
-            OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(uri), this, true);
-            mServerInfoOperation.execute(client, this, mHandler);
-            */
-            
             Intent getServerInfoIntent = new Intent();
             getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);
             getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri);
@@ -828,7 +844,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         mAuthStatusText = R.string.oauth_login_connection;
         showAuthStatus();
 
-
         // GET AUTHORIZATION request
         //Uri uri = Uri.parse(getString(R.string.oauth2_url_endpoint_auth));
         Uri uri = Uri.parse(mOAuthAuthEndpointText.getText().toString().trim());
@@ -881,7 +896,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 // multiple can be started if the user amends a URL quickly
 
         } else if (operation instanceof OAuth2GetAccessToken) {
-            onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);
+            onGetOAuthAccessTokenFinish(result);
 
         } else if (operation instanceof ExistenceCheckRemoteOperation)  {
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
@@ -1238,10 +1253,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * Processes the result of the request for and access token send 
      * to an OAuth authorization server.
      * 
-     * @param operation     Operation performed requesting the access token.
      * @param result        Result of the operation.
      */
-    private void onGetOAuthAccessTokenFinish(OAuth2GetAccessToken operation, RemoteOperationResult result) {
+    private void onGetOAuthAccessTokenFinish(RemoteOperationResult result) {
+        mOauth2GetAccessTokenOpId = -1;
         try {
             dismissDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);
         } catch (IllegalArgumentException e) {
@@ -1254,7 +1269,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             showDialog(DIALOG_LOGIN_PROGRESS);
 
             /// time to test the retrieved access token on the ownCloud server
-            mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);
+            @SuppressWarnings("unchecked")
+            Map<String, String> tokens = (Map<String, String>)(result.getData().get(0));
+            mAuthToken = tokens.get(OAuth2Constants.KEY_ACCESS_TOKEN);
+            //mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);
             mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false);
             OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);
@@ -1862,6 +1880,15 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 //Log.wtf(TAG, "found result of operation finished while rotating");
                 onGetServerInfoFinish(result);
             }
+            
+        } else if (mOauth2GetAccessTokenOpId != -1) {
+            RemoteOperationResult result = 
+                    mOperationsServiceBinder.getOperationResultIfFinished(
+                            mOauth2GetAccessTokenOpId);
+            if (result != null) {
+                //Log.wtf(TAG, "found result of operation finished while rotating");
+                onGetOAuthAccessTokenFinish(result);
+            }
         }
     }
     

+ 6 - 5
src/com/owncloud/android/operations/OAuth2GetAccessToken.java

@@ -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 {

+ 14 - 2
src/com/owncloud/android/services/OperationsService.java

@@ -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);
                     }
                 }