From e0e41d9e5c5893fd1a5ec4aed404be1a5c6b3bfb Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 17 May 2019 15:33:38 +0200 Subject: [PATCH] Create common action --- .../android/internal/crypto/CryptoManager.kt | 23 ++------- .../android/internal/crypto/CryptoModule.kt | 9 ++++ .../actions/SetDeviceVerificationAction.kt | 49 +++++++++++++++++++ .../DefaultSasVerificationService.kt | 9 ++-- 4 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt 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 89aaa974d5..15e06bba34 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 @@ -37,6 +37,7 @@ import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.room.Room import im.vector.matrix.android.api.session.room.RoomService import im.vector.matrix.android.api.session.room.model.Membership +import im.vector.matrix.android.internal.crypto.actions.SetDeviceVerificationAction import im.vector.matrix.android.internal.crypto.algorithms.IMXEncrypting import im.vector.matrix.android.internal.crypto.keysbackup.KeysBackup import im.vector.matrix.android.internal.crypto.model.* @@ -95,6 +96,8 @@ internal class CryptoManager( private val mRoomService: RoomService, // Olm Manager private val mOlmManager: OlmManager, + // Actions + private val mSetDeviceVerificationAction: SetDeviceVerificationAction, // Tasks private val mClaimOneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask, private val mDeleteDeviceTask: DeleteDeviceTask, @@ -529,25 +532,7 @@ internal class CryptoManager( * @param callback the asynchronous callback */ override fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String) { - val device = mCryptoStore.getUserDevice(deviceId, userId) - - // Sanity check - if (null == device) { - Timber.w("## setDeviceVerification() : Unknown device $userId:$deviceId") - return - } - - if (device.mVerified != verificationStatus) { - device.mVerified = verificationStatus - mCryptoStore.storeUserDevice(userId, device) - - if (userId == mCredentials.userId) { - // If one of the user's own devices is being marked as verified / unverified, - // check the key backup status, since whether or not we use this depends on - // whether it has a signature from a verified device - mKeysBackup.checkAndStartKeysBackup() - } - } + mSetDeviceVerificationAction.handle(verificationStatus, deviceId, userId) } /** 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 770c728f03..9093a5fe3f 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,6 +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.SetDeviceVerificationAction 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 @@ -107,6 +108,12 @@ internal class CryptoModule { OneTimeKeysManager(get(), get(), get(), get(), get()) } + // Actions + scope(DefaultSession.SCOPE) { + SetDeviceVerificationAction(get(), get(), get()) + } + + // CryptoManager scope(DefaultSession.SCOPE) { CryptoManager( @@ -124,6 +131,8 @@ internal class CryptoModule { get(), get(), get(), + // Actions + get(), // Tasks get(), get(), get(), get(), get(), get(), get(), // Task executor diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt new file mode 100644 index 0000000000..26f6b765ce --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.matrix.android.internal.crypto.actions + +import im.vector.matrix.android.api.auth.data.Credentials +import im.vector.matrix.android.internal.crypto.keysbackup.KeysBackup +import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore +import timber.log.Timber + +internal class SetDeviceVerificationAction(val mCryptoStore: IMXCryptoStore, + val mCredentials: Credentials, + val mKeysBackup: KeysBackup) { + + fun handle(verificationStatus: Int, deviceId: String, userId: String) { + val device = mCryptoStore.getUserDevice(deviceId, userId) + + // Sanity check + if (null == device) { + Timber.w("## setDeviceVerification() : Unknown device $userId:$deviceId") + return + } + + if (device.mVerified != verificationStatus) { + device.mVerified = verificationStatus + mCryptoStore.storeUserDevice(userId, device) + + if (userId == mCredentials.userId) { + // If one of the user's own devices is being marked as verified / unverified, + // check the key backup status, since whether or not we use this depends on + // whether it has a signature from a verified device + mKeysBackup.checkAndStartKeysBackup() + } + } + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt index 85648b8413..0355c1126d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt @@ -29,6 +29,7 @@ import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.internal.crypto.CryptoAsyncHelper import im.vector.matrix.android.internal.crypto.DeviceListManager +import im.vector.matrix.android.internal.crypto.actions.SetDeviceVerificationAction import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.model.rest.* @@ -48,6 +49,7 @@ import kotlin.collections.HashMap internal class DefaultSasVerificationService(private val mCredentials: Credentials, private val mCryptoStore: IMXCryptoStore, private val deviceListManager: DeviceListManager, + private val setDeviceVerificationAction: SetDeviceVerificationAction, private val mSendToDeviceTask: SendToDeviceTask, private val mTaskExecutor: TaskExecutor) : VerificationTransaction.Listener, SasVerificationService { @@ -130,7 +132,7 @@ internal class DefaultSasVerificationService(private val mCredentials: Credentia } override fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) { - mCryptoListener.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED, + setDeviceVerificationAction.handle(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED, deviceID, userId) @@ -427,12 +429,7 @@ internal class DefaultSasVerificationService(private val mCredentials: Credentia mCryptoListener = listener } - fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String) { - mCryptoListener.setDeviceVerification(verificationStatus, deviceId, userId) - } - interface SasCryptoListener { - fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String) fun getMyDevice(): MXDeviceInfo } } \ No newline at end of file