Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
package com.owncloud.android.authentication.server

import java.net.URI
import java.net.URLEncoder

/**
* Helper class for authenticator-URL related logic.
*/
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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down