From ca98ff5864b2150c798f713c341a11d3dcf64315 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 13 Jun 2019 10:58:45 +0200 Subject: [PATCH] Clear crypto database when signing out --- .../api/session/crypto/CryptoService.kt | 2 + .../android/internal/crypto/CryptoManager.kt | 8 +++ .../android/internal/crypto/CryptoModule.kt | 63 ++++--------------- .../internal/session/DefaultSession.kt | 26 +++++++- .../android/internal/session/SessionModule.kt | 7 ++- .../riotredesign/features/MainActivity.kt | 6 +- 6 files changed, 54 insertions(+), 58 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt index e720a75c35..e7c3c445ee 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt @@ -103,4 +103,6 @@ interface CryptoService { fun downloadKeys(userIds: List, forceDownload: Boolean, callback: MatrixCallback>) + fun clearCryptoCache(callback: MatrixCallback) + } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt index 74105a18fc..370a7b38d4 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt @@ -61,6 +61,7 @@ import im.vector.matrix.android.internal.crypto.tasks.SetDeviceNameTask import im.vector.matrix.android.internal.crypto.tasks.UploadKeysTask import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificationService import im.vector.matrix.android.internal.di.MoshiProvider +import im.vector.matrix.android.internal.session.cache.ClearCacheTask import im.vector.matrix.android.internal.session.room.membership.LoadRoomMembersTask import im.vector.matrix.android.internal.session.room.membership.RoomMembers import im.vector.matrix.android.internal.session.sync.model.SyncResponse @@ -125,6 +126,7 @@ internal class CryptoManager( private val setDeviceNameTask: SetDeviceNameTask, private val uploadKeysTask: UploadKeysTask, private val loadRoomMembersTask: LoadRoomMembersTask, + private val clearCryptoDataTask: ClearCacheTask, private val monarchy: Monarchy, private val coroutineDispatchers: MatrixCoroutineDispatchers, private val taskExecutor: TaskExecutor @@ -1061,6 +1063,12 @@ internal class CryptoManager( } } + override fun clearCryptoCache(callback: MatrixCallback) { + clearCryptoDataTask.configureWith(Unit) + .dispatchTo(callback) + .executeBy(taskExecutor) + } + /* ========================================================================================== * DEBUG INFO * ========================================================================================== */ diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt index 47889721d0..84029ba64b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt @@ -19,11 +19,7 @@ package im.vector.matrix.android.internal.crypto import android.content.Context import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.api.session.crypto.CryptoService -import im.vector.matrix.android.internal.crypto.actions.EnsureOlmSessionsForDevicesAction -import im.vector.matrix.android.internal.crypto.actions.EnsureOlmSessionsForUsersAction -import im.vector.matrix.android.internal.crypto.actions.MegolmSessionDataImporter -import im.vector.matrix.android.internal.crypto.actions.MessageEncrypter -import im.vector.matrix.android.internal.crypto.actions.SetDeviceVerificationAction +import im.vector.matrix.android.internal.crypto.actions.* import im.vector.matrix.android.internal.crypto.algorithms.megolm.MXMegolmDecryptionFactory import im.vector.matrix.android.internal.crypto.algorithms.megolm.MXMegolmEncryptionFactory import im.vector.matrix.android.internal.crypto.algorithms.olm.MXOlmDecryptionFactory @@ -31,58 +27,18 @@ import im.vector.matrix.android.internal.crypto.algorithms.olm.MXOlmEncryptionFa import im.vector.matrix.android.internal.crypto.api.CryptoApi import im.vector.matrix.android.internal.crypto.keysbackup.KeysBackup import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.CreateKeysBackupVersionTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultCreateKeysBackupVersionTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultDeleteBackupTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultDeleteRoomSessionDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultDeleteRoomSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultDeleteSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultGetKeysBackupLastVersionTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultGetKeysBackupVersionTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultGetRoomSessionDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultGetRoomSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultGetSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultStoreRoomSessionDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultStoreRoomSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultStoreSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DefaultUpdateKeysBackupVersionTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DeleteBackupTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DeleteRoomSessionDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DeleteRoomSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.DeleteSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.GetKeysBackupLastVersionTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.GetKeysBackupVersionTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.GetRoomSessionDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.GetRoomSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.GetSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.StoreRoomSessionDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.StoreRoomSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.StoreSessionsDataTask -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.UpdateKeysBackupVersionTask +import im.vector.matrix.android.internal.crypto.keysbackup.tasks.* import im.vector.matrix.android.internal.crypto.repository.WarnOnUnknownDeviceRepository import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStore import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStoreMigration import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStoreModule import im.vector.matrix.android.internal.crypto.store.db.hash -import im.vector.matrix.android.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask -import im.vector.matrix.android.internal.crypto.tasks.DefaultClaimOneTimeKeysForUsersDevice -import im.vector.matrix.android.internal.crypto.tasks.DefaultDeleteDeviceTask -import im.vector.matrix.android.internal.crypto.tasks.DefaultDownloadKeysForUsers -import im.vector.matrix.android.internal.crypto.tasks.DefaultGetDevicesTask -import im.vector.matrix.android.internal.crypto.tasks.DefaultGetKeyChangesTask -import im.vector.matrix.android.internal.crypto.tasks.DefaultSendToDeviceTask -import im.vector.matrix.android.internal.crypto.tasks.DefaultSetDeviceNameTask -import im.vector.matrix.android.internal.crypto.tasks.DefaultUploadKeysTask -import im.vector.matrix.android.internal.crypto.tasks.DeleteDeviceTask -import im.vector.matrix.android.internal.crypto.tasks.DownloadKeysForUsersTask -import im.vector.matrix.android.internal.crypto.tasks.GetDevicesTask -import im.vector.matrix.android.internal.crypto.tasks.GetKeyChangesTask -import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask -import im.vector.matrix.android.internal.crypto.tasks.SetDeviceNameTask -import im.vector.matrix.android.internal.crypto.tasks.UploadKeysTask +import im.vector.matrix.android.internal.crypto.tasks.* import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificationService import im.vector.matrix.android.internal.session.DefaultSession +import im.vector.matrix.android.internal.session.cache.ClearCacheTask +import im.vector.matrix.android.internal.session.cache.RealmClearCacheTask import io.realm.RealmConfiguration import org.koin.dsl.module.module import org.matrix.olm.OlmManager @@ -115,8 +71,8 @@ internal class CryptoModule { // CryptoStore scope(DefaultSession.SCOPE) { RealmCryptoStore(false /* TODO*/, - get("CryptoRealmConfiguration"), - get()) as IMXCryptoStore + get("CryptoRealmConfiguration"), + get()) as IMXCryptoStore } scope(DefaultSession.SCOPE) { @@ -242,6 +198,7 @@ internal class CryptoModule { setDeviceNameTask = get(), uploadKeysTask = get(), loadRoomMembersTask = get(), + clearCryptoDataTask = get("ClearTaskCryptoCache"), monarchy = get(), coroutineDispatchers = get(), taskExecutor = get() @@ -291,6 +248,10 @@ internal class CryptoModule { DefaultUploadKeysTask(get()) as UploadKeysTask } + scope(DefaultSession.SCOPE, name = "ClearTaskCryptoCache") { + RealmClearCacheTask(get("CryptoRealmConfiguration")) as ClearCacheTask + } + /* ========================================================================================== * Keys backup * ========================================================================================== */ diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index 76f0ccea93..b132f33da2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -71,6 +71,7 @@ import im.vector.matrix.android.internal.session.sync.job.SyncThread import im.vector.matrix.android.internal.session.user.UserModule import org.koin.core.scope.Scope import org.koin.standalone.inject +import timber.log.Timber internal class DefaultSession(override val sessionParams: SessionParams) : Session, MatrixKoinComponent { @@ -151,19 +152,36 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi @MainThread override fun signOut(callback: MatrixCallback) { + Timber.w("SIGN_OUT: start") + assert(isOpen) + Timber.w("SIGN_OUT: kill sync thread") syncThread.kill() + Timber.w("SIGN_OUT: call webservice") return signOutService.signOut(object : MatrixCallback { override fun onSuccess(data: Unit) { + Timber.w("SIGN_OUT: call webservice -> SUCCESS: clear cache") + // Clear the cache - cacheService.clearCache(object : MatrixCallbackDelegate(callback) {}) + cacheService.clearCache(object : MatrixCallback { + override fun onSuccess(data: Unit) { + Timber.w("SIGN_OUT: clear cache -> SUCCESS: clear crypto cache") + cryptoService.clearCryptoCache(MatrixCallbackDelegate(callback)) + } + + override fun onFailure(failure: Throwable) { + // ignore error + Timber.e("SIGN_OUT: clear cache -> ERROR: ignoring") + onSuccess(Unit) + } + }) } override fun onFailure(failure: Throwable) { // Ignore failure + Timber.e("SIGN_OUT: call webservice -> ERROR: ignoring") onSuccess(Unit) - // callback.onFailure(failure) } }) } @@ -395,6 +413,10 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi cryptoService.downloadKeys(userIds, forceDownload, callback) } + override fun clearCryptoCache(callback: MatrixCallback) { + cryptoService.clearCryptoCache(callback) + } + // Private methods ***************************************************************************** private fun assertMainThread() { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt index f46ab758ac..4a5d3b4e53 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt @@ -130,10 +130,11 @@ internal class SessionModule(private val sessionParams: SessionParams) { } scope(DefaultSession.SCOPE) { - RealmCacheService(get(), get()) as CacheService + RealmCacheService(get("ClearTaskMainCache"), get()) as CacheService } - scope(DefaultSession.SCOPE) { + // Give a name, because we have a clear task for crypto store as well + scope(DefaultSession.SCOPE, name = "ClearTaskMainCache") { RealmClearCacheTask(get("SessionRealmConfiguration")) as ClearCacheTask } @@ -157,7 +158,7 @@ internal class SessionModule(private val sessionParams: SessionParams) { DefaultFilterService(get(), get(), get()) as FilterService } - scope(DefaultSession.SCOPE) { + scope(DefaultSession.SCOPE) { val retrofit: Retrofit = get() retrofit.create(FilterApi::class.java) } diff --git a/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt b/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt index c26a940b96..2cc31dff1e 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/MainActivity.kt @@ -24,6 +24,7 @@ import im.vector.matrix.android.api.MatrixCallback import im.vector.riotredesign.core.platform.VectorBaseActivity import im.vector.riotredesign.features.home.HomeActivity import im.vector.riotredesign.features.login.LoginActivity +import timber.log.Timber class MainActivity : VectorBaseActivity() { @@ -60,15 +61,16 @@ class MainActivity : VectorBaseActivity() { when { clearCredentials -> session.signOut(object : MatrixCallback { override fun onSuccess(data: Unit) { + Timber.w("SIGN_OUT: success, start app") start() } }) - clearCache -> session.clearCache(object : MatrixCallback { + clearCache -> session.clearCache(object : MatrixCallback { override fun onSuccess(data: Unit) { start() } }) - else -> start() + else -> start() } } }