From 225b1c380e42aa97aa6c58c79eac23d42b070fb8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 13 May 2020 16:12:03 +0200 Subject: [PATCH] Identity: retrieve the default identity server url --- .../homeserver/HomeServerCapabilities.kt | 6 +++++- .../api/session/identity/IdentityService.kt | 5 +++-- .../mapper/HomeServerCapabilitiesMapper.kt | 5 +++-- .../model/HomeServerCapabilitiesEntity.kt | 1 + .../DefaultGetHomeServerCapabilitiesTask.kt | 21 ++++++++++++++++--- .../identity/DefaultIdentityService.kt | 17 ++++++++++----- .../change/SetIdentityServerState.kt | 1 + .../change/SetIdentityServerViewModel.kt | 9 ++++++++ 8 files changed, 52 insertions(+), 13 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt index 5a8770f262..1c2b8de83b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt @@ -28,7 +28,11 @@ data class HomeServerCapabilities( /** * Last version identity server and binding supported */ - val lastVersionIdentityServerSupported: Boolean = false + val lastVersionIdentityServerSupported: Boolean = false, + /** + * Default identity server url, provided in Wellknown + */ + val defaultIdentityServerUrl: String? = null ) { companion object { const val MAX_UPLOAD_FILE_SIZE_UNKNOWN = -1L diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/identity/IdentityService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/identity/IdentityService.kt index 221bb68386..2f2821d7a8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/identity/IdentityService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/identity/IdentityService.kt @@ -24,10 +24,11 @@ import im.vector.matrix.android.api.util.Cancelable */ interface IdentityService { /** - * Return the default identity server of the homeserver (using Wellknown request). + * Return the default identity server of the user, which may have been provided at login time by the homeserver, + * or by the Well-known setup of the homeserver * It may be different from the current configured identity server */ - fun getDefaultIdentityServer(callback: MatrixCallback): Cancelable + fun getDefaultIdentityServer(): String? /** * Return the current identity server URL used by this account. Returns null if no identity server is configured. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt index 212db3f653..a66f587cec 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt @@ -20,7 +20,7 @@ import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilities import im.vector.matrix.android.internal.database.model.HomeServerCapabilitiesEntity /** - * HomeServerCapabilitiesEntity <-> HomeSeverCapabilities + * HomeServerCapabilitiesEntity -> HomeSeverCapabilities */ internal object HomeServerCapabilitiesMapper { @@ -28,7 +28,8 @@ internal object HomeServerCapabilitiesMapper { return HomeServerCapabilities( canChangePassword = entity.canChangePassword, maxUploadFileSize = entity.maxUploadFileSize, - lastVersionIdentityServerSupported = entity.lastVersionIdentityServerSupported + lastVersionIdentityServerSupported = entity.lastVersionIdentityServerSupported, + defaultIdentityServerUrl = entity.defaultIdentityServerUrl ) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt index 1fe22e4960..a6b250b8fa 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt @@ -23,6 +23,7 @@ internal open class HomeServerCapabilitiesEntity( var canChangePassword: Boolean = true, var maxUploadFileSize: Long = HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN, var lastVersionIdentityServerSupported: Boolean = false, + var defaultIdentityServerUrl: String? = null, var lastUpdatedTimestamp: Long = 0L ) : RealmObject() { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt index bf8f9f272f..20d1e12a17 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt @@ -19,9 +19,12 @@ package im.vector.matrix.android.internal.session.homeserver import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.auth.data.Versions import im.vector.matrix.android.api.auth.data.isLoginAndRegistrationSupportedBySdk +import im.vector.matrix.android.api.auth.wellknown.WellknownResult import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilities +import im.vector.matrix.android.internal.auth.wellknown.GetWellknownTask import im.vector.matrix.android.internal.database.model.HomeServerCapabilitiesEntity import im.vector.matrix.android.internal.database.query.getOrCreate +import im.vector.matrix.android.internal.di.UserId import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.util.awaitTransaction @@ -34,7 +37,10 @@ internal interface GetHomeServerCapabilitiesTask : Task internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( private val capabilitiesAPI: CapabilitiesAPI, private val monarchy: Monarchy, - private val eventBus: EventBus + private val eventBus: EventBus, + private val getWellknownTask: GetWellknownTask, + @UserId + private val userId: String ) : GetHomeServerCapabilitiesTask { override suspend fun execute(params: Unit) { @@ -67,12 +73,17 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( } }.getOrNull() - insertInDb(capabilities, uploadCapabilities, versions) + val wellknownResult = runCatching { + getWellknownTask.execute(GetWellknownTask.Params(userId)) + }.getOrNull() + + insertInDb(capabilities, uploadCapabilities, versions, wellknownResult) } private suspend fun insertInDb(getCapabilitiesResult: GetCapabilitiesResult?, getUploadCapabilitiesResult: GetUploadCapabilitiesResult?, - getVersionResult: Versions?) { + getVersionResult: Versions?, + getWellknownResult: WellknownResult?) { monarchy.awaitTransaction { realm -> val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm) @@ -89,6 +100,10 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( homeServerCapabilitiesEntity.lastVersionIdentityServerSupported = getVersionResult.isLoginAndRegistrationSupportedBySdk() } + if (getWellknownResult != null && getWellknownResult is WellknownResult.Prompt) { + homeServerCapabilitiesEntity.defaultIdentityServerUrl = getWellknownResult.identityServerUrl + } + homeServerCapabilitiesEntity.lastUpdatedTimestamp = Date().time } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/identity/DefaultIdentityService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/identity/DefaultIdentityService.kt index ba7b6c2c1d..c12e68fd26 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/identity/DefaultIdentityService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/identity/DefaultIdentityService.kt @@ -21,6 +21,7 @@ import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleRegistry import dagger.Lazy import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.extensions.tryThis import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.failure.MatrixError @@ -77,7 +78,8 @@ internal class DefaultIdentityService @Inject constructor( private val unbindThreePidsTask: UnbindThreePidsTask, private val identityApiProvider: IdentityApiProvider, private val accountDataDataSource: AccountDataDataSource, - private val homeServerCapabilitiesService: HomeServerCapabilitiesService + private val homeServerCapabilitiesService: HomeServerCapabilitiesService, + private val sessionParams: SessionParams ) : IdentityService { private val lifecycleOwner: LifecycleOwner = LifecycleOwner { lifecycleRegistry } @@ -114,10 +116,15 @@ internal class DefaultIdentityService @Inject constructor( lifecycleRegistry.currentState = Lifecycle.State.DESTROYED } - override fun getDefaultIdentityServer(callback: MatrixCallback): Cancelable { - // TODO Use Wellknown request, but waiting for PR about Wellknown to be merged - callback.onSuccess(null) - return NoOpCancellable + /** + * First return the identity server provided during login phase. + * If null, provide the one in wellknown configuration of the homeserver + * Else return null + */ + override fun getDefaultIdentityServer(): String? { + return sessionParams.defaultIdentityServerUrl + ?.takeIf { it.isNotEmpty() } + ?: homeServerCapabilitiesService.getHomeServerCapabilities().defaultIdentityServerUrl } override fun getCurrentIdentityServerUrl(): String? { diff --git a/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerState.kt b/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerState.kt index 45d348f938..bf703fd8e9 100644 --- a/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerState.kt +++ b/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerState.kt @@ -20,6 +20,7 @@ import androidx.annotation.StringRes import com.airbnb.mvrx.MvRxState data class SetIdentityServerState( + // At first, will contain the default identity server url if any val newIdentityServerUrl: String? = null, @StringRes val errorMessageId: Int? = null, val isVerifyingServer: Boolean = false diff --git a/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerViewModel.kt b/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerViewModel.kt index 386cd7be30..29eea88cc9 100644 --- a/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/discovery/change/SetIdentityServerViewModel.kt @@ -27,6 +27,7 @@ import im.vector.matrix.android.api.session.identity.IdentityServiceError import im.vector.matrix.android.api.session.terms.GetTermsResponse import im.vector.matrix.android.api.session.terms.TermsService import im.vector.riotx.R +import im.vector.riotx.core.di.HasScreenInjector import im.vector.riotx.core.extensions.exhaustive import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.resources.StringProvider @@ -44,6 +45,14 @@ class SetIdentityServerViewModel @AssistedInject constructor( companion object : MvRxViewModelFactory { + override fun initialState(viewModelContext: ViewModelContext): SetIdentityServerState? { + val session = (viewModelContext.activity as HasScreenInjector).injector().activeSessionHolder().getActiveSession() + + return SetIdentityServerState( + newIdentityServerUrl = session.identityService().getDefaultIdentityServer() + ) + } + @JvmStatic override fun create(viewModelContext: ViewModelContext, state: SetIdentityServerState): SetIdentityServerViewModel? { val fragment: SetIdentityServerFragment = (viewModelContext as FragmentViewModelContext).fragment()