Commit 4baa623504

4baa623504c8991b739384e995dc69d6ac42f74f

parent: beb8e5265d

Unregistered key

Ashutosh Panda <133190480+Ashutosh-Panda2004@users.noreply.github.com> · 2026-10-04 23:27 UTC
committer: <noreply@github.com>

Reuse the old device ID when logging back in (#777)

Layout: unified · split

src/config/mod.rs +44 −1
@@ -14,7 +14,7 @@ use matrix_sdk::EncryptionState;
14use matrix_sdk::authentication::matrix::MatrixSession; 14use matrix_sdk::authentication::matrix::MatrixSession;
15use matrix_sdk::media::MediaRetentionPolicy; 15use matrix_sdk::media::MediaRetentionPolicy;
16use matrix_sdk::reqwest::header::{HeaderMap, HeaderValue}; 16use matrix_sdk::reqwest::header::{HeaderMap, HeaderValue};
17use matrix_sdk::ruma::{OwnedDeviceId, owned_server_name}; 17use matrix_sdk::ruma::{DeviceId, OwnedDeviceId, owned_server_name};
18use modalkit::crossterm; 18use modalkit::crossterm;
19use modalkit::env::vim::VimMode; 19use modalkit::env::vim::VimMode;
20use modalkit::keybindings::InputKey; 20use modalkit::keybindings::InputKey;
@@ -278,6 +278,18 @@ pub struct Session {
278 device_id: OwnedDeviceId, 278 device_id: OwnedDeviceId,
279} 279}
280 280
281/// A device ID saved at logout so that the next login can reuse it.
282///
283/// The SDK store is keyed by user and device ID, so logging back in with
284/// a freshly issued device ID would leave the existing store unusable
285/// (#775). Saving the ID lets the next login ask the homeserver for the
286/// same device and keep using that store.
287#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
288pub struct SavedDevice {
289 user_id: OwnedUserId,
290 device_id: OwnedDeviceId,
291}
292
281impl From<Session> for MatrixSession { 293impl From<Session> for MatrixSession {
282 fn from(session: Session) -> Self { 294 fn from(session: Session) -> Self {
283 MatrixSession { 295 MatrixSession {
@@ -1203,6 +1215,7 @@ impl IambConfig {
1203pub struct ApplicationSettings { 1215pub struct ApplicationSettings {
1204 pub layout_json: PathBuf, 1216 pub layout_json: PathBuf,
1205 pub session_json: PathBuf, 1217 pub session_json: PathBuf,
1218 pub device_json: PathBuf,
1206 pub session_json_old: PathBuf, 1219 pub session_json_old: PathBuf,
1207 pub sled_dir: PathBuf, 1220 pub sled_dir: PathBuf,
1208 pub sqlite_dir: PathBuf, 1221 pub sqlite_dir: PathBuf,
@@ -1343,6 +1356,9 @@ impl ApplicationSettings {
1343 let mut session_json = profile_data_dir.clone(); 1356 let mut session_json = profile_data_dir.clone();
1344 session_json.push("session.json"); 1357 session_json.push("session.json");
1345 1358
1359 let mut device_json = profile_data_dir;
1360 device_json.push("device.json");
1361
1346 let mut session_json_old = profile_dir; 1362 let mut session_json_old = profile_dir;
1347 session_json_old.push("session.json"); 1363 session_json_old.push("session.json");
1348 1364
@@ -1361,6 +1377,7 @@ impl ApplicationSettings {
1361 sled_dir, 1377 sled_dir,
1362 layout_json, 1378 layout_json,
1363 session_json, 1379 session_json,
1380 device_json,
1364 session_json_old, 1381 session_json_old,
1365 sqlite_dir, 1382 sqlite_dir,
1366 sqlite_cache_dir, 1383 sqlite_cache_dir,
@@ -1412,6 +1429,32 @@ impl ApplicationSettings {
1412 Ok(()) 1429 Ok(())
1413 } 1430 }
1414 1431
1432 /// The device ID saved at logout, if it was saved for this profile's
1433 /// user. A missing or stale file is not an error: the next login then
1434 /// simply gets a fresh device ID from the homeserver.
1435 pub fn read_saved_device(&self) -> Option<OwnedDeviceId> {
1436 let file = File::open(self.device_json.as_path()).ok()?;
1437 let reader = BufReader::new(file);
1438 let saved: SavedDevice = serde_json::from_reader(reader).ok()?;
1439
1440 if saved.user_id == self.profile.user_id {
1441 Some(saved.device_id)
1442 } else {
1443 None
1444 }
1445 }
1446
1447 pub fn write_saved_device(&self, device_id: &DeviceId) -> Result<(), IambError> {
1448 let file = File::create(self.device_json.as_path())?;
1449 let writer = BufWriter::new(file);
1450 let saved = SavedDevice {
1451 user_id: self.profile.user_id.clone(),
1452 device_id: device_id.to_owned(),
1453 };
1454 serde_json::to_writer(writer, &saved).map_err(IambError::from)?;
1455 Ok(())
1456 }
1457
1415 pub fn get_user_char_span(&self, user_id: &UserId) -> Span<'_> { 1458 pub fn get_user_char_span(&self, user_id: &UserId) -> Span<'_> {
1416 let (color, c) = self 1459 let (color, c) = self
1417 .tunables 1460 .tunables
src/tests.rs +1
@@ -206,6 +206,7 @@ pub fn mock_settings() -> ApplicationSettings {
206 ApplicationSettings { 206 ApplicationSettings {
207 layout_json: PathBuf::new(), 207 layout_json: PathBuf::new(),
208 session_json: PathBuf::new(), 208 session_json: PathBuf::new(),
209 device_json: PathBuf::new(),
209 session_json_old: PathBuf::new(), 210 session_json_old: PathBuf::new(),
210 sled_dir: PathBuf::new(), 211 sled_dir: PathBuf::new(),
211 sqlite_dir: PathBuf::new(), 212 sqlite_dir: PathBuf::new(),
src/worker.rs +20 −10
@@ -2118,18 +2118,19 @@ impl ClientWorker {
2118 client.restore_session(session).await.map_err(IambError::from)?; 2118 client.restore_session(session).await.map_err(IambError::from)?;
2119 }, 2119 },
2120 LoginStyle::Password(password) => { 2120 LoginStyle::Password(password) => {
2121 let resp = client 2121 let mut login = client
2122 .matrix_auth() 2122 .matrix_auth()
2123 .login_username(&self.settings.profile.user_id, &password) 2123 .login_username(&self.settings.profile.user_id, &password)
2124 .initial_device_display_name(initial_devname().as_str()) 2124 .initial_device_display_name(initial_devname().as_str());
2125 .send() 2125 if let Some(device_id) = self.settings.read_saved_device() {
2126 .await 2126 login = login.device_id(device_id.as_str());
2127 .map_err(IambError::from)?; 2127 }
2128 let resp = login.send().await.map_err(IambError::from)?;
2128 let session = MatrixSession::from(&resp); 2129 let session = MatrixSession::from(&resp);
2129 self.settings.write_session(session)?; 2130 self.settings.write_session(session)?;
2130 }, 2131 },
2131 LoginStyle::SingleSignOn => { 2132 LoginStyle::SingleSignOn => {
2132 let resp = client 2133 let mut login = client
2133 .matrix_auth() 2134 .matrix_auth()
2134 .login_sso(|url| { 2135 .login_sso(|url| {
2135 let opened = format!( 2136 let opened = format!(
@@ -2142,10 +2143,11 @@ impl ClientWorker {
2142 Ok(()) 2143 Ok(())
2143 } 2144 }
2144 }) 2145 })
2145 .initial_device_display_name(initial_devname().as_str()) 2146 .initial_device_display_name(initial_devname().as_str());
2146 .send() 2147 if let Some(device_id) = self.settings.read_saved_device() {
2147 .await 2148 login = login.device_id(device_id.as_str());
2148 .map_err(IambError::from)?; 2149 }
2150 let resp = login.send().await.map_err(IambError::from)?;
2149 2151
2150 let session = MatrixSession::from(&resp); 2152 let session = MatrixSession::from(&resp);
2151 self.settings.write_session(session)?; 2153 self.settings.write_session(session)?;
@@ -2176,6 +2178,14 @@ impl ClientWorker {
2176 return Err(err); 2178 return Err(err);
2177 } 2179 }
2178 2180
2181 // Capture the device ID before logging out, while the session is
2182 // still active. The next login reuses it, which keeps the existing
2183 // SDK store valid: the store is keyed by user and device ID, and a
2184 // freshly issued device ID would no longer match it.
2185 if let Some(device_id) = self.client.device_id() {
2186 self.settings.write_saved_device(device_id)?;
2187 }
2188
2179 // Send the logout request. 2189 // Send the logout request.
2180 if let Err(e) = self.client.matrix_auth().logout().await { 2190 if let Err(e) = self.client.matrix_auth().logout().await {
2181 let msg = format!("Failed to logout: {e}"); 2191 let msg = format!("Failed to logout: {e}");