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