crypto: Add support to accept the short SAS verification flow

This commit is contained in:
Damir Jelić 2021-06-29 09:21:32 +02:00
parent 03499b5309
commit 53b3f54808
11 changed files with 74 additions and 23 deletions

View File

@ -32,5 +32,7 @@ interface SasVerificationTransaction : VerificationTransaction {
*/
fun userHasVerifiedShortCode()
fun acceptVerification()
fun shortCodeDoesNotMatch()
}

View File

@ -40,7 +40,7 @@ internal class SasVerification(
private var inner: Sas,
private val sender: RequestSender,
private val listeners: ArrayList<VerificationService.Listener>,
) :
) :
SasVerificationTransaction {
private val uiHandler = Handler(Looper.getMainLooper())
@ -58,10 +58,11 @@ internal class SasVerification(
private fun refreshData() {
when (val verification = this.machine.getVerification(this.inner.otherUserId, this.inner.flowId)) {
is Verification.SasV1 -> {
is Verification.SasV1 -> {
this.inner = verification.sas
}
else -> {}
else -> {
}
}
return
@ -73,7 +74,8 @@ internal class SasVerification(
override var otherDeviceId: String?
get() = this.inner.otherDeviceId
@Suppress("UNUSED_PARAMETER")
set(value) {}
set(value) {
}
override val otherUserId: String = this.inner.otherUserId
@ -81,21 +83,21 @@ internal class SasVerification(
get() {
refreshData()
return when {
this.inner.isDone -> VerificationTxState.Verified
this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted
this.inner.canBePresented -> VerificationTxState.ShortCodeReady
this.inner.isCancelled -> {
this.inner.isCancelled -> {
val cancelCode = safeValueOf(this.inner.cancelCode)
val byMe = this.inner.cancelledByUs ?: false
VerificationTxState.Cancelled(cancelCode, byMe)
}
else -> {
VerificationTxState.Started
}
this.inner.isDone -> VerificationTxState.Verified
this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted
this.inner.canBePresented -> VerificationTxState.ShortCodeReady
this.inner.hasBeenAccepted -> VerificationTxState.Accepted
else -> VerificationTxState.OnStarted
}
}
@Suppress("UNUSED_PARAMETER")
set(v) {}
set(v) {
}
override val transactionId: String
get() = this.inner.flowId
@ -132,6 +134,10 @@ internal class SasVerification(
sendRequest(request)
}
override fun acceptVerification() {
runBlocking { accept() }
}
suspend fun accept() {
val request = this.machine.acceptSasVerification(this.inner.otherUserId, inner.flowId)

View File

@ -33,9 +33,7 @@ import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_REC
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
internal class VerificationRequest(

View File

@ -82,6 +82,10 @@ internal class DefaultIncomingSASDefaultVerificationTransaction(
}
}
override fun acceptVerification() {
this.performAccept()
}
override fun onVerificationStart(startReq: ValidVerificationInfoStart.SasVerificationInfoStart) {
Timber.v("## SAS I: received verification request from state $state")
if (state != VerificationTxState.None) {

View File

@ -85,6 +85,10 @@ internal class DefaultOutgoingSASDefaultVerificationTransaction(
cancel(CancelCode.UnexpectedMessage)
}
override fun acceptVerification() {
return
}
fun start() {
if (state != VerificationTxState.None) {
Timber.e("## SAS O: start verification from invalid state")

View File

@ -103,10 +103,11 @@ constructor(
}
override fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) {
// TODO this doesn't seem to be used anymore?
runBlocking { olmMachine.markDeviceAsTrusted(userId, deviceID) }
}
fun onEvent(event: Event) = when (event.getClearType()) {
suspend fun onEvent(event: Event) = when (event.getClearType()) {
// TODO most of those methods do the same, we just need to get the
// flow id and the sender from the event, can we add a generic method for this?
EventType.KEY_VERIFICATION_START -> onStart(event)
@ -133,12 +134,36 @@ constructor(
this.getVerificationRequest(sender, flowId)?.dispatchRequestUpdated()
getAndDispatch(sender, flowId)
}
private fun onStart(event: Event) {
private suspend fun onStart(event: Event) {
val content = event.getClearContent().toModel<KeyVerificationStart>() ?: return
val flowId = content.transactionId ?: return
val sender = event.senderId ?: return
getAndDispatch(sender, flowId)
val verification = this.getExistingTransaction(sender, flowId) ?: return
val request = this.getVerificationRequest(sender, flowId)
if (request != null && request.isReady()) {
// If this is a SAS verification originating from a `m.key.verification.request`
// event we auto-accept here considering that we either initiated the request or
// accepted the request, otherwise it's a QR code verification, just dispatch an update.
if (verification is SasVerification) {
// Accept dispatches an update, no need to do it twice.
verification.accept()
} else {
dispatchTxUpdated(verification)
}
} else {
Timber.d("HELLOOOOO DISPATCHING NEW VERIFICATIONO $verification")
// This didn't originate from a request, so tell our listeners that
// this is a new verification.
dispatchTxAdded(verification)
// The IncomingVerificationRequestHandler seems to only listen to updates
// so let's trigger an update after the addition as well.
dispatchTxUpdated(verification)
}
}
private fun onDone(event: Event) {
@ -292,7 +317,7 @@ constructor(
): Boolean {
Timber.e("## TRYING TO READY PENDING ROOM VERIFICATION")
// TODO do the same as readyPendingVerification
return true
TODO()
}
private fun getVerificationRequest(otherUserId: String, transactionId: String): VerificationRequest? {

View File

@ -25,11 +25,11 @@ features = ["lax_deserialize"]
[dependencies.matrix-sdk-common]
git = "https://github.com/matrix-org/matrix-rust-sdk/"
rev = "59a07da99e76a162b71b026ed244fb0cbc39f0c9"
rev = "d2e4b3f3bbcdc139560cdacbdf62dedab6f156b9"
[dependencies.matrix-sdk-crypto]
git = "https://github.com/matrix-org/matrix-rust-sdk/"
rev = "59a07da99e76a162b71b026ed244fb0cbc39f0c9"
rev = "d2e4b3f3bbcdc139560cdacbdf62dedab6f156b9"
features = ["sled_cryptostore"]
[dependencies.tokio]

View File

@ -63,6 +63,7 @@ pub struct Sas {
pub is_done: bool,
pub cancel_code: Option<String>,
pub cancelled_by_us: Option<bool>,
pub has_been_accepted: bool,
pub we_started: bool,
pub can_be_presented: bool,
pub supports_emoji: bool,
@ -122,6 +123,7 @@ impl From<InnerSas> for Sas {
we_started: sas.we_started(),
room_id: sas.room_id().map(|r| r.to_string()),
cancelled_by_us: sas.cancelled_by_us(),
has_been_accepted: sas.has_been_accepted(),
}
}
}

View File

@ -78,6 +78,7 @@ dictionary Sas {
string? room_id;
boolean we_started;
boolean? cancelled_by_us;
boolean has_been_accepted;
boolean have_we_confirmed;
boolean is_done;
boolean is_cancelled;

View File

@ -41,7 +41,6 @@ import org.matrix.android.sdk.api.session.crypto.crosssigning.MASTER_KEY_SSSS_NA
import org.matrix.android.sdk.api.session.crypto.crosssigning.SELF_SIGNING_KEY_SSSS_NAME
import org.matrix.android.sdk.api.session.crypto.crosssigning.USER_SIGNING_KEY_SSSS_NAME
import org.matrix.android.sdk.api.session.crypto.verification.CancelCode
import org.matrix.android.sdk.api.session.crypto.verification.IncomingSasVerificationTransaction
import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest
import org.matrix.android.sdk.api.session.crypto.verification.QrCodeVerificationTransaction
import org.matrix.android.sdk.api.session.crypto.verification.SasVerificationTransaction
@ -466,11 +465,18 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(
// is this an incoming with that user
if (tx.isIncoming && tx.otherUserId == state.otherUserMxItem?.id) {
// Also auto accept incoming if needed!
// TODO is state.transactionId ever null for self verifications, doesn't seem
// like this will ever trigger
if (tx is SasVerificationTransaction && tx.state == VerificationTxState.OnStarted) {
tx.acceptVerification()
}
/*
if (tx is IncomingSasVerificationTransaction) {
if (tx.uxState == IncomingSasVerificationTransaction.UxState.SHOW_ACCEPT) {
tx.performAccept()
}
}
*/
// Use this one!
setState {
copy(

View File

@ -85,7 +85,7 @@ import im.vector.app.features.terms.ReviewTermsActivity
import im.vector.app.features.widgets.WidgetActivity
import im.vector.app.features.widgets.WidgetArgsBuilder
import im.vector.app.space
import org.matrix.android.sdk.api.session.crypto.verification.IncomingSasVerificationTransaction
import org.matrix.android.sdk.api.session.crypto.verification.SasVerificationTransaction
import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoom
import org.matrix.android.sdk.api.session.terms.TermsService
import org.matrix.android.sdk.api.session.widgets.model.Widget
@ -159,7 +159,10 @@ class DefaultNavigator @Inject constructor(
val session = sessionHolder.getSafeActiveSession() ?: return
val tx = session.cryptoService().verificationService().getExistingTransaction(otherUserId, sasTransactionId)
?: return
(tx as? IncomingSasVerificationTransaction)?.performAccept()
if (tx is SasVerificationTransaction && tx.isIncoming) {
tx.acceptVerification()
}
if (context is AppCompatActivity) {
VerificationBottomSheet.withArgs(
roomId = null,