Inject userId and deviceId instead of sessionParam
This commit is contained in:
parent
e6d5d0f9c6
commit
63eb119df6
@ -23,7 +23,6 @@ import io.realm.Realm
|
|||||||
import io.realm.RealmConfiguration
|
import io.realm.RealmConfiguration
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
import io.realm.kotlin.where
|
import io.realm.kotlin.where
|
||||||
import org.matrix.android.sdk.api.auth.data.Credentials
|
|
||||||
import org.matrix.android.sdk.api.session.crypto.crosssigning.MXCrossSigningInfo
|
import org.matrix.android.sdk.api.session.crypto.crosssigning.MXCrossSigningInfo
|
||||||
import org.matrix.android.sdk.api.session.events.model.Event
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
import org.matrix.android.sdk.api.session.events.model.LocalEcho
|
import org.matrix.android.sdk.api.session.events.model.LocalEcho
|
||||||
@ -86,7 +85,9 @@ import org.matrix.android.sdk.internal.crypto.store.db.query.getById
|
|||||||
import org.matrix.android.sdk.internal.crypto.store.db.query.getOrCreate
|
import org.matrix.android.sdk.internal.crypto.store.db.query.getOrCreate
|
||||||
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
|
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
|
||||||
import org.matrix.android.sdk.internal.di.CryptoDatabase
|
import org.matrix.android.sdk.internal.di.CryptoDatabase
|
||||||
|
import org.matrix.android.sdk.internal.di.DeviceId
|
||||||
import org.matrix.android.sdk.internal.di.MoshiProvider
|
import org.matrix.android.sdk.internal.di.MoshiProvider
|
||||||
|
import org.matrix.android.sdk.internal.di.UserId
|
||||||
import org.matrix.android.sdk.internal.session.SessionScope
|
import org.matrix.android.sdk.internal.session.SessionScope
|
||||||
import org.matrix.olm.OlmAccount
|
import org.matrix.olm.OlmAccount
|
||||||
import org.matrix.olm.OlmException
|
import org.matrix.olm.OlmException
|
||||||
@ -98,7 +99,9 @@ import kotlin.collections.set
|
|||||||
internal class RealmCryptoStore @Inject constructor(
|
internal class RealmCryptoStore @Inject constructor(
|
||||||
@CryptoDatabase private val realmConfiguration: RealmConfiguration,
|
@CryptoDatabase private val realmConfiguration: RealmConfiguration,
|
||||||
private val crossSigningKeysMapper: CrossSigningKeysMapper,
|
private val crossSigningKeysMapper: CrossSigningKeysMapper,
|
||||||
private val credentials: Credentials) : IMXCryptoStore {
|
@UserId private val userId: String,
|
||||||
|
@DeviceId private val deviceId: String?
|
||||||
|
) : IMXCryptoStore {
|
||||||
|
|
||||||
/* ==========================================================================================
|
/* ==========================================================================================
|
||||||
* Memory cache, to correctly release JNI objects
|
* Memory cache, to correctly release JNI objects
|
||||||
@ -141,8 +144,8 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
// Check credentials
|
// Check credentials
|
||||||
// The device id may not have been provided in credentials.
|
// The device id may not have been provided in credentials.
|
||||||
// Check it only if provided, else trust the stored one.
|
// Check it only if provided, else trust the stored one.
|
||||||
if (currentMetadata.userId != credentials.userId
|
if (currentMetadata.userId != userId
|
||||||
|| (credentials.deviceId != null && credentials.deviceId != currentMetadata.deviceId)) {
|
|| (deviceId != null && deviceId != currentMetadata.deviceId)) {
|
||||||
Timber.w("## open() : Credentials do not match, close this store and delete data")
|
Timber.w("## open() : Credentials do not match, close this store and delete data")
|
||||||
deleteAll = true
|
deleteAll = true
|
||||||
currentMetadata = null
|
currentMetadata = null
|
||||||
@ -155,8 +158,8 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Metadata not found, or database cleaned, create it
|
// Metadata not found, or database cleaned, create it
|
||||||
realm.createObject(CryptoMetadataEntity::class.java, credentials.userId).apply {
|
realm.createObject(CryptoMetadataEntity::class.java, userId).apply {
|
||||||
deviceId = credentials.deviceId
|
deviceId = this@RealmCryptoStore.deviceId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -312,7 +315,7 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
Timber.d("## CrossSigning MSK change for $userId")
|
Timber.d("## CrossSigning MSK change for $userId")
|
||||||
val keyEntity = crossSigningKeysMapper.map(masterKey)
|
val keyEntity = crossSigningKeysMapper.map(masterKey)
|
||||||
signingInfo.setMasterKey(keyEntity)
|
signingInfo.setMasterKey(keyEntity)
|
||||||
if (userId == credentials.userId) {
|
if (userId == this.userId) {
|
||||||
shouldResetMyDevicesLocalTrust = true
|
shouldResetMyDevicesLocalTrust = true
|
||||||
// my msk has changed! clear my private key
|
// my msk has changed! clear my private key
|
||||||
// Could we have some race here? e.g I am the one that did change the keys
|
// Could we have some race here? e.g I am the one that did change the keys
|
||||||
@ -331,7 +334,7 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
Timber.d("## CrossSigning SSK change for $userId")
|
Timber.d("## CrossSigning SSK change for $userId")
|
||||||
val keyEntity = crossSigningKeysMapper.map(selfSigningKey)
|
val keyEntity = crossSigningKeysMapper.map(selfSigningKey)
|
||||||
signingInfo.setSelfSignedKey(keyEntity)
|
signingInfo.setSelfSignedKey(keyEntity)
|
||||||
if (userId == credentials.userId) {
|
if (userId == this.userId) {
|
||||||
shouldResetMyDevicesLocalTrust = true
|
shouldResetMyDevicesLocalTrust = true
|
||||||
// my ssk has changed! clear my private key
|
// my ssk has changed! clear my private key
|
||||||
realm.where<CryptoMetadataEntity>().findFirst()?.apply {
|
realm.where<CryptoMetadataEntity>().findFirst()?.apply {
|
||||||
@ -349,7 +352,7 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
Timber.d("## CrossSigning USK change for $userId")
|
Timber.d("## CrossSigning USK change for $userId")
|
||||||
val keyEntity = crossSigningKeysMapper.map(userSigningKey)
|
val keyEntity = crossSigningKeysMapper.map(userSigningKey)
|
||||||
signingInfo.setUserSignedKey(keyEntity)
|
signingInfo.setUserSignedKey(keyEntity)
|
||||||
if (userId == credentials.userId) {
|
if (userId == this.userId) {
|
||||||
shouldResetMyDevicesLocalTrust = true
|
shouldResetMyDevicesLocalTrust = true
|
||||||
// my usk has changed! clear my private key
|
// my usk has changed! clear my private key
|
||||||
realm.where<CryptoMetadataEntity>().findFirst()?.apply {
|
realm.where<CryptoMetadataEntity>().findFirst()?.apply {
|
||||||
@ -362,11 +365,11 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
// When my cross signing keys are reset, we consider clearing all existing device trust
|
// When my cross signing keys are reset, we consider clearing all existing device trust
|
||||||
if (shouldResetMyDevicesLocalTrust) {
|
if (shouldResetMyDevicesLocalTrust) {
|
||||||
realm.where<UserEntity>()
|
realm.where<UserEntity>()
|
||||||
.equalTo(UserEntityFields.USER_ID, credentials.userId)
|
.equalTo(UserEntityFields.USER_ID, this.userId)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
?.devices?.forEach {
|
?.devices?.forEach {
|
||||||
it?.trustLevelEntity?.crossSignedVerified = false
|
it?.trustLevelEntity?.crossSignedVerified = false
|
||||||
it?.trustLevelEntity?.locallyVerified = it.deviceId == credentials.deviceId
|
it?.trustLevelEntity?.locallyVerified = it.deviceId == deviceId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
userEntity.crossSigningInfoEntity = signingInfo
|
userEntity.crossSigningInfoEntity = signingInfo
|
||||||
@ -1355,7 +1358,7 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
.findAll()
|
.findAll()
|
||||||
xInfoEntities?.forEach { info ->
|
xInfoEntities?.forEach { info ->
|
||||||
// Need to ignore mine
|
// Need to ignore mine
|
||||||
if (info.userId != credentials.userId) {
|
if (info.userId != userId) {
|
||||||
info.crossSigningKeys.forEach {
|
info.crossSigningKeys.forEach {
|
||||||
it.trustLevelEntity = null
|
it.trustLevelEntity = null
|
||||||
}
|
}
|
||||||
@ -1370,7 +1373,7 @@ internal class RealmCryptoStore @Inject constructor(
|
|||||||
.findAll()
|
.findAll()
|
||||||
xInfoEntities?.forEach { xInfoEntity ->
|
xInfoEntities?.forEach { xInfoEntity ->
|
||||||
// Need to ignore mine
|
// Need to ignore mine
|
||||||
if (xInfoEntity.userId == credentials.userId) return@forEach
|
if (xInfoEntity.userId == userId) return@forEach
|
||||||
val mapped = mapCrossSigningInfoEntity(xInfoEntity)
|
val mapped = mapCrossSigningInfoEntity(xInfoEntity)
|
||||||
val currentTrust = mapped.isTrusted()
|
val currentTrust = mapped.isTrusted()
|
||||||
val newTrust = check(mapped.userId)
|
val newTrust = check(mapped.userId)
|
||||||
|
Loading…
Reference in New Issue
Block a user