X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/fba65e37f68f5cd72fb20c96982607702193a509..288c148b51b951017a3a9b333797a7e8eeede38a:/src/com/owncloud/android/authentication/AuthenticatorActivity.java diff --git a/src/com/owncloud/android/authentication/AuthenticatorActivity.java b/src/com/owncloud/android/authentication/AuthenticatorActivity.java index e2c3c033..713851ba 100644 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -1,6 +1,11 @@ -/* ownCloud Android client application +/** + * ownCloud Android client application + * + * @author Bartek Przybylski + * @author David A. Velasco + * @author masensio * Copyright (C) 2012 Bartek Przybylski - * Copyright (C) 2012-2015 ownCloud Inc. + * Copyright (C) 2015 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -33,7 +38,6 @@ import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.net.Uri; import android.net.http.SslError; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -65,8 +69,8 @@ import com.actionbarsherlock.app.SherlockDialogFragment; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener; -import com.owncloud.android.lib.common.OwnCloudAccount; -import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; +import com.owncloud.android.lib.common.OwnCloudCredentials; +import com.owncloud.android.lib.common.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.accounts.AccountTypeUtils; import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; import com.owncloud.android.lib.common.accounts.AccountUtils.Constants; @@ -76,7 +80,6 @@ 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.common.utils.Log_OC; -import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation; import com.owncloud.android.lib.resources.status.OwnCloudVersion; import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation; import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod; @@ -93,10 +96,6 @@ import com.owncloud.android.utils.DisplayUtils; /** * This Activity is used to add an ownCloud account to the App - * - * @author Bartek Przybylski - * @author David A. Velasco - * @author masensio */ public class AuthenticatorActivity extends AccountAuthenticatorActivity implements OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener, @@ -190,7 +189,12 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity /// Identifier of operation in progress which result shouldn't be lost private long mWaitingForOpId = Long.MAX_VALUE; - + private final String BASIC_TOKEN_TYPE = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); + private final String OAUTH_TOKEN_TYPE = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()); + private final String SAML_TOKEN_TYPE = + AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()); + + /** * {@inheritDoc} * @@ -230,14 +234,12 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mWaitingForOpId = savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID); mIsFirstAuthAttempt = savedInstanceState.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG); } - - /// load user interface setContentView(R.layout.account_setup); /// initialize general UI elements - initOverallUi(savedInstanceState); + initOverallUi(); mOkButton = findViewById(R.id.buttonOK); @@ -275,21 +277,19 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private String chooseAuthTokenType(boolean oauth, boolean saml) { if (saml) { - return AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()); + return SAML_TOKEN_TYPE; } else if (oauth) { - return AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()); + return OAUTH_TOKEN_TYPE; } else { - return AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); + return BASIC_TOKEN_TYPE; } } /** * Configures elements in the user interface under direct control of the Activity. - * - * @param savedInstanceState Saved activity state, as in {{@link #onCreate(Bundle)} */ - private void initOverallUi(Bundle savedInstanceState) { + private void initOverallUi() { /// step 1 - load and process relevant inputs (resources, intent, savedInstanceState) boolean isWelcomeLinkVisible = getResources().getBoolean(R.bool.show_welcome_link); @@ -380,7 +380,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity findViewById(R.id.hostUrlFrame).setVisibility(View.GONE); mRefreshButton = findViewById(R.id.centeredRefreshButton); } - showRefreshButton(mServerIsChecked && !mServerIsValid && + showRefreshButton(mServerIsChecked && !mServerIsValid && mWaitingForOpId > Integer.MAX_VALUE); mServerStatusView = (TextView) findViewById(R.id.server_status_text); showServerStatus(); @@ -613,11 +613,18 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity // AsyncTask boolean inProgress = savedInstanceState.getBoolean(KEY_ASYNC_TASK_IN_PROGRESS); if (inProgress){ - mAsyncTask = new AuthenticatorAsyncTask(this); String username = savedInstanceState.getString(KEY_USERNAME); String password = savedInstanceState.getString(KEY_PASSWORD); - String[] params = {mServerInfo.mBaseUrl, username, password, mAuthToken, mAuthTokenType}; - mAsyncTask.execute(params); + + OwnCloudCredentials credentials = null; + if (BASIC_TOKEN_TYPE.equals(mAuthTokenType)) { + credentials = OwnCloudCredentialsFactory.newBasicCredentials(username, password); + + } else if (OAUTH_TOKEN_TYPE.equals(mAuthTokenType)) { + credentials = OwnCloudCredentialsFactory.newBearerCredentials(mAuthToken); + + } + accessRootFolder(credentials); } } @@ -644,7 +651,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity */ @Override protected void onResume() { - //Log_OC.wtf(TAG, "onResume init" ); super.onResume(); // bound here to avoid spurious changes triggered by Android on device rotations @@ -659,15 +665,12 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity doOnResumeAndBound(); } - //Log_OC.wtf(TAG, "onResume end" ); } @Override protected void onPause() { - //Log_OC.wtf(TAG, "onPause init" ); if (mOperationsServiceBinder != null) { - //Log_OC.wtf(TAG, "unregistering to listen for operation callbacks" ); mOperationsServiceBinder.removeOperationListener(this); } @@ -675,7 +678,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mHostUrlInput.setOnFocusChangeListener(null); super.onPause(); - //Log_OC.wtf(TAG, "onPause end" ); } @Override @@ -731,14 +733,14 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity public void onFocusChange(View view, boolean hasFocus) { if (view.getId() == R.id.hostUrlInput) { if (!hasFocus) { - onUrlInputFocusLost((TextView) view); + onUrlInputFocusLost(); } else { showRefreshButton(false); } } else if (view.getId() == R.id.account_password) { - onPasswordFocusChanged((TextView) view, hasFocus); + onPasswordFocusChanged(hasFocus); } } @@ -751,10 +753,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * started. * * When hasFocus: user 'comes back' to write again the server URL. - * - * @param hostInput TextView with the URL input field receiving the change of focus. */ - private void onUrlInputFocusLost(TextView hostInput) { + private void onUrlInputFocusLost() { if (!mServerInfo.mBaseUrl.equals( normalizeUrl(mHostUrlInput.getText().toString(), mServerInfo.mIsSslConn))) { // check server again only if the user changed something in the field @@ -773,10 +773,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mOkButton.setEnabled(false); mServerInfo = new GetServerInfoOperation.ServerInfo(); showRefreshButton(false); - + if (uri.length() != 0) { // Handle internationalized domain names uri = DisplayUtils.convertIdn(uri, true); + mServerStatusText = R.string.auth_testing_connection; mServerStatusIcon = R.drawable.progress_small; showServerStatus(); @@ -808,10 +809,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * * When (!hasFocus), the button is made invisible and the password is hidden. * - * @param passwordInput TextView with the password input field receiving the change of focus. * @param hasFocus 'True' if focus is received, 'false' if is lost */ - private void onPasswordFocusChanged(TextView passwordInput, boolean hasFocus) { + private void onPasswordFocusChanged(boolean hasFocus) { if (hasFocus) { showViewPasswordButton(); } else { @@ -876,7 +876,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mServerStatusText = R.string.auth_wtf_reenter_URL; showServerStatus(); mOkButton.setEnabled(false); - //Log_OC.wtf(TAG, "The user was allowed to click 'connect' to an unchecked server!!"); return; } @@ -909,23 +908,17 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity dialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG); /// validate credentials accessing the root folder - accessRootFolderRemoteOperation(username, password); - + OwnCloudCredentials credentials = OwnCloudCredentialsFactory.newBasicCredentials(username, password); + accessRootFolder(credentials); } - private void accessRootFolderRemoteOperation(String username, String password) { - // delete the account if the token has changed - if (mAction == ACTION_UPDATE_TOKEN || mAction == ACTION_UPDATE_EXPIRED_TOKEN) { - // Remove the cookies in AccountManager - mAccountMgr.setUserData(mAccount, Constants.KEY_COOKIES, null); - } - + private void accessRootFolder(OwnCloudCredentials credentials) { mAsyncTask = new AuthenticatorAsyncTask(this); - String[] params = { mServerInfo.mBaseUrl, username, password, mAuthToken, mAuthTokenType}; + Object[] params = { mServerInfo.mBaseUrl, credentials }; mAsyncTask.execute(params); - } + /** * Starts the OAuth 'grant type' flow to get an access token, with * a GET AUTHORIZATION request to the BUILT-IN authorization server. @@ -963,17 +956,16 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * in the server. */ private void startSamlBasedFederatedSingleSignOnAuthorization() { - // be gentle with the user + /// be gentle with the user mAuthStatusIcon = R.drawable.progress_small; mAuthStatusText = R.string.auth_connecting_auth_server; showAuthStatus(); - IndeterminateProgressDialog dialog = - IndeterminateProgressDialog.newInstance(R.string.auth_trying_to_login, true); - dialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG); - - /// validate credentials accessing the root folder - accessRootFolderRemoteOperation("", ""); + /// Show SAML-based SSO web dialog + String targetUrl = mServerInfo.mBaseUrl + + AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType); + SamlWebViewDialog dialog = SamlWebViewDialog.newInstance(targetUrl, targetUrl); + dialog.show(getSupportFragmentManager(), SAML_DIALOG_TAG); } /** @@ -993,16 +985,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } else if (operation instanceof OAuth2GetAccessToken) { onGetOAuthAccessTokenFinish(result); - } else if (operation instanceof ExistenceCheckRemoteOperation) { - // TODO : remove this response?? - //Log_OC.wtf(TAG, "received detection response through callback" ); - if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()). - equals(mAuthTokenType)) { - onSamlBasedFederatedSingleSignOnAuthorizationStart(result); - - } else { - onAuthorizationCheckFinish(result); - } } else if (operation instanceof GetRemoteUserNameOperation) { onGetUserNameFinish(result); } @@ -1050,30 +1032,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } - private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) { - mWaitingForOpId = Long.MAX_VALUE; - dismissDialog(WAIT_DIALOG_TAG); - - if (result.isIdPRedirection()) { - String targetUrl = mServerInfo.mBaseUrl - + AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType); - - // Show dialog - SamlWebViewDialog dialog = SamlWebViewDialog.newInstance(targetUrl, targetUrl); - dialog.show(getSupportFragmentManager(), SAML_DIALOG_TAG); - - mAuthStatusIcon = 0; - mAuthStatusText = 0; - - } else { - mAuthStatusIcon = R.drawable.common_error; - mAuthStatusText = R.string.auth_unsupported_auth_method; - - } - showAuthStatus(); - } - - /** * Processes the result of the server check performed when the user finishes the enter of the * server URL. @@ -1122,16 +1080,12 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private boolean authSupported(AuthenticationMethod authMethod) { - String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); - String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()); - String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()); - - 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)) + return (( BASIC_TOKEN_TYPE.equals(mAuthTokenType) && + AuthenticationMethod.BASIC_HTTP_AUTH.equals(authMethod) ) || + ( OAUTH_TOKEN_TYPE.equals(mAuthTokenType) && + AuthenticationMethod.BEARER_TOKEN.equals(authMethod)) || + ( SAML_TOKEN_TYPE.equals(mAuthTokenType) && + AuthenticationMethod.SAML_WEB_SSO.equals(authMethod)) ); } @@ -1166,13 +1120,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity // 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()); - } 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()); - } + if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0_AND_LATER)){ + url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0_AND_LATER.length()); + } return (url != null ? url : ""); } @@ -1372,8 +1322,10 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity Map tokens = (Map)(result.getData().get(0)); mAuthToken = tokens.get(OAuth2Constants.KEY_ACCESS_TOKEN); Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken); - - accessRootFolderRemoteOperation("", ""); + + /// validate token accessing to root folder / getting session + OwnCloudCredentials credentials = OwnCloudCredentialsFactory.newBearerCredentials(mAuthToken); + accessRootFolder(credentials); } else { updateAuthStatusIconAndText(result); @@ -1390,7 +1342,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * * @param result Result of the operation. */ - private void onAuthorizationCheckFinish(RemoteOperationResult result) { + @Override + public void onAuthenticatorTaskCallback(RemoteOperationResult result) { mWaitingForOpId = Long.MAX_VALUE; dismissDialog(WAIT_DIALOG_TAG); @@ -1460,14 +1413,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * the new credentials when needed. */ private void updateAccountAuthentication() throws AccountNotFoundException { - try { - OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor( - new OwnCloudAccount(mAccount, this) - ); - } catch (Exception e) { - Log_OC.e(TAG, "Exception", e); - } - + Bundle response = new Bundle(); response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name); response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type); @@ -1538,6 +1484,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity ); } + // include account version with the new account + mAccountMgr.setUserData( + mAccount, + Constants.KEY_OC_ACCOUNT_VERSION, + Integer.toString(AccountUtils.ACCOUNT_VERSION) + ); + /// add the new account as default in preferences, if there is none already Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this); if (defaultAccount == null) { @@ -1553,8 +1506,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity final Intent intent = new Intent(); intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType()); intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name); - /*if (!isOAuth) - intent.putExtra(AccountManager.KEY_AUTHTOKEN, MainApp.getAccountType()); */ intent.putExtra(AccountManager.KEY_USERDATA, username); if (isOAuth || isSaml) { mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken); @@ -1599,6 +1550,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity /** * Updates the content and visibility state of the icon and text associated * to the last check on the ownCloud server. + * */ private void showServerStatus() { if (mServerStatusIcon == 0 && mServerStatusText == 0) { @@ -1676,9 +1628,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity public void onCheckClick(View view) { CheckBox oAuth2Check = (CheckBox)view; if (oAuth2Check.isChecked()) { - mAuthTokenType = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()); + mAuthTokenType = OAUTH_TOKEN_TYPE; } else { - mAuthTokenType = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType()); + mAuthTokenType = BASIC_TOKEN_TYPE; } updateAuthenticationPreFragmentVisibility(); } @@ -1752,7 +1704,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity getUserNameIntent.putExtra(OperationsService.EXTRA_COOKIE, sessionCookie); if (mOperationsServiceBinder != null) { - //Log_OC.wtf(TAG, "starting getRemoteUserNameOperation..." ); mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(getUserNameIntent); } } @@ -1885,7 +1836,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity if (component.equals( new ComponentName(AuthenticatorActivity.this, OperationsService.class) )) { - //Log_OC.wtf(TAG, "Operations service connected"); mOperationsServiceBinder = (OperationsServiceBinder) service; doOnResumeAndBound(); @@ -1943,15 +1893,4 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } - @Override - public void onAuthenticatorTaskCallback(RemoteOperationResult result) { - //Log_OC.wtf(TAG, "received detection response through callback" ); - if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()). - equals(mAuthTokenType)) { - onSamlBasedFederatedSingleSignOnAuthorizationStart(result); - - } else { - onAuthorizationCheckFinish(result); - } - } }