Identity: rename a few class and add a mapper to avoid using Entities in the code

This commit is contained in:
Benoit Marty 2020-05-13 00:18:33 +02:00
parent 59d60813fb
commit e78fde4eca
17 changed files with 194 additions and 101 deletions

View File

@ -38,7 +38,7 @@ import im.vector.matrix.android.internal.di.AuthenticatedIdentity
import im.vector.matrix.android.internal.di.Unauthenticated
import im.vector.matrix.android.internal.network.RetrofitFactory
import im.vector.matrix.android.internal.session.SessionScope
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.todelete.AccountDataDataSource
import im.vector.matrix.android.internal.session.identity.todelete.observeNotNull
import im.vector.matrix.android.internal.session.openid.GetOpenIdTokenTask
@ -58,9 +58,9 @@ import javax.net.ssl.HttpsURLConnection
@SessionScope
internal class DefaultIdentityService @Inject constructor(
private val identityServiceStore: IdentityServiceStore,
private val identityStore: IdentityStore,
private val getOpenIdTokenTask: GetOpenIdTokenTask,
private val bulkLookupTask: BulkLookupTask,
private val identityBulkLookupTask: IdentityBulkLookupTask,
private val identityRegisterTask: IdentityRegisterTask,
private val identityPingTask: IdentityPingTask,
private val identityDisconnectTask: IdentityDisconnectTask,
@ -95,16 +95,16 @@ internal class DefaultIdentityService @Inject constructor(
}
// Init identityApi
updateIdentityAPI(identityServiceStore.getIdentityServerDetails()?.identityServerUrl)
updateIdentityAPI(identityStore.getIdentityData()?.identityServerUrl)
}
private fun notifyIdentityServerUrlChange(baseUrl: String?) {
// This is maybe not a real change (echo of account data we are just setting)
if (identityServiceStore.getIdentityServerDetails()?.identityServerUrl == baseUrl) {
if (identityStore.getIdentityData()?.identityServerUrl == baseUrl) {
Timber.d("Echo of local identity server url change, or no change")
} else {
// Url has changed, we have to reset our store, update internal configuration and notify listeners
identityServiceStore.setUrl(baseUrl)
identityStore.setUrl(baseUrl)
updateIdentityAPI(baseUrl)
listeners.toList().forEach { tryThis { it.onIdentityServerChange() } }
}
@ -121,7 +121,7 @@ internal class DefaultIdentityService @Inject constructor(
}
override fun getCurrentIdentityServerUrl(): String? {
return identityServiceStore.getIdentityServerDetails()?.identityServerUrl
return identityStore.getIdentityData()?.identityServerUrl
}
override fun startBindThreePid(threePid: ThreePid, callback: MatrixCallback<Unit>): Cancelable {
@ -137,7 +137,7 @@ internal class DefaultIdentityService @Inject constructor(
override fun cancelBindThreePid(threePid: ThreePid, callback: MatrixCallback<Unit>): Cancelable {
return GlobalScope.launchToCallback(coroutineDispatchers.main, callback) {
identityServiceStore.deletePendingBinding(threePid)
identityStore.deletePendingBinding(threePid)
}
}
@ -187,7 +187,7 @@ internal class DefaultIdentityService @Inject constructor(
return GlobalScope.launchToCallback(coroutineDispatchers.main, callback) {
identityDisconnectTask.execute(Unit)
identityServiceStore.setUrl(null)
identityStore.setUrl(null)
updateIdentityAPI(null)
updateAccountData(null)
}
@ -217,8 +217,8 @@ internal class DefaultIdentityService @Inject constructor(
// Try to get a token
val token = getNewIdentityServerToken(urlCandidate)
identityServiceStore.setUrl(urlCandidate)
identityServiceStore.setToken(token)
identityStore.setUrl(urlCandidate)
identityStore.setToken(token)
updateIdentityAPI(urlCandidate)
updateAccountData(urlCandidate)
@ -261,7 +261,7 @@ internal class DefaultIdentityService @Inject constructor(
threePids.associateWith { threePid ->
// If not in lookup result, check if there is a pending binding
if (lookupResult.firstOrNull { it.threePid == threePid } == null) {
if (identityServiceStore.getPendingBinding(threePid) == null) {
if (identityStore.getPendingBinding(threePid) == null) {
SharedState.NOT_SHARED
} else {
SharedState.BINDING_IN_PROGRESS
@ -277,12 +277,12 @@ internal class DefaultIdentityService @Inject constructor(
ensureToken()
return try {
bulkLookupTask.execute(BulkLookupTask.Params(threePids))
identityBulkLookupTask.execute(IdentityBulkLookupTask.Params(threePids))
} catch (throwable: Throwable) {
// Refresh token?
when {
throwable.isInvalidToken() && canRetry -> {
identityServiceStore.setToken(null)
identityStore.setToken(null)
lookUpInternal(false, threePids)
}
throwable.isTermsNotSigned() -> throw IdentityServiceError.TermsNotSignedException
@ -292,13 +292,13 @@ internal class DefaultIdentityService @Inject constructor(
}
private suspend fun ensureToken() {
val entity = identityServiceStore.getIdentityServerDetails() ?: throw IdentityServiceError.NoIdentityServerConfigured
val url = entity.identityServerUrl ?: throw IdentityServiceError.NoIdentityServerConfigured
val identityData = identityStore.getIdentityData() ?: throw IdentityServiceError.NoIdentityServerConfigured
val url = identityData.identityServerUrl ?: throw IdentityServiceError.NoIdentityServerConfigured
if (entity.token == null) {
if (identityData.token == null) {
// Try to get a token
val token = getNewIdentityServerToken(url)
identityServiceStore.setToken(token)
identityStore.setToken(token)
}
}

View File

@ -17,11 +17,11 @@
package im.vector.matrix.android.internal.session.identity
import im.vector.matrix.android.internal.network.token.AccessTokenProvider
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import javax.inject.Inject
internal class IdentityAccessTokenProvider @Inject constructor(
private val identityServiceStore: IdentityServiceStore
private val identityStore: IdentityStore
) : AccessTokenProvider {
override fun getToken() = identityServiceStore.getIdentityServerDetails()?.token
override fun getToken() = identityStore.getIdentityData()?.token
}

View File

@ -26,7 +26,7 @@ import im.vector.matrix.android.internal.crypto.attachments.MXEncryptedAttachmen
import im.vector.matrix.android.internal.crypto.tools.withOlmUtility
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.model.IdentityHashDetailResponse
import im.vector.matrix.android.internal.session.identity.model.IdentityLookUpParams
import im.vector.matrix.android.internal.session.identity.model.IdentityLookUpResponse
@ -34,27 +34,27 @@ import im.vector.matrix.android.internal.task.Task
import java.util.Locale
import javax.inject.Inject
internal interface BulkLookupTask : Task<BulkLookupTask.Params, List<FoundThreePid>> {
internal interface IdentityBulkLookupTask : Task<IdentityBulkLookupTask.Params, List<FoundThreePid>> {
data class Params(
val threePids: List<ThreePid>
)
}
internal class DefaultBulkLookupTask @Inject constructor(
internal class DefaultIdentityBulkLookupTask @Inject constructor(
private val identityApiProvider: IdentityApiProvider,
private val identityServiceStore: IdentityServiceStore,
private val identityStore: IdentityStore,
@UserId private val userId: String
) : BulkLookupTask {
) : IdentityBulkLookupTask {
override suspend fun execute(params: BulkLookupTask.Params): List<FoundThreePid> {
override suspend fun execute(params: IdentityBulkLookupTask.Params): List<FoundThreePid> {
val identityAPI = getIdentityApiAndEnsureTerms(identityApiProvider, userId)
val entity = identityServiceStore.getIdentityServerDetails() ?: throw IdentityServiceError.NoIdentityServerConfigured
val pepper = entity.hashLookupPepper
val identityData = identityStore.getIdentityData() ?: throw IdentityServiceError.NoIdentityServerConfigured
val pepper = identityData.hashLookupPepper
val hashDetailResponse = if (pepper == null) {
// We need to fetch the hash details first
fetchAndStoreHashDetails(identityAPI)
} else {
IdentityHashDetailResponse(pepper, entity.hashLookupAlgorithm.toList())
IdentityHashDetailResponse(pepper, identityData.hashLookupAlgorithm)
}
if (hashDetailResponse.algorithms.contains("sha256").not()) {
@ -97,7 +97,7 @@ internal class DefaultBulkLookupTask @Inject constructor(
if (!failure.error.newLookupPepper.isNullOrEmpty()) {
// Store it and use it right now
hashDetailResponse.copy(pepper = failure.error.newLookupPepper)
.also { identityServiceStore.setHashDetails(it) }
.also { identityStore.setHashDetails(it) }
.let { lookUpInternal(identityAPI, hashedAddresses, it, false /* Avoid infinite loop */) }
} else {
// Retrieve the new hash details
@ -122,7 +122,7 @@ internal class DefaultBulkLookupTask @Inject constructor(
return executeRequest<IdentityHashDetailResponse>(null) {
apiCall = identityAPI.hashDetails()
}
.also { identityServiceStore.setHashDetails(it) }
.also { identityStore.setHashDetails(it) }
}
private fun handleSuccess(threePids: List<ThreePid>, hashedAddresses: List<String>, identityLookUpResponse: IdentityLookUpResponse): List<FoundThreePid> {

View File

@ -30,8 +30,8 @@ import im.vector.matrix.android.internal.network.token.AccessTokenProvider
import im.vector.matrix.android.internal.session.SessionModule
import im.vector.matrix.android.internal.session.SessionScope
import im.vector.matrix.android.internal.session.identity.db.IdentityRealmModule
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.db.RealmIdentityServiceStore
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.db.RealmIdentityStore
import io.realm.RealmConfiguration
import okhttp3.OkHttpClient
import java.io.File
@ -75,7 +75,7 @@ internal abstract class IdentityModule {
abstract fun bindAccessTokenProvider(provider: IdentityAccessTokenProvider): AccessTokenProvider
@Binds
abstract fun bindIdentityServiceStore(store: RealmIdentityServiceStore): IdentityServiceStore
abstract fun bindIdentityStore(store: RealmIdentityStore): IdentityStore
@Binds
abstract fun bindIdentityPingTask(task: DefaultIdentityPingTask): IdentityPingTask
@ -90,7 +90,7 @@ internal abstract class IdentityModule {
abstract fun bindIdentitySubmitTokenForBindingTask(task: DefaultIdentitySubmitTokenForBindingTask): IdentitySubmitTokenForBindingTask
@Binds
abstract fun bindBulkLookupTask(task: DefaultBulkLookupTask): BulkLookupTask
abstract fun bindIdentityBulkLookupTask(task: DefaultIdentityBulkLookupTask): IdentityBulkLookupTask
@Binds
abstract fun bindIdentityDisconnectTask(task: DefaultIdentityDisconnectTask): IdentityDisconnectTask

View File

@ -21,7 +21,8 @@ import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.api.session.identity.getCountryCode
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.data.IdentityPendingBinding
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.model.IdentityRequestTokenForEmailBody
import im.vector.matrix.android.internal.session.identity.model.IdentityRequestTokenForMsisdnBody
import im.vector.matrix.android.internal.session.identity.model.IdentityRequestTokenResponse
@ -39,21 +40,21 @@ internal interface IdentityRequestTokenForBindingTask : Task<IdentityRequestToke
internal class DefaultIdentityRequestTokenForBindingTask @Inject constructor(
private val identityApiProvider: IdentityApiProvider,
private val identityServiceStore: IdentityServiceStore,
private val identityStore: IdentityStore,
@UserId private val userId: String
) : IdentityRequestTokenForBindingTask {
override suspend fun execute(params: IdentityRequestTokenForBindingTask.Params) {
val identityAPI = getIdentityApiAndEnsureTerms(identityApiProvider, userId)
val pendingBindingEntity = identityServiceStore.getPendingBinding(params.threePid)
val identityPendingBinding = identityStore.getPendingBinding(params.threePid)
if (params.sendAgain && pendingBindingEntity == null) {
if (params.sendAgain && identityPendingBinding == null) {
throw IdentityServiceError.NoCurrentBindingError
}
val clientSecret = pendingBindingEntity?.clientSecret ?: UUID.randomUUID().toString()
val sendAttempt = pendingBindingEntity?.sendAttempt?.inc() ?: 1
val clientSecret = identityPendingBinding?.clientSecret ?: UUID.randomUUID().toString()
val sendAttempt = identityPendingBinding?.sendAttempt?.inc() ?: 1
val tokenResponse = executeRequest<IdentityRequestTokenResponse>(null) {
apiCall = when (params.threePid) {
@ -73,11 +74,14 @@ internal class DefaultIdentityRequestTokenForBindingTask @Inject constructor(
}
}
// Store client secret and sid
identityServiceStore.storePendingBinding(
// Store client secret, send attempt and sid
identityStore.storePendingBinding(
params.threePid,
clientSecret,
sendAttempt,
tokenResponse.sid)
IdentityPendingBinding(
clientSecret = clientSecret,
sendAttempt = sendAttempt,
sid = tokenResponse.sid
)
)
}
}

View File

@ -22,7 +22,7 @@ import im.vector.matrix.android.api.session.identity.toMedium
import im.vector.matrix.android.internal.auth.registration.SuccessResult
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.model.IdentityRequestOwnershipParams
import im.vector.matrix.android.internal.task.Task
import javax.inject.Inject
@ -36,20 +36,20 @@ internal interface IdentitySubmitTokenForBindingTask : Task<IdentitySubmitTokenF
internal class DefaultIdentitySubmitTokenForBindingTask @Inject constructor(
private val identityApiProvider: IdentityApiProvider,
private val identityServiceStore: IdentityServiceStore,
private val identityStore: IdentityStore,
@UserId private val userId: String
) : IdentitySubmitTokenForBindingTask {
override suspend fun execute(params: IdentitySubmitTokenForBindingTask.Params) {
val identityAPI = getIdentityApiAndEnsureTerms(identityApiProvider, userId)
val pendingThreePid = identityServiceStore.getPendingBinding(params.threePid) ?: throw IdentityServiceError.NoCurrentBindingError
val identityPendingBinding = identityStore.getPendingBinding(params.threePid) ?: throw IdentityServiceError.NoCurrentBindingError
val tokenResponse = executeRequest<SuccessResult>(null) {
apiCall = identityAPI.submitToken(
params.threePid.toMedium(),
IdentityRequestOwnershipParams(
clientSecret = pendingThreePid.clientSecret,
sid = pendingThreePid.sid,
clientSecret = identityPendingBinding.clientSecret,
sid = identityPendingBinding.sid,
token = params.token
))
}

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2020 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.session.identity.data
internal data class IdentityData(
val identityServerUrl: String?,
val token: String?,
val hashLookupPepper: String?,
val hashLookupAlgorithm: List<String>
)

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2020 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.session.identity.data
internal data class IdentityPendingBinding(
/* Managed by Riot */
val clientSecret: String,
/* Managed by Riot */
val sendAttempt: Int,
/* Provided by the identity server */
val sid: String
)

View File

@ -14,14 +14,14 @@
* limitations under the License.
*/
package im.vector.matrix.android.internal.session.identity.db
package im.vector.matrix.android.internal.session.identity.data
import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.internal.session.identity.model.IdentityHashDetailResponse
internal interface IdentityServiceStore {
internal interface IdentityStore {
fun getIdentityServerDetails(): IdentityServerEntity?
fun getIdentityData(): IdentityData?
fun setUrl(url: String?)
@ -32,16 +32,13 @@ internal interface IdentityServiceStore {
/**
* Store details about a current binding
*/
fun storePendingBinding(threePid: ThreePid,
clientSecret: String,
sendAttempt: Int,
sid: String)
fun storePendingBinding(threePid: ThreePid, data: IdentityPendingBinding)
fun getPendingBinding(threePid: ThreePid): IdentityPendingBindingEntity?
fun getPendingBinding(threePid: ThreePid): IdentityPendingBinding?
fun deletePendingBinding(threePid: ThreePid)
}
internal fun IdentityServiceStore.getIdentityServerUrlWithoutProtocol(): String? {
return getIdentityServerDetails()?.identityServerUrl?.substringAfter("://")
internal fun IdentityStore.getIdentityServerUrlWithoutProtocol(): String? {
return getIdentityData()?.identityServerUrl?.substringAfter("://")
}

View File

@ -19,7 +19,7 @@ package im.vector.matrix.android.internal.session.identity.db
import io.realm.RealmList
import io.realm.RealmObject
internal open class IdentityServerEntity(
internal open class IdentityDataEntity(
var identityServerUrl: String? = null,
var token: String? = null,
var hashLookupPepper: String? = null,

View File

@ -24,17 +24,17 @@ import io.realm.kotlin.where
/**
* Only one object can be stored at a time
*/
internal fun IdentityServerEntity.Companion.get(realm: Realm): IdentityServerEntity? {
return realm.where<IdentityServerEntity>().findFirst()
internal fun IdentityDataEntity.Companion.get(realm: Realm): IdentityDataEntity? {
return realm.where<IdentityDataEntity>().findFirst()
}
private fun IdentityServerEntity.Companion.getOrCreate(realm: Realm): IdentityServerEntity {
private fun IdentityDataEntity.Companion.getOrCreate(realm: Realm): IdentityDataEntity {
return get(realm) ?: realm.createObject()
}
internal fun IdentityServerEntity.Companion.setUrl(realm: Realm,
url: String?) {
realm.where<IdentityServerEntity>().findAll().deleteAllFromRealm()
internal fun IdentityDataEntity.Companion.setUrl(realm: Realm,
url: String?) {
realm.where<IdentityDataEntity>().findAll().deleteAllFromRealm()
// Delete all pending binding if any
IdentityPendingBindingEntity.deleteAll(realm)
@ -45,16 +45,16 @@ internal fun IdentityServerEntity.Companion.setUrl(realm: Realm,
}
}
internal fun IdentityServerEntity.Companion.setToken(realm: Realm,
newToken: String?) {
internal fun IdentityDataEntity.Companion.setToken(realm: Realm,
newToken: String?) {
get(realm)?.apply {
token = newToken
}
}
internal fun IdentityServerEntity.Companion.setHashDetails(realm: Realm,
pepper: String,
algorithms: List<String>) {
internal fun IdentityDataEntity.Companion.setHashDetails(realm: Realm,
pepper: String,
algorithms: List<String>) {
get(realm)?.apply {
hashLookupPepper = pepper
hashLookupAlgorithm = RealmList<String>().apply { addAll(algorithms) }

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.matrix.android.internal.session.identity.db
import im.vector.matrix.android.internal.session.identity.data.IdentityData
import im.vector.matrix.android.internal.session.identity.data.IdentityPendingBinding
internal object IdentityMapper {
fun map(entity: IdentityDataEntity): IdentityData {
return IdentityData(
identityServerUrl = entity.identityServerUrl,
token = entity.token,
hashLookupPepper = entity.hashLookupPepper,
hashLookupAlgorithm = entity.hashLookupAlgorithm.toList()
)
}
fun map(entity: IdentityPendingBindingEntity): IdentityPendingBinding {
return IdentityPendingBinding(
clientSecret = entity.clientSecret,
sendAttempt = entity.sendAttempt,
sid = entity.sid
)
}
}

View File

@ -23,7 +23,7 @@ import io.realm.annotations.RealmModule
*/
@RealmModule(library = true,
classes = [
IdentityServerEntity::class,
IdentityDataEntity::class,
IdentityPendingBindingEntity::class
])
internal class IdentityRealmModule

View File

@ -19,27 +19,30 @@ package im.vector.matrix.android.internal.session.identity.db
import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.internal.di.IdentityDatabase
import im.vector.matrix.android.internal.session.SessionScope
import im.vector.matrix.android.internal.session.identity.data.IdentityPendingBinding
import im.vector.matrix.android.internal.session.identity.data.IdentityData
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.model.IdentityHashDetailResponse
import io.realm.Realm
import io.realm.RealmConfiguration
import javax.inject.Inject
@SessionScope
internal class RealmIdentityServiceStore @Inject constructor(
internal class RealmIdentityStore @Inject constructor(
@IdentityDatabase
private val realmConfiguration: RealmConfiguration
) : IdentityServiceStore {
) : IdentityStore {
override fun getIdentityServerDetails(): IdentityServerEntity? {
override fun getIdentityData(): IdentityData? {
return Realm.getInstance(realmConfiguration).use { realm ->
IdentityServerEntity.get(realm)?.let { realm.copyFromRealm(it) }
IdentityDataEntity.get(realm)?.let { IdentityMapper.map(it) }
}
}
override fun setUrl(url: String?) {
Realm.getInstance(realmConfiguration).use {
it.executeTransaction { realm ->
IdentityServerEntity.setUrl(realm, url)
IdentityDataEntity.setUrl(realm, url)
}
}
}
@ -47,7 +50,7 @@ internal class RealmIdentityServiceStore @Inject constructor(
override fun setToken(token: String?) {
Realm.getInstance(realmConfiguration).use {
it.executeTransaction { realm ->
IdentityServerEntity.setToken(realm, token)
IdentityDataEntity.setToken(realm, token)
}
}
}
@ -55,26 +58,26 @@ internal class RealmIdentityServiceStore @Inject constructor(
override fun setHashDetails(hashDetailResponse: IdentityHashDetailResponse) {
Realm.getInstance(realmConfiguration).use {
it.executeTransaction { realm ->
IdentityServerEntity.setHashDetails(realm, hashDetailResponse.pepper, hashDetailResponse.algorithms)
IdentityDataEntity.setHashDetails(realm, hashDetailResponse.pepper, hashDetailResponse.algorithms)
}
}
}
override fun storePendingBinding(threePid: ThreePid, clientSecret: String, sendAttempt: Int, sid: String) {
override fun storePendingBinding(threePid: ThreePid, data: IdentityPendingBinding) {
Realm.getInstance(realmConfiguration).use {
it.executeTransaction { realm ->
IdentityPendingBindingEntity.getOrCreate(realm, threePid).let { entity ->
entity.clientSecret = clientSecret
entity.sendAttempt = sendAttempt
entity.sid = sid
entity.clientSecret = data.clientSecret
entity.sendAttempt = data.sendAttempt
entity.sid = data.sid
}
}
}
}
override fun getPendingBinding(threePid: ThreePid): IdentityPendingBindingEntity? {
override fun getPendingBinding(threePid: ThreePid): IdentityPendingBinding? {
return Realm.getInstance(realmConfiguration).use { realm ->
IdentityPendingBindingEntity.get(realm, threePid)?.let { realm.copyFromRealm(it) }
IdentityPendingBindingEntity.get(realm, threePid)?.let { IdentityMapper.map(it) }
}
}

View File

@ -21,8 +21,8 @@ import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.internal.di.AuthenticatedIdentity
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.network.token.AccessTokenProvider
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.db.getIdentityServerUrlWithoutProtocol
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.data.getIdentityServerUrlWithoutProtocol
import im.vector.matrix.android.internal.task.Task
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject
@ -34,27 +34,26 @@ internal abstract class BindThreePidsTask : Task<BindThreePidsTask.Params, Unit>
}
internal class DefaultBindThreePidsTask @Inject constructor(private val profileAPI: ProfileAPI,
private val identityServiceStore: IdentityServiceStore,
private val identityStore: IdentityStore,
@AuthenticatedIdentity
private val accessTokenProvider: AccessTokenProvider,
private val eventBus: EventBus) : BindThreePidsTask() {
override suspend fun execute(params: Params) {
val identityServerUrlWithoutProtocol = identityServiceStore.getIdentityServerUrlWithoutProtocol()
?: throw IdentityServiceError.NoIdentityServerConfigured
val identityServerUrlWithoutProtocol = identityStore.getIdentityServerUrlWithoutProtocol() ?: throw IdentityServiceError.NoIdentityServerConfigured
val identityServerAccessToken = accessTokenProvider.getToken() ?: throw IdentityServiceError.NoIdentityServerConfigured
val pendingThreePid = identityServiceStore.getPendingBinding(params.threePid) ?: throw IdentityServiceError.NoCurrentBindingError
val identityPendingBinding = identityStore.getPendingBinding(params.threePid) ?: throw IdentityServiceError.NoCurrentBindingError
executeRequest<Unit>(eventBus) {
apiCall = profileAPI.bindThreePid(
BindThreePidBody(
clientSecret = pendingThreePid.clientSecret,
clientSecret = identityPendingBinding.clientSecret,
identityServerUrlWithoutProtocol = identityServerUrlWithoutProtocol,
identityServerAccessToken = identityServerAccessToken,
sid = pendingThreePid.sid
sid = identityPendingBinding.sid
))
}
// Binding is over, cleanup the store
identityServiceStore.deletePendingBinding(params.threePid)
identityStore.deletePendingBinding(params.threePid)
}
}

View File

@ -20,8 +20,8 @@ import im.vector.matrix.android.api.session.identity.IdentityServiceError
import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.api.session.identity.toMedium
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.identity.db.IdentityServiceStore
import im.vector.matrix.android.internal.session.identity.db.getIdentityServerUrlWithoutProtocol
import im.vector.matrix.android.internal.session.identity.data.IdentityStore
import im.vector.matrix.android.internal.session.identity.data.getIdentityServerUrlWithoutProtocol
import im.vector.matrix.android.internal.task.Task
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject
@ -33,10 +33,10 @@ internal abstract class UnbindThreePidsTask : Task<UnbindThreePidsTask.Params, B
}
internal class DefaultUnbindThreePidsTask @Inject constructor(private val profileAPI: ProfileAPI,
private val identityServiceStore: IdentityServiceStore,
private val identityStore: IdentityStore,
private val eventBus: EventBus) : UnbindThreePidsTask() {
override suspend fun execute(params: Params): Boolean {
val identityServerUrlWithoutProtocol = identityServiceStore.getIdentityServerUrlWithoutProtocol()
val identityServerUrlWithoutProtocol = identityStore.getIdentityServerUrlWithoutProtocol()
?: throw IdentityServiceError.NoIdentityServerConfigured
return executeRequest<UnbindThreePidResponse>(eventBus) {