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?hp=c2c10bcff893790e7af07c51afeb43315ff2ed84 Merge remote-tracking branch 'origin/operations_service' into operations_service --- diff --git a/owncloud-android-library b/owncloud-android-library index cecda333..6b69b5af 160000 --- a/owncloud-android-library +++ b/owncloud-android-library @@ -1 +1 @@ -Subproject commit cecda3333ac511267d6a70c61b1475211484ec84 +Subproject commit 6b69b5af1a79275a23339ad6382210d75b061f9f diff --git a/src/com/owncloud/android/authentication/AuthenticatorActivity.java b/src/com/owncloud/android/authentication/AuthenticatorActivity.java index 3c144046..fc1e3982 100644 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/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; @@ -45,7 +46,6 @@ import android.support.v4.app.FragmentTransaction; 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,13 +69,12 @@ import com.owncloud.android.lib.common.accounts.AccountTypeUtils; 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; @@ -141,21 +140,24 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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 GetServerInfoOperation mServerInfoOperation; + //private ExistenceCheckRemoteOperation mAuthCheckOperation; private int mExistenceCheckOpId = -1; @@ -196,6 +198,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { private OperationsServiceBinder mOperationsServiceBinder = null; + private GetServerInfoOperation.ServerInfo mServerInfo; + /** * {@inheritDoc} * @@ -336,7 +340,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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); } @@ -359,8 +363,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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) || @@ -486,7 +489,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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); @@ -508,7 +511,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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" ); } @@ -599,7 +602,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), @@ -607,6 +627,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); + */ + } @@ -620,7 +642,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { onUrlInputFocusLost((TextView) view); } else { - hideRefreshButton(); + showRefreshButton(false); } } else if (view.getId() == R.id.account_password) { @@ -645,32 +667,39 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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; @@ -817,7 +846,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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); } } @@ -832,7 +861,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()); @@ -879,14 +907,17 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { @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(result); @@ -896,44 +927,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { } 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()) { @@ -968,6 +965,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { } private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) { + mExistenceCheckOpId = -1; try { dismissDialog(DIALOG_LOGIN_PROGRESS); } catch (IllegalArgumentException e) { @@ -1002,71 +1000,68 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { * @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(); @@ -1090,6 +1085,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { } + // 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()); @@ -1276,10 +1272,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) { @@ -1292,7 +1288,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 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); String remotePath =""; @@ -1317,6 +1316,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { * @param result Result of the operation. */ private void onAuthorizationCheckFinish(RemoteOperationResult result) { + mExistenceCheckOpId = -1; try { dismissDialog(DIALOG_LOGIN_PROGRESS); } catch (IllegalArgumentException e) { @@ -1344,7 +1344,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { mServerIsChecked = true; mServerIsValid = false; mIsSslConn = false; - mOcServerChkOperation = null; + mServerInfoOperation = null; mDiscoveredVersion = null; mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString()); @@ -1356,7 +1356,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { 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?) @@ -1610,12 +1610,12 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { } - 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); + } } /** @@ -1893,21 +1893,30 @@ SsoWebViewClientListener, OnSslUntrustedCertListener { //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_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 { diff --git a/src/com/owncloud/android/operations/DetectAuthenticationMethodOperation.java b/src/com/owncloud/android/operations/DetectAuthenticationMethodOperation.java index 6bc87503..b817e724 100644 --- a/src/com/owncloud/android/operations/DetectAuthenticationMethodOperation.java +++ b/src/com/owncloud/android/operations/DetectAuthenticationMethodOperation.java @@ -126,6 +126,9 @@ public class DetectAuthenticationMethodOperation extends RemoteOperation { // else - fall back to UNKNOWN Log.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod)); + if (!authMethod.equals(AuthenticationMethod.UNKNOWN)) { + result = new RemoteOperationResult(true, result.getHttpCode(), null); + } ArrayList data = new ArrayList(); data.add(authMethod); result.setData(data); diff --git a/src/com/owncloud/android/operations/GetServerInfoOperation.java b/src/com/owncloud/android/operations/GetServerInfoOperation.java new file mode 100644 index 00000000..c210153e --- /dev/null +++ b/src/com/owncloud/android/operations/GetServerInfoOperation.java @@ -0,0 +1,163 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2014 ownCloud Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.operations; + +import java.util.ArrayList; + +import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.lib.common.OwnCloudClient; +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; +import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation; +import com.owncloud.android.lib.resources.status.OwnCloudVersion; +import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod; +import com.owncloud.android.utils.Log_OC; + +import android.content.Context; + +/** + * Get basic information from an ownCloud server given its URL. + * + * Checks the existence of a configured ownCloud server in the URL, gets its version + * and finds out what authentication method is needed to access files in it. + * + * @author David A. Velasco + * @author masensio + */ + +public class GetServerInfoOperation extends RemoteOperation { + + private static final String TAG = GetServerInfoOperation.class.getSimpleName(); + + private String mUrl; + private String mAuthTokenType; + private Context mContext; + + private ServerInfo mResultData; + + /** + * Constructor. + * + * @param url URL to an ownCloud server. + * @param authTokenType Identifies the authorization token supported by the caller; + * TODO ugly dependency, get rid of it. + * @param context Android context; needed to check network state + * TODO ugly dependency, get rid of it. + */ + public GetServerInfoOperation(String url, String authTokenType, Context context) { + mUrl = trimWebdavSuffix(url); + mAuthTokenType = authTokenType; + mContext = context; + + mResultData = new ServerInfo(); + } + + + /** + * Performs the operation + * + * @return Result of the operation. If successful, includes an instance of + * {@link ServerInfo} with the information retrieved from the server. + * Call {@link RemoteOperationResult#getData()}.get(0) to get it. + */ + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + + // first: check the status of the server (including its version) + GetRemoteStatusOperation getStatus = new GetRemoteStatusOperation(mUrl, mContext); + RemoteOperationResult result = getStatus.execute(client); + + if (result.isSuccess()) { + // second: get authentication method required by the server + mResultData.mVersion = (OwnCloudVersion)(result.getData().get(0)); + boolean isSslConn = (result.getCode() == ResultCode.OK_SSL); + mResultData.mBaseUrl = normalizeProtocolPrefix(mUrl, isSslConn); + RemoteOperationResult detectAuthResult = detectAuthorizationMethod(client); + + // third: merge results + if (detectAuthResult.isSuccess()) { + mResultData.mAuthMethod = + (AuthenticationMethod)detectAuthResult.getData().get(0); + ArrayList data = new ArrayList(); + data.add(mResultData); + result.setData(data); + } else { + result = detectAuthResult; + } + } + return result; + } + + + private RemoteOperationResult detectAuthorizationMethod(OwnCloudClient client) { + Log_OC.d(TAG, "Trying empty authorization to detect authentication method"); + String webdav_path = AccountUtils.getWebdavPath(mResultData.mVersion, mAuthTokenType); + String webdav_url = mResultData.mBaseUrl + webdav_path; + DetectAuthenticationMethodOperation operation = + new DetectAuthenticationMethodOperation(mContext, webdav_url); + return operation.execute(client); + } + + + private String trimWebdavSuffix(String url) { + if (url == null) { + url = ""; + } else { + if (url.endsWith("/")) { + url = url.substring(0, url.length() - 1); + } + if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0)){ + url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0.length()); + } else if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_2_0)){ + url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_2_0.length()); + } else if (url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_1_2)){ + url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_1_2.length()); + } + } + return url; + } + + + private String normalizeProtocolPrefix(String url, boolean isSslConn) { + if (!url.toLowerCase().startsWith("http://") && + !url.toLowerCase().startsWith("https://")) { + if (isSslConn) { + return "https://" + url; + } else { + return "http://" + url; + } + } + return url; + } + + + public static class ServerInfo { + public OwnCloudVersion mVersion; + public String mBaseUrl; + public AuthenticationMethod mAuthMethod; + } + +} diff --git a/src/com/owncloud/android/operations/OAuth2GetAccessToken.java b/src/com/owncloud/android/operations/OAuth2GetAccessToken.java index e2d72c5c..5f6a085b 100644 --- a/src/com/owncloud/android/operations/OAuth2GetAccessToken.java +++ b/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(); mResultTokenMap = null; } - - - public Map getOauth2AutorizationResponse() { - return mOAuth2ParsedAuthorizationResponse; - } + /* public Map 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 data = new ArrayList(); + data.add(mResultTokenMap); + result.setData(data); } } else { diff --git a/src/com/owncloud/android/services/OperationsService.java b/src/com/owncloud/android/services/OperationsService.java index d44a3af4..f0f37f5c 100644 --- a/src/com/owncloud/android/services/OperationsService.java +++ b/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; @@ -33,7 +34,8 @@ 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; @@ -57,9 +59,13 @@ 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"; + + // 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"; @@ -69,7 +75,8 @@ public class OperationsService extends Service { 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"; @@ -271,6 +278,7 @@ public class OperationsService extends Service { 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) { @@ -278,13 +286,24 @@ public class OperationsService extends Service { 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); diff --git a/src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java b/src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java index cbf1acb0..0c17a6f2 100644 --- a/src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java +++ b/src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java @@ -72,7 +72,7 @@ public class SamlWebViewDialog extends SherlockDialogFragment { * * @param handler * @param Url Url to open at WebView - * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType) + * @param targetURL mBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType) * @return New dialog instance, ready to show. */ public static SamlWebViewDialog newInstance(String url, String targetUrl) {