From 5e176d447322ebd9577e5021f4fe646a6e24869f Mon Sep 17 00:00:00 2001 From: vlad Date: Thu, 19 Nov 2026 10:12:15 +0100 Subject: [PATCH] feat(auth): add the account user to the login URL when it signs in again When an existing account signs in again, the app opens the login URL of login flow v2 without the user of the account. In the same case, the desktop client adds the user to the login URL, as the "user" parameter. A server can use this parameter in these ways: - Nextcloud pre-fills its login form with the user. - A server can refuse the grant when another account is signed in in the browser. - A server can hand out the login name that the account was created with. The last point matters because of commit 1c3451b0f. That commit made the app compare the user ID of the new credentials with the user ID of the account. Some servers gave an account another login name in the past. Without the parameter, such a server cannot know which login name the account holds. The server then hands out the current name, and the app refuses the new credentials after the person granted access. The app now appends the user name of the account to the login URL that the server returns, when it signs an existing account in again. The login URL of a new account, and the default login URL, stay unchanged. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- .../authentication/AuthenticatorActivity.kt | 14 ++++- .../server/AuthenticatorUrlUtils.kt | 11 ++++ .../server/AuthenticatorUrlUtilsTest.kt | 51 ++++++++++++++++++- 3 files changed, 74 insertions(+), 2 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 e829b39be8e8..124edb16fdff 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.kt @@ -65,6 +65,7 @@ import com.nextcloud.utils.mdm.MDMConfig import com.nextcloud.utils.text.LinkFormatter import com.owncloud.android.MainApp import com.owncloud.android.R +import com.owncloud.android.authentication.server.AuthenticatorUrlUtils.appendLoginUser import com.owncloud.android.authentication.server.AuthenticatorUrlUtils.normalizeScheme import com.owncloud.android.authentication.server.AuthenticatorUrlUtils.normalizeUrlSuffix import com.owncloud.android.authentication.server.AuthenticatorUrlUtils.stripIndexPhpOrAppsFiles @@ -568,7 +569,7 @@ open class AuthenticatorActivity : return@launch } - val loginUrl = loginFlow.parseLoginUrl(response) ?: defaultLoginUrl() + val loginUrl = loginFlow.parseLoginUrl(response)?.let(::withAccountUser) ?: defaultLoginUrl() initLoginInfoView() ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleEventObserver) loginBrowserLauncher.open(loginUrl) @@ -582,6 +583,17 @@ open class AuthenticatorActivity : anonymouslyPostLoginRequest(serverInfo.mBaseUrl + WEB_LOGIN) } + private fun withAccountUser(loginUrl: String): String { + if (authenticatorAction == ACTION_CREATE) { + return loginUrl + } + val user = account?.let { AccountUtils.getUsernameForAccount(it) } + if (user.isNullOrEmpty()) { + return loginUrl + } + return appendLoginUser(loginUrl, user) + } + 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) diff --git a/app/src/main/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtils.kt b/app/src/main/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtils.kt index b59ce9176a1c..07eb368ea8ee 100644 --- a/app/src/main/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtils.kt +++ b/app/src/main/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtils.kt @@ -8,6 +8,7 @@ package com.owncloud.android.authentication.server import java.net.URI +import java.net.URLEncoder /** * Helper class for authenticator-URL related logic. @@ -15,6 +16,7 @@ import java.net.URI object AuthenticatorUrlUtils { private const val REMOTE_PHP_PATH = "/remote.php/dav" + private const val LOGIN_USER_PARAMETER = "user" fun normalizeUrlSuffix(url: String): String { var normalizedUrl = url @@ -52,6 +54,15 @@ object AuthenticatorUrlUtils { return strippedUrl } + /** + * The server pre-fills its login form with [user], and can refuse the grant to another account that is signed in + * in the browser. + */ + fun appendLoginUser(loginUrl: String, user: String): String { + val separator = if (loginUrl.contains('?')) '&' else '?' + return "$loginUrl$separator$LOGIN_USER_PARAMETER=${URLEncoder.encode(user, Charsets.UTF_8.name())}" + } + fun normalizeScheme(url: String): String = if (url.matches("[a-zA-Z][a-zA-Z0-9+.-]+://.+".toRegex())) { val uri = URI.create(url) val lcScheme = uri.scheme.lowercase() diff --git a/app/src/test/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtilsTest.kt b/app/src/test/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtilsTest.kt index 2bc8061c1a83..fa3339e3b04c 100644 --- a/app/src/test/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtilsTest.kt +++ b/app/src/test/java/com/owncloud/android/authentication/server/AuthenticatorUrlUtilsTest.kt @@ -6,6 +6,7 @@ */ package com.owncloud.android.authentication.server +import com.owncloud.android.authentication.server.AuthenticatorUrlUtils.appendLoginUser import com.owncloud.android.authentication.server.AuthenticatorUrlUtils.normalizeScheme import org.junit.Assert.assertEquals import org.junit.Assert.assertSame @@ -15,9 +16,57 @@ import org.junit.runners.Suite import org.junit.runners.Suite.SuiteClasses @RunWith(Suite::class) -@SuiteClasses(AuthenticatorUrlUtilsTest.NormalizeScheme::class) +@SuiteClasses(AuthenticatorUrlUtilsTest.NormalizeScheme::class, AuthenticatorUrlUtilsTest.AppendLoginUser::class) class AuthenticatorUrlUtilsTest { + class AppendLoginUser { + + @Test + fun urlWithoutQuery() { + // GIVEN + // login URL has no query + val url = "https://host.net/index.php/login/v2/flow/abc" + + // WHEN + // user is appended + val loginUrl = appendLoginUser(url, "jane") + + // THEN + // user starts the query + assertEquals("https://host.net/index.php/login/v2/flow/abc?user=jane", loginUrl) + } + + @Test + fun urlWithQuery() { + // GIVEN + // login URL has a query + val url = "https://host.net/index.php/login/v2/grant?token=abc" + + // WHEN + // user is appended + val loginUrl = appendLoginUser(url, "jane") + + // THEN + // user is added to the query + assertEquals("https://host.net/index.php/login/v2/grant?token=abc&user=jane", loginUrl) + } + + @Test + fun userWithReservedCharacters() { + // GIVEN + // user contains a space and an at sign + val url = "https://host.net/index.php/login/v2/flow/abc" + + // WHEN + // user is appended + val loginUrl = appendLoginUser(url, "jane doe@example.com") + + // THEN + // user is URL-encoded + assertEquals("https://host.net/index.php/login/v2/flow/abc?user=jane+doe%40example.com", loginUrl) + } + } + class NormalizeScheme { @Test