Let the Matrix SDK compute the Fallback urls

This commit is contained in:
Benoit Marty 2020-12-21 12:08:49 +01:00
parent 13938f2ab3
commit 36a553a886
5 changed files with 50 additions and 30 deletions

View file

@ -46,6 +46,11 @@ interface AuthenticationService {
*/ */
fun getSsoUrl(redirectUrl: String, deviceId: String?, providerId: String?): String? fun getSsoUrl(redirectUrl: String, deviceId: String?, providerId: String?): String?
/**
* Get the sign in or sign up fallback URL
*/
fun getFallbackUrl(forSignIn: Boolean, deviceId: String?): String?
/** /**
* Return a LoginWizard, to login to the homeserver. The login flow has to be retrieved first. * Return a LoginWizard, to login to the homeserver. The login flow has to be retrieved first.
*/ */

View file

@ -20,13 +20,13 @@ package org.matrix.android.sdk.api.auth
* Path to use when the client does not supported any or all login flows * Path to use when the client does not supported any or all login flows
* Ref: https://matrix.org/docs/spec/client_server/latest#login-fallback * Ref: https://matrix.org/docs/spec/client_server/latest#login-fallback
*/ */
const val LOGIN_FALLBACK_PATH = "/_matrix/static/client/login/" internal const val LOGIN_FALLBACK_PATH = "/_matrix/static/client/login/"
/** /**
* Path to use when the client does not supported any or all registration flows * Path to use when the client does not supported any or all registration flows
* Not documented * Not documented
*/ */
const val REGISTER_FALLBACK_PATH = "/_matrix/static/client/register/" internal const val REGISTER_FALLBACK_PATH = "/_matrix/static/client/register/"
/** /**
* Path to use when the client want to connect using SSO * Path to use when the client want to connect using SSO

View file

@ -23,7 +23,9 @@ import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.auth.AuthenticationService import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.LOGIN_FALLBACK_PATH
import org.matrix.android.sdk.api.auth.MSC2858_SSO_REDIRECT_PATH import org.matrix.android.sdk.api.auth.MSC2858_SSO_REDIRECT_PATH
import org.matrix.android.sdk.api.auth.REGISTER_FALLBACK_PATH
import org.matrix.android.sdk.api.auth.SSO_REDIRECT_PATH import org.matrix.android.sdk.api.auth.SSO_REDIRECT_PATH
import org.matrix.android.sdk.api.auth.SSO_REDIRECT_URL_PARAM import org.matrix.android.sdk.api.auth.SSO_REDIRECT_URL_PARAM
import org.matrix.android.sdk.api.auth.data.Credentials import org.matrix.android.sdk.api.auth.data.Credentials
@ -104,27 +106,51 @@ internal class DefaultAuthenticationService @Inject constructor(
} }
override fun getSsoUrl(redirectUrl: String, deviceId: String?, providerId: String?): String? { override fun getSsoUrl(redirectUrl: String, deviceId: String?, providerId: String?): String? {
return pendingSessionData?.let { safePendingSessionData -> val homeServerUrlBase = getHomeServerUrlBase() ?: return null
val homeServerUrl = safePendingSessionData.homeServerConnectionConfig.homeServerUri.toString()
buildString { return buildString {
append(homeServerUrl.trim { it == '/' }) append(homeServerUrlBase)
if (providerId != null) { if (providerId != null) {
append(MSC2858_SSO_REDIRECT_PATH) append(MSC2858_SSO_REDIRECT_PATH)
append("/$providerId") append("/$providerId")
} else { } else {
append(SSO_REDIRECT_PATH) append(SSO_REDIRECT_PATH)
} }
// Set a redirect url we will intercept later // Set the redirect url
appendParamToUrl(SSO_REDIRECT_URL_PARAM, redirectUrl) appendParamToUrl(SSO_REDIRECT_URL_PARAM, redirectUrl)
deviceId?.takeIf { it.isNotBlank() }?.let {
// But https://github.com/matrix-org/synapse/issues/5755
appendParamToUrl("device_id", it)
}
}
}
override fun getFallbackUrl(forSignIn: Boolean, deviceId: String?): String? {
val homeServerUrlBase = getHomeServerUrlBase() ?: return null
return buildString {
append(homeServerUrlBase)
if (forSignIn) {
append(LOGIN_FALLBACK_PATH)
deviceId?.takeIf { it.isNotBlank() }?.let { deviceId?.takeIf { it.isNotBlank() }?.let {
// But https://github.com/matrix-org/synapse/issues/5755 // But https://github.com/matrix-org/synapse/issues/5755
appendParamToUrl("device_id", it) appendParamToUrl("device_id", it)
} }
} else {
// For sign up
append(REGISTER_FALLBACK_PATH)
} }
} }
} }
private fun getHomeServerUrlBase(): String? {
return pendingSessionData
?.homeServerConnectionConfig
?.homeServerUri
?.toString()
?.trim { it == '/' }
}
override fun getLoginFlow(homeServerConnectionConfig: HomeServerConnectionConfig, callback: MatrixCallback<LoginFlowResult>): Cancelable { override fun getLoginFlow(homeServerConnectionConfig: HomeServerConnectionConfig, callback: MatrixCallback<LoginFlowResult>): Cancelable {
pendingSessionData = null pendingSessionData = null

View file

@ -822,4 +822,8 @@ class LoginViewModel @AssistedInject constructor(
fun getSsoUrl(redirectUrl: String, deviceId: String?, providerId: String?): String? { fun getSsoUrl(redirectUrl: String, deviceId: String?, providerId: String?): String? {
return authenticationService.getSsoUrl(redirectUrl, deviceId, providerId) return authenticationService.getSsoUrl(redirectUrl, deviceId, providerId)
} }
fun getFallbackUrl(forSignIn: Boolean, deviceId: String?): String? {
return authenticationService.getFallbackUrl(forSignIn, deviceId)
}
} }

View file

@ -38,10 +38,7 @@ import im.vector.app.databinding.FragmentLoginWebBinding
import im.vector.app.features.signout.soft.SoftLogoutAction import im.vector.app.features.signout.soft.SoftLogoutAction
import im.vector.app.features.signout.soft.SoftLogoutViewModel import im.vector.app.features.signout.soft.SoftLogoutViewModel
import org.matrix.android.sdk.api.auth.LOGIN_FALLBACK_PATH
import org.matrix.android.sdk.api.auth.REGISTER_FALLBACK_PATH
import org.matrix.android.sdk.api.auth.data.Credentials import org.matrix.android.sdk.api.auth.data.Credentials
import org.matrix.android.sdk.api.util.appendParamToUrl
import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.MoshiProvider
import timber.log.Timber import timber.log.Timber
import java.net.URLDecoder import java.net.URLDecoder
@ -119,19 +116,7 @@ class LoginWebFragment @Inject constructor(
} }
private fun launchWebView(state: LoginViewState) { private fun launchWebView(state: LoginViewState) {
val url = buildString { val url = loginViewModel.getFallbackUrl(state.signMode == SignMode.SignIn, state.deviceId) ?: return
append(state.homeServerUrl?.trim { it == '/' })
if (state.signMode == SignMode.SignIn) {
append(LOGIN_FALLBACK_PATH)
state.deviceId?.takeIf { it.isNotBlank() }?.let {
// But https://github.com/matrix-org/synapse/issues/5755
appendParamToUrl("device_id", it)
}
} else {
// MODE_REGISTER
append(REGISTER_FALLBACK_PATH)
}
}
views.loginWebWebView.loadUrl(url) views.loginWebWebView.loadUrl(url)