From: David A. Velasco Date: Tue, 1 Apr 2014 13:17:33 +0000 (+0200) Subject: Merge remote-tracking branch 'origin/operations_service' into operations_service X-Git-Tag: oc-android-1.7.0_signed~345^2~14 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/e566d041d199b3a91bfcca7cda72160d7d4ddf8e?ds=inline;hp=-c Merge remote-tracking branch 'origin/operations_service' into operations_service --- e566d041d199b3a91bfcca7cda72160d7d4ddf8e diff --combined owncloud-android-library index a3683116,cecda333..6b69b5af --- a/owncloud-android-library +++ b/owncloud-android-library @@@ -1,1 -1,1 +1,1 @@@ - Subproject commit a3683116ae04f639ff39484b1c54788bbce9fd30 -Subproject commit cecda3333ac511267d6a70c61b1475211484ec84 ++Subproject commit 6b69b5af1a79275a23339ad6382210d75b061f9f diff --combined src/com/owncloud/android/authentication/AuthenticatorActivity.java index 84362ba9,3c144046..fc1e3982 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@@ -19,7 -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; @@@ -46,6 -45,7 +46,6 @@@ import android.support.v4.app.FragmentT import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; -import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; @@@ -69,12 -69,13 +69,12 @@@ import com.owncloud.android.lib.common. import com.owncloud.android.lib.common.accounts.AccountUtils.Constants; import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.operations.DetectAuthenticationMethodOperation; import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod; +import com.owncloud.android.operations.GetServerInfoOperation; import com.owncloud.android.operations.OAuth2GetAccessToken; import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; -import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; @@@ -111,7 -112,6 +111,6 @@@ SsoWebViewClientListener, OnSslUntruste private static final String KEY_AUTH_MESSAGE_TEXT = "AUTH_MESSAGE_TEXT"; private static final String KEY_HOST_URL_TEXT = "HOST_URL_TEXT"; private static final String KEY_OC_VERSION = "OC_VERSION"; - private static final String KEY_OC_VERSION_STRING = "OC_VERSION_STRING"; private static final String KEY_ACCOUNT = "ACCOUNT"; private static final String KEY_SERVER_VALID = "SERVER_VALID"; private static final String KEY_SERVER_CHECKED = "SERVER_CHECKED"; @@@ -141,26 -141,24 +140,27 @@@ private static final String TAG_SAML_DIALOG = "samlWebViewDialog"; - private String mHostBaseUrl; - private OwnCloudVersion mDiscoveredVersion; + private String mHostBaseUrl; // TODO remove + private OwnCloudVersion mDiscoveredVersion; // TODO remove private String mAuthMessageText; private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon; private boolean mServerIsChecked, mServerIsValid, mIsSslConn; private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN; - private int mDetectAuthOpId = -1; + + private int mGetServerInfoOpId = -1; + private int mOauth2GetAccessTokenOpId = -1; private int mAuthStatusText, mAuthStatusIcon; private TextView mAuthStatusLayout; private final Handler mHandler = new Handler(); private Thread mOperationThread; - //private GetRemoteStatusOperation mOcServerChkOperation; - private GetRemoteStatusOperation mOcServerChkOperation; + private GetServerInfoOperation mServerInfoOperation; - private ExistenceCheckRemoteOperation mAuthCheckOperation; + + //private ExistenceCheckRemoteOperation mAuthCheckOperation; + private int mExistenceCheckOpId = -1; + private Uri mNewCapturedUriFromOAuth2Redirection; private AccountManager mAccountMgr; @@@ -198,8 -196,6 +198,8 @@@ private OperationsServiceBinder mOperationsServiceBinder = null; + private GetServerInfoOperation.ServerInfo mServerInfo; + /** * {@inheritDoc} * @@@ -281,10 -277,9 +281,9 @@@ /// retrieve extras from intent mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT); if (mAccount != null) { - String ocVersion = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION); - String ocVersionString = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION_STRING); + String ocVersion = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION); if (ocVersion != null) { - mDiscoveredVersion = new OwnCloudVersion(ocVersion, ocVersionString); + mDiscoveredVersion = new OwnCloudVersion(ocVersion); } mHostBaseUrl = normalizeUrl(mAccountMgr.getUserData(mAccount, Constants.KEY_OC_BASE_URL)); mHostUrlInput.setText(mHostBaseUrl); @@@ -317,9 -312,8 +316,8 @@@ /// server data String ocVersion = savedInstanceState.getString(KEY_OC_VERSION); - String ocVersionString = savedInstanceState.getString(KEY_OC_VERSION_STRING); if (ocVersion != null) { - mDiscoveredVersion = new OwnCloudVersion(ocVersion, ocVersionString); + mDiscoveredVersion = new OwnCloudVersion(ocVersion); } mHostBaseUrl = savedInstanceState.getString(KEY_HOST_URL_TEXT); @@@ -342,7 -336,7 +340,7 @@@ mServerAuthMethod = AuthenticationMethod.valueOf( savedInstanceState.getString(KEY_SERVER_AUTH_METHOD)); - mDetectAuthOpId = savedInstanceState.getInt(KEY_DETECT_AUTH_OP_ID); + mGetServerInfoOpId = savedInstanceState.getInt(KEY_DETECT_AUTH_OP_ID); } @@@ -365,7 -359,8 +363,7 @@@ mOAuth2Check.setVisibility(View.GONE); } - //if (mServerIsChecked && !mServerIsValid && mRefreshButtonEnabled) showRefreshButton(); - if (mServerIsChecked && !mServerIsValid && refreshButtonEnabled) showRefreshButton(); + showRefreshButton(mServerIsChecked && !mServerIsValid && refreshButtonEnabled); mOkButton.setEnabled(mServerIsValid); // state not automatically recovered in configuration changes if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType) || @@@ -491,7 -486,7 +489,7 @@@ outState.putInt(KEY_SERVER_STATUS_ICON, mServerStatusIcon); outState.putBoolean(KEY_SERVER_VALID, mServerIsValid); outState.putBoolean(KEY_SERVER_CHECKED, mServerIsChecked); - outState.putBoolean(KEY_SERVER_CHECK_IN_PROGRESS, (!mServerIsValid && mOcServerChkOperation != null)); + outState.putBoolean(KEY_SERVER_CHECK_IN_PROGRESS, (!mServerIsValid && mServerInfoOperation != null)); outState.putBoolean(KEY_IS_SSL_CONN, mIsSslConn); outState.putBoolean(KEY_PASSWORD_VISIBLE, isPasswordVisible()); outState.putInt(KEY_AUTH_STATUS_ICON, mAuthStatusIcon); @@@ -500,7 -495,6 +498,6 @@@ /// server data if (mDiscoveredVersion != null) { outState.putString(KEY_OC_VERSION, mDiscoveredVersion.getVersion()); - outState.putString(KEY_OC_VERSION_STRING, mDiscoveredVersion.getVersionString()); } outState.putString(KEY_HOST_URL_TEXT, mHostBaseUrl); @@@ -514,7 -508,7 +511,7 @@@ outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE)); outState.putString(KEY_SERVER_AUTH_METHOD, mServerAuthMethod.name()); - outState.putInt(KEY_DETECT_AUTH_OP_ID, mDetectAuthOpId); + outState.putInt(KEY_DETECT_AUTH_OP_ID, mGetServerInfoOpId); //Log.wtf(TAG, "onSaveInstanceState end" ); } @@@ -605,24 -599,7 +602,24 @@@ /// 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), @@@ -630,8 -607,6 +627,8 @@@ //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); + */ + } @@@ -645,7 -620,7 +642,7 @@@ onUrlInputFocusLost((TextView) view); } else { - hideRefreshButton(); + showRefreshButton(false); } } else if (view.getId() == R.id.account_password) { @@@ -670,39 -645,32 +667,39 @@@ checkOcServer(); } else { mOkButton.setEnabled(mServerIsValid); - if (!mServerIsValid) { - showRefreshButton(); - } + showRefreshButton(!mServerIsValid); } } private void checkOcServer() { - String uri = trimUrlWebdav(mHostUrlInput.getText().toString().trim()); + String uri = mHostUrlInput.getText().toString().trim(); if (!mHostUrlInputEnabled){ - uri = getString(R.string.server_url); + uri = getString(R.string.server_url).trim(); } mServerIsValid = false; mServerIsChecked = false; mOkButton.setEnabled(false); mDiscoveredVersion = null; - hideRefreshButton(); + mServerAuthMethod = AuthenticationMethod.UNKNOWN; + showRefreshButton(false); + if (uri.length() != 0) { mServerStatusText = R.string.auth_testing_connection; mServerStatusIcon = R.drawable.progress_small; showServerStatus(); - mOcServerChkOperation = new GetRemoteStatusOperation(uri, this); - OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(uri), this, true); - mOperationThread = mOcServerChkOperation.execute(client, this, mHandler); + + Intent getServerInfoIntent = new Intent(); + getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO); + getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri); + getServerInfoIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN_TYPE, mAuthTokenType); + if (mOperationsServiceBinder != null) { + //Log.wtf(TAG, "checking server..." ); + mGetServerInfoOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent); + } + } else { mServerStatusText = 0; mServerStatusIcon = 0; @@@ -827,12 -795,32 +824,32 @@@ showDialog(DIALOG_LOGIN_PROGRESS); /// test credentials accessing the root folder - mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false); - OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true); - client.setBasicCredentials(username, password); - mOperationThread = mAuthCheckOperation.execute(client, this, mHandler); + String remotePath =""; + boolean successIfAbsent = false; + boolean followRedirects = true; + startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, username, password, followRedirects); + } + private void startExistenceCheckRemoteOperation(String remotePath, Context context, boolean successIfAbsent, String webdav_path, + String username, String password, boolean followRedirects) { + + Intent existenceCheckIntent = new Intent(); + existenceCheckIntent.setAction(OperationsService.ACTION_EXISTENCE_CHECK); + existenceCheckIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mHostBaseUrl); + existenceCheckIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath); + existenceCheckIntent.putExtra(OperationsService.EXTRA_SUCCESS_IF_ABSENT, successIfAbsent); + existenceCheckIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path); + existenceCheckIntent.putExtra(OperationsService.EXTRA_USERNAME, username); + existenceCheckIntent.putExtra(OperationsService.EXTRA_PASSWORD, password); + existenceCheckIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN, mAuthToken); + existenceCheckIntent.putExtra(OperationsService.EXTRA_FOLLOW_REDIRECTS, followRedirects); + + if (mOperationsServiceBinder != null) { - Log.wtf(TAG, "starting existenceCheckRemoteOperation..." ); ++ Log_OC.wtf(TAG, "starting existenceCheckRemoteOperation..." ); + mExistenceCheckOpId = mOperationsServiceBinder.newOperation(existenceCheckIntent); + } + } /** * Starts the OAuth 'grant type' flow to get an access token, with @@@ -844,6 -832,7 +861,6 @@@ 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()); @@@ -875,9 -864,10 +892,10 @@@ String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType); /// test credentials accessing the root folder - mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false); - OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, false); - mOperationThread = mAuthCheckOperation.execute(client, this, mHandler); + String remotePath =""; + boolean successIfAbsent = false; + boolean followRedirections = false; + startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, "", "", followRedirections); } @@@ -889,29 -879,61 +907,30 @@@ @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { - if (operation instanceof GetRemoteStatusOperation) { - onOcServerCheckFinish((GetRemoteStatusOperation) operation, result); + if (operation instanceof GetServerInfoOperation) { + if (operation.hashCode() == mGetServerInfoOpId) { + onGetServerInfoFinish(result); + } // else nothing ; only the last check operation is considered; + // 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) { - Log.wtf(TAG, "received detection response through callback" ); ++ Log_OC.wtf(TAG, "received detection response through callback" ); if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) { - onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result); + onSamlBasedFederatedSingleSignOnAuthorizationStart(result); } else { - onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result); + onAuthorizationCheckFinish(result); } } else if (operation instanceof GetRemoteUserNameOperation) { onGetUserNameFinish((GetRemoteUserNameOperation) operation, result); - } else if (operation instanceof DetectAuthenticationMethodOperation) { - Log.wtf(TAG, "received detection response through callback" ); - onDetectAuthenticationFinish(result); } } - private void onDetectAuthenticationFinish(RemoteOperationResult result) { - // Read authentication method - mDetectAuthOpId = -1; - if (result.getData().size() > 0) { - AuthenticationMethod authMethod = (AuthenticationMethod) result.getData().get(0); - String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); - String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()); - String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()); - - if ( ( mAuthTokenType.equals(basic) && !authMethod.equals(AuthenticationMethod.BASIC_HTTP_AUTH) ) || - ( mAuthTokenType.equals(oAuth) && !authMethod.equals(AuthenticationMethod.BEARER_TOKEN) ) || - ( mAuthTokenType.equals(saml) && !authMethod.equals(AuthenticationMethod.SAML_WEB_SSO) ) ) { - - mOkButton.setEnabled(false); - mServerIsValid = false; - //show an alert message ( Server Status ) - updateServerStatusIconNoRegularAuth(); - showServerStatus(); - - } else { - mOkButton.setEnabled(true); - - // Show server status - showServerStatus(); - } - - } - } - - - private void onGetUserNameFinish(GetRemoteUserNameOperation operation, RemoteOperationResult result) { if (result.isSuccess()) { @@@ -945,7 -967,7 +964,8 @@@ } - private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) { + private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) { ++ mExistenceCheckOpId = -1; try { dismissDialog(DIALOG_LOGIN_PROGRESS); } catch (IllegalArgumentException e) { @@@ -980,68 -1002,71 +1000,68 @@@ * @param operation Server check performed. * @param result Result of the check. */ - private void onOcServerCheckFinish(GetRemoteStatusOperation operation, RemoteOperationResult result) { - if (operation.equals(mOcServerChkOperation)) { - /// save result state - mServerIsChecked = true; - mServerIsValid = result.isSuccess(); - mIsSslConn = (result.getCode() == ResultCode.OK_SSL); - mOcServerChkOperation = null; - - - /// retrieve discovered version and normalize server URL - mDiscoveredVersion = operation.getDiscoveredVersion(); - mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString()); + private void onGetServerInfoFinish(RemoteOperationResult result) { + /// update activity state + mServerIsChecked = true; + mIsSslConn = (result.getCode() == ResultCode.OK_SSL); + mServerInfoOperation = null; + mGetServerInfoOpId = -1; + + // update server status, but don't show it yet + updateServerStatusIconAndText(result); - // Refresh server status, but don't show it - updateServerStatusIconAndText(result); + if (result.isSuccess()) { + /// SUCCESS means: + // 1. connection succeeded, and we know if it's SSL or not + // 2. server is installed + // 3. we got the server version + // 4. we got the authentication method required by the server + mServerInfo = (GetServerInfoOperation.ServerInfo) (result.getData().get(0)); + mDiscoveredVersion = mServerInfo.mVersion; + mHostBaseUrl = mServerInfo.mBaseUrl; + mServerAuthMethod = mServerInfo.mAuthMethod; + + if (!authSupported(mServerAuthMethod)) { + + updateServerStatusIconNoRegularAuth(); // overrides updateServerStatusIconAndText() + mServerIsValid = false; - /// update status icon and text - if (mServerIsValid) { - hideRefreshButton(); - // Try to create an account with user and pass "", to know if it is a regular server - // Update connect button in the answer of this method - detectAuthorizationMethod(); } else { - showRefreshButton(); - // Show server status - showServerStatus(); + mServerIsValid = true; } + + } else { + mServerIsValid = false; + } - /// very special case (TODO: move to a common place for all the remote operations) - if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) { - showUntrustedCertDialog(result); - } - - - } // else nothing ; only the last check operation is considered; - // multiple can be triggered if the user amends a URL before a previous check can be triggered + // refresh UI + showRefreshButton(!mServerIsValid); + showServerStatus(); + mOkButton.setEnabled(mServerIsValid); + + /// very special case (TODO: move to a common place for all the remote operations) + if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) { + showUntrustedCertDialog(result); + } } - /** - * Try to access with user/pass ""/"", to know if it is a regular server - */ - private void detectAuthorizationMethod() { - - Log_OC.d(TAG, "Trying empty authorization to detect authentication method"); - - String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType); + private boolean authSupported(AuthenticationMethod authMethod) { + String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); + String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()); + String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()); - /// test credentials - //Intent detectAuthIntent = new Intent(this, OperationsService.class); - Intent detectAuthIntent = new Intent(); - detectAuthIntent.setAction(OperationsService.ACTION_DETECT_AUTHENTICATION_METHOD); - detectAuthIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mHostBaseUrl); - detectAuthIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path); - - //if (mOperationsBinder != null) { // let's let it crash to detect if is really possible - mServerAuthMethod = AuthenticationMethod.UNKNOWN; - if (mOperationsServiceBinder != null) { - //Log.wtf(TAG, "starting detection..." ); - mDetectAuthOpId = mOperationsServiceBinder.newOperation(detectAuthIntent); - } - //} + return (( mAuthTokenType.equals(basic) && + authMethod.equals(AuthenticationMethod.BASIC_HTTP_AUTH) ) || + ( mAuthTokenType.equals(oAuth) && + authMethod.equals(AuthenticationMethod.BEARER_TOKEN)) || + ( mAuthTokenType.equals(saml) && + authMethod.equals(AuthenticationMethod.SAML_WEB_SSO)) + ); } + // TODO remove, if possible private String normalizeUrl(String url) { if (url != null && url.length() > 0) { url = url.trim(); @@@ -1053,8 -1078,7 +1073,7 @@@ url = "http://" + url; } } - - // OC-208: Add suffix remote.php/webdav to normalize (OC-34) + url = trimUrlWebdav(url); if (url.endsWith("/")) { @@@ -1066,7 -1090,6 +1085,7 @@@ } + // TODO remove, if possible private String trimUrlWebdav(String url){ if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0)){ url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0.length()); @@@ -1253,10 -1276,10 +1272,10 @@@ * 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) { @@@ -1269,15 -1292,13 +1288,16 @@@ 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 tokens = (Map)(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); - client.setBearerCredentials(mAuthToken); - mAuthCheckOperation.execute(client, this, mHandler); + + String remotePath =""; + boolean successIfAbsent = false; + boolean followRedirects = true; + startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, "", "", followRedirects); } else { updateAuthStatusIconAndText(result); @@@ -1295,7 -1316,7 +1315,8 @@@ * @param operation Access check performed. * @param result Result of the operation. */ - private void onAuthorizationCheckFinish(ExistenceCheckRemoteOperation operation, RemoteOperationResult result) { + private void onAuthorizationCheckFinish(RemoteOperationResult result) { ++ mExistenceCheckOpId = -1; try { dismissDialog(DIALOG_LOGIN_PROGRESS); } catch (IllegalArgumentException e) { @@@ -1323,7 -1344,7 +1344,7 @@@ mServerIsChecked = true; mServerIsValid = false; mIsSslConn = false; - mOcServerChkOperation = null; + mServerInfoOperation = null; mDiscoveredVersion = null; mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString()); @@@ -1335,7 -1356,7 +1356,7 @@@ showAuthStatus(); // update input controls state - showRefreshButton(); + showRefreshButton(true); mOkButton.setEnabled(false); // very special case (TODO: move to a common place for all the remote operations) (dangerous here?) @@@ -1442,7 -1463,6 +1463,6 @@@ } /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION, mDiscoveredVersion.getVersion()); - mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION_STRING, mDiscoveredVersion.getVersionString()); mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL, mHostBaseUrl); if (isSaml) { @@@ -1590,12 -1610,12 +1610,12 @@@ } - private void showRefreshButton() { - mRefreshButton.setVisibility(View.VISIBLE); - } - - private void hideRefreshButton() { - mRefreshButton.setVisibility(View.GONE); + private void showRefreshButton (boolean show) { + if (show) { + mRefreshButton.setVisibility(View.VISIBLE); + } else { + mRefreshButton.setVisibility(View.GONE); + } } /** @@@ -1873,22 -1893,27 +1893,36 @@@ //Log.wtf(TAG, "registering to listen for operation callbacks" ); mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler); - if (mDetectAuthOpId != -1) { + if (mGetServerInfoOpId != -1) { RemoteOperationResult result = - mOperationsServiceBinder.getOperationResultIfFinished(mDetectAuthOpId); + mOperationsServiceBinder.getOperationResultIfFinished(mGetServerInfoOpId); if (result != null) { -- //Log.wtf(TAG, "found result of operation finished while rotating"); - onDetectAuthenticationFinish(result); ++ //Log_OC.wtf(TAG, "found result of operation finished while rotating"); + onGetServerInfoFinish(result); } - } - - if (mExistenceCheckOpId != -1) { + + } else if (mOauth2GetAccessTokenOpId != -1) { + RemoteOperationResult result = + mOperationsServiceBinder.getOperationResultIfFinished( + mOauth2GetAccessTokenOpId); + if (result != null) { - //Log.wtf(TAG, "found result of operation finished while rotating"); ++ //Log_OC.wtf(TAG, "found result of operation finished while rotating"); + onGetOAuthAccessTokenFinish(result); + } ++ ++ } else if (mExistenceCheckOpId != -1) { + RemoteOperationResult result = + mOperationsServiceBinder.getOperationResultIfFinished(mExistenceCheckOpId); + if (result != null) { - Log.wtf(TAG, "found result of operation finished while rotating"); - if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) { ++ //Log_OC.wtf(TAG, "found result of operation finished while rotating"); ++ if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie( ++ MainApp.getAccountType()).equals(mAuthTokenType)) { + onSamlBasedFederatedSingleSignOnAuthorizationStart(result); + + } else { + onAuthorizationCheckFinish(result); + } + } } } diff --combined src/com/owncloud/android/services/OperationsService.java index 42b46e80,d44a3af4..f0f37f5c --- a/src/com/owncloud/android/services/OperationsService.java +++ b/src/com/owncloud/android/services/OperationsService.java @@@ -23,18 -23,17 +23,19 @@@ import java.util.concurrent.ConcurrentH 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; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; + import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation; 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,16 -57,20 +59,25 @@@ public class OperationsService extends 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"; + ++ // TODO review if ALL OF THEM are necessary + public static final String EXTRA_WEBDAV_PATH = "WEBDAV_PATH"; + public static final String EXTRA_SUCCESS_IF_ABSENT = "SUCCESS_IF_ABSENT"; + public static final String EXTRA_USERNAME = "USERNAME"; + public static final String EXTRA_PASSWORD = "PASSWORD"; + public static final String EXTRA_AUTH_TOKEN = "AUTH_TOKEN"; + public static final String EXTRA_FOLLOW_REDIRECTS = "FOLLOW_REDIRECTS"; + 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_EXISTENCE_CHECK = "EXISTENCE_CHECK"; public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED"; public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED"; @@@ -81,9 -84,21 +91,21 @@@ private static class Target { public Uri mServerUrl = null; public Account mAccount = null; - public Target(Account account, Uri serverUrl) { + public String mWebDavUrl = ""; + public String mUsername = ""; + public String mPassword = ""; + public String mAuthToken = ""; + public boolean mFollowRedirects = true; + + public Target(Account account, Uri serverUrl, String webdavUrl, String username, String password, String authToken, + boolean followRedirects) { mAccount = account; mServerUrl = serverUrl; + mWebDavUrl = webdavUrl; + mUsername = username; + mPassword = password; + mAuthToken = authToken; + mFollowRedirects = followRedirects; } } @@@ -232,9 -247,20 +254,20 @@@ } else { Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT); String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL); + String webDavPath = operationIntent.getStringExtra(EXTRA_WEBDAV_PATH); + String webDavUrl = serverUrl + webDavPath; + String username = operationIntent.getStringExtra(EXTRA_USERNAME); + String password = operationIntent.getStringExtra(EXTRA_PASSWORD); + String authToken = operationIntent.getStringExtra(EXTRA_AUTH_TOKEN); + boolean followRedirects = operationIntent.getBooleanExtra(EXTRA_FOLLOW_REDIRECTS, true); target = new Target( account, - (serverUrl == null) ? null : Uri.parse(serverUrl) + (serverUrl == null) ? null : Uri.parse(serverUrl), + ((webDavPath == null) || (serverUrl == null)) ? "" : webDavUrl, + (username == null) ? "" : username, + (password == null) ? "" : password, + (authToken == null) ? "" : authToken, + followRedirects ); String action = operationIntent.getAction(); @@@ -245,6 -271,6 +278,7 @@@ operation = new CreateShareOperation(remotePath, ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent); } ++ } else if (action.equals(ACTION_UNSHARE)) { // Unshare file String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH); if (remotePath.length() > 0) { @@@ -252,22 -278,19 +286,30 @@@ remotePath, OperationsService.this); } - } else if (action.equals(ACTION_DETECT_AUTHENTICATION_METHOD)) { - // Detect Authentication Method - String webdav_url = - serverUrl + operationIntent.getStringExtra(EXTRA_WEBDAV_PATH); - operation = new DetectAuthenticationMethodOperation( - OperationsService.this, - webdav_url); ++ + } else if (action.equals(ACTION_GET_SERVER_INFO)) { + // check OC server and get basic information from it + String authTokenType = + 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); ++ + } else if (action.equals(ACTION_EXISTENCE_CHECK)) { + // Existence Check + String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH); + boolean successIfAbsent = operationIntent.getBooleanExtra(EXTRA_SUCCESS_IF_ABSENT, true); + operation = new ExistenceCheckRemoteOperation(remotePath, OperationsService.this, successIfAbsent); + } } @@@ -329,7 -352,7 +371,7 @@@ synchronized(mPendingOperations) { next = mPendingOperations.peek(); } - + if (next != null) { mCurrentOperation = next.second; @@@ -342,7 -365,16 +384,16 @@@ mOwnCloudClient = OwnCloudClientFactory.createOwnCloudClient(mLastTarget.mAccount, getApplicationContext()); mStorageManager = new FileDataStorageManager(mLastTarget.mAccount, getContentResolver()); } else { - mOwnCloudClient = OwnCloudClientFactory.createOwnCloudClient(mLastTarget.mServerUrl, getApplicationContext(), true); // this is not good enough + mOwnCloudClient = OwnCloudClientFactory.createOwnCloudClient(mLastTarget.mServerUrl, getApplicationContext(), + mLastTarget.mFollowRedirects); // this is not good enough + if (mLastTarget.mWebDavUrl != "") { + mOwnCloudClient.setWebdavUri(Uri.parse(mLastTarget.mWebDavUrl)); + } + if (mLastTarget.mUsername != "" && mLastTarget.mPassword != "") { + mOwnCloudClient.setBasicCredentials(mLastTarget.mUsername, mLastTarget.mPassword); + } else if (mLastTarget.mAuthToken != "") { + mOwnCloudClient.setBearerCredentials(mLastTarget.mAuthToken); + } mStorageManager = null; } }