crypto: Use the new CancelInfo struct

This commit is contained in:
Damir Jelić 2021-07-19 14:21:11 +02:00
parent 7650e43362
commit b33537fd6e
7 changed files with 50 additions and 33 deletions

View file

@ -208,6 +208,7 @@ internal class QrCodeVerification(
get() { get() {
refreshData() refreshData()
val inner = this.inner val inner = this.inner
val cancelInfo = inner?.cancelInfo
return if (inner != null) { return if (inner != null) {
when { when {
@ -215,9 +216,9 @@ internal class QrCodeVerification(
inner.reciprocated -> VerificationTxState.Started inner.reciprocated -> VerificationTxState.Started
inner.hasBeenConfirmed -> VerificationTxState.WaitingOtherReciprocateConfirm inner.hasBeenConfirmed -> VerificationTxState.WaitingOtherReciprocateConfirm
inner.otherSideScanned -> VerificationTxState.QrScannedByOther inner.otherSideScanned -> VerificationTxState.QrScannedByOther
inner.isCancelled -> { cancelInfo != null -> {
val cancelCode = safeValueOf(inner.cancelCode) val cancelCode = safeValueOf(cancelInfo.cancelCode)
val byMe = inner.cancelledByUs ?: false val byMe = cancelInfo.cancelledByUs
VerificationTxState.Cancelled(cancelCode, byMe) VerificationTxState.Cancelled(cancelCode, byMe)
} }
else -> { else -> {

View file

@ -82,11 +82,11 @@ internal class SasVerification(
override var state: VerificationTxState override var state: VerificationTxState
get() { get() {
refreshData() refreshData()
val cancelInfo = this.inner.cancelInfo
return when { return when {
this.inner.isCancelled -> { cancelInfo != null -> {
val cancelCode = safeValueOf(this.inner.cancelCode) val cancelCode = safeValueOf(cancelInfo.cancelCode)
val byMe = this.inner.cancelledByUs ?: false VerificationTxState.Cancelled(cancelCode, cancelInfo.cancelledByUs)
VerificationTxState.Cancelled(cancelCode, byMe)
} }
this.inner.isDone -> VerificationTxState.Verified this.inner.isDone -> VerificationTxState.Verified
this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted

View file

@ -180,11 +180,10 @@ internal class VerificationRequest(
fun toPendingVerificationRequest(): PendingVerificationRequest { fun toPendingVerificationRequest(): PendingVerificationRequest {
refreshData() refreshData()
val code = this.inner.cancelCode val cancelInfo = this.inner.cancelInfo
val cancelCode = val cancelCode =
if (code != null) { if (cancelInfo != null) {
safeValueOf(code) safeValueOf(cancelInfo.cancelCode)
} else { } else {
null null
} }

View file

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

View file

@ -8,8 +8,8 @@ pub use device::Device;
pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError};
pub use logger::{set_logger, Logger}; pub use logger::{set_logger, Logger};
pub use machine::{ pub use machine::{
KeyRequestPair, OlmMachine, QrCode, RequestVerificationResult, Sas, ScanResult, StartSasResult, CancelInfo, KeyRequestPair, OlmMachine, QrCode, RequestVerificationResult, Sas, ScanResult,
Verification, VerificationRequest, StartSasResult, Verification, VerificationRequest,
}; };
pub use responses::{ pub use responses::{
DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType,

View file

@ -30,9 +30,10 @@ use tokio::runtime::Runtime;
use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid}; use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid};
use matrix_sdk_crypto::{ use matrix_sdk_crypto::{
decrypt_key_export, encrypt_key_export, matrix_qrcode::QrVerificationData, EncryptionSettings, decrypt_key_export, encrypt_key_export, matrix_qrcode::QrVerificationData,
LocalTrust, OlmMachine as InnerMachine, QrVerification as InnerQr, Sas as InnerSas, CancelInfo as RustCancelInfo, EncryptionSettings, LocalTrust, OlmMachine as InnerMachine,
Verification as RustVerification, VerificationRequest as InnerVerificationRequest, QrVerification as InnerQr, Sas as InnerSas, Verification as RustVerification,
VerificationRequest as InnerVerificationRequest,
}; };
use crate::{ use crate::{
@ -61,8 +62,7 @@ pub struct Sas {
pub have_we_confirmed: bool, pub have_we_confirmed: bool,
pub is_cancelled: bool, pub is_cancelled: bool,
pub is_done: bool, pub is_done: bool,
pub cancel_code: Option<String>, pub cancel_info: Option<CancelInfo>,
pub cancelled_by_us: Option<bool>,
pub has_been_accepted: bool, pub has_been_accepted: bool,
pub we_started: bool, pub we_started: bool,
pub can_be_presented: bool, pub can_be_presented: bool,
@ -81,8 +81,7 @@ pub struct QrCode {
pub other_side_scanned: bool, pub other_side_scanned: bool,
pub has_been_confirmed: bool, pub has_been_confirmed: bool,
pub reciprocated: bool, pub reciprocated: bool,
pub cancel_code: Option<String>, pub cancel_info: Option<CancelInfo>,
pub cancelled_by_us: Option<bool>,
} }
impl From<InnerQr> for QrCode { impl From<InnerQr> for QrCode {
@ -92,8 +91,7 @@ impl From<InnerQr> for QrCode {
flow_id: qr.flow_id().as_str().to_owned(), flow_id: qr.flow_id().as_str().to_owned(),
is_cancelled: qr.is_cancelled(), is_cancelled: qr.is_cancelled(),
is_done: qr.is_done(), is_done: qr.is_done(),
cancel_code: qr.cancel_code().map(|c| c.to_string()), cancel_info: qr.cancel_info().map(|c| c.into()),
cancelled_by_us: qr.cancelled_by_us(),
reciprocated: qr.reciprocated(), reciprocated: qr.reciprocated(),
we_started: qr.we_started(), we_started: qr.we_started(),
other_side_scanned: qr.has_been_scanned(), other_side_scanned: qr.has_been_scanned(),
@ -104,6 +102,22 @@ impl From<InnerQr> for QrCode {
} }
} }
pub struct CancelInfo {
pub reason: String,
pub cancel_code: String,
pub cancelled_by_us: bool,
}
impl From<RustCancelInfo> for CancelInfo {
fn from(c: RustCancelInfo) -> Self {
Self {
reason: c.reason().to_owned(),
cancel_code: c.cancel_code().to_string(),
cancelled_by_us: c.cancelled_by_us(),
}
}
}
pub struct StartSasResult { pub struct StartSasResult {
pub sas: Sas, pub sas: Sas,
pub request: OutgoingVerificationRequest, pub request: OutgoingVerificationRequest,
@ -126,11 +140,10 @@ impl From<InnerSas> for Sas {
timed_out: sas.timed_out(), timed_out: sas.timed_out(),
supports_emoji: sas.supports_emoji(), supports_emoji: sas.supports_emoji(),
have_we_confirmed: sas.have_we_confirmed(), have_we_confirmed: sas.have_we_confirmed(),
cancel_code: sas.cancel_code().map(|c| c.as_str().to_owned()),
we_started: sas.we_started(), we_started: sas.we_started(),
room_id: sas.room_id().map(|r| r.to_string()), room_id: sas.room_id().map(|r| r.to_string()),
cancelled_by_us: sas.cancelled_by_us(),
has_been_accepted: sas.has_been_accepted(), has_been_accepted: sas.has_been_accepted(),
cancel_info: sas.cancel_info().map(|c| c.into()),
} }
} }
} }
@ -148,11 +161,11 @@ pub struct VerificationRequest {
pub is_done: bool, pub is_done: bool,
pub is_ready: bool, pub is_ready: bool,
pub room_id: Option<String>, pub room_id: Option<String>,
pub cancel_code: Option<String>,
pub we_started: bool, pub we_started: bool,
pub is_passive: bool, pub is_passive: bool,
pub their_methods: Option<Vec<String>>, pub their_methods: Option<Vec<String>>,
pub our_methods: Option<Vec<String>>, pub our_methods: Option<Vec<String>>,
pub cancel_info: Option<CancelInfo>,
} }
impl From<InnerVerificationRequest> for VerificationRequest { impl From<InnerVerificationRequest> for VerificationRequest {
@ -165,9 +178,9 @@ impl From<InnerVerificationRequest> for VerificationRequest {
is_done: v.is_done(), is_done: v.is_done(),
is_ready: v.is_ready(), is_ready: v.is_ready(),
room_id: v.room_id().map(|r| r.to_string()), room_id: v.room_id().map(|r| r.to_string()),
cancel_code: v.cancel_code().map(|c| c.as_str().to_owned()),
we_started: v.we_started(), we_started: v.we_started(),
is_passive: v.is_passive(), is_passive: v.is_passive(),
cancel_info: v.cancel_info().map(|c| c.into()),
their_methods: v their_methods: v
.their_supported_methods() .their_supported_methods()
.map(|v| v.into_iter().map(|m| m.to_string()).collect()), .map(|v| v.into_iter().map(|m| m.to_string()).collect()),

View file

@ -65,6 +65,12 @@ dictionary Device {
boolean cross_signing_trusted; boolean cross_signing_trusted;
}; };
dictionary CancelInfo {
string cancel_code;
string reason;
boolean cancelled_by_us;
};
dictionary StartSasResult { dictionary StartSasResult {
Sas sas; Sas sas;
OutgoingVerificationRequest request; OutgoingVerificationRequest request;
@ -74,10 +80,8 @@ dictionary Sas {
string other_user_id; string other_user_id;
string other_device_id; string other_device_id;
string flow_id; string flow_id;
string? cancel_code;
string? room_id; string? room_id;
boolean we_started; boolean we_started;
boolean? cancelled_by_us;
boolean has_been_accepted; boolean has_been_accepted;
boolean have_we_confirmed; boolean have_we_confirmed;
boolean is_done; boolean is_done;
@ -85,6 +89,7 @@ dictionary Sas {
boolean can_be_presented; boolean can_be_presented;
boolean timed_out; boolean timed_out;
boolean supports_emoji; boolean supports_emoji;
CancelInfo? cancel_info;
}; };
dictionary ScanResult { dictionary ScanResult {
@ -97,15 +102,14 @@ dictionary QrCode {
string other_user_id; string other_user_id;
string other_device_id; string other_device_id;
string flow_id; string flow_id;
string? cancel_code;
string? room_id; string? room_id;
boolean reciprocated; boolean reciprocated;
boolean we_started; boolean we_started;
boolean has_been_confirmed; boolean has_been_confirmed;
boolean? cancelled_by_us;
boolean is_done; boolean is_done;
boolean is_cancelled; boolean is_cancelled;
boolean other_side_scanned; boolean other_side_scanned;
CancelInfo? cancel_info;
}; };
dictionary VerificationRequest { dictionary VerificationRequest {
@ -118,7 +122,7 @@ dictionary VerificationRequest {
boolean we_started; boolean we_started;
boolean is_passive; boolean is_passive;
string? room_id; string? room_id;
string? cancel_code; CancelInfo? cancel_info;
sequence<string>? their_methods; sequence<string>? their_methods;
sequence<string>? our_methods; sequence<string>? our_methods;