Merge pull request #3724 from vector-im/feature/bma/ensureOTK
Ensure OTKs are uploaded when the session is created
This commit is contained in:
commit
b764746323
1
changelog.d/3724.bugfix
Normal file
1
changelog.d/3724.bugfix
Normal file
@ -0,0 +1 @@
|
||||
Ensure OTKs are uploaded when the session is created
|
@ -314,6 +314,12 @@ internal class DefaultCryptoService @Inject constructor(
|
||||
cryptoCoroutineScope.launchToCallback(coroutineDispatchers.crypto, NoOpMatrixCallback()) {
|
||||
// Open the store
|
||||
cryptoStore.open()
|
||||
|
||||
if (!cryptoStore.areDeviceKeysUploaded()) {
|
||||
// Schedule upload of OTK
|
||||
oneTimeKeysUploader.updateOneTimeKeyCount(0)
|
||||
}
|
||||
|
||||
// this can throw if no network
|
||||
tryOrNull {
|
||||
uploadDeviceKeys()
|
||||
@ -905,7 +911,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||
* Upload my user's device keys.
|
||||
*/
|
||||
private suspend fun uploadDeviceKeys() {
|
||||
if (cryptoStore.getDeviceKeysUploaded()) {
|
||||
if (cryptoStore.areDeviceKeysUploaded()) {
|
||||
Timber.d("Keys already uploaded, nothing to do")
|
||||
return
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.matrix.android.sdk.internal.crypto
|
||||
|
||||
import org.matrix.android.sdk.api.extensions.tryOrNull
|
||||
import org.matrix.android.sdk.internal.crypto.model.MXKey
|
||||
import org.matrix.android.sdk.internal.crypto.model.rest.KeysUploadResponse
|
||||
import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask
|
||||
@ -77,6 +78,10 @@ internal class OneTimeKeysUploader @Inject constructor(
|
||||
// discard the oldest private keys first. This will eventually clean
|
||||
// out stale private keys that won't receive a message.
|
||||
val keyLimit = floor(maxOneTimeKeys / 2.0).toInt()
|
||||
if (oneTimeKeyCount == null) {
|
||||
// Ask the server how many otk he has
|
||||
oneTimeKeyCount = fetchOtkCount()
|
||||
}
|
||||
val oneTimeKeyCountFromSync = oneTimeKeyCount
|
||||
if (oneTimeKeyCountFromSync != null) {
|
||||
// We need to keep a pool of one time public keys on the server so that
|
||||
@ -90,17 +95,22 @@ internal class OneTimeKeysUploader @Inject constructor(
|
||||
// private keys clogging up our local storage.
|
||||
// So we need some kind of engineering compromise to balance all of
|
||||
// these factors.
|
||||
try {
|
||||
tryOrNull("Unable to upload OTK") {
|
||||
val uploadedKeys = uploadOTK(oneTimeKeyCountFromSync, keyLimit)
|
||||
Timber.v("## uploadKeys() : success, $uploadedKeys key(s) sent")
|
||||
} finally {
|
||||
oneTimeKeyCheckInProgress = false
|
||||
}
|
||||
} else {
|
||||
Timber.w("maybeUploadOneTimeKeys: waiting to know the number of OTK from the sync")
|
||||
oneTimeKeyCheckInProgress = false
|
||||
lastOneTimeKeyCheck = 0
|
||||
}
|
||||
oneTimeKeyCheckInProgress = false
|
||||
}
|
||||
|
||||
private suspend fun fetchOtkCount(): Int? {
|
||||
return tryOrNull("Unable to get OTK count") {
|
||||
val result = uploadKeysTask.execute(UploadKeysTask.Params(null, null))
|
||||
result.oneTimeKeyCountsForAlgorithm(MXKey.KEY_SIGNED_CURVE_25519_TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -475,7 +475,7 @@ internal interface IMXCryptoStore {
|
||||
fun getGossipingEvents(): List<Event>
|
||||
|
||||
fun setDeviceKeysUploaded(uploaded: Boolean)
|
||||
fun getDeviceKeysUploaded(): Boolean
|
||||
fun areDeviceKeysUploaded(): Boolean
|
||||
fun tidyUpDataBase()
|
||||
fun logDbUsageInfo()
|
||||
}
|
||||
|
@ -937,7 +937,7 @@ internal class RealmCryptoStore @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override fun getDeviceKeysUploaded(): Boolean {
|
||||
override fun areDeviceKeysUploaded(): Boolean {
|
||||
return doWithRealm(realmConfiguration) {
|
||||
it.where<CryptoMetadataEntity>().findFirst()?.deviceKeysSentToServer
|
||||
} ?: false
|
||||
|
Loading…
Reference in New Issue
Block a user