713205a95af2 | atagen | 2026-09-26 09:24 UTC | 1 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 2 | use std::path::{Path, PathBuf}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 3 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 4 | use matrix_sdk::{BoxFuture, SendOutsideWasm, SyncOutsideWasm}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 5 | use matrix_sdk_store_encryption::StoreCipher; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 6 | use tokio::sync::Mutex; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 7 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 8 | use crate::{AccountError, SessionRecord, StoreConfig, account::account_error}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 9 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 10 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 11 | type RecordLocation = PathBuf; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 12 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 13 | type RecordLocation = String; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 14 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 15 | /// one login's record and the matrix store it belongs to. the engine rotates refresh tokens |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 16 | /// and each works once, so a `save` has to land before the next refresh or the login is gone |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 17 | pub trait SessionStore: SendOutsideWasm + SyncOutsideWasm { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 18 | fn store(&self) -> &StoreConfig; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 19 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 20 | fn load(&self) -> BoxFuture<'_, Result<Option<SessionRecord>, AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 21 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 22 | /// replace the saved record, all or nothing |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 23 | fn save<'a>(&'a self, record: &'a SessionRecord) -> BoxFuture<'a, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 24 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 25 | /// `save`, but without awaiting. the sdk calls it from inside a refresh, before the new |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 26 | /// refresh token is ever used, so a store that can write synchronously closes the gap |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 27 | /// a crash would otherwise lose the login through. `None` leaves the refresh to `save` |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 28 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 29 | fn save_blocking(&self, _record: &SessionRecord) -> Option<Result<(), AccountError>> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 30 | None |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 31 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 32 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 33 | /// swap the record for one that only says who to log in again as, keeping the store. |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 34 | /// `Session::restore` then answers `Restore::SoftLoggedOut`, even after a restart |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 35 | fn soft_log_out(&self) -> BoxFuture<'_, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 36 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 37 | /// drop the record and delete the store, once nothing has the store open |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 38 | fn discard(&self) -> BoxFuture<'_, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 39 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 40 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 41 | /// keeps the record sealed with the store's passphrase, in a file natively and in an |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 42 | /// IndexedDB database of its own in a browser |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 43 | pub struct EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 44 | record: RecordLocation, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 45 | store: StoreConfig, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 46 | sealer: Mutex<Option<Sealer>>, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 47 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 48 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 49 | struct Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 50 | cipher: StoreCipher, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 51 | exported: Vec<u8>, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 52 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 53 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 54 | impl EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 55 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 56 | #[must_use] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 57 | pub fn file(record: impl Into<PathBuf>, store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 58 | Self::new(record.into(), store) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 59 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 60 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 61 | /// the record lives beside the store, in `<name>::macltok-session` |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 62 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 63 | #[must_use] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 64 | pub fn indexeddb(store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 65 | let record = format!("{}::macltok-session", store.indexeddb_name()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 66 | Self::new(record, store) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 67 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 68 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 69 | fn new(record: RecordLocation, store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 70 | Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 71 | record, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 72 | store, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 73 | sealer: Mutex::new(None), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 74 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 75 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 76 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 77 | async fn load_record(&self) -> Result<Option<SessionRecord>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 78 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 79 | self.read(&mut sealer).await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 80 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 81 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 82 | async fn save_record(&self, record: &SessionRecord) -> Result<(), AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 83 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 84 | self.write(&mut sealer, record).await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 85 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 86 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 87 | async fn soft_log_out_record(&self) -> Result<(), AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 88 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 89 | match self.read(&mut sealer).await? { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 90 | Some(record) => self.write(&mut sealer, &record.soft_logged_out()).await, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 91 | None => Ok(()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 92 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 93 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 94 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 95 | async fn discard_all(&self) -> Result<(), AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 96 | let mut sealer = self.sealer.lock().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 97 | backend::remove(&self.record) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 98 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 99 | .map_err(|error| account_error("removing the session record", error))?; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 100 | *sealer = None; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 101 | self.store.delete().await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 102 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 103 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 104 | async fn read( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 105 | &self, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 106 | sealer: &mut Option<Sealer>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 107 | ) -> Result<Option<SessionRecord>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 108 | let Some(sealed) = backend::read(&self.record) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 109 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 110 | .map_err(|error| account_error("reading the session record", error))? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 111 | else { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 112 | return Ok(None); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 113 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 114 | let (opened, record) = unseal(self.store.passphrase(), &sealed)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 115 | *sealer = Some(opened); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 116 | Ok(Some(record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 117 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 118 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 119 | async fn write( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 120 | &self, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 121 | sealer: &mut Option<Sealer>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 122 | record: &SessionRecord, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 123 | ) -> Result<(), AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 124 | let sealed = self.seal(sealer, record)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 125 | backend::write(&self.record, &sealed) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 126 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 127 | .map_err(|error| account_error("writing the session record", error)) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 128 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 129 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 130 | fn seal( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 131 | &self, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 132 | sealer: &mut Option<Sealer>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 133 | record: &SessionRecord, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 134 | ) -> Result<Vec<u8>, AccountError> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 135 | let sealer = match sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 136 | Some(sealer) => sealer, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 137 | empty => empty.insert(Sealer::new(self.store.passphrase())?), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 138 | }; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 139 | sealer.seal(record) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 140 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 141 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 142 | /// the lock is only ever held around one small write, so losing the race to it just |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 143 | /// hands this refresh to the async save |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 144 | #[cfg(feature = "sqlite")] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 145 | fn save_now(&self, record: &SessionRecord) -> Option<Result<(), AccountError>> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 146 | let mut sealer = self.sealer.try_lock().ok()?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 147 | Some(self.seal(&mut sealer, record).and_then(|sealed| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 148 | backend::write_blocking(&self.record, &sealed) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 149 | .map_err(|error| account_error("writing the session record", error)) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 150 | })) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 151 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 152 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 153 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 154 | impl SessionStore for EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 155 | fn store(&self) -> &StoreConfig { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 156 | &self.store |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 157 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 158 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 159 | fn load(&self) -> BoxFuture<'_, Result<Option<SessionRecord>, AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 160 | Box::pin(self.load_record()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 161 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 162 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 163 | fn save<'a>(&'a self, record: &'a SessionRecord) -> BoxFuture<'a, Result<(), AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 164 | Box::pin(self.save_record(record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 165 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 166 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 167 | #[cfg(feature = "sqlite")] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 168 | fn save_blocking(&self, record: &SessionRecord) -> Option<Result<(), AccountError>> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 169 | self.save_now(record) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 170 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 171 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 172 | fn soft_log_out(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 173 | Box::pin(self.soft_log_out_record()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 174 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 175 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 176 | fn discard(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 177 | Box::pin(self.discard_all()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 178 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 179 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 180 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 181 | impl Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 182 | fn new(passphrase: &str) -> Result<Self, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 183 | let cipher = StoreCipher::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 184 | .map_err(|error| account_error("creating the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 185 | let exported = cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 186 | .export(passphrase) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 187 | .map_err(|error| account_error("sealing the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 188 | Ok(Self { cipher, exported }) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 189 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 190 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 191 | fn seal(&self, record: &SessionRecord) -> Result<Vec<u8>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 192 | let value = self |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 193 | .cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 194 | .encrypt_value(record) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 195 | .map_err(|error| account_error("sealing the session record", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 196 | let length = u32::try_from(self.exported.len()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 197 | .expect("an exported store cipher is a few hundred bytes"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 198 | let mut sealed = Vec::with_capacity(4 + self.exported.len() + value.len()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 199 | sealed.extend_from_slice(&length.to_be_bytes()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 200 | sealed.extend_from_slice(&self.exported); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 201 | sealed.extend_from_slice(&value); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 202 | Ok(sealed) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 203 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 204 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 205 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 206 | fn unseal(passphrase: &str, sealed: &[u8]) -> Result<(Sealer, SessionRecord), AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 207 | let truncated = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 208 | account_error( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 209 | "opening the session record", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 210 | std::io::Error::from(std::io::ErrorKind::UnexpectedEof), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 211 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 212 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 213 | let (length, rest) = sealed.split_first_chunk::<4>().ok_or_else(truncated)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 214 | let (exported, value) = rest |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 215 | .split_at_checked(u32::from_be_bytes(*length) as usize) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 216 | .ok_or_else(truncated)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 217 | let cipher = StoreCipher::import(passphrase, exported) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 218 | .map_err(|error| account_error("opening the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 219 | let record = cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 220 | .decrypt_value(value) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 221 | .map_err(|error| account_error("opening the session record", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 222 | let sealer = Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 223 | cipher, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 224 | exported: exported.to_vec(), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 225 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 226 | Ok((sealer, record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 227 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 228 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 229 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 230 | mod backend { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 231 | use std::{ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 232 | fs::{self, File, OpenOptions}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 233 | io::{self, ErrorKind, Write}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 234 | path::{Path, PathBuf}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 235 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 236 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 237 | pub(super) async fn read(path: &Path) -> io::Result<Option<Vec<u8>>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 238 | match fs::read(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 239 | Ok(sealed) => Ok(Some(sealed)), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 240 | Err(error) if error.kind() == ErrorKind::NotFound => Ok(None), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 241 | Err(error) => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 242 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 243 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 244 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 245 | pub(super) async fn write(path: &Path, sealed: &[u8]) -> io::Result<()> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 246 | write_blocking(path, sealed) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 247 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 248 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 249 | pub(super) fn write_blocking(path: &Path, sealed: &[u8]) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 250 | let mut temporary = path.as_os_str().to_owned(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 251 | temporary.push(".tmp"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 252 | let temporary = PathBuf::from(temporary); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 253 | let parent = match path.parent() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 254 | Some(parent) if !parent.as_os_str().is_empty() => parent, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 255 | _ => Path::new("."), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 256 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 257 | fs::create_dir_all(parent)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 258 | let written = replace(path, &temporary, parent, sealed); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 259 | if written.is_err() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 260 | let _ = fs::remove_file(&temporary); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 261 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 262 | written |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 263 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 264 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 265 | fn replace(path: &Path, temporary: &Path, parent: &Path, sealed: &[u8]) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 266 | let mut file = private_file(temporary)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 267 | file.write_all(sealed)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 268 | file.sync_all()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 269 | fs::rename(temporary, path)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 270 | sync_directory(parent) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 271 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 272 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 273 | pub(super) async fn remove(path: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 274 | match fs::remove_file(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 275 | Err(error) if error.kind() != ErrorKind::NotFound => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 276 | _ => Ok(()), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 277 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 278 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 279 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 280 | #[cfg(unix)] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 281 | fn private_file(path: &Path) -> io::Result<File> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 282 | use std::os::unix::fs::OpenOptionsExt; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 283 | OpenOptions::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 284 | .write(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 285 | .create(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 286 | .truncate(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 287 | .mode(0o600) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 288 | .open(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 289 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 290 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 291 | #[cfg(not(unix))] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 292 | fn private_file(path: &Path) -> io::Result<File> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 293 | OpenOptions::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 294 | .write(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 295 | .create(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 296 | .truncate(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 297 | .open(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 298 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 299 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 300 | /// a rename isn't durable until its directory is synced |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 301 | #[cfg(unix)] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 302 | fn sync_directory(path: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 303 | File::open(path)?.sync_all() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 304 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 305 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 306 | #[cfg(not(unix))] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 307 | fn sync_directory(_: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 308 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 309 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 310 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 311 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 312 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 313 | mod backend { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 314 | use indexed_db_futures::{ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 315 | Build, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 316 | database::Database, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 317 | error::{Error, OpenDbError}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 318 | prelude::*, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 319 | transaction::{Transaction, TransactionMode}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 320 | typed_array::{Uint8Array, Uint8ArraySlice}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 321 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 322 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 323 | const RECORDS: &str = "records"; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 324 | const KEY: &str = "record"; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 325 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 326 | async fn open(name: &str) -> Result<Database, OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 327 | Database::open(name) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 328 | .with_version(1u32) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 329 | .with_on_upgrade_needed(|_, transaction: &Transaction<'_>| -> Result<(), Error> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 330 | transaction.db().create_object_store(RECORDS).build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 331 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 332 | }) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 333 | .build()? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 334 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 335 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 336 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 337 | pub(super) async fn read(name: &str) -> Result<Option<Vec<u8>>, OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 338 | let database = open(name).await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 339 | let sealed: Option<Uint8Array> = database |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 340 | .transaction(RECORDS) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 341 | .with_mode(TransactionMode::Readonly) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 342 | .build()? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 343 | .object_store(RECORDS)? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 344 | .get(KEY) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 345 | .await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 346 | Ok(sealed.map(Into::into)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 347 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 348 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 349 | pub(super) async fn write(name: &str, sealed: &[u8]) -> Result<(), OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 350 | let database = open(name).await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 351 | let transaction = database |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 352 | .transaction(RECORDS) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 353 | .with_mode(TransactionMode::Readwrite) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 354 | .build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 355 | transaction |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 356 | .object_store(RECORDS)? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 357 | .put(Uint8ArraySlice::new(sealed)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 358 | .with_key(KEY) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 359 | .build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 360 | Ok(transaction.commit().await?) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 361 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 362 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 363 | pub(super) async fn remove(name: &str) -> Result<(), OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 364 | Ok(Database::delete_by_name(name)?.await?) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 365 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 366 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 367 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 368 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 369 | pub(crate) fn delete_sqlite_store(path: &Path) -> std::io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 370 | use std::{fs, io::ErrorKind}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 371 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 372 | // matrix-sdk-sqlite 0.18's database names, which it keeps private |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 373 | const DATABASES: [&str; 4] = [ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 374 | "matrix-sdk-state.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 375 | "matrix-sdk-crypto.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 376 | "matrix-sdk-event-cache.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 377 | "matrix-sdk-media.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 378 | ]; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 379 | for database in DATABASES { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 380 | for suffix in ["", "-wal", "-shm", "-journal"] { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 381 | match fs::remove_file(path.join(format!("{database}{suffix}"))) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 382 | Err(error) if error.kind() != ErrorKind::NotFound => return Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 383 | _ => {} |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 384 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 385 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 386 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 387 | match fs::remove_dir(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 388 | Err(error) if error.kind() != ErrorKind::NotFound => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 389 | _ => Ok(()), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 390 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 391 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 392 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 393 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 394 | pub(crate) async fn delete_indexeddb_store( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 395 | name: &str, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 396 | ) -> Result<(), indexed_db_futures::error::OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 397 | use indexed_db_futures::database::Database; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 398 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 399 | // the databases matrix-sdk-indexeddb 0.18 opens under one store name |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 400 | for suffix in [ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 401 | "", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 402 | "::matrix-sdk-state", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 403 | "::matrix-sdk-crypto", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 404 | "::matrix-sdk-crypto-meta", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 405 | "::event_cache", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 406 | "::media", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 407 | ] { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 408 | Database::delete_by_name(&format!("{name}{suffix}"))?.await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 409 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 410 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 411 | } |