Add MXCryptoConfig flag for key history sharing

Add shared_history flag to sessionBackupData
This commit is contained in:
ariskotsomitopoulos 2022-05-20 17:46:31 +03:00 committed by Valere
parent fb352ffa38
commit a9a7400fef
5 changed files with 29 additions and 5 deletions

View file

@ -38,4 +38,10 @@ data class MXCryptoConfig constructor(
* You can limit request only to your sessions by turning this setting to `true`
*/
val limitRoomKeyRequestsToMyDevices: Boolean = false,
/**
* Flag that indicates whether or not key history will be shared to invited
* users with respect to room visibility
*/
val shouldShareKeyHistory: Boolean = true,
)

View file

@ -1429,7 +1429,8 @@ internal class DefaultKeysBackupService @Inject constructor(
"sender_key" to sessionData.senderKey,
"sender_claimed_keys" to sessionData.senderClaimedKeys,
"forwarding_curve25519_key_chain" to (sessionData.forwardingCurve25519KeyChain.orEmpty()),
"session_key" to sessionData.sessionKey
"session_key" to sessionData.sessionKey,
"org.matrix.msc3061.shared_history" to sessionData.sharedHistory
)
val json = MoshiProvider.providesMoshi()
@ -1456,7 +1457,7 @@ internal class DefaultKeysBackupService @Inject constructor(
},
forwardedCount = olmInboundGroupSessionWrapper.sessionData.forwardingCurve25519KeyChain.orEmpty().size,
isVerified = device?.isVerified == true,
sharedHistory = olmInboundGroupSessionWrapper.sessionData.sharedHistory,
sessionData = mapOf(
"ciphertext" to encryptedSessionBackupData.mCipherText,
"mac" to encryptedSessionBackupData.mMac,

View file

@ -50,5 +50,12 @@ internal data class KeyBackupData(
* Algorithm-dependent data.
*/
@Json(name = "session_data")
val sessionData: JsonDict
val sessionData: JsonDict,
/**
* Flag that indicates whether or not the current inboundSession will be shared to
* invited users to decrypt past messages
*/
@Json(name = "org.matrix.msc3061.shared_history")
val sharedHistory: Boolean = false
)

View file

@ -26,6 +26,7 @@ import io.realm.RealmConfiguration
import io.realm.Sort
import io.realm.kotlin.createObject
import io.realm.kotlin.where
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.logger.LoggerTag
@ -111,6 +112,7 @@ internal class RealmCryptoStore @Inject constructor(
private val crossSigningKeysMapper: CrossSigningKeysMapper,
@UserId private val userId: String,
@DeviceId private val deviceId: String?,
private val matrixConfiguration: MatrixConfiguration,
private val clock: Clock,
) : IMXCryptoStore {
@ -658,6 +660,7 @@ internal class RealmCryptoStore @Inject constructor(
}
override fun shouldShareHistory(roomId: String): Boolean {
if (!matrixConfiguration.cryptoConfig.shouldShareKeyHistory) return false
return doWithRealm(realmConfiguration) {
CryptoRoomEntity.getById(it, roomId)?.shouldShareHistory
}

View file

@ -24,6 +24,7 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import io.realm.Realm
import io.realm.RealmQuery
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.session.crypto.CryptoService
import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.room.members.MembershipService
@ -57,6 +58,7 @@ internal class DefaultMembershipService @AssistedInject constructor(
private val cryptoService: CryptoService,
@UserId
private val userId: String,
private val matrixConfiguration: MatrixConfiguration,
private val queryStringValueProcessor: QueryStringValueProcessor
) : MembershipService {
@ -144,13 +146,18 @@ internal class DefaultMembershipService @AssistedInject constructor(
}
override suspend fun invite(userId: String, reason: String?) {
sendShareHistoryKeysIfNeeded(userId)
val params = InviteTask.Params(roomId, userId, reason)
inviteTask.execute(params)
}
private suspend fun sendShareHistoryKeysIfNeeded(userId: String) {
if (!matrixConfiguration.cryptoConfig.shouldShareKeyHistory) return
// TODO not sure it's the right way to get the latest messages in a room
val sessionInfo = Realm.getInstance(monarchy.realmConfiguration).use {
ChunkEntity.findLatestSessionInfo(it, roomId)
}
cryptoService.sendSharedHistoryKeys(roomId, userId, sessionInfo)
val params = InviteTask.Params(roomId, userId, reason)
inviteTask.execute(params)
}
override suspend fun invite3pid(threePid: ThreePid) {