From a726ebab39d8808e233af0a2ede948c5de844c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 29 Jul 2020 13:53:33 +0200 Subject: [PATCH] crypto: Allow Sas objects to be canceled. --- matrix_sdk_crypto/src/verification/sas.rs | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/matrix_sdk_crypto/src/verification/sas.rs b/matrix_sdk_crypto/src/verification/sas.rs index 28cdf4e5..ad82dd3e 100644 --- a/matrix_sdk_crypto/src/verification/sas.rs +++ b/matrix_sdk_crypto/src/verification/sas.rs @@ -165,6 +165,21 @@ impl Sas { }) } + /// Cancel the verification. + /// + /// This cancels the verification with the `CancelCode::User`. + /// + /// Returns None if the `Sas` object is already in a canceled state, + /// otherwise it returns a request that needs to be sent out. + pub fn cancel(&self) -> Option { + let mut guard = self.inner.lock().unwrap(); + let sas: InnerSas = (*guard).clone(); + let (sas, content) = sas.cancel(); + *guard = sas; + + content.map(|c| self.content_to_request(c)) + } + /// Are we in a state where we can show the short auth string. pub fn can_be_presented(&self) -> bool { self.inner.lock().unwrap().can_be_presented() @@ -251,6 +266,21 @@ impl InnerSas { } } + fn cancel(self) -> (InnerSas, Option) { + let sas = match self { + InnerSas::Created(s) => s.cancel(CancelCode::User), + InnerSas::Started(s) => s.cancel(CancelCode::User), + InnerSas::Accepted(s) => s.cancel(CancelCode::User), + InnerSas::KeyRecieved(s) => s.cancel(CancelCode::User), + InnerSas::MacReceived(s) => s.cancel(CancelCode::User), + _ => return (self, None), + }; + + let content = sas.as_content(); + + (InnerSas::Canceled(sas), Some(content)) + } + fn confirm(self) -> (InnerSas, Option) { match self { InnerSas::KeyRecieved(s) => {