Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When creating an account with MAS, change the prompt parameter to create #3635

Closed
wants to merge 1 commit into from
Closed
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 @@ -15,6 +15,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.core.net.toUri
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
Expand All @@ -23,6 +24,7 @@ import io.element.android.features.login.impl.accountprovider.AccountProviderDat
import io.element.android.features.login.impl.error.ChangeServerError
import io.element.android.features.login.impl.screens.createaccount.AccountCreationNotSupported
import io.element.android.features.login.impl.web.WebClientUrlForAuthenticationRetriever
import io.element.android.libraries.androidutils.uri.setQueryParameter
import io.element.android.libraries.architecture.AsyncData
import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.architecture.runCatchingUpdatingState
Expand Down Expand Up @@ -92,7 +94,16 @@ class ConfirmAccountProviderPresenter @AssistedInject constructor(
val matrixHomeServerDetails = authenticationService.getHomeserverDetails().value!!
if (matrixHomeServerDetails.supportsOidcLogin) {
// Retrieve the details right now
LoginFlow.OidcFlow(authenticationService.getOidcUrl().getOrThrow())
val oidcDetails = authenticationService.getOidcUrl().getOrThrow()
if (params.isAccountCreation) {
// In this case, add or replace the "prompt" parameter to "create"
val newUrl = oidcDetails.url.toUri()
.setQueryParameter("prompt", "create")
.toString()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the provided value for the "prompt" parameter on beta.matrix.org is "consent".

LoginFlow.OidcFlow(oidcDetails.copy(url = newUrl))
} else {
LoginFlow.OidcFlow(oidcDetails)
}
} else if (params.isAccountCreation) {
val url = webClientUrlForAuthenticationRetriever.retrieve(homeserverUrl)
LoginFlow.AccountCreationFlow(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ import android.net.Uri
const val IGNORED_SCHEMA = "ignored"

fun createIgnoredUri(path: String): Uri = Uri.parse("$IGNORED_SCHEMA://$path")

fun Uri.setQueryParameter(key: String, value: String): Uri {
val existingParams = queryParameterNames
return buildUpon().apply {
clearQuery()
existingParams.forEach { existingKey ->
if (existingKey != key) {
appendQueryParameter(existingKey, getQueryParameter(existingKey))
}
}
appendQueryParameter(key, value)
}.build()
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only appendQueryParameter exists in Uri.Builder, so sadly, I had to create this extension.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only
* Please see LICENSE in the repository root for full details.
*/

package io.element.android.libraries.androidutils.uri

import androidx.core.net.toUri
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class UriExtensionTest {
@Test
fun `url with prompt parameter should replace existing value`() {
val url = "https://beta.element.io/account/authorize" +
"?response_type=code" +
"&client_id=01J9RB9MEJCMVHWNYYHTVDNBVJ" +
"&redirect_uri=io.element%3A%2Fcallback" +
"&scope=scope" +
"&state=x61ILblUF6BwOTUA" +
"&nonce=N7TdVfDhyVNF9PbH" +
"&prompt=consent" +
"&code_challenge_method=S256" +
"&code_challenge=bDV2DWX0j0U-QtewSUJeXr3DEmvFxlHfQN_1UxXpOUk"
val result = url.toUri()
.setQueryParameter("prompt", "create")
.toString()
assertThat(result).isEqualTo(
"https://beta.element.io/account/authorize" +
"?response_type=code" +
"&client_id=01J9RB9MEJCMVHWNYYHTVDNBVJ" +
"&redirect_uri=io.element%3A%2Fcallback" +
"&scope=scope" +
"&state=x61ILblUF6BwOTUA" +
"&nonce=N7TdVfDhyVNF9PbH" +
"&code_challenge_method=S256" +
"&code_challenge=bDV2DWX0j0U-QtewSUJeXr3DEmvFxlHfQN_1UxXpOUk" +
"&prompt=create"
)
}

@Test
fun `url without prompt parameter should add the parameter`() {
val url = "https://beta.element.io/account/authorize" +
"?response_type=code" +
"&client_id=01J9RB9MEJCMVHWNYYHTVDNBVJ" +
"&redirect_uri=io.element%3A%2Fcallback" +
"&scope=scope" +
"&state=x61ILblUF6BwOTUA" +
"&nonce=N7TdVfDhyVNF9PbH" +
"&code_challenge_method=S256" +
"&code_challenge=bDV2DWX0j0U-QtewSUJeXr3DEmvFxlHfQN_1UxXpOUk"
val result = url.toUri()
.setQueryParameter("prompt", "create")
.toString()
assertThat(result).isEqualTo(
"https://beta.element.io/account/authorize" +
"?response_type=code" +
"&client_id=01J9RB9MEJCMVHWNYYHTVDNBVJ" +
"&redirect_uri=io.element%3A%2Fcallback" +
"&scope=scope" +
"&state=x61ILblUF6BwOTUA" +
"&nonce=N7TdVfDhyVNF9PbH" +
"&code_challenge_method=S256" +
"&code_challenge=bDV2DWX0j0U-QtewSUJeXr3DEmvFxlHfQN_1UxXpOUk" +
"&prompt=create"
)
}
}
Loading