Fix 404 when uploading xsigning keys

Own device keys was not yet uploaded, now it's done on session open . + added some cleaning of legacy things
This commit is contained in:
Valere 2020-06-26 18:58:57 +02:00
parent 6131c10d31
commit 4ce2478e44
7 changed files with 68 additions and 36 deletions

View File

@ -29,6 +29,7 @@ import dagger.Lazy
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.NoOpMatrixCallback
import im.vector.matrix.android.api.crypto.MXCryptoConfig
import im.vector.matrix.android.api.extensions.tryThis
import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.listeners.ProgressListener
import im.vector.matrix.android.api.session.crypto.CryptoService
@ -327,35 +328,65 @@ internal class DefaultCryptoService @Inject constructor(
* and, then, if this is the first time, this new device will be announced to all other users
* devices.
*
* @param isInitialSync true if it starts from an initial sync
*/
fun start(isInitialSync: Boolean) {
if (isStarted.get() || isStarting.get()) {
return
}
isStarting.set(true)
fun start() {
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
internalStart(isInitialSync)
internalStart()
}
// Just update
fetchDevicesList(NoOpMatrixCallback())
}
private suspend fun internalStart(isInitialSync: Boolean) {
// Open the store
cryptoStore.open()
runCatching {
fun ensureDevice() {
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
// Open the store
cryptoStore.open()
// TODO why do that everytime? we should mark that it was done
uploadDeviceKeys()
oneTimeKeysUploader.maybeUploadOneTimeKeys()
keysBackupService.checkAndStartKeysBackup()
if (isInitialSync) {
// refresh the devices list for each known room members
deviceListManager.invalidateAllDeviceLists()
deviceListManager.refreshOutdatedDeviceLists()
} else {
incomingGossipingRequestManager.processReceivedGossipingRequests()
// this can throw if no backup
tryThis {
keysBackupService.checkAndStartKeysBackup()
}
}
}
fun onSyncWillProcess(isInitialSync: Boolean) {
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
if (isInitialSync) {
try {
// On initial sync, we start all our tracking from
// scratch, so mark everything as untracked. onCryptoEvent will
// be called for all e2e rooms during the processing of the sync,
// at which point we'll start tracking all the users of that room.
deviceListManager.invalidateAllDeviceLists()
deviceListManager.refreshOutdatedDeviceLists()
} catch (failure: Throwable) {
Timber.e(failure, "## CRYPTO onSyncWillProcess ")
}
}
}
}
private fun internalStart() {
if (isStarted.get() || isStarting.get()) {
return
}
isStarting.set(true)
// Open the store
cryptoStore.open()
runCatching {
// if (isInitialSync) {
// // refresh the devices list for each known room members
// deviceListManager.invalidateAllDeviceLists()
// deviceListManager.refreshOutdatedDeviceLists()
// } else {
// Why would we do that? it will be called at end of syn
incomingGossipingRequestManager.processReceivedGossipingRequests()
// }
}.fold(
{
isStarting.set(false)
@ -624,10 +655,10 @@ internal class DefaultCryptoService @Inject constructor(
roomId: String,
callback: MatrixCallback<MXEncryptEventContentResult>) {
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
if (!isStarted()) {
Timber.v("## CRYPTO | encryptEventContent() : wait after e2e init")
internalStart(false)
}
// if (!isStarted()) {
// Timber.v("## CRYPTO | encryptEventContent() : wait after e2e init")
// internalStart(false)
// }
val userIds = getRoomUserIds(roomId)
var alg = roomEncryptorsStore.get(roomId)
if (alg == null) {
@ -1158,10 +1189,10 @@ internal class DefaultCryptoService @Inject constructor(
}
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
if (!isStarted()) {
Timber.v("## CRYPTO | requestRoomKeyForEvent() : wait after e2e init")
internalStart(false)
}
// if (!isStarted()) {
// Timber.v("## CRYPTO | requestRoomKeyForEvent() : wait after e2e init")
// internalStart(false)
// }
roomDecryptorProvider
.getOrCreateRoomDecryptor(event.roomId, wireContent.algorithm)
?.requestKeysForEvent(event) ?: run {

View File

@ -174,7 +174,11 @@ internal class RealmCryptoStore @Inject constructor(
}
override fun open() {
realmLocker = Realm.getInstance(realmConfiguration)
synchronized(this) {
if (realmLocker == null) {
realmLocker = Realm.getInstance(realmConfiguration)
}
}
}
override fun close() {

View File

@ -145,6 +145,7 @@ internal class DefaultSession @Inject constructor(
override fun open() {
assert(!isOpen)
isOpen = true
cryptoService.get().ensureDevice()
uiHandler.post {
lifecycleObservers.forEach { it.onStart() }
}

View File

@ -49,10 +49,7 @@ internal class SyncResponseHandler @Inject constructor(@SessionDatabase private
val reporter = initialSyncProgressService.takeIf { isInitialSync }
measureTimeMillis {
if (!cryptoService.isStarted()) {
Timber.v("Should start cryptoService")
cryptoService.start(isInitialSync)
}
cryptoService.onSyncWillProcess(isInitialSync)
}.also {
Timber.v("Finish handling start cryptoService in $it ms")
}

View File

@ -34,4 +34,4 @@ class TemporaryStoreTest {
sleep(20)
store.data shouldBe null
}
}
}

View File

@ -303,7 +303,7 @@ class BootstrapSharedViewModel @AssistedInject constructor(
}
}
private fun startMigrationFlow(previousStep: BootstrapStep, passphrase: String?, recoveryKey: String?) {//TODO Rename param
private fun startMigrationFlow(previousStep: BootstrapStep, passphrase: String?, recoveryKey: String?) { // TODO Rename param
setState {
copy(step = BootstrapStep.Initializing)
}
@ -370,7 +370,6 @@ class BootstrapSharedViewModel @AssistedInject constructor(
}
}
viewModelScope.launch(Dispatchers.IO) {
val userPasswordAuth = userPassword?.let {
UserPasswordAuth(

View File

@ -128,7 +128,7 @@ class HomeActivityViewModel @AssistedInject constructor(
val mxCrossSigningInfo = session.cryptoService().crossSigningService().getMyCrossSigningKeys()
if (mxCrossSigningInfo != null) {
// Cross-signing is already set up for this user, is it trusted?
if(!mxCrossSigningInfo.isTrusted()) {
if (!mxCrossSigningInfo.isTrusted()) {
// New session
_viewEvents.post(HomeActivityViewEvents.OnNewSession(session.getUser(session.myUserId)?.toMatrixItem()))
}