mirror of
https://github.com/element-hq/element-android
synced 2024-12-19 07:45:17 +03:00
crypto: Move more of the request sending logic into the sender class
This commit is contained in:
parent
6523ca5afe
commit
4473af85b1
5 changed files with 66 additions and 56 deletions
|
@ -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, "{}")
|
||||
}
|
||||
|
||||
|
|
|
@ -135,7 +135,6 @@ internal class QrCodeVerification(
|
|||
private val listeners: ArrayList<VerificationService.Listener>,
|
||||
) : QrCodeVerificationTransaction {
|
||||
private val uiHandler = Handler(Looper.getMainLooper())
|
||||
private var stateField: VerificationTxState = VerificationTxState.OnStarted
|
||||
|
||||
private fun dispatchTxUpdated() {
|
||||
uiHandler.post {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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<VerificationService.Listener>,
|
||||
) {
|
||||
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<VerificationMethod>): OutgoingVerificationRequest? {
|
||||
suspend fun acceptWithMethods(methods: List<VerificationMethod>) {
|
||||
val stringMethods: MutableList<String> =
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue