From f95c4ae08836ae4666f0ac21204b0781c70c1398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 25 Jun 2021 13:40:11 +0200 Subject: [PATCH] crypto: Allow cancelling of SAS transactions --- .../android/sdk/internal/crypto/OlmMachine.kt | 2 +- .../internal/crypto/RustSasVerification.kt | 76 +++++++++---------- 2 files changed, 39 insertions(+), 39 deletions(-) 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 e05287052a..7d93ee715e 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 @@ -112,7 +112,7 @@ private fun toCryptoDeviceInfo(device: Device): CryptoDeviceInfo { // Kotlin side care about signatures? mapOf(), UnsignedDeviceInfo(device.displayName), - DeviceTrustLevel(crossSigningVerified = device.verified, locallyVerified = device.locallyVerified), + DeviceTrustLevel(crossSigningVerified = device.crossSigningTrusted, locallyVerified = device.locallyTrusted), device.isBlocked, // TODO null) 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 6a8ab11d82..86be1b9cc6 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 @@ -68,7 +68,7 @@ internal class SasVerification( override val isIncoming: Boolean get() { - return false + return !this.inner.weStarted } override var otherDeviceId: String? @@ -113,19 +113,19 @@ internal class SasVerification( } override fun cancel() { - TODO() + this.cancelHelper(CancelCode.User) } override fun cancel(code: CancelCode) { - TODO() + this.cancelHelper(code) } override fun shortCodeDoesNotMatch() { - TODO() + this.cancelHelper(CancelCode.MismatchedSas) } override fun isToDeviceTransport(): Boolean { - return false + return this.inner.roomId == null } override fun supportsDecimal(): Boolean { @@ -142,41 +142,23 @@ internal class SasVerification( } override fun userHasVerifiedShortCode() { - runBlocking { - when (val request = confirm()) { + val request = runBlocking { confirm() } ?: return + sendRequest(request) + } + + suspend fun accept() { + 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() } - - else -> {} - } + refreshData() + dispatchTxUpdated() } - refreshData() - dispatchTxUpdated() - } - - fun isCanceled(): Boolean { - refreshData() - return this.inner.isCancelled - } - - fun isDone(): Boolean { - refreshData() - return this.inner.isDone - } - - fun timedOut(): Boolean { - refreshData() - return this.inner.timedOut - } - - fun canBePresented(): Boolean { - refreshData() - return this.inner.canBePresented - } - - fun accept(): OutgoingVerificationRequest? { - return this.machine.acceptSasVerification(this.inner.otherUserId, inner.flowId) } @Throws(CryptoStoreErrorException::class) @@ -185,8 +167,12 @@ internal class SasVerification( machine.confirmVerification(inner.otherUserId, inner.flowId) } - fun cancelHelper(): OutgoingVerificationRequest? { - return this.machine.cancelVerification(this.inner.otherUserId, inner.flowId) + fun cancelHelper(code: CancelCode) { + val request = this.machine.cancelVerification(this.inner.otherUserId, inner.flowId, code.value) + + if (request != null) { + sendRequest(request) + } } override fun getEmojiCodeRepresentation(): List { @@ -200,4 +186,18 @@ internal class SasVerification( return decimals?.joinToString(" ") ?: "" } + + fun sendRequest(request: OutgoingVerificationRequest) { + runBlocking { + when (request) { + is OutgoingVerificationRequest.ToDevice -> { + sender.sendToDevice(request.eventType, request.body) + } + is OutgoingVerificationRequest.InRoom -> TODO() + } + } + + refreshData() + dispatchTxUpdated() + } }