Identity: retrieve the default identity server url

This commit is contained in:
Benoit Marty 2020-05-13 16:12:03 +02:00
parent 60d80ea0ba
commit 225b1c380e
8 changed files with 52 additions and 13 deletions

View File

@ -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

View File

@ -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<String?>): Cancelable
fun getDefaultIdentityServer(): String?
/**
* Return the current identity server URL used by this account. Returns null if no identity server is configured.

View File

@ -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
)
}
}

View File

@ -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() {

View File

@ -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<Unit, Unit>
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
}
}

View File

@ -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<String?>): 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? {

View File

@ -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

View File

@ -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<SetIdentityServerViewModel, SetIdentityServerState> {
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()