7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | use std::sync::Arc; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | use futures_util::{Stream, StreamExt}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | use matrix_sdk_common::executor::Handle; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | use matrix_sdk_crypto::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | CancelInfo as RustCancelInfo, QrVerification as InnerQr, QrVerificationState, Sas as InnerSas, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | SasState as RustSasState, Verification as InnerVerification, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | VerificationRequest as InnerVerificationRequest, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | VerificationRequestState as RustVerificationRequestState, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | matrix_sdk_qrcode::QrVerificationData, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | use ruma::events::key::verification::VerificationMethod; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | use vodozemac::{base64_decode, base64_encode}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use crate::{CryptoStoreError, OutgoingVerificationRequest, SignatureUploadRequest}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | /// Listener that will be passed over the FFI to report changes to a SAS |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | /// verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | pub trait SasListener: Send { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | /// The callback that should be called on the Rust side |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | /// * `state` - The current state of the SAS verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | fn on_change(&self, state: SasState); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | /// An Enum describing the state the SAS verification is in. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | pub enum SasState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | /// The verification has been created, the protocols that should be used |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | /// have been proposed to the other party. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | Created, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | /// The verification has been started, the other party proposed the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | /// protocols that should be used and that can be accepted. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | Started, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | /// The verification has been accepted and both sides agreed to a set of |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | /// protocols that will be used for the verification process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | Accepted, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | /// The public keys have been exchanged and the short auth string can be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | /// presented to the user. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | KeysExchanged { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | /// The emojis that represent the short auth string, will be `None` if |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | /// the emoji SAS method wasn't one of accepted protocols. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | emojis: Option<Vec<i32>>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | /// The list of decimals that represent the short auth string. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | decimals: Vec<i32>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | /// The verification process has been confirmed from our side, we're waiting |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | /// for the other side to confirm as well. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | Confirmed, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | /// The verification process has been successfully concluded. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | /// The verification process has been cancelled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | Cancelled { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | /// Information about the reason of the cancellation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | cancel_info: CancelInfo, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | impl From<RustSasState> for SasState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | fn from(s: RustSasState) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | match s { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | RustSasState::Created { .. } => Self::Created, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | RustSasState::Started { .. } => Self::Started, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | RustSasState::Accepted { .. } => Self::Accepted, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | RustSasState::KeysExchanged { emojis, decimals } => Self::KeysExchanged { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | emojis: emojis.map(|e| e.indices.map(|i| i as i32).to_vec()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | decimals: [decimals.0.into(), decimals.1.into(), decimals.2.into()].to_vec(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | RustSasState::Confirmed => Self::Confirmed, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | RustSasState::Done { .. } => Self::Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | RustSasState::Cancelled(c) => Self::Cancelled { cancel_info: c.into() }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | /// Enum representing the different verification flows we support. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | pub struct Verification { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | pub(crate) inner: InnerVerification, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | pub(crate) runtime: Handle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | impl Verification { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | /// Try to represent the `Verification` as an `Sas` verification object, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | /// returns `None` if the verification is not a `Sas` verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | pub fn as_sas(&self) -> Option<Arc<Sas>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | if let InnerVerification::SasV1(sas) = &self.inner { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | Some(Sas { inner: sas.clone(), runtime: self.runtime.to_owned() }.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | None |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | /// Try to represent the `Verification` as an `QrCode` verification object, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | /// returns `None` if the verification is not a `QrCode` verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | pub fn as_qr(&self) -> Option<Arc<QrCode>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | if let InnerVerification::QrV1(qr) = &self.inner { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | Some(QrCode { inner: qr.clone(), runtime: self.runtime.to_owned() }.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | None |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 106 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 107 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 108 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 109 | /// The `m.sas.v1` verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 110 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 111 | pub struct Sas { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 112 | pub(crate) inner: Box<InnerSas>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 113 | pub(crate) runtime: Handle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 114 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 115 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 116 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 117 | impl Sas { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 118 | /// Get the user id of the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 119 | pub fn other_user_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 120 | self.inner.other_user_id().to_string() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 121 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 122 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 123 | /// Get the device ID of the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 124 | pub fn other_device_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 125 | self.inner.other_device_id().to_string() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 126 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 127 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 128 | /// Get the unique ID that identifies this SAS verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 129 | pub fn flow_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 130 | self.inner.flow_id().as_str().to_owned() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 131 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 132 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 133 | /// Get the room id if the verification is happening inside a room. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 134 | pub fn room_id(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 135 | self.inner.room_id().map(|r| r.to_string()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 136 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 137 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 138 | /// Is the SAS flow done. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 139 | pub fn is_done(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 140 | self.inner.is_done() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 141 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 142 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 143 | /// Did we initiate the verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 144 | pub fn we_started(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 145 | self.inner.we_started() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 146 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 147 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 148 | /// Accept that we're going forward with the short auth string verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 149 | pub fn accept(&self) -> Option<OutgoingVerificationRequest> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 150 | self.inner.accept().map(|r| r.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 151 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 152 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 153 | /// Confirm a verification was successful. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 154 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 155 | /// This method should be called if a short auth string should be confirmed |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 156 | /// as matching. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 157 | pub fn confirm(&self) -> Result<Option<ConfirmVerificationResult>, CryptoStoreError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 158 | let (requests, signature_request) = self.runtime.block_on(self.inner.confirm())?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 159 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 160 | let requests = requests.into_iter().map(|r| r.into()).collect(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 161 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 162 | Ok(Some(ConfirmVerificationResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 163 | requests, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 164 | signature_request: signature_request.map(|s| s.into()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 165 | })) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 166 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 167 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 168 | /// Cancel the SAS verification using the given cancel code. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 169 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 170 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 171 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 172 | /// * `cancel_code` - The error code for why the verification was cancelled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 173 | /// manual cancellatio usually happens with `m.user` cancel code. The full |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 174 | /// list of cancel codes can be found in the [spec] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 175 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 176 | /// [spec]: https://spec.matrix.org/unstable/client-server-api/#mkeyverificationcancel |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 177 | pub fn cancel(&self, cancel_code: String) -> Option<OutgoingVerificationRequest> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 178 | self.inner.cancel_with_code(cancel_code.into()).map(|r| r.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 179 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 180 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 181 | /// Get a list of emoji indices of the emoji representation of the short |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 182 | /// auth string. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 183 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 184 | /// *Note*: A SAS verification needs to be started and in the presentable |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 185 | /// state for this to return the list of emoji indices, otherwise returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 186 | /// `None`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 187 | pub fn get_emoji_indices(&self) -> Option<Vec<i32>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 188 | self.inner.emoji_index().map(|v| v.iter().map(|i| (*i).into()).collect()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 189 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 190 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 191 | /// Get the decimal representation of the short auth string. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 192 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 193 | /// *Note*: A SAS verification needs to be started and in the presentable |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 194 | /// state for this to return the list of decimals, otherwise returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 195 | /// `None`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 196 | pub fn get_decimals(&self) -> Option<Vec<i32>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 197 | self.inner.decimals().map(|v| [v.0.into(), v.1.into(), v.2.into()].to_vec()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 198 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 199 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 200 | /// Set a listener for changes in the SAS verification process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 201 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 202 | /// The given callback will be called whenever the state changes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 203 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 204 | /// This method can be used to react to changes in the state of the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 205 | /// verification process, or rather the method can be used to handle |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 206 | /// each step of the verification process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 207 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 208 | /// This method will spawn a tokio task on the Rust side, once we reach the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 209 | /// Done or Cancelled state, the task will stop listening for changes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 210 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 211 | /// # Flowchart |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 212 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 213 | /// The flow of the verification process is pictured below. Please note |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 214 | /// that the process can be cancelled at each step of the process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 215 | /// Either side can cancel the process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 216 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 217 | /// ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 218 | /// ┌───────┐ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 219 | /// │Started│ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 220 | /// └───┬───┘ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 221 | /// │ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 222 | /// ┌────⌄───┐ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 223 | /// │Accepted│ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 224 | /// └────┬───┘ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 225 | /// │ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 226 | /// ┌───────⌄──────┐ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 227 | /// │Keys Exchanged│ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 228 | /// └───────┬──────┘ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 229 | /// │ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 230 | /// ________⌄________ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 231 | /// ╱ ╲ ┌─────────┐ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 232 | /// ╱ Does the short ╲______│Cancelled│ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 233 | /// ╲ auth string match ╱ no └─────────┘ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 234 | /// ╲_________________╱ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 235 | /// │yes |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 236 | /// │ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 237 | /// ┌────⌄────┐ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 238 | /// │Confirmed│ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 239 | /// └────┬────┘ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 240 | /// │ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 241 | /// ┌───⌄───┐ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 242 | /// │ Done │ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 243 | /// └───────┘ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 244 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 245 | pub fn set_changes_listener(&self, listener: Box<dyn SasListener>) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 246 | let stream = self.inner.changes(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 247 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 248 | self.runtime.spawn(Self::changes_listener(stream, listener)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 249 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 250 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 251 | /// Get the current state of the SAS verification process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 252 | pub fn state(&self) -> SasState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 253 | self.inner.state().into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 254 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 255 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 256 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 257 | impl Sas { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 258 | async fn changes_listener( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 259 | mut stream: impl Stream<Item = RustSasState> + std::marker::Unpin, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 260 | listener: Box<dyn SasListener>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 261 | ) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 262 | while let Some(state) = stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 263 | // If we receive a done or a cancelled state we're at the end of our road, we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 264 | // break out of the loop to deallocate the stream and finish the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 265 | // task. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 266 | let should_break = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 267 | matches!(state, RustSasState::Done { .. } | RustSasState::Cancelled { .. }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 268 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 269 | listener.on_change(state.into()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 270 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 271 | if should_break { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 272 | break; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 273 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 274 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 275 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 276 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 277 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 278 | /// Listener that will be passed over the FFI to report changes to a QrCode |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 279 | /// verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 280 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 281 | pub trait QrCodeListener: Send { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 282 | /// The callback that should be called on the Rust side |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 283 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 284 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 285 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 286 | /// * `state` - The current state of the QrCode verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 287 | fn on_change(&self, state: QrCodeState); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 288 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 289 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 290 | /// An Enum describing the state the QrCode verification is in. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 291 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 292 | pub enum QrCodeState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 293 | /// The QR verification has been started. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 294 | Started, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 295 | /// The QR verification has been scanned by the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 296 | Scanned, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 297 | /// The scanning of the QR code has been confirmed by us. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 298 | Confirmed, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 299 | /// We have successfully scanned the QR code and are able to send a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 300 | /// reciprocation event. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 301 | Reciprocated, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 302 | /// The verification process has been successfully concluded. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 303 | Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 304 | /// The verification process has been cancelled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 305 | Cancelled { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 306 | /// Information about the reason of the cancellation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 307 | cancel_info: CancelInfo, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 308 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 309 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 310 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 311 | impl From<QrVerificationState> for QrCodeState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 312 | fn from(value: QrVerificationState) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 313 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 314 | QrVerificationState::Started => Self::Started, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 315 | QrVerificationState::Scanned => Self::Scanned, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 316 | QrVerificationState::Confirmed => Self::Confirmed, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 317 | QrVerificationState::Reciprocated => Self::Reciprocated, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 318 | QrVerificationState::Done { .. } => Self::Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 319 | QrVerificationState::Cancelled(c) => Self::Cancelled { cancel_info: c.into() }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 320 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 321 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 322 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 323 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 324 | /// The `m.qr_code.scan.v1`, `m.qr_code.show.v1`, and `m.reciprocate.v1` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 325 | /// verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 326 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 327 | pub struct QrCode { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 328 | pub(crate) inner: Box<InnerQr>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 329 | pub(crate) runtime: Handle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 330 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 331 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 332 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 333 | impl QrCode { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 334 | /// Get the user id of the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 335 | pub fn other_user_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 336 | self.inner.other_user_id().to_string() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 337 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 338 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 339 | /// Get the device ID of the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 340 | pub fn other_device_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 341 | self.inner.other_device_id().to_string() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 342 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 343 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 344 | /// Get the unique ID that identifies this QR code verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 345 | pub fn flow_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 346 | self.inner.flow_id().as_str().to_owned() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 347 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 348 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 349 | /// Get the room id if the verification is happening inside a room. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 350 | pub fn room_id(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 351 | self.inner.room_id().map(|r| r.to_string()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 352 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 353 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 354 | /// Is the QR code verification done. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 355 | pub fn is_done(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 356 | self.inner.is_done() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 357 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 358 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 359 | /// Has the verification flow been cancelled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 360 | pub fn is_cancelled(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 361 | self.inner.is_cancelled() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 362 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 363 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 364 | /// Did we initiate the verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 365 | pub fn we_started(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 366 | self.inner.we_started() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 367 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 368 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 369 | /// Get the CancelInfo of this QR code verification object. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 370 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 371 | /// Will be `None` if the flow has not been cancelled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 372 | pub fn cancel_info(&self) -> Option<CancelInfo> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 373 | self.inner.cancel_info().map(|c| c.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 374 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 375 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 376 | /// Has the QR verification been scanned by the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 377 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 378 | /// When the verification object is in this state it's required that the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 379 | /// user confirms that the other side has scanned the QR code. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 380 | pub fn has_been_scanned(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 381 | self.inner.has_been_scanned() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 382 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 383 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 384 | /// Have we successfully scanned the QR code and are able to send a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 385 | /// reciprocation event. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 386 | pub fn reciprocated(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 387 | self.inner.reciprocated() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 388 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 389 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 390 | /// Cancel the QR code verification using the given cancel code. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 391 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 392 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 393 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 394 | /// * `cancel_code` - The error code for why the verification was cancelled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 395 | /// manual cancellatio usually happens with `m.user` cancel code. The full |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 396 | /// list of cancel codes can be found in the [spec] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 397 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 398 | /// [spec]: https://spec.matrix.org/unstable/client-server-api/#mkeyverificationcancel |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 399 | pub fn cancel(&self, cancel_code: String) -> Option<OutgoingVerificationRequest> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 400 | self.inner.cancel_with_code(cancel_code.into()).map(|r| r.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 401 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 402 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 403 | /// Confirm a verification was successful. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 404 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 405 | /// This method should be called if we want to confirm that the other side |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 406 | /// has scanned our QR code. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 407 | pub fn confirm(&self) -> Option<ConfirmVerificationResult> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 408 | self.inner.confirm_scanning().map(|r| ConfirmVerificationResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 409 | requests: vec![r.into()], |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 410 | signature_request: None, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 411 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 412 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 413 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 414 | /// Generate data that should be encoded as a QR code. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 415 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 416 | /// This method should be called right before a QR code should be displayed, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 417 | /// the returned data is base64 encoded (without padding) and needs to be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 418 | /// decoded on the other side before it can be put through a QR code |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 419 | /// generator. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 420 | pub fn generate_qr_code(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 421 | self.inner.to_bytes().map(base64_encode).ok() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 422 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 423 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 424 | /// Set a listener for changes in the QrCode verification process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 425 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 426 | /// The given callback will be called whenever the state changes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 427 | pub fn set_changes_listener(&self, listener: Box<dyn QrCodeListener>) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 428 | let stream = self.inner.changes(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 429 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 430 | self.runtime.spawn(Self::changes_listener(stream, listener)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 431 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 432 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 433 | /// Get the current state of the QrCode verification process. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 434 | pub fn state(&self) -> QrCodeState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 435 | self.inner.state().into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 436 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 437 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 438 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 439 | impl QrCode { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 440 | async fn changes_listener( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 441 | mut stream: impl Stream<Item = QrVerificationState> + std::marker::Unpin, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 442 | listener: Box<dyn QrCodeListener>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 443 | ) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 444 | while let Some(state) = stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 445 | // If we receive a done or a cancelled state we're at the end of our road, we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 446 | // break out of the loop to deallocate the stream and finish the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 447 | // task. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 448 | let should_break = matches!( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 449 | state, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 450 | QrVerificationState::Done { .. } | QrVerificationState::Cancelled { .. } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 451 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 452 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 453 | listener.on_change(state.into()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 454 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 455 | if should_break { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 456 | break; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 457 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 458 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 459 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 460 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 461 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 462 | /// Information on why a verification flow has been cancelled and by whom. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 463 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 464 | pub struct CancelInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 465 | /// The textual representation of the cancel reason |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 466 | pub reason: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 467 | /// The code describing the cancel reason |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 468 | pub cancel_code: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 469 | /// Was the verification flow cancelled by us |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 470 | pub cancelled_by_us: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 471 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 472 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 473 | impl From<RustCancelInfo> for CancelInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 474 | fn from(c: RustCancelInfo) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 475 | Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 476 | reason: c.reason().to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 477 | cancel_code: c.cancel_code().to_string(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 478 | cancelled_by_us: c.cancelled_by_us(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 479 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 480 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 481 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 482 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 483 | /// A result type for starting SAS verifications. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 484 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 485 | pub struct StartSasResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 486 | /// The SAS verification object that got created. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 487 | pub sas: Arc<Sas>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 488 | /// The request that needs to be sent out to notify the other side that a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 489 | /// SAS verification should start. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 490 | pub request: OutgoingVerificationRequest, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 491 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 492 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 493 | /// A result type for scanning QR codes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 494 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 495 | pub struct ScanResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 496 | /// The QR code verification object that got created. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 497 | pub qr: Arc<QrCode>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 498 | /// The request that needs to be sent out to notify the other side that a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 499 | /// QR code verification should start. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 500 | pub request: OutgoingVerificationRequest, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 501 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 502 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 503 | /// A result type for requesting verifications. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 504 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 505 | pub struct RequestVerificationResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 506 | /// The verification request object that got created. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 507 | pub verification: Arc<VerificationRequest>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 508 | /// The request that needs to be sent out to notify the other side that |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 509 | /// we're requesting verification to begin. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 510 | pub request: OutgoingVerificationRequest, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 511 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 512 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 513 | /// A result type for confirming verifications. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 514 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 515 | pub struct ConfirmVerificationResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 516 | /// The requests that needs to be sent out to notify the other side that we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 517 | /// confirmed the verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 518 | pub requests: Vec<OutgoingVerificationRequest>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 519 | /// A request that will upload signatures of the verified device or user, if |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 520 | /// the verification is completed and we're able to sign devices or users |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 521 | pub signature_request: Option<SignatureUploadRequest>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 522 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 523 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 524 | /// Listener that will be passed over the FFI to report changes to a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 525 | /// verification request. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 526 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 527 | pub trait VerificationRequestListener: Send { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 528 | /// The callback that should be called on the Rust side |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 529 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 530 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 531 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 532 | /// * `state` - The current state of the verification request. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 533 | fn on_change(&self, state: VerificationRequestState); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 534 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 535 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 536 | /// An Enum describing the state the QrCode verification is in. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 537 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 538 | pub enum VerificationRequestState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 539 | /// The verification request was sent |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 540 | Requested, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 541 | /// The verification request is ready to start a verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 542 | Ready { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 543 | /// The verification methods supported by the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 544 | their_methods: Vec<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 545 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 546 | /// The verification methods supported by the us. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 547 | our_methods: Vec<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 548 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 549 | /// The verification flow that was started with this request has finished. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 550 | Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 551 | /// The verification process has been cancelled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 552 | Cancelled { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 553 | /// Information about the reason of the cancellation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 554 | cancel_info: CancelInfo, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 555 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 556 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 557 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 558 | /// The verificatoin request object which then can transition into some concrete |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 559 | /// verification method |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 560 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 561 | pub struct VerificationRequest { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 562 | pub(crate) inner: InnerVerificationRequest, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 563 | pub(crate) runtime: Handle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 564 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 565 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 566 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 567 | impl VerificationRequest { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 568 | /// The id of the other user that is participating in this verification |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 569 | /// request. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 570 | pub fn other_user_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 571 | self.inner.other_user().to_string() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 572 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 573 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 574 | /// The id of the other device that is participating in this verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 575 | pub fn other_device_id(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 576 | self.inner.other_device_id().map(|d| d.to_string()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 577 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 578 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 579 | /// Get the unique ID of this verification request |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 580 | pub fn flow_id(&self) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 581 | self.inner.flow_id().as_str().to_owned() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 582 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 583 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 584 | /// Get the room id if the verification is happening inside a room. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 585 | pub fn room_id(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 586 | self.inner.room_id().map(|r| r.to_string()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 587 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 588 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 589 | /// Has the verification flow that was started with this request finished. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 590 | pub fn is_done(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 591 | self.inner.is_done() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 592 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 593 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 594 | /// Is the verification request ready to start a verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 595 | pub fn is_ready(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 596 | self.inner.is_ready() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 597 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 598 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 599 | /// Did we initiate the verification request |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 600 | pub fn we_started(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 601 | self.inner.we_started() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 602 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 603 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 604 | /// Has the verification request been answered by another device. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 605 | pub fn is_passive(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 606 | self.inner.is_passive() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 607 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 608 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 609 | /// Has the verification flow that been cancelled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 610 | pub fn is_cancelled(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 611 | self.inner.is_cancelled() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 612 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 613 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 614 | /// Get info about the cancellation if the verification request has been |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 615 | /// cancelled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 616 | pub fn cancel_info(&self) -> Option<CancelInfo> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 617 | self.inner.cancel_info().map(|v| v.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 618 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 619 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 620 | /// Get the supported verification methods of the other side. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 621 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 622 | /// Will be present only if the other side requested the verification or if |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 623 | /// we're in the ready state. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 624 | pub fn their_supported_methods(&self) -> Option<Vec<String>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 625 | self.inner.their_supported_methods().map(|m| m.iter().map(|m| m.to_string()).collect()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 626 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 627 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 628 | /// Get our own supported verification methods that we advertised. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 629 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 630 | /// Will be present only we requested the verification or if we're in the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 631 | /// ready state. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 632 | pub fn our_supported_methods(&self) -> Option<Vec<String>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 633 | self.inner.our_supported_methods().map(|m| m.iter().map(|m| m.to_string()).collect()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 634 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 635 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 636 | /// Accept a verification requests that we share with the given user with |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 637 | /// the given flow id. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 638 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 639 | /// This will move the verification request into the ready state. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 640 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 641 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 642 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 643 | /// * `user_id` - The ID of the user for which we would like to accept the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 644 | /// verification requests. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 645 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 646 | /// * `flow_id` - The ID that uniquely identifies the verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 647 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 648 | /// * `methods` - A list of verification methods that we want to advertise |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 649 | /// as supported. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 650 | pub fn accept(&self, methods: Vec<String>) -> Option<OutgoingVerificationRequest> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 651 | let methods = methods.into_iter().map(VerificationMethod::from).collect(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 652 | self.inner.accept_with_methods(methods).map(|r| r.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 653 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 654 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 655 | /// Cancel a verification for the given user with the given flow id using |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 656 | /// the given cancel code. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 657 | pub fn cancel(&self) -> Option<OutgoingVerificationRequest> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 658 | self.inner.cancel().map(|r| r.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 659 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 660 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 661 | /// Transition from a verification request into short auth string based |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 662 | /// verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 663 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 664 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 665 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 666 | /// * `user_id` - The ID of the user for which we would like to start the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 667 | /// SAS verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 668 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 669 | /// * `flow_id` - The ID of the verification request that initiated the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 670 | /// verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 671 | pub fn start_sas_verification(&self) -> Result<Option<StartSasResult>, CryptoStoreError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 672 | Ok(self.runtime.block_on(self.inner.start_sas())?.map(|(sas, r)| StartSasResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 673 | sas: Arc::new(Sas { inner: Box::new(sas), runtime: self.runtime.clone() }), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 674 | request: r.into(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 675 | })) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 676 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 677 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 678 | /// Transition from a verification request into QR code verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 679 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 680 | /// This method should be called when one wants to display a QR code so the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 681 | /// other side can scan it and move the QR code verification forward. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 682 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 683 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 684 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 685 | /// * `user_id` - The ID of the user for which we would like to start the QR |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 686 | /// code verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 687 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 688 | /// * `flow_id` - The ID of the verification request that initiated the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 689 | /// verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 690 | pub fn start_qr_verification(&self) -> Result<Option<Arc<QrCode>>, CryptoStoreError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 691 | Ok(self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 692 | .runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 693 | .block_on(self.inner.generate_qr_code())? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 694 | .map(|qr| QrCode { inner: Box::new(qr), runtime: self.runtime.clone() }.into())) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 695 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 696 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 697 | /// Pass data from a scanned QR code to an active verification request and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 698 | /// transition into QR code verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 699 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 700 | /// This requires an active `VerificationRequest` to succeed, returns `None` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 701 | /// if no `VerificationRequest` is found or if the QR code data is invalid. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 702 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 703 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 704 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 705 | /// * `user_id` - The ID of the user for which we would like to start the QR |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 706 | /// code verification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 707 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 708 | /// * `flow_id` - The ID of the verification request that initiated the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 709 | /// verification flow. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 710 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 711 | /// * `data` - The data that was extracted from the scanned QR code as an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 712 | /// base64 encoded string, without padding. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 713 | pub fn scan_qr_code(&self, data: String) -> Option<ScanResult> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 714 | let data = base64_decode(data).ok()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 715 | let data = QrVerificationData::from_bytes(data).ok()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 716 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 717 | if let Some(qr) = self.runtime.block_on(self.inner.scan_qr_code(data)).ok()? { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 718 | let request = qr.reciprocate()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 719 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 720 | Some(ScanResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 721 | qr: QrCode { inner: Box::new(qr), runtime: self.runtime.clone() }.into(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 722 | request: request.into(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 723 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 724 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 725 | None |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 726 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 727 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 728 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 729 | /// Set a listener for changes in the verification request |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 730 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 731 | /// The given callback will be called whenever the state changes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 732 | pub fn set_changes_listener(&self, listener: Box<dyn VerificationRequestListener>) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 733 | let stream = self.inner.changes(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 734 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 735 | self.runtime.spawn(Self::changes_listener(self.inner.to_owned(), stream, listener)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 736 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 737 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 738 | /// Get the current state of the verification request. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 739 | pub fn state(&self) -> VerificationRequestState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 740 | Self::convert_verification_request(&self.inner, self.inner.state()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 741 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 742 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 743 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 744 | impl VerificationRequest { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 745 | fn convert_verification_request( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 746 | request: &InnerVerificationRequest, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 747 | value: RustVerificationRequestState, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 748 | ) -> VerificationRequestState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 749 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 750 | // The clients do not need to distinguish `Created` and `Requested` state |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 751 | RustVerificationRequestState::Created { .. } => VerificationRequestState::Requested, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 752 | RustVerificationRequestState::Requested { .. } => VerificationRequestState::Requested, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 753 | RustVerificationRequestState::Ready { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 754 | their_methods, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 755 | our_methods, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 756 | other_device_data: _, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 757 | } => VerificationRequestState::Ready { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 758 | their_methods: their_methods.iter().map(|m| m.to_string()).collect(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 759 | our_methods: our_methods.iter().map(|m| m.to_string()).collect(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 760 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 761 | RustVerificationRequestState::Done => VerificationRequestState::Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 762 | RustVerificationRequestState::Transitioned { .. } => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 763 | let their_methods = request |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 764 | .their_supported_methods() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 765 | .expect("The transitioned state should know the other side's methods") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 766 | .into_iter() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 767 | .map(|m| m.to_string()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 768 | .collect(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 769 | let our_methods = request |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 770 | .our_supported_methods() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 771 | .expect("The transitioned state should know our own supported methods") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 772 | .iter() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 773 | .map(|m| m.to_string()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 774 | .collect(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 775 | VerificationRequestState::Ready { their_methods, our_methods } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 776 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 777 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 778 | RustVerificationRequestState::Cancelled(c) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 779 | VerificationRequestState::Cancelled { cancel_info: c.into() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 780 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 781 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 782 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 783 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 784 | async fn changes_listener( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 785 | request: InnerVerificationRequest, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 786 | mut stream: impl Stream<Item = RustVerificationRequestState> + std::marker::Unpin, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 787 | listener: Box<dyn VerificationRequestListener>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 788 | ) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 789 | while let Some(state) = stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 790 | // If we receive a done or a cancelled state we're at the end of our road, we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 791 | // break out of the loop to deallocate the stream and finish the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 792 | // task. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 793 | let should_break = matches!( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 794 | state, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 795 | RustVerificationRequestState::Done | RustVerificationRequestState::Cancelled { .. } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 796 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 797 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 798 | let state = Self::convert_verification_request(&request, state); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 799 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 800 | listener.on_change(state); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 801 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 802 | if should_break { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 803 | break; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 804 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 805 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 806 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 807 | } |