From e6ed8e8076d3bc32654f6dab8fe2751908e19c2e Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Mon, 16 Nov 2026 13:51:34 +0100 Subject: [PATCH 1/2] fix invalid url for sign up Signed-off-by: alperozturk96 --- .../authentication/AuthenticatorActivity.kt | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt index e78e720d432d..7169eedda545 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt @@ -209,18 +209,13 @@ open class AuthenticatorActivity : isFirstAuthAttempt = it.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG) } - val webLoginUrl = mdmWebLoginUrl() ?: providerWebLoginUrl() ?: brandedWebLoginUrl() - if (webLoginUrl != null) { - inflateWebViewLayout() - anonymouslyPostLoginRequest(webLoginUrl) - } else { - initServerUrlLogin(savedInstanceState) - } + startInitialLogin(savedInstanceState) val actionName = if (authenticatorAction == ACTION_CREATE) "create account" else "update credentials" + val isWebViewLogin = accountSetupWebviewBinding != null Log_OC.d( TAG, - "Started, action=$actionName, recreated=${savedInstanceState != null}, webViewLogin=${webLoginUrl != null}" + "Started, action=$actionName, recreated=${savedInstanceState != null}, webViewLogin=$isWebViewLogin" ) ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleEventObserver) } @@ -349,10 +344,28 @@ open class AuthenticatorActivity : return MDMConfig.getBaseUrl(this).takeIf { it.isNotEmpty() }?.plus(WEB_LOGIN) } - private fun providerWebLoginUrl(): String? = if (intent.getBooleanExtra(EXTRA_USE_PROVIDER_AS_WEBLOGIN, false)) { - getString(R.string.provider_registration_server) - } else { - null + private fun startInitialLogin(savedInstanceState: Bundle?) { + mdmWebLoginUrl()?.let { + startWebLoginFlow(it) + return + } + + if (intent.getBooleanExtra(EXTRA_USE_PROVIDER_AS_WEBLOGIN, false)) { + startSignupWebViewLogin() + return + } + + brandedWebLoginUrl()?.let { + startWebLoginFlow(it) + return + } + + initServerUrlLogin(savedInstanceState) + } + + private fun startWebLoginFlow(url: String) { + inflateWebViewLayout() + anonymouslyPostLoginRequest(url) } private fun brandedWebLoginUrl(): String? = getString(R.string.webview_login_url) From 1c3451b0fbe98438bb00fbff8cd5745b139cf861 Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Mon, 16 Nov 2026 16:11:11 +0100 Subject: [PATCH 2/2] fix(auth): open simple sign-up in WebView and harden login flow Signed-off-by: alperozturk96 --- .../authentication/AuthenticatorActivity.kt | 176 ++++++++++++------ .../account/AuthenticatorAccountStore.kt | 3 + .../account/AuthenticatorViewModel.kt | 14 ++ .../signup/SignupWebViewLogin.kt | 17 +- 4 files changed, 150 insertions(+), 60 deletions(-) create mode 100644 app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorViewModel.kt diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt index 7169eedda545..e829b39be8e8 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt @@ -35,7 +35,9 @@ import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.TextView import android.widget.TextView.OnEditorActionListener +import androidx.activity.addCallback import androidx.activity.result.contract.ActivityResultContracts +import androidx.activity.viewModels import androidx.annotation.VisibleForTesting import androidx.core.content.ContextCompat import androidx.core.net.toUri @@ -72,6 +74,7 @@ import com.owncloud.android.authentication.status.AuthenticatorStatus import com.owncloud.android.authentication.status.AuthenticatorStatusMapper import com.owncloud.android.authentication.account.AuthenticatorAccountStore import com.owncloud.android.authentication.account.CredentialsVerifier +import com.owncloud.android.authentication.account.AuthenticatorViewModel import com.owncloud.android.authentication.dialog.LoginDialog import com.owncloud.android.authentication.loginflow.LoginBrowserLauncher import com.owncloud.android.authentication.loginflow.LoginFlowV2 @@ -108,7 +111,6 @@ import com.owncloud.android.utils.PermissionUtil import com.owncloud.android.utils.theme.ViewThemeUtils import edu.umd.cs.findbugs.annotations.SuppressFBWarnings import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import javax.inject.Inject @@ -166,15 +168,17 @@ open class AuthenticatorActivity : private var operationsServiceConnection: ServiceConnection? = null private var operationsServiceBinder: OperationsServiceBinder? = null - private var credentialsCheckJob: Job? = null private var serverInfo = ServerInfo() private var serverStatus = AuthenticatorStatus.NONE private var authStatus = AuthenticatorStatus.NONE private var showWebViewLoginUrl = false - private var webViewUser: String? = null - private var webViewPassword: String? = null + private val viewModel: AuthenticatorViewModel by viewModels() + private val webViewUser: String? + get() = viewModel.credentials?.loginName + private val webViewPassword: String? + get() = viewModel.credentials?.appPassword private val loginDataScheme: String get() = getString(R.string.login_data_own_scheme) @@ -209,7 +213,9 @@ open class AuthenticatorActivity : isFirstAuthAttempt = it.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG) } + registerBackNavigation() startInitialLogin(savedInstanceState) + resumePendingCredentialsCheck() val actionName = if (authenticatorAction == ACTION_CREATE) "create account" else "update credentials" val isWebViewLogin = accountSetupWebviewBinding != null @@ -230,23 +236,18 @@ open class AuthenticatorActivity : outState.putString(KEY_SERVER_AUTH_METHOD, serverInfo.mAuthMethod.name) outState.putBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG, isFirstAuthAttempt) loginFlow.pendingAuthObjectJson?.let { outState.putString(KEY_LOGIN_FLOW_AUTH_OBJECT, it) } - - credentialsCheckJob?.cancel() - outState.putBoolean(KEY_CREDENTIALS_CHECK_IN_PROGRESS, credentialsCheckJob != null) - credentialsCheckJob = null + saveSignupWebViewState(outState) } - override fun onRestoreInstanceState(savedInstanceState: Bundle) { - super.onRestoreInstanceState(savedInstanceState) - if (!savedInstanceState.getBoolean(KEY_CREDENTIALS_CHECK_IN_PROGRESS)) { + private fun saveSignupWebViewState(outState: Bundle) { + val webView = accountSetupWebviewBinding?.loginWebview ?: return + if (webView.url == null) { return } - val credentials = OwnCloudCredentialsFactory.newBasicCredentials( - savedInstanceState.getString(KEY_USERNAME), - savedInstanceState.getString(KEY_PASSWORD) - ) - accessRootFolder(credentials) + val webViewState = Bundle() + webView.saveState(webViewState) + outState.putBundle(KEY_SIGNUP_WEBVIEW_STATE, webViewState) } /** @@ -284,6 +285,17 @@ open class AuthenticatorActivity : override fun onResume() { super.onResume() + bindOperationsServiceIfNeeded() + + if (operationsServiceBinder != null) { + doOnResumeAndBound() + } + } + + private fun bindOperationsServiceIfNeeded() { + if (operationsServiceConnection != null) { + return + } val connection = OperationsServiceConnection() operationsServiceConnection = connection @@ -291,10 +303,6 @@ open class AuthenticatorActivity : SnackbarUtil.show(accountSetupBinding?.scroll, R.string.error_cant_bind_to_operations_service) finish() } - - if (operationsServiceBinder != null) { - doOnResumeAndBound() - } } override fun onPause() { @@ -311,19 +319,18 @@ open class AuthenticatorActivity : super.onDestroy() } - override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { - val webViewBinding = accountSetupWebviewBinding - val isBackPressed = event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK - if (webViewBinding == null || !isBackPressed) { - return super.onKeyDown(keyCode, event) - } + private fun registerBackNavigation() { + onBackPressedDispatcher.addCallback(this) { + val webView = accountSetupWebviewBinding?.loginWebview + if (webView?.canGoBack() == true) { + webView.goBack() + return@addCallback + } - if (webViewBinding.loginWebview.canGoBack()) { - webViewBinding.loginWebview.goBack() - } else { - finish() + isEnabled = false + onBackPressedDispatcher.onBackPressed() + isEnabled = true } - return true } // endregion @@ -346,28 +353,47 @@ open class AuthenticatorActivity : private fun startInitialLogin(savedInstanceState: Bundle?) { mdmWebLoginUrl()?.let { - startWebLoginFlow(it) + startWebLoginFlow(it, savedInstanceState) return } if (intent.getBooleanExtra(EXTRA_USE_PROVIDER_AS_WEBLOGIN, false)) { - startSignupWebViewLogin() + startSignupWebViewLogin(savedInstanceState) return } brandedWebLoginUrl()?.let { - startWebLoginFlow(it) + startWebLoginFlow(it, savedInstanceState) return } initServerUrlLogin(savedInstanceState) } - private fun startWebLoginFlow(url: String) { + private fun startWebLoginFlow(url: String, savedInstanceState: Bundle?) { + if (restorePendingLoginFlow(savedInstanceState)) { + return + } + inflateWebViewLayout() + if (viewModel.credentials != null) { + return + } + anonymouslyPostLoginRequest(url) } + private fun resumePendingCredentialsCheck() { + val credentials = viewModel.credentials ?: return + if (waitingForOpId != NO_PENDING_OPERATION) { + return + } + + Log_OC.d(TAG, "Login: resuming check of received credentials after activity recreation") + serverInfo.mBaseUrl = normalizeUrlSuffix(credentials.server) + isServerCheckPendingServiceBinding = true + } + private fun brandedWebLoginUrl(): String? = getString(R.string.webview_login_url) .takeIf { it.isNotEmpty() } ?.also { showWebViewLoginUrl = resources.getBoolean(R.bool.show_server_url_input) } @@ -375,11 +401,13 @@ open class AuthenticatorActivity : private fun inflateWebViewLayout(): AccountSetupWebviewBinding = AccountSetupWebviewBinding.inflate(layoutInflater).also { accountSetupWebviewBinding = it + accountSetupBinding = null setContentView(it.root) } private fun inflateAccountSetupLayout(): AccountSetupBinding = AccountSetupBinding.inflate(layoutInflater).also { accountSetupBinding = it + accountSetupWebviewBinding = null setContentView(it.root) } @@ -398,7 +426,7 @@ open class AuthenticatorActivity : } private fun initOverallUi(binding: AccountSetupBinding) { - binding.hostUrlContainer.setEndIconOnClickListener { checkOcServer() } + binding.hostUrlContainer.setEndIconOnClickListener { checkServerFromInput() } binding.hostUrlInputHelperText.text = getString(R.string.login_url_helper_text, getString(R.string.app_name)) @@ -450,7 +478,7 @@ open class AuthenticatorActivity : return } accountSetupBinding?.hostUrlInput?.setText(url) - checkOcServer() + checkServerFromInput() } override fun onNothingSelected(parent: AdapterView<*>?) = Unit @@ -542,10 +570,18 @@ open class AuthenticatorActivity : val loginUrl = loginFlow.parseLoginUrl(response) ?: defaultLoginUrl() initLoginInfoView() + ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleEventObserver) loginBrowserLauncher.open(loginUrl) } } + private fun restartLoginFlow() { + Log_OC.d(TAG, "Login flow v2: restarting flow after rejected credentials") + loginFlow.isCompleted = false + inflateWebViewLayout() + anonymouslyPostLoginRequest(serverInfo.mBaseUrl + WEB_LOGIN) + } + private fun defaultLoginUrl(): String { Log_OC.e(TAG, "Login flow v2: no login URL in response, using default login URL") SnackbarUtil.show(this, R.string.authenticator_activity_login_error) @@ -582,13 +618,14 @@ open class AuthenticatorActivity : Log_OC.d(TAG, "Login flow v2: polling stopped") } - private fun restorePendingLoginFlow(savedInstanceState: Bundle?) { - val authObjectJson = savedInstanceState?.getString(KEY_LOGIN_FLOW_AUTH_OBJECT) ?: return + private fun restorePendingLoginFlow(savedInstanceState: Bundle?): Boolean { + val authObjectJson = savedInstanceState?.getString(KEY_LOGIN_FLOW_AUTH_OBJECT) ?: return false loginFlow.restore(authObjectJson) Log_OC.d(TAG, "Login flow v2: pending flow restored after activity recreation") inflateWebViewLayout() initLoginInfoView() + return true } private fun initLoginInfoView() { @@ -615,15 +652,19 @@ open class AuthenticatorActivity : // endregion // region WebView login - private fun startSignupWebViewLogin() { + private fun startSignupWebViewLogin(savedInstanceState: Bundle? = null) { val binding = inflateWebViewLayout() + if (viewModel.credentials != null) { + return + } + SignupWebViewLogin( activity = this, viewThemeUtils = viewThemeUtils, currentBinding = { accountSetupWebviewBinding }, loginDataPrefix = loginDataPrefix, onLoginDataUrl = ::parseAndLoginFromWebView - ).start(binding) + ).start(binding, savedInstanceState?.getBundle(KEY_SIGNUP_WEBVIEW_STATE)) } fun parseAndLoginFromWebView(dataString: String) { @@ -653,6 +694,12 @@ open class AuthenticatorActivity : // endregion // region Server check + private fun checkServerFromInput() { + loginFlow.isCompleted = false + isRedirectedToTheDefaultBrowser = false + checkOcServer() + } + @SuppressFBWarnings("NP") private fun checkOcServer() { val binding = accountSetupBinding @@ -807,10 +854,21 @@ open class AuthenticatorActivity : } private fun showServerStatus() { - val binding = accountSetupBinding ?: return + val binding = accountSetupBinding + if (binding == null) { + showStatusAsSnackbar(serverStatus) + return + } showStatus(binding.serverStatusText, serverStatus) } + private fun showStatusAsSnackbar(status: AuthenticatorStatus) { + if (status.isNone) { + return + } + SnackbarUtil.show(this, status.text) + } + private fun showAuthStatus() { val binding = accountSetupBinding ?: return showStatus(binding.authStatusText, authStatus) @@ -837,7 +895,7 @@ open class AuthenticatorActivity : private fun accessRootFolder(credentials: OwnCloudCredentials) { val baseUrl = serverInfo.mBaseUrl Log_OC.d(TAG, "Credential check: started for $baseUrl") - credentialsCheckJob = lifecycleScope.launch { + lifecycleScope.launch { val result = CredentialsVerifier(applicationContext).verify(baseUrl, credentials) onCredentialsChecked(result) } @@ -866,7 +924,6 @@ open class AuthenticatorActivity : private fun onCredentialsChecked(result: RemoteOperationResult) { waitingForOpId = NO_PENDING_OPERATION dismissWaitingDialog() - credentialsCheckJob = null when { result.isSuccess -> onCredentialsAccepted(result) @@ -911,8 +968,7 @@ open class AuthenticatorActivity : private fun onCredentialsRejected(result: RemoteOperationResult) { Log_OC.e(TAG, "Credential check: rejected, code=${result.code}, ${result.logMessage}") if (accountSetupWebviewBinding != null) { - Log_OC.d(TAG, "Login flow v2: restarting flow after rejected credentials") - anonymouslyPostLoginRequest(serverInfo.mBaseUrl + WEB_LOGIN) + restartLoginFlow() } else { SnackbarUtil.show(this, R.string.auth_access_failed, result.getLogMessage(this)) updateAuthStatus(result) @@ -922,15 +978,13 @@ open class AuthenticatorActivity : } private fun resetWebViewCredentials() { - webViewPassword = null - webViewUser = null + viewModel.credentials = null deleteCookies() } private fun applyLoginCredentials(loginUrlInfo: LoginUrlInfo, password: String = loginUrlInfo.appPassword) { serverInfo.mBaseUrl = normalizeUrlSuffix(loginUrlInfo.server) - webViewUser = loginUrlInfo.loginName - webViewPassword = password + viewModel.credentials = loginUrlInfo.copy(appPassword = password) } private fun dismissWaitingDialog() { @@ -950,6 +1004,13 @@ open class AuthenticatorActivity : return createAccount(result) } + if (!isLoggedInUserOfAccount(result)) { + Log_OC.w(TAG, "Account: received credentials belong to another user, ${account?.name} not updated") + updateAuthStatus(RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME)) + showAuthStatus() + return false + } + return try { updateAccountAuthentication() true @@ -961,6 +1022,15 @@ open class AuthenticatorActivity : } } + private fun isLoggedInUserOfAccount(result: RemoteOperationResult): Boolean { + val account = account ?: return false + val accountUserId = accountStore.userId(account) + if (accountUserId.isNullOrEmpty()) { + return true + } + return result.resultData?.id == accountUserId + } + private fun fetchCapabilitiesAndFinish(accountName: String) { val user = accountManager.getUser(accountName) if (!user.isPresent) { @@ -1125,7 +1195,7 @@ open class AuthenticatorActivity : override fun onEditorAction(inputField: TextView?, actionId: Int, event: KeyEvent?): Boolean { val isConfirmAction = actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_NULL if (isConfirmAction && inputField != null && inputField == accountSetupBinding?.hostUrlInput) { - checkOcServer() + checkServerFromInput() } return false // always return false to grant that the software keyboard is hidden anyway } @@ -1217,9 +1287,7 @@ open class AuthenticatorActivity : private const val WAIT_DIALOG_TAG = "WAIT_DIALOG" private const val KEY_AUTH_IS_FIRST_ATTEMPT_TAG = "KEY_AUTH_IS_FIRST_ATTEMPT" - private const val KEY_USERNAME = "USERNAME" - private const val KEY_PASSWORD = "PASSWORD" - private const val KEY_CREDENTIALS_CHECK_IN_PROGRESS = "AUTH_IN_PROGRESS" + private const val KEY_SIGNUP_WEBVIEW_STATE = "SIGNUP_WEBVIEW_STATE" private const val NO_PENDING_OPERATION = Long.MAX_VALUE private const val LOGIN_PATH = "login/" diff --git a/app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorAccountStore.kt b/app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorAccountStore.kt index fe6954afcdff..d3444d61263b 100644 --- a/app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorAccountStore.kt +++ b/app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorAccountStore.kt @@ -30,6 +30,9 @@ class AuthenticatorAccountStore( fun baseUrl(account: Account): String = platformAccountManager.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL).orEmpty() + fun userId(account: Account): String? = + platformAccountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID) + /** * Registers [account] and returns the result intent for the AccountAuthenticator that started the login. */ diff --git a/app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorViewModel.kt b/app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorViewModel.kt new file mode 100644 index 000000000000..bbcf44e0126a --- /dev/null +++ b/app/src/main/java/com/owncloud/android/authentication/account/AuthenticatorViewModel.kt @@ -0,0 +1,14 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2026 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +package com.owncloud.android.authentication.account + +import androidx.lifecycle.ViewModel +import com.owncloud.android.authentication.loginurl.LoginUrlInfo + +class AuthenticatorViewModel : ViewModel() { + var credentials: LoginUrlInfo? = null +} diff --git a/app/src/main/java/com/owncloud/android/authentication/signup/SignupWebViewLogin.kt b/app/src/main/java/com/owncloud/android/authentication/signup/SignupWebViewLogin.kt index f82660408fa7..f6020817f147 100644 --- a/app/src/main/java/com/owncloud/android/authentication/signup/SignupWebViewLogin.kt +++ b/app/src/main/java/com/owncloud/android/authentication/signup/SignupWebViewLogin.kt @@ -8,6 +8,7 @@ package com.owncloud.android.authentication.signup import android.annotation.SuppressLint import android.content.Context +import android.os.Bundle import android.view.View import android.webkit.WebResourceError import android.webkit.WebResourceRequest @@ -36,7 +37,7 @@ class SignupWebViewLogin( @SuppressFBWarnings("ANDROID_WEB_VIEW_JAVASCRIPT") @SuppressLint("SetJavaScriptEnabled") @Suppress("DEPRECATION") - fun start(binding: AccountSetupWebviewBinding) { + fun start(binding: AccountSetupWebviewBinding, savedWebViewState: Bundle?) { viewThemeUtils.platform.colorCircularProgressBar( binding.loginWebviewProgressBar, ColorRole.ON_PRIMARY_CONTAINER @@ -51,13 +52,17 @@ class SignupWebViewLogin( savePassword = false } - val headers = mapOf(RemoteOperation.OCS_API_HEADER to RemoteOperation.OCS_API_HEADER_VALUE) - ProxyUtil(WeakReference(activity)).applyProxy { - currentBinding()?.loginWebview?.loadUrl(activity.getString(R.string.provider_registration_server), headers) - } binding.loginFlowV2.loginFlowInfoV2.visibility = View.GONE - setWebViewClient(binding) + + ProxyUtil(WeakReference(activity)).applyProxy { + val webView = currentBinding()?.loginWebview ?: return@applyProxy + val isRestored = savedWebViewState?.let(webView::restoreState) != null + if (!isRestored) { + val headers = mapOf(RemoteOperation.OCS_API_HEADER to RemoteOperation.OCS_API_HEADER_VALUE) + webView.loadUrl(activity.getString(R.string.provider_registration_server), headers) + } + } } private fun setWebViewClient(binding: AccountSetupWebviewBinding) {