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.task.launchToCallback
|
||||||
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import uniffi.olm.OutgoingVerificationRequest
|
||||||
import uniffi.olm.Request
|
import uniffi.olm.Request
|
||||||
import uniffi.olm.RequestType
|
import uniffi.olm.RequestType
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -103,6 +104,21 @@ import kotlin.math.max
|
||||||
internal class RequestSender(
|
internal class RequestSender(
|
||||||
private val sendToDeviceTask: SendToDeviceTask,
|
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) {
|
suspend fun sendToDevice(eventType: String, body: String) {
|
||||||
// TODO this produces floats for the Olm type fields, which
|
// TODO this produces floats for the Olm type fields, which
|
||||||
// are integers originally.
|
// are integers originally.
|
||||||
|
@ -808,7 +824,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun sendToDevice(request: Request.ToDevice) {
|
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, "{}")
|
olmMachine!!.markRequestAsSent(request.requestId, RequestType.TO_DEVICE, "{}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -135,7 +135,6 @@ internal class QrCodeVerification(
|
||||||
private val listeners: ArrayList<VerificationService.Listener>,
|
private val listeners: ArrayList<VerificationService.Listener>,
|
||||||
) : QrCodeVerificationTransaction {
|
) : QrCodeVerificationTransaction {
|
||||||
private val uiHandler = Handler(Looper.getMainLooper())
|
private val uiHandler = Handler(Looper.getMainLooper())
|
||||||
private var stateField: VerificationTxState = VerificationTxState.OnStarted
|
|
||||||
|
|
||||||
private fun dispatchTxUpdated() {
|
private fun dispatchTxUpdated() {
|
||||||
uiHandler.post {
|
uiHandler.post {
|
||||||
|
|
|
@ -152,12 +152,7 @@ internal class SasVerification(
|
||||||
val request = this.machine.acceptSasVerification(this.inner.otherUserId, inner.flowId)
|
val request = this.machine.acceptSasVerification(this.inner.otherUserId, inner.flowId)
|
||||||
|
|
||||||
if (request != null) {
|
if (request != null) {
|
||||||
when (request) {
|
this.sender.sendVerificationRequest(request)
|
||||||
is OutgoingVerificationRequest.ToDevice -> {
|
|
||||||
sender.sendToDevice(request.eventType, request.body)
|
|
||||||
}
|
|
||||||
is OutgoingVerificationRequest.InRoom -> TODO()
|
|
||||||
}
|
|
||||||
refreshData()
|
refreshData()
|
||||||
dispatchTxUpdated()
|
dispatchTxUpdated()
|
||||||
}
|
}
|
||||||
|
@ -191,12 +186,7 @@ internal class SasVerification(
|
||||||
|
|
||||||
fun sendRequest(request: OutgoingVerificationRequest) {
|
fun sendRequest(request: OutgoingVerificationRequest) {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
when (request) {
|
sender.sendVerificationRequest(request)
|
||||||
is OutgoingVerificationRequest.ToDevice -> {
|
|
||||||
sender.sendToDevice(request.eventType, request.body)
|
|
||||||
}
|
|
||||||
is OutgoingVerificationRequest.InRoom -> TODO()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshData()
|
refreshData()
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package org.matrix.android.sdk.internal.crypto
|
package org.matrix.android.sdk.internal.crypto
|
||||||
|
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest
|
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_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_RECIPROCATE
|
||||||
import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_SAS
|
import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_SAS
|
||||||
|
import timber.log.Timber
|
||||||
import uniffi.olm.OlmMachine
|
import uniffi.olm.OlmMachine
|
||||||
import uniffi.olm.OutgoingVerificationRequest
|
import uniffi.olm.OutgoingVerificationRequest
|
||||||
|
import uniffi.olm.Sas
|
||||||
import uniffi.olm.StartSasResult
|
import uniffi.olm.StartSasResult
|
||||||
import uniffi.olm.VerificationRequest
|
import uniffi.olm.VerificationRequest
|
||||||
|
|
||||||
|
@ -39,6 +43,8 @@ internal class VerificationRequest(
|
||||||
private val sender: RequestSender,
|
private val sender: RequestSender,
|
||||||
private val listeners: ArrayList<VerificationService.Listener>,
|
private val listeners: ArrayList<VerificationService.Listener>,
|
||||||
) {
|
) {
|
||||||
|
private val uiHandler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
private fun refreshData() {
|
private fun refreshData() {
|
||||||
val request = this.machine.getVerificationRequest(this.inner.otherUserId, this.inner.flowId)
|
val request = this.machine.getVerificationRequest(this.inner.otherUserId, this.inner.flowId)
|
||||||
|
|
||||||
|
@ -49,6 +55,18 @@ internal class VerificationRequest(
|
||||||
return
|
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? {
|
internal fun startQrVerification(): QrCodeVerification? {
|
||||||
val qrcode = this.machine.startQrVerification(this.inner.otherUserId, this.inner.flowId)
|
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> =
|
val stringMethods: MutableList<String> =
|
||||||
methods
|
methods
|
||||||
.map {
|
.map {
|
||||||
|
@ -81,8 +99,13 @@ internal class VerificationRequest(
|
||||||
stringMethods.add(VERIFICATION_METHOD_RECIPROCATE)
|
stringMethods.add(VERIFICATION_METHOD_RECIPROCATE)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.machine.acceptVerificationRequest(
|
val request = this.machine.acceptVerificationRequest(
|
||||||
this.inner.otherUserId, this.inner.flowId, stringMethods)
|
this.inner.otherUserId, this.inner.flowId, stringMethods)
|
||||||
|
|
||||||
|
if (request != null) {
|
||||||
|
this.sender.sendVerificationRequest(request)
|
||||||
|
this.dispatchRequestUpdated()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isCanceled(): Boolean {
|
fun isCanceled(): Boolean {
|
||||||
|
@ -100,11 +123,17 @@ internal class VerificationRequest(
|
||||||
return this.inner.isReady
|
return this.inner.isReady
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun startSasVerification(): StartSasResult? {
|
suspend fun startSasVerification(): Sas? {
|
||||||
refreshData()
|
refreshData()
|
||||||
|
|
||||||
return withContext(Dispatchers.IO) {
|
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.KeyVerificationDone
|
||||||
import org.matrix.android.sdk.internal.crypto.model.rest.KeyVerificationKey
|
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.KeyVerificationRequest
|
||||||
|
import org.matrix.android.sdk.internal.crypto.model.rest.KeyVerificationStart
|
||||||
import org.matrix.android.sdk.internal.session.SessionScope
|
import org.matrix.android.sdk.internal.session.SessionScope
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import uniffi.olm.OutgoingVerificationRequest
|
|
||||||
import uniffi.olm.Verification
|
import uniffi.olm.Verification
|
||||||
|
|
||||||
@SessionScope
|
@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) {
|
override fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) {
|
||||||
TODO()
|
TODO()
|
||||||
// setDeviceVerificationAction.handle(DeviceTrustLevel(false, true),
|
// setDeviceVerificationAction.handle(DeviceTrustLevel(false, true),
|
||||||
|
@ -224,17 +212,19 @@ constructor(
|
||||||
otherDeviceId: String,
|
otherDeviceId: String,
|
||||||
transactionId: String?
|
transactionId: String?
|
||||||
): String? {
|
): String? {
|
||||||
|
val flowId = transactionId ?: return null
|
||||||
|
|
||||||
// should check if already one (and cancel it)
|
// should check if already one (and cancel it)
|
||||||
return if (method == VerificationMethod.SAS) {
|
return if (method == VerificationMethod.SAS) {
|
||||||
val flowId = transactionId ?: return null
|
|
||||||
val request = this.getVerificationRequest(otherUserId, flowId)
|
val request = this.getVerificationRequest(otherUserId, flowId)
|
||||||
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val response = request?.startSasVerification()
|
val sas = request?.startSasVerification()
|
||||||
if (response != null) {
|
|
||||||
sendRequest(response.request)
|
if (sas != null) {
|
||||||
val sas = SasVerification(olmMachine.inner(), response.sas, requestSender, listeners)
|
val sasTransaction = SasVerification(olmMachine.inner(), sas, requestSender, listeners)
|
||||||
dispatchTxAdded(sas)
|
dispatchTxAdded(sasTransaction)
|
||||||
sas.transactionId
|
sasTransaction.transactionId
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
@ -337,15 +327,15 @@ constructor(
|
||||||
val request = this.getVerificationRequest(otherUserId, transactionId)
|
val request = this.getVerificationRequest(otherUserId, transactionId)
|
||||||
|
|
||||||
return if (request != null) {
|
return if (request != null) {
|
||||||
val outgoingRequest = request.acceptWithMethods(methods)
|
runBlocking { request.acceptWithMethods(methods) }
|
||||||
|
|
||||||
if (outgoingRequest != null) {
|
if (request.isReady()) {
|
||||||
runBlocking { sendRequest(outgoingRequest) }
|
|
||||||
dispatchRequestUpdated(request.toPendingVerificationRequest())
|
|
||||||
val qrcode = request.startQrVerification()
|
val qrcode = request.startQrVerification()
|
||||||
|
|
||||||
if (qrcode != null) {
|
if (qrcode != null) {
|
||||||
dispatchTxAdded(qrcode)
|
dispatchTxAdded(qrcode)
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
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) {
|
override fun transactionUpdated(tx: VerificationTransaction) {
|
||||||
dispatchTxUpdated(tx)
|
dispatchTxUpdated(tx)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue