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