96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | #![cfg(feature = "sqlite")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | mod support; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 4 | use std::{path::PathBuf, sync::Arc, time::SystemTime}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 6 | use macltok::{ |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 7 | AccountError, Accounts, EncryptedSessionStore, EngineConfig, Event, OAuthApplicationType, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 8 | OAuthLogin, PasswordLogin, Restore, Session, SessionRecord, SessionStore, StoreConfig, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 9 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | use matrix_sdk::{ruma::api::client::sync::sync_events::v5, test_utils::mocks::MatrixMockServer}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | #[tokio::test] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 13 | async fn password_session_round_trips_and_discards_with_its_store() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | .mock_versions() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | .with_simplified_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | .ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | server.mock_login().ok().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | .await; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 27 | let root = temporary_path(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 28 | let store_path = root.join("store"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 29 | let record_path = root.join("records").join("session"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 30 | let sessions = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 31 | EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 32 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 33 | StoreConfig::sqlite(&store_path, "test store passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 34 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 35 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | let session = Session::login_password( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | PasswordLogin::new(server.uri(), "example", "wordpass") |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 39 | .allow_insecure_loopback(true) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | .with_device_display_name("macltok test"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 41 | Arc::new(sessions()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | let (engine, handle, mut events) = session.build(config.clone()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | matches!(event, Event::Authenticated { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | support::shut_down(&handle, &mut events, running).await; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 52 | drop(handle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 54 | let wrong = EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 55 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 56 | StoreConfig::sqlite(&store_path, "someone else's passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 57 | ); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 58 | assert!(wrong.load().await.is_err()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 59 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 60 | let sessions = Arc::new(sessions()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 61 | let Some(Restore::Resumed(session)) = Session::restore(sessions.clone()).await.unwrap() else { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 62 | panic!("the saved login didn't resume"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 63 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | let (engine, handle, mut events) = session.build(config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | matches!(event, Event::Authenticated { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | support::shut_down(&handle, &mut events, running).await; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 71 | drop(handle); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 72 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 73 | sessions.discard().await.unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 74 | assert!(!record_path.exists()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 75 | assert!(!store_path.exists()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 76 | assert!(Session::restore(sessions).await.unwrap().is_none()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 77 | std::fs::remove_dir_all(root).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 78 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 79 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 80 | #[tokio::test] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 81 | async fn a_restart_after_a_soft_logout_still_knows_the_device() { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 82 | let server = MatrixMockServer::new().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 83 | server.mock_versions().ok().mount().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 84 | server.mock_login().ok().mount().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 85 | let root = temporary_path(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 86 | let sessions = || { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 87 | EncryptedSessionStore::file( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 88 | root.join("session"), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 89 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 90 | ) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 91 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 92 | Session::login_password( |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 93 | PasswordLogin::new(server.uri(), "example", "wordpass").allow_insecure_loopback(true), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 94 | Arc::new(sessions()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 95 | ) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 96 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 97 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 98 | sessions().soft_log_out().await.unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 99 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 100 | let Some(Restore::SoftLoggedOut(soft)) = Session::restore(Arc::new(sessions())).await.unwrap() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 101 | else { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 102 | panic!("a soft logged out record came back as something else"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 103 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 104 | // the device matrix-sdk-test's canned login response hands out |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 105 | assert_eq!(soft.device_id, "GHTYAJCE"); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 106 | Session::login_password(soft.password_login("wordpass"), Arc::new(sessions())) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 107 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 108 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 109 | let requests = server.server().received_requests().await.unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 110 | let relogin: serde_json::Value = requests |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 111 | .iter() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 112 | .rfind(|request| request.url.path().ends_with("/login")) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 113 | .unwrap() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 114 | .body_json() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 115 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 116 | assert_eq!(relogin["device_id"], "GHTYAJCE"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 117 | assert!(matches!( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 118 | Session::restore(Arc::new(sessions())).await.unwrap(), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 119 | Some(Restore::Resumed(_)) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 120 | )); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 121 | std::fs::remove_dir_all(root).unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 122 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 123 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 124 | #[tokio::test] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 125 | async fn a_login_whose_first_save_fails_can_still_be_saved() { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 126 | let server = MatrixMockServer::new().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 127 | server.mock_versions().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 128 | server.mock_login().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 129 | let root = temporary_path(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 130 | let records = root.join("records"); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 131 | std::fs::create_dir_all(&records).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 132 | let sessions = || { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 133 | EncryptedSessionStore::file( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 134 | records.join("session"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 135 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 136 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 137 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 138 | let writable = std::fs::metadata(&records).unwrap().permissions(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 139 | let mut read_only = writable.clone(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 140 | read_only.set_readonly(true); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 141 | std::fs::set_permissions(&records, read_only).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 142 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 143 | let login = Session::login_password( |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 144 | PasswordLogin::new(server.uri(), "example", "wordpass").allow_insecure_loopback(true), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 145 | Arc::new(sessions()), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 146 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 147 | .await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 148 | let Err(AccountError::Unsaved { session, .. }) = login else { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 149 | panic!("a login that couldn't save its record didn't hand the session back"); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 150 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 151 | std::fs::set_permissions(&records, writable).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 152 | drop(session.save().await.unwrap()); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 153 | assert!(matches!( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 154 | Session::restore(Arc::new(sessions())).await.unwrap(), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 155 | Some(Restore::Resumed(_)) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 156 | )); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 157 | std::fs::remove_dir_all(root).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 158 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 159 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 160 | #[tokio::test] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 161 | async fn a_store_takes_one_session_at_a_time() { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 162 | let server = MatrixMockServer::new().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 163 | server.mock_versions().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 164 | server.mock_login().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 165 | let root = temporary_path(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 166 | let sessions = || { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 167 | EncryptedSessionStore::file( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 168 | root.join("session"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 169 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 170 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 171 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 172 | let (engine, handle, events) = Session::login_password( |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 173 | PasswordLogin::new(server.uri(), "example", "wordpass").allow_insecure_loopback(true), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 174 | Arc::new(sessions()), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 175 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 176 | .await |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 177 | .unwrap() |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 178 | .build(EngineConfig::default()) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 179 | .await |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 180 | .unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 181 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 182 | assert!(matches!( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 183 | Session::restore(Arc::new(sessions())).await, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 184 | Err(AccountError::StoreInUse) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 185 | )); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 186 | drop((engine, events)); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 187 | assert!(matches!( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 188 | Session::restore(Arc::new(sessions())).await, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 189 | Err(AccountError::StoreInUse) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 190 | )); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 191 | drop(handle); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 192 | let resumed = Session::restore(Arc::new(sessions())).await.unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 193 | assert!(matches!(resumed, Some(Restore::Resumed(_)))); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 194 | drop(resumed); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 195 | sessions().discard().await.unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 196 | assert!(!root.join("store").exists()); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 197 | std::fs::remove_dir_all(root).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 198 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 199 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 200 | #[tokio::test] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 201 | async fn a_torn_or_failed_save_leaves_the_old_record() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 202 | let root = temporary_path(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 203 | let record_path = root.join("session"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 204 | let sessions = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 205 | EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 206 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 207 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 208 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 209 | }; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 210 | let temporary = root.join("session.1.0.tmp"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 211 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 212 | .save(&record("https://old.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 213 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 214 | .unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 215 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 216 | std::fs::write(&temporary, b"half a rec").unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 217 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 218 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 219 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 220 | let writable = std::fs::metadata(&root).unwrap().permissions(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 221 | let mut read_only = writable.clone(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 222 | read_only.set_readonly(true); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 223 | std::fs::set_permissions(&root, read_only).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 224 | assert!( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 225 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 226 | .save(&record("https://new.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 227 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 228 | .is_err() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 229 | ); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 230 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 231 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 232 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 233 | std::fs::set_permissions(&root, writable).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 234 | std::fs::remove_dir_all(root).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 235 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 237 | #[tokio::test] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 238 | async fn a_refresh_is_saved_before_anyone_hears_of_it() { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 239 | use matrix_sdk_test::test_json; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 240 | use wiremock::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 241 | Mock, ResponseTemplate, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 242 | matchers::{header, method, path, path_regex}, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 243 | }; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 244 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 245 | let server = MatrixMockServer::new().await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 246 | server.mock_versions().ok().mount().await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 247 | server |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 248 | .mock_login() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 249 | .respond_with( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 250 | ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_WITH_REFRESH_TOKEN), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 251 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 252 | .mount() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 253 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 254 | Mock::given(method("POST")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 255 | .and(path_regex("sync")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 256 | .and(header("authorization", "Bearer abc123")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 257 | .respond_with(ResponseTemplate::new(401).set_body_json(serde_json::json!({ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 258 | "errcode": "M_UNKNOWN_TOKEN", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 259 | "error": "expired", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 260 | "soft_logout": false, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 261 | }))) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 262 | .with_priority(1) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 263 | .mount(server.server()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 264 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 265 | Mock::given(method("POST")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 266 | .and(path("/_matrix/client/v3/refresh")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 267 | .respond_with( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 268 | ResponseTemplate::new(200).set_body_json(&*test_json::REFRESH_TOKEN_WITH_REFRESH_TOKEN), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 269 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 270 | .mount(server.server()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 271 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 272 | server |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 273 | .mock_sliding_sync() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 274 | .ok(v5::Response::new("1".to_owned())) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 275 | .mount() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 276 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 277 | let root = temporary_path(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 278 | let sessions = Arc::new(EncryptedSessionStore::file( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 279 | root.join("session"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 280 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 281 | )); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 282 | let session = Session::login_password( |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 283 | PasswordLogin::new(server.uri(), "example", "wordpass").allow_insecure_loopback(true), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 284 | sessions.clone(), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 285 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 286 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 287 | .unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 288 | let (engine, handle, mut events) = session |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 289 | .build(EngineConfig::default().with_initial_history_limit(0)) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 290 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 291 | .unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 292 | let running = tokio::spawn(engine.run()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 293 | support::receive_until(&mut events, |event| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 294 | matches!(event, Event::SessionTokensChanged) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 295 | }) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 296 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 297 | let saved = serde_json::to_value(sessions.load().await.unwrap().unwrap()).unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 298 | assert_eq!(saved["authentication"]["session"]["refresh_token"], "wxyz"); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 299 | support::shut_down(&handle, &mut events, running).await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 300 | drop(handle); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 301 | std::fs::remove_dir_all(root).unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 302 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 303 | |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 304 | #[tokio::test] |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 305 | async fn logging_in_again_after_a_soft_logout_keeps_the_device_and_its_store() { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 306 | let server = MatrixMockServer::new().await; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 307 | server.mock_versions().ok().mount().await; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 308 | server.mock_login().ok().mount().await; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 309 | let root = temporary_path(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 310 | let accounts = Accounts::sqlite(&root, "test store passphrase"); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 311 | let login = |username: &str| { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 312 | PasswordLogin::new(server.uri(), username, "wordpass").allow_insecure_loopback(true) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 313 | }; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 314 | let device_ids = || async { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 315 | let requests = server.server().received_requests().await.unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 316 | requests |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 317 | .iter() |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 318 | .filter(|request| request.url.path().ends_with("/login")) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 319 | .map(|request| request.body_json::<serde_json::Value>().unwrap()["device_id"].clone()) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 320 | .collect::<Vec<_>>() |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 321 | }; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 322 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 323 | let (first, session) = accounts |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 324 | .login_password(login("cheeky_monkey")) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 325 | .await |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 326 | .unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 327 | drop(session); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 328 | assert!(matches!( |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 329 | accounts.login_password(login("cheeky_monkey")).await, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 330 | Err(AccountError::AlreadyLoggedIn(_)) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 331 | )); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 332 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 333 | first.sessions().soft_log_out().await.unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 334 | let (resumed, session) = accounts |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 335 | .login_password(login(" Cheeky_Monkey ")) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 336 | .await |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 337 | .unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 338 | drop(session); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 339 | assert_eq!(resumed.id(), first.id()); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 340 | assert_eq!(device_ids().await.last().unwrap(), "GHTYAJCE"); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 341 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 342 | resumed.sessions().soft_log_out().await.unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 343 | let (other, session) = accounts |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 344 | .login_password(login("someone_else")) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 345 | .await |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 346 | .unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 347 | drop(session); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 348 | assert_ne!(other.id(), first.id()); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 349 | assert!(device_ids().await.last().unwrap().is_null()); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 350 | assert_eq!(accounts.list().await.unwrap().len(), 2); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 351 | std::fs::remove_dir_all(root).unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 352 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 353 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 354 | #[tokio::test] |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 355 | async fn a_refused_login_leaves_no_account_and_says_why() { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 356 | use wiremock::ResponseTemplate; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 357 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 358 | let server = MatrixMockServer::new().await; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 359 | server.mock_versions().ok().mount().await; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 360 | server |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 361 | .mock_login() |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 362 | .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({ |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 363 | "errcode": "M_FORBIDDEN", |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 364 | "error": "Invalid username or password", |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 365 | }))) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 366 | .mount() |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 367 | .await; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 368 | let root = temporary_path(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 369 | let accounts = Accounts::sqlite(&root, "test store passphrase"); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 370 | let Err(refused) = accounts |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 371 | .login_password( |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 372 | PasswordLogin::new(server.uri(), "example", "wrong").allow_insecure_loopback(true), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 373 | ) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 374 | .await |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 375 | else { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 376 | panic!("a refused login went through"); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 377 | }; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 378 | let server_error = refused.server().unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 379 | assert_eq!(server_error.errcode, "M_FORBIDDEN"); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 380 | assert_eq!(server_error.message, "Invalid username or password"); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 381 | assert!(accounts.list().await.unwrap().is_empty()); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 382 | assert_eq!(std::fs::read_dir(&root).unwrap().count(), 0); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 383 | std::fs::remove_dir_all(root).unwrap(); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 384 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 385 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 386 | #[tokio::test] |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 387 | async fn a_homeserver_that_cant_be_one_is_refused_before_anything_is_sent() { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 388 | let root = temporary_path(); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 389 | let sessions: Arc<dyn SessionStore> = Arc::new(EncryptedSessionStore::file( |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 390 | root.join("session"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 391 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 392 | )); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 393 | for (homeserver, loopback) in [ |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 394 | ("", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 395 | (" ", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 396 | ("example .org", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 397 | ("ftp://example.org", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 398 | ("https://user@example.org", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 399 | ("https://", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 400 | ("example.org/path", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 401 | ("http://example.org", false), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 402 | ("http://example.org", true), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 403 | ("http://localhost:8008", false), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 404 | ] { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 405 | let login = |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 406 | PasswordLogin::new(homeserver, "example", "wordpass").allow_insecure_loopback(loopback); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 407 | let refused = Session::login_password(login, sessions.clone()).await; |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 408 | assert!( |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 409 | matches!(refused, Err(AccountError::InvalidHomeserver(_))), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 410 | "{homeserver:?} got through" |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 411 | ); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 412 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 413 | assert!(!root.exists()); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 414 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 415 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 416 | fn record(homeserver: &str) -> SessionRecord { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 417 | serde_json::from_value(serde_json::json!({ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 418 | "homeserver": homeserver, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 419 | "authentication": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 420 | "type": "matrix", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 421 | "session": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 422 | "user_id": "@example:example.org", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 423 | "device_id": "DEVICE", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 424 | "access_token": "secret", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 425 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 426 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 427 | })) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 428 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 429 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 430 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 431 | fn temporary_path() -> PathBuf { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 432 | let nonce = SystemTime::now() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 433 | .duration_since(SystemTime::UNIX_EPOCH) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | .as_nanos(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 436 | PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("macltok-account-{nonce}")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 438 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 439 | #[tokio::test] |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 440 | async fn an_oauth_login_survives_the_page_going_away_for_its_redirect() { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 441 | let server = MatrixMockServer::new().await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 442 | server |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 443 | .mock_versions() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 444 | .with_simplified_sliding_sync() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 445 | .ok() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 446 | .mount() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 447 | .await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 448 | server.mock_who_am_i().ok().mount().await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 449 | let oauth = server.oauth(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 450 | oauth.mock_server_metadata().ok().mount().await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 451 | oauth.mock_registration().ok().mount().await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 452 | oauth.mock_token().ok().mount().await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 453 | let root = temporary_path(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 454 | let accounts = Accounts::sqlite(&root, "test store passphrase"); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 455 | let redirect_uri = "http://127.0.0.1/callback"; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 456 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 457 | let (account, pending) = accounts |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 458 | .begin_oauth( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 459 | OAuthLogin::new( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 460 | server.uri(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 461 | redirect_uri, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 462 | "http://127.0.0.1/", |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 463 | OAuthApplicationType::Native, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 464 | ) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 465 | .allow_insecure_loopback(true), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 466 | ) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 467 | .await |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 468 | .unwrap(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 469 | let state = url::Url::parse(pending.authorization_url()) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 470 | .unwrap() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 471 | .query_pairs() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 472 | .find(|(key, _)| key == "state") |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 473 | .map(|(_, state)| state.into_owned()) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 474 | .unwrap(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 475 | drop(pending); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 476 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 477 | assert!(matches!( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 478 | account.restore().await.unwrap(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 479 | Some(Restore::OAuthPending) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 480 | )); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 481 | assert_eq!( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 482 | accounts.list().await.unwrap().len(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 483 | 1, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 484 | "the redirect's store was swept" |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 485 | ); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 486 | assert!(matches!( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 487 | accounts |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 488 | .finish_oauth(&format!("{redirect_uri}?code=42&state=someone-else")) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 489 | .await, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 490 | Err(AccountError::NoPendingOAuth) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 491 | )); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 492 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 493 | let (finished, session) = accounts |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 494 | .finish_oauth(&format!("{redirect_uri}?code=42&state={state}")) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 495 | .await |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 496 | .unwrap(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 497 | assert_eq!(finished.id(), account.id()); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 498 | drop(session); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 499 | assert!(matches!( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 500 | account.restore().await.unwrap(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 501 | Some(Restore::Resumed(_)) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 502 | )); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 503 | } |