Create common action

This commit is contained in:
Benoit Marty 2019-05-17 15:33:38 +02:00
parent 340830d45f
commit e0e41d9e5c
4 changed files with 65 additions and 25 deletions

View File

@ -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)
}

/**

View File

@ -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

View File

@ -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()
}
}
}
}

View File

@ -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
}
}