96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | use std::sync::Arc; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use futures_util::StreamExt; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | encryption::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | VerificationState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | backups::BackupState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | recovery::RecoveryState as SdkRecoveryState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | verification::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | SasState, SasVerification, Verification, VerificationRequest, VerificationRequestState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | ruma::events::key::verification::VerificationMethod, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | use ruma::{OwnedDeviceId, UserId}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | use tokio::{sync::Mutex, task::JoinHandle}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | EncryptionState, Error, ErrorKind, Event, KeyBackupState, RecoveryState, VerificationAction, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | VerificationState as UiState, VerificationTarget, model::VerificationFailure, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | transport::EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | pub(crate) fn encryption_state( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | device_id: OwnedDeviceId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | recovery: SdkRecoveryState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | verification: VerificationState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | backup: BackupState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | ) -> EncryptionState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | EncryptionState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | device_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | device_verified: matches!(verification, VerificationState::Verified), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | recovery: match recovery { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | SdkRecoveryState::Unknown => RecoveryState::Unknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | SdkRecoveryState::Disabled => RecoveryState::Disabled, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | SdkRecoveryState::Incomplete => RecoveryState::Incomplete, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | SdkRecoveryState::Enabled => RecoveryState::Enabled, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | key_backup: match backup { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | BackupState::Unknown => KeyBackupState::Unknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | BackupState::Creating => KeyBackupState::Creating, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | BackupState::Enabling => KeyBackupState::Enabling, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | BackupState::Resuming => KeyBackupState::Resuming, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | BackupState::Enabled => KeyBackupState::Enabled, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | BackupState::Downloading => KeyBackupState::Downloading, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | BackupState::Disabling => KeyBackupState::Disabling, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | #[derive(Default)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | pub(crate) struct VerificationController { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | active: Arc<Mutex<VerificationSession>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | task: Option<JoinHandle<()>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | #[derive(Default)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | struct VerificationSession { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | request: Option<VerificationRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | sas: Option<SasVerification>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | impl VerificationController { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | pub(crate) async fn apply( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | sender: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | action: VerificationAction, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | ) -> Result<(), Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | match action { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 71 | VerificationAction::Accept => self.accept().await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | VerificationAction::Confirm => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | // Retain a handle across the operation: the watcher clears |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | // `active` when it publishes a terminal event, but this clone |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | // still observes the shared SDK state afterwards. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | let sas = self.active.lock().await.sas.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | match self.confirm().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | Ok(()) => Ok(()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | Err(error) if error.kind == ErrorKind::VerificationInactive => Err(error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | // The confirm advanced local SDK state before its send failed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | // so the retained session could report success on retry without |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | // network traffic. Best-effort cancel (no lock held across the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | // network), clear for restart, then report the terminal state. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | // Stop the watcher first so it cannot emit a stale |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | // Confirmed from the local transition we are unwinding. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | self.stop_task().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | // The peer can complete or cancel the verification |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | // while our own Done send fails. The stopped watcher may |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | // already have published that terminal outcome (losing the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | // claim below); otherwise this path owns it and must emit |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | // it instead of regressing to Cancelled. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | if sas.as_ref().is_some_and(|sas| sas.is_done()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | if take_verification(&self.active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | sender.send(Event::VerificationChanged(UiState::Done)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | return Err(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | if let Some(reason) = sas |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | .as_ref() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | .and_then(|sas| sas.cancel_info()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | .map(|info| info.reason().to_owned()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | if take_verification(&self.active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | let _ = sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | .send(Event::VerificationChanged(UiState::Cancelled(reason))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | return Err(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | if let Some(sas) = sas { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | if sas.cancel().await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | // The best-effort unwind already advanced |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | // local state; clearing here keeps the failed |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | // cancellation from looking retryable. The |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | // send is owned only if the watcher has not |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | // already claimed the session for its own |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | // terminal outcome. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | if take_verification(&self.active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | let _ = sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | .send(Event::VerificationChanged(UiState::Cancelled( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | return Err(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | // The peer can complete while the unwind cancel is in |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | // flight and the SDK call no-ops; the Done then owns the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | // terminal outcome, so report it instead of attributing |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | // it to the send failure. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | if sas.is_done() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | if take_verification(&self.active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | let _ = sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | .send(Event::VerificationChanged(UiState::Done)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | return Err(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | if let Some(info) = sas.cancel_info() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | // Our own unwind cancel just marked the flow; it does |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | // not own the outcome yet, so fall through and report |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | // the send failure as Cancelled below. Only a peer |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | // cancellation racing the unwind owns it instead. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | if !info.cancelled_by_us() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | if take_verification(&self.active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | let _ = sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | .send(Event::VerificationChanged(UiState::Cancelled( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | if take_verification(&self.active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | let _ = sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | .send(Event::VerificationChanged(UiState::Cancelled( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | Err(error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | VerificationAction::Reject => self.reject().await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | VerificationAction::Cancel => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | // The SDK no-ops a cancel of an already-settled flow, so a |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | // failed claim inside `cancel` already reported the true |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | // outcome as an error and owns no event here. Only the claim |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | // winner emits the local Cancelled. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | let claimed = self.cancel_claimed(Some(&sender)).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | if claimed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | .send(Event::VerificationChanged(UiState::Cancelled( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | "cancelled by this client".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | ErrorKind::Interrupted, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | "event receiver has closed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | pub(crate) async fn start( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | sender: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | target: VerificationTarget, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | ) -> Result<(), Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | .wait_for_e2ee_initialization_tasks() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | let own_user_id = client.user_id().ok_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | ErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | "the Matrix client is not authenticated", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | if self.has_active_verification().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | self.cancel().await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | self.stop_task().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | let request = match &target { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | VerificationTarget::OwnIdentity => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | request_identity_verification(client, own_user_id).await? |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | VerificationTarget::User(user_id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | request_identity_verification(client, user_id).await? |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | VerificationTarget::Device { user_id, device_id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | let device = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | .get_device(user_id, device_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | ErrorKind::Encryption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | "reading the verification device failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | })? |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | .ok_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | ErrorKind::VerificationUnsupported, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | "the verification device is unavailable", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | device |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | .request_verification_with_methods(vec![VerificationMethod::SasV1]) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | Error::from_sdk("starting device verification failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | })? |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 252 | // subscribed before anything can wait on the event channel, or a quick peer's |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 253 | // answer lands while we're blocked and nobody ever hears it |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 254 | let changes = request.changes(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | self.active.lock().await.request = Some(request.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | .send(Event::VerificationTargetChanged(target)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | Error::with_source(ErrorKind::Interrupted, "event receiver has closed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | .send(Event::VerificationChanged(UiState::Waiting)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | Error::with_source(ErrorKind::Interrupted, "event receiver has closed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | let active = self.active.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | self.task = Some(tokio::spawn(async move { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 270 | follow_request(request, changes, active, sender).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | })); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 275 | /// another device asked us. only taken when nothing else is in flight, and not accepted |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 276 | /// until the user says so with `VerificationAction::Accept` |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 277 | pub(crate) async fn adopt_incoming( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 278 | &mut self, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 279 | sender: EventSender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 280 | request: VerificationRequest, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 281 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 282 | if request.we_started() || request.is_done() || request.cancel_info().is_some() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 283 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 284 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 285 | if self.has_active_verification().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 286 | tracing::debug!("ignoring a verification request while another is in flight"); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 287 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 288 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 289 | self.stop_task().await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 290 | let changes = request.changes(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 291 | self.active.lock().await.request = Some(request.clone()); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 292 | let incoming = UiState::Incoming { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 293 | user_id: request.other_user_id().to_owned(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 294 | device_id: match request.state() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 295 | VerificationRequestState::Requested { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 296 | other_device_data, .. |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 297 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 298 | | VerificationRequestState::Ready { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 299 | other_device_data, .. |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 300 | } => Some(other_device_data.device_id().to_owned()), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 301 | _ => None, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 302 | }, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 303 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 304 | let target = VerificationTarget::User(request.other_user_id().to_owned()); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 305 | for event in [ |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 306 | Event::VerificationTargetChanged(target), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 307 | Event::VerificationChanged(incoming), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 308 | ] { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 309 | if sender.send(event).await.is_err() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 310 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 311 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 312 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 313 | let active = self.active.clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 314 | self.task = Some(tokio::spawn(async move { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 315 | follow_request(request, changes, active, sender).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 316 | })); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 317 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 318 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 319 | async fn accept(&self) -> Result<(), Error> { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 320 | let inactive = |message: &str| Error::new(ErrorKind::VerificationInactive, message); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 321 | let (request, sas) = { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 322 | let active = self.active.lock().await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 323 | (active.request.clone(), active.sas.is_some()) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 324 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 325 | let request = request.ok_or_else(|| inactive("no verification request is pending"))?; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 326 | if request.we_started() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 327 | return Err(inactive("nobody asked us, we asked them")); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 328 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 329 | if request.is_done() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 330 | return Err(inactive("verification is already complete")); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 331 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 332 | if let Some(info) = request.cancel_info() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 333 | return Err(inactive(info.reason())); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 334 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 335 | if sas { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 336 | return Err(inactive("verification has already started")); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 337 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 338 | request |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 339 | .accept_with_methods(vec![VerificationMethod::SasV1]) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 340 | .await |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 341 | .map_err(|source| Error::from_sdk("accepting verification request failed", source)) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 342 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 343 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | async fn confirm(&self) -> Result<(), Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | let sas = self.active.lock().await.sas.clone().ok_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | "verification has not reached comparison", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | // Report a settled flow truthfully instead of as "not reached |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | // comparison": the SDK would no-op the confirm with success. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | if sas.is_done() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | if let Some(info) = sas.cancel_info() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | // Only the short-auth-string stage is confirmable. The SDK reports the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | // remote-MAC-arrived state through the same presentable flag, while an |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | // early confirm would locally no-op and falsely report success. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | if !sas.can_be_presented() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | "verification has not reached comparison", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | // Our own confirm can move the flow to Done, so only Done that predates |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | // the send means the SDK no-op'd on a stale session; a cancellation |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | // racing the send must not report success. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | let was_done = sas.is_done(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | sas.confirm() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | .map_err(|source| Error::from_sdk("confirming verification failed", source))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | if sas.is_done() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | if was_done { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | } else if sas.is_cancelled() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | let reason = sas |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | .cancel_info() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | .map(|info| info.reason().to_owned()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | .unwrap_or_else(|| "verification was cancelled".to_owned()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | return Err(Error::new(ErrorKind::VerificationInactive, reason)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | async fn reject(&self) -> Result<(), Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | let sas = self.active.lock().await.sas.clone().ok_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | "verification has not reached comparison", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | // Report a settled flow truthfully instead of as "not reached |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | // comparison": the SDK would no-op the mismatch with success. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | if sas.is_done() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 411 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 412 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 413 | if let Some(info) = sas.cancel_info() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | // Rejecting before the comparison stage would cancel a flow the user |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | // never compared, so it requires the same presentable state. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | if !sas.can_be_presented() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | "verification has not reached comparison", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 425 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 426 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 427 | sas.mismatch() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 428 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | .map_err(|source| Error::from_sdk("rejecting verification failed", source))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 430 | // A peer completing the flow concurrently must not be reported as |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 431 | // rejected. A remote cancellation racing the send turns the SDK call |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 432 | // into a no-op success, so report it instead of a false success. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 433 | if sas.is_done() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 434 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 438 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | if let Some(info) = sas.cancel_info() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | // Our own mismatch just marked the flow cancelled-by-us; that is the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | // expected reject path and stays Ok. Only a peer cancel reports an |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | // error. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | if !info.cancelled_by_us() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 444 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 445 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 450 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 451 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 452 | pub(crate) async fn cancel(&mut self) -> Result<(), Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 453 | self.cancel_claimed(None).await.map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 454 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 455 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 456 | /// Return whether this action won the claim for a local cancellation. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 457 | /// The optional sender reports a peer terminal outcome when it races after |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 458 | /// the claim; starting a replacement verification does not publish it. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | async fn cancel_claimed(&mut self, sender: Option<&EventSender>) -> Result<bool, Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 460 | let (sas, request) = { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 461 | let active = self.active.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 462 | (active.sas.clone(), active.request.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | if sas.is_none() && request.is_none() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 465 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 466 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | "no verification is active", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | // The watcher owns terminal emission and claims the session when it |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 471 | // publishes. If the SDK is already terminal, the watcher either already |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 472 | // reported it or is about to: leave session and watcher alone so the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 473 | // outcome is emitted exactly once, and report it instead of letting the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | // SDK no-op success regress Done to "cancelled by this client". |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | if let Some(sas) = sas.as_ref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | if sas.is_done() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 477 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 478 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | if let Some(info) = sas.cancel_info() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 483 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 484 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 485 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 486 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 487 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 488 | } else if let Some(request) = request.as_ref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 489 | if request.is_done() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 490 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 491 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 494 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | if let Some(info) = request.cancel_info() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 496 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 497 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 499 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 500 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | // Own the terminal outcome from here: claim the session before |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | // stopping the watcher so a watcher that already claimed (and will |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | // emit its own terminal event) turns this into a truthful error |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | // instead of a duplicate local cancel; no lock is held across sends. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 506 | if !take_verification(&self.active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 507 | // Re-check the SDK: the claim loss may be a stale session after a |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | // terminal outcome, or a concurrent start of a new flow. Only the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | // terminal case reports here; a live session belongs to the new |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | // flow and must not be disturbed. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 511 | if let Some(sas) = sas.as_ref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 512 | if sas.is_done() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 514 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 515 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 516 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 517 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 518 | if let Some(info) = sas.cancel_info() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 519 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 520 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 521 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 522 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 523 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 524 | } else if let Some(request) = request.as_ref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 525 | if request.is_done() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 526 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 527 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 528 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 529 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 530 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 531 | if let Some(info) = request.cancel_info() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 532 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 533 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 534 | info.reason().to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 535 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 536 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 537 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 538 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 539 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 540 | "no verification is active", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 541 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 542 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 543 | self.stop_task().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 544 | if let Some(sas) = sas { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 545 | let result = sas.cancel().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 546 | if sas.is_done() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 547 | report_claimed_terminal(sender, UiState::Done).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 548 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 549 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 550 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 551 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 552 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 553 | if let Some(info) = sas.cancel_info() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 554 | && !info.cancelled_by_us() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 555 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 556 | let reason = info.reason().to_owned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 557 | report_claimed_terminal(sender, UiState::Cancelled(reason.clone())).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 558 | return Err(Error::new(ErrorKind::VerificationInactive, reason)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 559 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 560 | if let Err(source) = result { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 561 | let error = Error::from_sdk("cancelling verification failed", source); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 562 | report_claimed_terminal(sender, UiState::Cancelled(error.to_string())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 563 | return Err(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 564 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 565 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 566 | } else if let Some(request) = request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | let result = request.cancel().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | if request.is_done() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | report_claimed_terminal(sender, UiState::Done).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 570 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 571 | ErrorKind::VerificationInactive, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | "verification is already complete", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 573 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 574 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | if let Some(info) = request.cancel_info() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 576 | && !info.cancelled_by_us() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 577 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | let reason = info.reason().to_owned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | report_claimed_terminal(sender, UiState::Cancelled(reason.clone())).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 580 | return Err(Error::new(ErrorKind::VerificationInactive, reason)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | if let Err(source) = result { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 583 | let error = Error::from_sdk("cancelling verification failed", source); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | report_claimed_terminal(sender, UiState::Cancelled(error.to_string())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | return Err(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 586 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 587 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 589 | Ok(false) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 590 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | async fn has_active_verification(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | let active = self.active.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | active.request.is_some() || active.sas.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 596 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 597 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 598 | async fn stop_task(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 599 | if let Some(task) = self.task.take() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 600 | task.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 601 | let _ = task.await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 602 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 603 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 604 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 605 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 606 | /// shutdown forgets the flow without telling the sdk, a network send could hang it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 607 | impl Drop for VerificationController { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 608 | fn drop(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 609 | if let Some(task) = self.task.take() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 610 | task.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 611 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 612 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | async fn request_identity_verification( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 616 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 617 | user_id: &UserId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 618 | ) -> Result<VerificationRequest, Error> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 619 | let identity = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 620 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 621 | .request_user_identity(user_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 622 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 623 | .map_err(|source| Error::from_sdk("requesting the user identity failed", source))? |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 624 | .ok_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 625 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 626 | ErrorKind::NoCrossSigningIdentity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 627 | "the user has no cross-signing identity", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 628 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 629 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 630 | identity |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 631 | .request_verification_with_methods(vec![VerificationMethod::SasV1]) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 632 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 633 | .map_err(|source| Error::from_verification_request("starting verification failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 634 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 635 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 636 | /// `changes` is subscribed by the caller before the request could move, and the current state |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 637 | /// is handled first, since the stream only reports what happens after it was taken |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 638 | async fn follow_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 639 | request: VerificationRequest, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 640 | changes: impl futures_util::Stream<Item = VerificationRequestState> + Unpin, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 641 | active: Arc<Mutex<VerificationSession>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 642 | sender: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 643 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 644 | let mut states = futures_util::stream::iter([request.state()]).chain(changes); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 645 | while let Some(state) = states.next().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 646 | match state { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 647 | // created is ours waiting on them, requested is theirs waiting on `Accept` |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 648 | VerificationRequestState::Created { .. } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 649 | | VerificationRequestState::Requested { .. } => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 650 | VerificationRequestState::Ready { .. } => match request.start_sas().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 651 | Ok(Some(sas)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 652 | follow_sas(sas, active.clone(), sender.clone()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 653 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 654 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 655 | Ok(None) => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 656 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 657 | tracing::warn!(error = %error, "starting SAS verification failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 658 | // Claim the session so this terminal outcome is emitted exactly |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 659 | // once; a lost claim means an action path already owns it. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 660 | report_watcher_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 661 | &active, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 662 | &sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 663 | UiState::Failed(VerificationFailure::SasStartup(error.to_string())), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 664 | "SAS startup failure", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 665 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 666 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 667 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 668 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 669 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 670 | VerificationRequestState::Transitioned { verification } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 671 | if let Verification::SasV1(sas) = verification { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 672 | follow_sas(sas, active.clone(), sender.clone()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 673 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 674 | report_watcher_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 675 | &active, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 676 | &sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 677 | UiState::Cancelled( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 678 | "the other device selected an unsupported method".into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 679 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 680 | "unsupported verification method", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 681 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 682 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 683 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 684 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 685 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 686 | VerificationRequestState::Done => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 687 | report_watcher_terminal(&active, &sender, UiState::Done, "completion").await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 688 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 689 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 690 | VerificationRequestState::Cancelled(info) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 691 | report_watcher_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 692 | &active, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 693 | &sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 694 | UiState::Cancelled(info.reason().to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 695 | "cancellation", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 696 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 697 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 698 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 699 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 700 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 701 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 702 | tracing::debug!("verification request watcher ended unexpectedly"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 703 | // The stream may end after the SDK already settled without delivering the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 704 | // terminal update; report the settled outcome instead of StreamEnded, and |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 705 | // stay silent if an action path already claimed the session. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 706 | if request.is_done() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 707 | report_watcher_terminal(&active, &sender, UiState::Done, "completion").await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 708 | } else if let Some(info) = request.cancel_info() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 709 | let reason = info.reason().to_owned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 710 | report_watcher_terminal(&active, &sender, UiState::Cancelled(reason), "cancellation").await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 711 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 712 | report_watcher_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 713 | &active, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 714 | &sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 715 | UiState::Failed(VerificationFailure::RequestStreamEnded), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 716 | "request stream termination", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 717 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 718 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 719 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 720 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 721 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 722 | async fn follow_sas( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 723 | sas: SasVerification, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 724 | active: Arc<Mutex<VerificationSession>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 725 | sender: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 726 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 727 | // the sdk may already have moved the flow along before we got here, a peer starting |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 728 | // sas is the usual case, so take the stream first and the current state after |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 729 | let changes = sas.changes(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 730 | active.lock().await.sas = Some(sas.clone()); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 731 | let mut states = futures_util::stream::iter([sas.state()]).chain(changes); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 732 | while let Some(state) = states.next().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 733 | let update = match state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 734 | SasState::Created { .. } | SasState::Accepted { .. } => continue, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 735 | SasState::Started { .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 736 | if let Err(error) = sas.accept().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 737 | tracing::warn!(error = %error, "accepting SAS verification failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 738 | // Claim the session so this terminal outcome is emitted exactly |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 739 | // once; a lost claim means an action path already owns it. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 740 | report_watcher_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 741 | &active, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 742 | &sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 743 | UiState::Failed(VerificationFailure::SasAcceptance(error.to_string())), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 744 | "SAS acceptance failure", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 745 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 746 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 747 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 748 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 749 | continue; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 750 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 751 | SasState::KeysExchanged { emojis, decimals } => UiState::Comparison { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 752 | emojis: emojis |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 753 | .map(|short_auth| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 754 | short_auth |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 755 | .emojis |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 756 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 757 | .map(|emoji| (emoji.symbol.to_owned(), emoji.description.to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 758 | .collect() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 759 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 760 | .unwrap_or_default(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 761 | decimals, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 762 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 763 | SasState::Confirmed => UiState::Confirmed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 764 | SasState::Done { .. } => UiState::Done, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 765 | SasState::Cancelled(info) => UiState::Cancelled(info.reason().to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 766 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 767 | let terminal = matches!(update, UiState::Done | UiState::Cancelled(_)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 768 | // Claim the session before the terminal send so a racing Cancel observes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 769 | // no active session and the SDK no-op cannot regress Done back to |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 770 | // "cancelled by this client". A lost claim means an action path already |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 771 | // owns the terminal outcome, so stay silent instead of duplicating it. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 772 | if terminal && !take_verification(&active).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 773 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 774 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 775 | if sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 776 | .send(Event::VerificationChanged(update)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 777 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 778 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 779 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 780 | tracing::debug!("SAS watcher stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 781 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 782 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 783 | if terminal { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 784 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 785 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 786 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 787 | tracing::debug!("SAS watcher ended unexpectedly"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 788 | // The stream may end after the SDK already settled without delivering the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 789 | // terminal update; report the settled outcome instead of StreamEnded, and |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 790 | // stay silent if an action path already claimed the session. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 791 | if sas.is_done() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 792 | report_watcher_terminal(&active, &sender, UiState::Done, "completion").await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 793 | } else if let Some(info) = sas.cancel_info() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 794 | let reason = info.reason().to_owned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 795 | report_watcher_terminal(&active, &sender, UiState::Cancelled(reason), "cancellation").await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 796 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 797 | report_watcher_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 798 | &active, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 799 | &sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 800 | UiState::Failed(VerificationFailure::SasStreamEnded), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 801 | "SAS stream termination", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 802 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 803 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 804 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 805 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 806 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 807 | async fn report_watcher_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 808 | active: &Arc<Mutex<VerificationSession>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 809 | sender: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 810 | state: UiState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 811 | context: &'static str, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 812 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 813 | if take_verification(active).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 814 | && sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 815 | .send(Event::VerificationChanged(state)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 816 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 817 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 818 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 819 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 820 | context, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 821 | "verification watcher could not report terminal outcome" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 822 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 823 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 824 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 825 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 826 | async fn report_claimed_terminal(sender: Option<&EventSender>, state: UiState) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 827 | if let Some(sender) = sender { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 828 | let _ = sender.send(Event::VerificationChanged(state)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 829 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 830 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 831 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 832 | /// Claim the active session for terminal emission, if one is still present. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 833 | /// |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 834 | /// Exactly one party (watcher or action path) wins the claim and may emit the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 835 | /// terminal event; the loser stays silent so Done is never overwritten by a |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 836 | /// later local Cancelled/Failed. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 837 | async fn take_verification(active: &Arc<Mutex<VerificationSession>>) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 838 | let mut active = active.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 839 | let had = active.sas.is_some() || active.request.is_some(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 840 | active.sas.take(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 841 | active.request.take(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 842 | had |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 843 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 844 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 845 | #[cfg(test)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 846 | mod tests { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 847 | use matrix_sdk::encryption::VerificationState; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 848 | use matrix_sdk::encryption::backups::BackupState; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 849 | use ruma::owned_device_id; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 850 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 851 | use super::*; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 852 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 853 | #[test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 854 | fn sdk_security_state_is_normalized() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 855 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 856 | encryption_state( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 857 | owned_device_id!("DEVICE"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 858 | SdkRecoveryState::Incomplete, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 859 | VerificationState::Unverified, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 860 | BackupState::Unknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 861 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 862 | EncryptionState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 863 | device_id: owned_device_id!("DEVICE"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 864 | device_verified: false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 865 | recovery: RecoveryState::Incomplete, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 866 | key_backup: KeyBackupState::Unknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 867 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 868 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 869 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 870 | encryption_state( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 871 | owned_device_id!("DEVICE"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 872 | SdkRecoveryState::Enabled, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 873 | VerificationState::Verified, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 874 | BackupState::Enabled, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 875 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 876 | EncryptionState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 877 | device_id: owned_device_id!("DEVICE"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 878 | device_verified: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 879 | recovery: RecoveryState::Enabled, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 880 | key_backup: KeyBackupState::Enabled, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 881 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 882 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 883 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 884 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 885 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 886 | async fn comparison_actions_require_an_active_sas() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 887 | let mut controller = VerificationController::default(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 888 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 889 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 890 | controller.confirm().await.unwrap_err().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 891 | "verification has not reached comparison" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 892 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 893 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 894 | controller.reject().await.unwrap_err().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 895 | "verification has not reached comparison" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 896 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 897 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 898 | controller.cancel().await.unwrap_err().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 899 | "no verification is active" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 900 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 901 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 902 | } |