From 4473af85b1b1d7a8bb0d1da00962107ec96329c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 28 Jun 2021 09:50:59 +0200 Subject: [PATCH] crypto: Move more of the request sending logic into the sender class --- .../internal/crypto/DefaultCryptoService.kt | 18 ++++++- .../android/sdk/internal/crypto/OlmMachine.kt | 1 - .../internal/crypto/RustSasVerification.kt | 14 +---- .../internal/crypto/VerificationRequest.kt | 37 +++++++++++-- .../verification/RustVerificationService.kt | 52 +++++-------------- 5 files changed, 66 insertions(+), 56 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index bacc9e1456..5764f078f7 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -92,6 +92,7 @@ import org.matrix.android.sdk.internal.task.configureWith import org.matrix.android.sdk.internal.task.launchToCallback import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers import timber.log.Timber +import uniffi.olm.OutgoingVerificationRequest import uniffi.olm.Request import uniffi.olm.RequestType import java.io.File @@ -103,6 +104,21 @@ import kotlin.math.max internal class RequestSender( private val sendToDeviceTask: SendToDeviceTask, ) { + suspend fun sendVerificationRequest(request: OutgoingVerificationRequest) { + when (request) { + is OutgoingVerificationRequest.InRoom -> TODO() + is OutgoingVerificationRequest.ToDevice -> sendToDevice(request) + } + } + + suspend fun sendToDevice(request: Request.ToDevice) { + sendToDevice(request.eventType, request.body) + } + + suspend fun sendToDevice(request: OutgoingVerificationRequest.ToDevice) { + sendToDevice(request.eventType, request.body) + } + suspend fun sendToDevice(eventType: String, body: String) { // TODO this produces floats for the Olm type fields, which // are integers originally. @@ -808,7 +824,7 @@ internal class DefaultCryptoService @Inject constructor( } private suspend fun sendToDevice(request: Request.ToDevice) { - this.sender.sendToDevice(request.eventType, request.body) + this.sender.sendToDevice(request) olmMachine!!.markRequestAsSent(request.requestId, RequestType.TO_DEVICE, "{}") } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt index 6868d5b997..59e122d518 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt @@ -135,7 +135,6 @@ internal class QrCodeVerification( private val listeners: ArrayList, ) : QrCodeVerificationTransaction { private val uiHandler = Handler(Looper.getMainLooper()) - private var stateField: VerificationTxState = VerificationTxState.OnStarted private fun dispatchTxUpdated() { uiHandler.post { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt index 9c4ca62e9f..d38662abec 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt @@ -152,12 +152,7 @@ internal class SasVerification( val request = this.machine.acceptSasVerification(this.inner.otherUserId, inner.flowId) if (request != null) { - when (request) { - is OutgoingVerificationRequest.ToDevice -> { - sender.sendToDevice(request.eventType, request.body) - } - is OutgoingVerificationRequest.InRoom -> TODO() - } + this.sender.sendVerificationRequest(request) refreshData() dispatchTxUpdated() } @@ -191,12 +186,7 @@ internal class SasVerification( fun sendRequest(request: OutgoingVerificationRequest) { runBlocking { - when (request) { - is OutgoingVerificationRequest.ToDevice -> { - sender.sendToDevice(request.eventType, request.body) - } - is OutgoingVerificationRequest.InRoom -> TODO() - } + sender.sendVerificationRequest(request) } refreshData() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt index 78df72a928..24edebc085 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt @@ -16,6 +16,8 @@ package org.matrix.android.sdk.internal.crypto +import android.os.Handler +import android.os.Looper import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest @@ -28,8 +30,10 @@ import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_QR_ import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_QR_CODE_SHOW import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_RECIPROCATE import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_SAS +import timber.log.Timber import uniffi.olm.OlmMachine import uniffi.olm.OutgoingVerificationRequest +import uniffi.olm.Sas import uniffi.olm.StartSasResult import uniffi.olm.VerificationRequest @@ -39,6 +43,8 @@ internal class VerificationRequest( private val sender: RequestSender, private val listeners: ArrayList, ) { + private val uiHandler = Handler(Looper.getMainLooper()) + private fun refreshData() { val request = this.machine.getVerificationRequest(this.inner.otherUserId, this.inner.flowId) @@ -49,6 +55,18 @@ internal class VerificationRequest( return } + private fun dispatchRequestUpdated() { + uiHandler.post { + listeners.forEach { + try { + it.verificationRequestUpdated(this.toPendingVerificationRequest()) + } catch (e: Throwable) { + Timber.e(e, "## Error while notifying listeners") + } + } + } + } + internal fun startQrVerification(): QrCodeVerification? { val qrcode = this.machine.startQrVerification(this.inner.otherUserId, this.inner.flowId) @@ -64,7 +82,7 @@ internal class VerificationRequest( } } - fun acceptWithMethods(methods: List): OutgoingVerificationRequest? { + suspend fun acceptWithMethods(methods: List) { val stringMethods: MutableList = methods .map { @@ -81,8 +99,13 @@ internal class VerificationRequest( stringMethods.add(VERIFICATION_METHOD_RECIPROCATE) } - return this.machine.acceptVerificationRequest( + val request = this.machine.acceptVerificationRequest( this.inner.otherUserId, this.inner.flowId, stringMethods) + + if (request != null) { + this.sender.sendVerificationRequest(request) + this.dispatchRequestUpdated() + } } fun isCanceled(): Boolean { @@ -100,11 +123,17 @@ internal class VerificationRequest( return this.inner.isReady } - suspend fun startSasVerification(): StartSasResult? { + suspend fun startSasVerification(): Sas? { refreshData() return withContext(Dispatchers.IO) { - machine.startSasVerification(inner.otherUserId, inner.flowId) + val result = machine.startSasVerification(inner.otherUserId, inner.flowId) + if (result != null) { + sender.sendVerificationRequest(result.request) + result.sas + } else { + null + } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt index cbf8ca32ca..d9af61029c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt @@ -36,9 +36,9 @@ import org.matrix.android.sdk.internal.crypto.VerificationRequest import org.matrix.android.sdk.internal.crypto.model.rest.KeyVerificationDone import org.matrix.android.sdk.internal.crypto.model.rest.KeyVerificationKey import org.matrix.android.sdk.internal.crypto.model.rest.KeyVerificationRequest +import org.matrix.android.sdk.internal.crypto.model.rest.KeyVerificationStart import org.matrix.android.sdk.internal.session.SessionScope import timber.log.Timber -import uniffi.olm.OutgoingVerificationRequest import uniffi.olm.Verification @SessionScope @@ -101,18 +101,6 @@ constructor( } } - private fun dispatchRequestUpdated(tx: PendingVerificationRequest) { - uiHandler.post { - listeners.forEach { - try { - it.verificationRequestUpdated(tx) - } catch (e: Throwable) { - Timber.e(e, "## Error while notifying listeners") - } - } - } - } - override fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) { TODO() // setDeviceVerificationAction.handle(DeviceTrustLevel(false, true), @@ -224,17 +212,19 @@ constructor( otherDeviceId: String, transactionId: String? ): String? { + val flowId = transactionId ?: return null + // should check if already one (and cancel it) return if (method == VerificationMethod.SAS) { - val flowId = transactionId ?: return null val request = this.getVerificationRequest(otherUserId, flowId) + runBlocking { - val response = request?.startSasVerification() - if (response != null) { - sendRequest(response.request) - val sas = SasVerification(olmMachine.inner(), response.sas, requestSender, listeners) - dispatchTxAdded(sas) - sas.transactionId + val sas = request?.startSasVerification() + + if (sas != null) { + val sasTransaction = SasVerification(olmMachine.inner(), sas, requestSender, listeners) + dispatchTxAdded(sasTransaction) + sasTransaction.transactionId } else { null } @@ -337,15 +327,15 @@ constructor( val request = this.getVerificationRequest(otherUserId, transactionId) return if (request != null) { - val outgoingRequest = request.acceptWithMethods(methods) + runBlocking { request.acceptWithMethods(methods) } - if (outgoingRequest != null) { - runBlocking { sendRequest(outgoingRequest) } - dispatchRequestUpdated(request.toPendingVerificationRequest()) + if (request.isReady()) { val qrcode = request.startQrVerification() + if (qrcode != null) { dispatchTxAdded(qrcode) } + true } else { false @@ -355,20 +345,6 @@ constructor( } } - // TODO create a class that handles this, the DefaultCryptoService has - // similar needs so we could share code there, beware that local echo seems - // to be handled here - suspend fun sendRequest(request: OutgoingVerificationRequest) { - when (request) { - is OutgoingVerificationRequest.ToDevice -> { - this.requestSender.sendToDevice(request.eventType, request.body) - } - else -> {} - } - - // TODO move this into the VerificationRequest and Verification classes? - } - override fun transactionUpdated(tx: VerificationTransaction) { dispatchTxUpdated(tx) }