From 90d4fbe0bd5ba3fa68460d44d7ff6168a2947d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Mon, 10 Aug 2026 14:26:10 +0200 Subject: [PATCH] Apply the `GetUrlResolver` pattern using unauthenticated matrix clients where possible --- .../enterprise/api/EnterpriseService.kt | 8 ++++- .../impl/DefaultEnterpriseService.kt | 5 ++- .../DefaultContentScannerUrlProvider.kt | 13 +++++-- .../DefaultAccountProviderAccessControl.kt | 29 ++++++++++------ .../WebClientUrlForAuthenticationRetriever.kt | 34 ++++++++++++------- .../api/scanner/ContentScannerUrlProvider.kt | 11 ++++-- .../matrix/impl/RustMatrixClientFactory.kt | 26 +++++++------- .../auth/RustMatrixAuthenticationService.kt | 20 +++++++---- 8 files changed, 97 insertions(+), 49 deletions(-) rename {libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix => features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise}/impl/scanner/DefaultContentScannerUrlProvider.kt (51%) diff --git a/features/enterprise/api/src/main/kotlin/io/element/android/features/enterprise/api/EnterpriseService.kt b/features/enterprise/api/src/main/kotlin/io/element/android/features/enterprise/api/EnterpriseService.kt index 7b595966d4..f2923c2b40 100644 --- a/features/enterprise/api/src/main/kotlin/io/element/android/features/enterprise/api/EnterpriseService.kt +++ b/features/enterprise/api/src/main/kotlin/io/element/android/features/enterprise/api/EnterpriseService.kt @@ -10,6 +10,7 @@ package io.element.android.features.enterprise.api import androidx.compose.ui.graphics.Color import io.element.android.compound.colors.SemanticColorsLightDark +import io.element.android.libraries.matrix.api.GetUrlResolver import io.element.android.libraries.matrix.api.core.SessionId import io.element.android.libraries.wellknown.api.ElementWellKnown import kotlinx.coroutines.flow.Flow @@ -17,7 +18,7 @@ import kotlinx.coroutines.flow.Flow interface EnterpriseService { val isEnterpriseBuild: Boolean suspend fun isEnterpriseUser(sessionId: SessionId): Boolean - suspend fun tweakMasUrl(url: String, homeserver: String): String + suspend fun tweakMasUrl(url: String, homeserver: String, getUrlResolver: GetUrlResolver): String fun defaultHomeserverList(): List suspend fun isAllowedToConnectToHomeserver(homeserverUrl: String): Boolean @@ -47,6 +48,11 @@ interface EnterpriseService { */ fun overriddenElementWellKnown(): ElementWellKnown? + /** + * Gets the Element Server Suite (ESS) config endpoint URL for the given domain. + */ + fun essConfigEndpointUrl(domain: String): String? + companion object { const val ANY_ACCOUNT_PROVIDER = "*" } diff --git a/features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise/impl/DefaultEnterpriseService.kt b/features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise/impl/DefaultEnterpriseService.kt index bc2a3f1bf1..567f598c22 100644 --- a/features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise/impl/DefaultEnterpriseService.kt +++ b/features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise/impl/DefaultEnterpriseService.kt @@ -14,6 +14,7 @@ import dev.zacsweers.metro.ContributesBinding import io.element.android.compound.colors.SemanticColorsLightDark import io.element.android.features.enterprise.api.BugReportUrl import io.element.android.features.enterprise.api.EnterpriseService +import io.element.android.libraries.matrix.api.GetUrlResolver import io.element.android.libraries.matrix.api.core.SessionId import io.element.android.libraries.wellknown.api.ElementWellKnown import kotlinx.coroutines.flow.Flow @@ -24,7 +25,7 @@ class DefaultEnterpriseService : EnterpriseService { override val isEnterpriseBuild = false override suspend fun isEnterpriseUser(sessionId: SessionId) = false - override suspend fun tweakMasUrl(url: String, homeserver: String) = url + override suspend fun tweakMasUrl(url: String, homeserver: String, getUrlResolver: GetUrlResolver) = url override fun defaultHomeserverList(): List = emptyList() override suspend fun isAllowedToConnectToHomeserver(homeserverUrl: String) = true @@ -48,4 +49,6 @@ class DefaultEnterpriseService : EnterpriseService { override fun getNoisyNotificationChannelId(sessionId: SessionId): String? = null override fun overriddenElementWellKnown(): ElementWellKnown? = null + + override fun essConfigEndpointUrl(domain: String): String? = null } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/scanner/DefaultContentScannerUrlProvider.kt b/features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise/impl/scanner/DefaultContentScannerUrlProvider.kt similarity index 51% rename from libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/scanner/DefaultContentScannerUrlProvider.kt rename to features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise/impl/scanner/DefaultContentScannerUrlProvider.kt index b43dcfb3e8..edd1514929 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/scanner/DefaultContentScannerUrlProvider.kt +++ b/features/enterprise/impl-foss/src/main/kotlin/io/element/android/features/enterprise/impl/scanner/DefaultContentScannerUrlProvider.kt @@ -5,10 +5,12 @@ * Please see LICENSE files in the repository root for full details. */ -package io.element.android.libraries.matrix.impl.scanner +package io.element.android.features.enterprise.impl.scanner import dev.zacsweers.metro.AppScope import dev.zacsweers.metro.ContributesBinding +import io.element.android.libraries.matrix.api.GetUrlResolver +import io.element.android.libraries.matrix.api.core.SessionId import io.element.android.libraries.matrix.api.scanner.ContentScannerUrlProvider /** @@ -16,5 +18,12 @@ import io.element.android.libraries.matrix.api.scanner.ContentScannerUrlProvider */ @ContributesBinding(AppScope::class) class DefaultContentScannerUrlProvider : ContentScannerUrlProvider { - override suspend fun getContentScannerUrl(homeserver: String): Result = Result.success(null) + override suspend fun getContentScannerUrl(sessionId: SessionId): Result = Result.success(null) + + @ContributesBinding(AppScope::class) + class Factory : ContentScannerUrlProvider.Factory { + override fun create(getUrlResolver: GetUrlResolver): ContentScannerUrlProvider { + return DefaultContentScannerUrlProvider() + } + } } diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accesscontrol/DefaultAccountProviderAccessControl.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accesscontrol/DefaultAccountProviderAccessControl.kt index db56047e15..854b4293f1 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accesscontrol/DefaultAccountProviderAccessControl.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accesscontrol/DefaultAccountProviderAccessControl.kt @@ -13,13 +13,15 @@ import dev.zacsweers.metro.ContributesBinding import io.element.android.features.enterprise.api.EnterpriseService import io.element.android.features.login.api.accesscontrol.AccountProviderAccessControl import io.element.android.features.login.impl.changeserver.AccountProviderAccessException -import io.element.android.libraries.core.uri.ensureProtocol +import io.element.android.libraries.matrix.api.TemporaryMatrixClientFactory +import io.element.android.libraries.wellknown.api.EnterpriseRemoteConfigSource import io.element.android.libraries.wellknown.api.WellknownRetriever @ContributesBinding(AppScope::class) class DefaultAccountProviderAccessControl( private val enterpriseService: EnterpriseService, - private val wellknownRetriever: WellknownRetriever, + private val wellknownRetrieverFactory: WellknownRetriever.Factory, + private val temporaryMatrixClientFactory: TemporaryMatrixClientFactory, ) : AccountProviderAccessControl { override suspend fun isAllowedToConnectToAccountProvider(accountProviderUrl: String) = try { assertIsAllowedToConnectToAccountProvider( @@ -38,14 +40,21 @@ class DefaultAccountProviderAccessControl( ) { if (enterpriseService.isEnterpriseBuild.not()) { // Ensure that Element Pro is not required for this account provider - val wellKnown = wellknownRetriever.getElementWellKnown( - baseUrl = accountProviderUrl.ensureProtocol(), - ).dataOrNull() - if (wellKnown?.enforceElementPro == true) { - throw AccountProviderAccessException.NeedElementProException( - unauthorisedAccountProviderTitle = title, - applicationId = ELEMENT_PRO_APPLICATION_ID, - ) + val temporaryMatrixClient = temporaryMatrixClientFactory.createTemporaryMatrixClient(accountProviderUrl).getOrThrow() + temporaryMatrixClient.use { + val wellknownRetriever = wellknownRetrieverFactory.create(temporaryMatrixClient) + val wellKnown = wellknownRetriever.getElementWellKnown( + host = accountProviderUrl, + // It's expected this is hardcoded to the well-known endpoint + source = EnterpriseRemoteConfigSource.WELLKNOWN_ENDPOINT, + ).dataOrNull() + + if (wellKnown?.enforceElementPro == true) { + throw AccountProviderAccessException.NeedElementProException( + unauthorisedAccountProviderTitle = title, + applicationId = ELEMENT_PRO_APPLICATION_ID, + ) + } } } if (enterpriseService.isAllowedToConnectToHomeserver(accountProviderUrl).not()) { diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/web/WebClientUrlForAuthenticationRetriever.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/web/WebClientUrlForAuthenticationRetriever.kt index df08cf468b..f9dcd83c24 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/web/WebClientUrlForAuthenticationRetriever.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/web/WebClientUrlForAuthenticationRetriever.kt @@ -13,6 +13,8 @@ import dev.zacsweers.metro.AppScope import dev.zacsweers.metro.ContributesBinding import io.element.android.appconfig.AuthenticationConfig import io.element.android.features.login.impl.screens.createaccount.AccountCreationNotSupported +import io.element.android.libraries.matrix.api.TemporaryMatrixClientFactory +import io.element.android.libraries.wellknown.api.EnterpriseRemoteConfigSource import io.element.android.libraries.wellknown.api.WellknownRetriever import timber.log.Timber @@ -22,24 +24,32 @@ interface WebClientUrlForAuthenticationRetriever { @ContributesBinding(AppScope::class) class DefaultWebClientUrlForAuthenticationRetriever( - private val wellknownRetriever: WellknownRetriever, + private val wellknownRetrieverFactory: WellknownRetriever.Factory, + private val temporaryMatrixClientFactory: TemporaryMatrixClientFactory, ) : WebClientUrlForAuthenticationRetriever { override suspend fun retrieve(homeServerUrl: String): String { if (homeServerUrl != AuthenticationConfig.MATRIX_ORG_URL) { Timber.w("Temporary account creation flow is only supported on matrix.org") throw AccountCreationNotSupported() } - val wellknown = wellknownRetriever.getElementWellKnown(homeServerUrl).dataOrNull() - ?: throw AccountCreationNotSupported() - val registrationHelperUrl = wellknown.registrationHelperUrl - return if (registrationHelperUrl != null) { - registrationHelperUrl.toUri() - .buildUpon() - .appendQueryParameter("hs_url", homeServerUrl) - .build() - .toString() - } else { - throw AccountCreationNotSupported() + val temporaryMatrixClient = temporaryMatrixClientFactory.createTemporaryMatrixClient(homeServerUrl).getOrThrow() + return temporaryMatrixClient.use { + val wellknownRetriever = wellknownRetrieverFactory.create(temporaryMatrixClient) + val wellknown = wellknownRetriever.getElementWellKnown( + host = homeServerUrl, + source = EnterpriseRemoteConfigSource.WELLKNOWN_ENDPOINT + ).dataOrNull() ?: throw AccountCreationNotSupported() + + val registrationHelperUrl = wellknown.registrationHelperUrl + if (registrationHelperUrl != null) { + registrationHelperUrl.toUri() + .buildUpon() + .appendQueryParameter("hs_url", homeServerUrl) + .build() + .toString() + } else { + throw AccountCreationNotSupported() + } } } } diff --git a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/scanner/ContentScannerUrlProvider.kt b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/scanner/ContentScannerUrlProvider.kt index 600866f586..32794bff7b 100644 --- a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/scanner/ContentScannerUrlProvider.kt +++ b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/scanner/ContentScannerUrlProvider.kt @@ -7,12 +7,19 @@ package io.element.android.libraries.matrix.api.scanner +import io.element.android.libraries.matrix.api.GetUrlResolver +import io.element.android.libraries.matrix.api.core.SessionId + /** * Provides the URL of the content scanner service for a given homeserver, if any is set up. */ fun interface ContentScannerUrlProvider { /** - * Returns the URL of the content scanner service for the given [homeserver], or `null` if no content scanner is set up. + * Returns the URL of the content scanner service for the given [sessionId], or `null` if no content scanner is set up. */ - suspend fun getContentScannerUrl(homeserver: String): Result + suspend fun getContentScannerUrl(sessionId: SessionId): Result + + fun interface Factory { + fun create(getUrlResolver: GetUrlResolver): ContentScannerUrlProvider + } } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt index d4b9063931..a0b06a2e1e 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt @@ -17,7 +17,7 @@ import io.element.android.libraries.di.CacheDirectory import io.element.android.libraries.di.annotations.AppCoroutineScope import io.element.android.libraries.featureflag.api.FeatureFlagService import io.element.android.libraries.featureflag.api.FeatureFlags -import io.element.android.libraries.matrix.api.core.UserId +import io.element.android.libraries.matrix.api.core.SessionId import io.element.android.libraries.matrix.api.paths.SessionPaths import io.element.android.libraries.matrix.api.scanner.ContentScannerUrlProvider import io.element.android.libraries.matrix.impl.analytics.UtdTracker @@ -70,7 +70,7 @@ class RustMatrixClientFactory( private val clientBuilderProvider: ClientBuilderProvider, private val sqliteStoreBuilderProvider: SqliteStoreBuilderProvider, private val workManagerScheduler: WorkManagerScheduler, - private val contentScannerUrlProvider: ContentScannerUrlProvider, + private val contentScannerUrlProviderFactory: ContentScannerUrlProvider.Factory, ) { private val sessionDelegate = RustClientSessionDelegate( sessionStore = sessionStore, @@ -135,21 +135,19 @@ class RustMatrixClientFactory( client.setUtdDelegate(UtdTracker(analyticsService)) - val domainName = UserId(client.userId()).domainName // If a content scanner URL is available for the homeserver, create a RustContentScanner and set it on the client. // This allows the SDK to use the content scanner for automatic media scanning. // If no content scanner URL is available, the contentScanner will be null. - val contentScanner = domainName?.let { - contentScannerUrlProvider.getContentScannerUrl(domainName) - .getOrNull() - ?.let { contentScannerUrl -> - val contentScanner = ContentScanner(contentScannerUrl) - client.setContentScanner(contentScanner) - RustContentScanner( - client = client, - rustScanner = contentScanner, - ) - } + val contentScannerUrlProvider = contentScannerUrlProviderFactory.create(RustTemporaryMatrixClient(client, null)) + val contentScanner = contentScannerUrlProvider.getContentScannerUrl(SessionId(client.userId())) + .getOrNull() + ?.let { contentScannerUrl -> + val contentScanner = ContentScanner(contentScannerUrl) + client.setContentScanner(contentScanner) + RustContentScanner( + client = client, + rustScanner = contentScanner, + ) } val syncService = client.syncService() diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/RustMatrixAuthenticationService.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/RustMatrixAuthenticationService.kt index dd9f73c381..bef83c35ff 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/RustMatrixAuthenticationService.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/RustMatrixAuthenticationService.kt @@ -35,6 +35,7 @@ import io.element.android.libraries.matrix.api.paths.SessionPaths import io.element.android.libraries.matrix.api.verification.SessionVerifiedStatus import io.element.android.libraries.matrix.impl.ClientBuilderSlidingSync import io.element.android.libraries.matrix.impl.RustMatrixClientFactory +import io.element.android.libraries.matrix.impl.RustTemporaryMatrixClient import io.element.android.libraries.matrix.impl.auth.qrlogin.QrErrorMapper import io.element.android.libraries.matrix.impl.auth.qrlogin.SdkQrCodeLoginData import io.element.android.libraries.matrix.impl.auth.qrlogin.toStep @@ -133,9 +134,10 @@ class RustMatrixAuthenticationService( } currentClient = client + client.homeserverLoginDetails().map() }.onFailure { - clear() + clear(destroyClient = true) }.mapFailure { failure -> Timber.e(failure, "Failed to set homeserver to $homeserver") failure.mapAuthenticationException() @@ -168,7 +170,7 @@ class RustMatrixAuthenticationService( sessionStore.addSession(sessionData) // Clean up the strong reference held here since it's no longer necessary - currentClient = null + clear(destroyClient = false) SessionId(sessionData.userId) }.mapFailure { failure -> @@ -248,7 +250,7 @@ class RustMatrixAuthenticationService( sessionStore.addSession(sessionData) // Clean up the strong reference held here since it's no longer necessary - currentClient = null + clear(destroyClient = false) SessionId(sessionData.userId) } @@ -271,11 +273,13 @@ class RustMatrixAuthenticationService( deviceId = null, additionalScopes = emptyList(), ) + val getUrlResolver = RustTemporaryMatrixClient(client, sessionPaths) val url = oAuthAuthorizationData.loginUrl() .let { enterpriseService.tweakMasUrl( url = it, homeserver = client.server() ?: client.homeserver(), + getUrlResolver = getUrlResolver, ) } pendingOAuthAuthorizationData = oAuthAuthorizationData @@ -335,7 +339,7 @@ class RustMatrixAuthenticationService( sessionStore.addSession(sessionData) // Clean up the strong reference held here since it's no longer necessary - currentClient = null + clear(destroyClient = false) SessionId(sessionData.userId) }.mapFailure { failure -> @@ -398,7 +402,7 @@ class RustMatrixAuthenticationService( sessionStore.addSession(sessionData) // Clean up the strong reference held here since it's no longer necessary - currentClient = null + clear(destroyClient = false) SessionId(sessionData.userId) }.mapFailure { @@ -464,8 +468,10 @@ class RustMatrixAuthenticationService( .build() } - private fun clear() { - currentClient?.close() + private fun clear(destroyClient: Boolean) { + if (destroyClient) { + currentClient?.close() + } currentClient = null }