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 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 7 | use crate::{AccountError, SessionRecord, StoreConfig, account::account_error}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 8 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 9 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 10 | type RecordLocation = PathBuf; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 11 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 12 | type RecordLocation = String; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 13 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 14 | #[cfg(not(target_family = "wasm"))] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 15 | type Lock<T> = std::sync::Mutex<T>; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 16 | #[cfg(target_family = "wasm")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 17 | type Lock<T> = tokio::sync::Mutex<T>; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 18 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 19 | /// one login's record and the matrix store it belongs to. the engine rotates refresh tokens |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 20 | /// 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 | 21 | pub trait SessionStore: SendOutsideWasm + SyncOutsideWasm { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 22 | fn store(&self) -> &StoreConfig; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 23 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 24 | fn load(&self) -> BoxFuture<'_, Result<Option<SessionRecord>, AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 25 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 26 | /// replace the saved record, all or nothing |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 27 | fn save<'a>(&'a self, record: &'a SessionRecord) -> BoxFuture<'a, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 28 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 29 | /// `save`, but without awaiting. the sdk calls it from inside a refresh, before the new |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 30 | /// refresh token is ever used, so a store that can write synchronously closes the gap |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 31 | /// a crash would otherwise lose the login through. `None` leaves the refresh to `save` |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 32 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 33 | fn save_blocking(&self, _record: &SessionRecord) -> Option<Result<(), AccountError>> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 34 | None |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 35 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 36 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 37 | /// 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 | 38 | /// `Session::restore` then answers `Restore::SoftLoggedOut`, even after a restart |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 39 | fn soft_log_out(&self) -> BoxFuture<'_, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 40 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 41 | /// drop the record and delete the store, once nothing has the store open |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 42 | fn discard(&self) -> BoxFuture<'_, Result<(), AccountError>>; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 43 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 44 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 45 | /// keeps the record sealed with the store's passphrase, in a file natively and in an |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 46 | /// IndexedDB database of its own in a browser |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 47 | pub struct EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 48 | record: RecordLocation, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 49 | store: StoreConfig, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 50 | sealer: Lock<Option<Sealer>>, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 51 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 52 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 53 | struct Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 54 | cipher: StoreCipher, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 55 | exported: Vec<u8>, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 56 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 57 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 58 | impl EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 59 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 60 | #[must_use] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 61 | pub fn file(record: impl Into<PathBuf>, store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 62 | Self::new(record.into(), store) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 63 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 64 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 65 | /// the record lives beside the store, in `<name>::macltok-session` |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 66 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 67 | #[must_use] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 68 | pub fn indexeddb(store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 69 | let record = format!("{}::macltok-session", store.indexeddb_name()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 70 | Self::new(record, store) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 71 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 72 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 73 | fn new(record: RecordLocation, store: StoreConfig) -> Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 74 | Self { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 75 | record, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 76 | store, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 77 | sealer: Lock::new(None), |
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 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 81 | fn open( |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 82 | &self, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 83 | sealer: &mut Option<Sealer>, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 84 | sealed: Option<Vec<u8>>, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 85 | ) -> Result<Option<SessionRecord>, AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 86 | let Some(sealed) = sealed else { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 87 | return Ok(None); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 88 | }; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 89 | let (opened, record) = unseal(self.store.passphrase(), &sealed)?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 90 | *sealer = Some(opened); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 91 | Ok(Some(record)) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 92 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 93 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 94 | fn seal( |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 95 | &self, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 96 | sealer: &mut Option<Sealer>, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 97 | record: &SessionRecord, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 98 | ) -> Result<Vec<u8>, AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 99 | let sealer = match sealer { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 100 | Some(sealer) => sealer, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 101 | empty => empty.insert(Sealer::new(self.store.passphrase())?), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 102 | }; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 103 | sealer.seal(record) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 104 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 105 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 106 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 107 | #[cfg(not(target_family = "wasm"))] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 108 | impl EncryptedSessionStore { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 109 | fn lock(&self) -> std::sync::MutexGuard<'_, Option<Sealer>> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 110 | self.sealer |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 111 | .lock() |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 112 | .unwrap_or_else(std::sync::PoisonError::into_inner) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 113 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 114 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 115 | fn read(&self, sealer: &mut Option<Sealer>) -> Result<Option<SessionRecord>, AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 116 | let sealed = backend::read(&self.record) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 117 | .map_err(|error| account_error("reading the session record", error))?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 118 | self.open(sealer, sealed) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 119 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 120 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 121 | fn write( |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 122 | &self, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 123 | sealer: &mut Option<Sealer>, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 124 | record: &SessionRecord, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 125 | ) -> Result<(), AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 126 | let sealed = self.seal(sealer, record)?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 127 | backend::write(&self.record, &sealed) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 128 | .map_err(|error| account_error("writing the session record", error)) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 129 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 130 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 131 | fn load_record(&self) -> Result<Option<SessionRecord>, AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 132 | self.read(&mut self.lock()) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 133 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 134 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 135 | fn save_record(&self, record: &SessionRecord) -> Result<(), AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 136 | self.write(&mut self.lock(), record) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 137 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 138 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 139 | fn soft_log_out_record(&self) -> Result<(), AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 140 | let mut sealer = self.lock(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 141 | match self.read(&mut sealer)? { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 142 | Some(record) => self.write(&mut sealer, &record.soft_logged_out()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 143 | None => Ok(()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 144 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 145 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 146 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 147 | fn forget_record(&self) -> Result<(), AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 148 | let mut sealer = self.lock(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 149 | backend::remove(&self.record) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 150 | .map_err(|error| account_error("removing the session record", error))?; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 151 | *sealer = None; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 152 | Ok(()) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 153 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 154 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 155 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 156 | #[cfg(not(target_family = "wasm"))] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 157 | impl SessionStore for EncryptedSessionStore { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 158 | fn store(&self) -> &StoreConfig { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 159 | &self.store |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 160 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 161 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 162 | fn load(&self) -> BoxFuture<'_, Result<Option<SessionRecord>, AccountError>> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 163 | Box::pin(async move { self.load_record() }) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 164 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 165 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 166 | fn save<'a>(&'a self, record: &'a SessionRecord) -> BoxFuture<'a, Result<(), AccountError>> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 167 | Box::pin(async move { self.save_record(record) }) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 168 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 169 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 170 | /// blocks the calling worker for the write and its fsyncs, about a millisecond |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 171 | fn save_blocking(&self, record: &SessionRecord) -> Option<Result<(), AccountError>> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 172 | Some(self.save_record(record)) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 173 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 174 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 175 | fn soft_log_out(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 176 | Box::pin(async move { self.soft_log_out_record() }) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 177 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 178 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 179 | fn discard(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 180 | Box::pin(async move { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 181 | self.forget_record()?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 182 | self.store.delete().await |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 183 | }) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 184 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 185 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 186 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 187 | #[cfg(target_family = "wasm")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 188 | impl EncryptedSessionStore { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 189 | async fn read( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 190 | &self, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 191 | sealer: &mut Option<Sealer>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 192 | ) -> Result<Option<SessionRecord>, AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 193 | let sealed = backend::read(&self.record) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 194 | .await |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 195 | .map_err(|error| account_error("reading the session record", error))?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 196 | self.open(sealer, sealed) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 197 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 198 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 199 | async fn write( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 200 | &self, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 201 | sealer: &mut Option<Sealer>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 202 | record: &SessionRecord, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 203 | ) -> Result<(), AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 204 | let sealed = self.seal(sealer, record)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 205 | backend::write(&self.record, &sealed) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 206 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 207 | .map_err(|error| account_error("writing the session record", error)) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 208 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 209 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 210 | async fn load_record(&self) -> Result<Option<SessionRecord>, AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 211 | self.read(&mut *self.sealer.lock().await).await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 212 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 213 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 214 | async fn save_record(&self, record: &SessionRecord) -> Result<(), AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 215 | self.write(&mut *self.sealer.lock().await, record).await |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 216 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 217 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 218 | async fn soft_log_out_record(&self) -> Result<(), AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 219 | let mut sealer = self.sealer.lock().await; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 220 | match self.read(&mut sealer).await? { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 221 | Some(record) => self.write(&mut sealer, &record.soft_logged_out()).await, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 222 | None => Ok(()), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 223 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 224 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 225 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 226 | async fn discard_all(&self) -> Result<(), AccountError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 227 | let mut sealer = self.sealer.lock().await; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 228 | backend::remove(&self.record) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 229 | .await |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 230 | .map_err(|error| error.into_account_error("removing the session record"))?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 231 | *sealer = None; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 232 | self.store.delete().await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 233 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 234 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 235 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 236 | #[cfg(target_family = "wasm")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 237 | impl SessionStore for EncryptedSessionStore { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 238 | fn store(&self) -> &StoreConfig { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 239 | &self.store |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 240 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 241 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 242 | fn load(&self) -> BoxFuture<'_, Result<Option<SessionRecord>, AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 243 | Box::pin(self.load_record()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 244 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 245 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 246 | fn save<'a>(&'a self, record: &'a SessionRecord) -> BoxFuture<'a, Result<(), AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 247 | Box::pin(self.save_record(record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 248 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 249 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 250 | fn soft_log_out(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 251 | Box::pin(self.soft_log_out_record()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 252 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 253 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 254 | fn discard(&self) -> BoxFuture<'_, Result<(), AccountError>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 255 | Box::pin(self.discard_all()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 256 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 257 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 258 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 259 | impl Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 260 | fn new(passphrase: &str) -> Result<Self, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 261 | let cipher = StoreCipher::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 262 | .map_err(|error| account_error("creating the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 263 | let exported = cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 264 | .export(passphrase) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 265 | .map_err(|error| account_error("sealing the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 266 | Ok(Self { cipher, exported }) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 267 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 268 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 269 | fn seal(&self, record: &SessionRecord) -> Result<Vec<u8>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 270 | let value = self |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 271 | .cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 272 | .encrypt_value(record) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 273 | .map_err(|error| account_error("sealing the session record", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 274 | let length = u32::try_from(self.exported.len()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 275 | .expect("an exported store cipher is a few hundred bytes"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 276 | let mut sealed = Vec::with_capacity(4 + self.exported.len() + value.len()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 277 | sealed.extend_from_slice(&length.to_be_bytes()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 278 | sealed.extend_from_slice(&self.exported); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 279 | sealed.extend_from_slice(&value); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 280 | Ok(sealed) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 281 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 282 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 283 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 284 | fn unseal(passphrase: &str, sealed: &[u8]) -> Result<(Sealer, SessionRecord), AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 285 | let truncated = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 286 | account_error( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 287 | "opening the session record", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 288 | std::io::Error::from(std::io::ErrorKind::UnexpectedEof), |
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 | let (length, rest) = sealed.split_first_chunk::<4>().ok_or_else(truncated)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 292 | let (exported, value) = rest |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 293 | .split_at_checked(u32::from_be_bytes(*length) as usize) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 294 | .ok_or_else(truncated)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 295 | let cipher = StoreCipher::import(passphrase, exported) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 296 | .map_err(|error| account_error("opening the session record cipher", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 297 | let record = cipher |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 298 | .decrypt_value(value) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 299 | .map_err(|error| account_error("opening the session record", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 300 | let sealer = Sealer { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 301 | cipher, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 302 | exported: exported.to_vec(), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 303 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 304 | Ok((sealer, record)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 305 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 306 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 307 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 308 | mod backend { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 309 | use std::{ |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 310 | ffi::OsString, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 311 | fs::{self, File, OpenOptions}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 312 | io::{self, ErrorKind, Write}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 313 | path::{Path, PathBuf}, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 314 | sync::atomic::{AtomicU64, Ordering}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 315 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 316 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 317 | static WRITES: AtomicU64 = AtomicU64::new(0); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 318 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 319 | pub(super) fn read(path: &Path) -> io::Result<Option<Vec<u8>>> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 320 | match fs::read(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 321 | Ok(sealed) => Ok(Some(sealed)), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 322 | Err(error) if error.kind() == ErrorKind::NotFound => Ok(None), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 323 | Err(error) => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 324 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 325 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 326 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 327 | pub(super) fn write(path: &Path, sealed: &[u8]) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 328 | let parent = match path.parent() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 329 | Some(parent) if !parent.as_os_str().is_empty() => parent, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 330 | _ => Path::new("."), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 331 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 332 | fs::create_dir_all(parent)?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 333 | let (temporary, mut file) = temporary_file(path)?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 334 | let written = replace(path, &temporary, &mut file, parent, sealed); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 335 | if written.is_err() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 336 | let _ = fs::remove_file(&temporary); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 337 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 338 | written |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 339 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 340 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 341 | fn temporary_file(path: &Path) -> io::Result<(PathBuf, File)> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 342 | loop { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 343 | let mut name = OsString::from(path.as_os_str()); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 344 | let write = WRITES.fetch_add(1, Ordering::Relaxed); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 345 | name.push(format!(".{}.{write}.tmp", std::process::id())); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 346 | let temporary = PathBuf::from(name); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 347 | match private_file(&temporary) { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 348 | Ok(file) => return Ok((temporary, file)), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 349 | Err(error) if error.kind() == ErrorKind::AlreadyExists => {} |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 350 | Err(error) => return Err(error), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 351 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 352 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 353 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 354 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 355 | fn replace( |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 356 | path: &Path, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 357 | temporary: &Path, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 358 | file: &mut File, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 359 | parent: &Path, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 360 | sealed: &[u8], |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 361 | ) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 362 | file.write_all(sealed)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 363 | file.sync_all()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 364 | fs::rename(temporary, path)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 365 | sync_directory(parent) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 366 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 367 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 368 | pub(super) fn remove(path: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 369 | match fs::remove_file(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 370 | Err(error) if error.kind() != ErrorKind::NotFound => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 371 | _ => Ok(()), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 372 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 373 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 374 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 375 | /// `create_new` is `O_EXCL`, which posix says never follows a symlink, even a dangling one |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 376 | #[cfg(unix)] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 377 | fn private_file(path: &Path) -> io::Result<File> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 378 | use std::os::unix::fs::OpenOptionsExt; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 379 | OpenOptions::new() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 380 | .write(true) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 381 | .create_new(true) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 382 | .mode(0o600) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 383 | .open(path) |
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 | #[cfg(not(unix))] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 387 | fn private_file(path: &Path) -> io::Result<File> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 388 | OpenOptions::new().write(true).create_new(true).open(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 389 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 390 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 391 | /// a rename isn't durable until its directory is synced |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 392 | #[cfg(unix)] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 393 | fn sync_directory(path: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 394 | File::open(path)?.sync_all() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 395 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 396 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 397 | #[cfg(not(unix))] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 398 | fn sync_directory(_: &Path) -> io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 399 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 400 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 401 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 402 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 403 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 404 | mod backend { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 405 | use indexed_db_futures::{ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 406 | Build, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 407 | database::Database, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 408 | error::{Error, OpenDbError}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 409 | prelude::*, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 410 | transaction::{Transaction, TransactionMode}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 411 | typed_array::{Uint8Array, Uint8ArraySlice}, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 412 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 413 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 414 | const RECORDS: &str = "records"; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 415 | const KEY: &str = "record"; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 416 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 417 | async fn open(name: &str) -> Result<Database, OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 418 | Database::open(name) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 419 | .with_version(1u32) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 420 | .with_on_upgrade_needed(|_, transaction: &Transaction<'_>| -> Result<(), Error> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 421 | transaction.db().create_object_store(RECORDS).build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 422 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 423 | }) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 424 | .build()? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 425 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 426 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 427 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 428 | /// the crate's `Database` never closes itself on drop, and an open one blocks deleting it |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 429 | pub(super) async fn read(name: &str) -> Result<Option<Vec<u8>>, OpenDbError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 430 | let database = open(name).await?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 431 | let sealed = get(&database).await; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 432 | database.close(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 433 | Ok(sealed?.map(Into::into)) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 434 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 435 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 436 | pub(super) async fn write(name: &str, sealed: &[u8]) -> Result<(), OpenDbError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 437 | let database = open(name).await?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 438 | let written = put(&database, sealed).await; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 439 | database.close(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 440 | Ok(written?) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 441 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 442 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 443 | pub(super) async fn remove(name: &str) -> Result<(), super::DeleteError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 444 | super::delete_database(name).await |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 445 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 446 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 447 | async fn get(database: &Database) -> Result<Option<Uint8Array>, Error> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 448 | database |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 449 | .transaction(RECORDS) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 450 | .with_mode(TransactionMode::Readonly) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 451 | .build()? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 452 | .object_store(RECORDS)? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 453 | .get(KEY) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 454 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 455 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 456 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 457 | async fn put(database: &Database, sealed: &[u8]) -> Result<(), Error> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 458 | let transaction = database |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 459 | .transaction(RECORDS) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 460 | .with_mode(TransactionMode::Readwrite) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 461 | .build()?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 462 | transaction |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 463 | .object_store(RECORDS)? |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 464 | .put(Uint8ArraySlice::new(sealed)) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 465 | .with_key(KEY) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 466 | .build()?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 467 | transaction.commit().await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 468 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 469 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 470 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 471 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 472 | pub(crate) fn delete_sqlite_store(path: &Path) -> std::io::Result<()> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 473 | use std::{fs, io::ErrorKind}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 474 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 475 | // matrix-sdk-sqlite 0.18's database names, which it keeps private |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 476 | const DATABASES: [&str; 6] = [ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 477 | "matrix-sdk-state.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 478 | "matrix-sdk-crypto.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 479 | "matrix-sdk-event-cache.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 480 | "matrix-sdk-media.sqlite3", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 481 | "macltok-mentions.sqlite3", |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 482 | "macltok-search.sqlite3", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 483 | ]; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 484 | for database in DATABASES { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 485 | for suffix in ["", "-wal", "-shm", "-journal"] { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 486 | match fs::remove_file(path.join(format!("{database}{suffix}"))) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 487 | Err(error) if error.kind() != ErrorKind::NotFound => return Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 488 | _ => {} |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 489 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 490 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 491 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 492 | match fs::remove_file(path.join(crate::store_lock::LOCK_FILE)) { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 493 | Err(error) if error.kind() != ErrorKind::NotFound => return Err(error), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 494 | _ => {} |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 495 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 496 | match fs::remove_dir(path) { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 497 | Err(error) if error.kind() != ErrorKind::NotFound => Err(error), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 498 | _ => Ok(()), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 499 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 500 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 501 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 502 | #[cfg(feature = "indexeddb")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 503 | pub(crate) async fn delete_indexeddb_store(name: &str) -> Result<(), DeleteError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 504 | // the databases matrix-sdk-indexeddb 0.18 opens under one store name |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 505 | for suffix in [ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 506 | "", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 507 | "::matrix-sdk-state", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 508 | "::matrix-sdk-crypto", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 509 | "::matrix-sdk-crypto-meta", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 510 | "::event_cache", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 511 | "::media", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 512 | "::macltok-mentions", |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 513 | "::macltok-search", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 514 | ] { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 515 | delete_database(&format!("{name}{suffix}")).await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 516 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 517 | Ok(()) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 518 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 519 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 520 | #[cfg(feature = "indexeddb")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 521 | #[derive(Debug, thiserror::Error)] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 522 | pub(crate) enum DeleteError { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 523 | #[error("the database is still open somewhere, it goes once that closes")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 524 | Blocked, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 525 | #[error("this global scope has no IndexedDB")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 526 | Unavailable, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 527 | #[error("deleting the database failed, {0}")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 528 | Failed(String), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 529 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 530 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 531 | #[cfg(feature = "indexeddb")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 532 | impl DeleteError { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 533 | pub(crate) fn into_account_error(self, context: &'static str) -> AccountError { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 534 | match self { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 535 | Self::Blocked => AccountError::StoreDeletePending, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 536 | error => account_error(context, error), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 537 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 538 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 539 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 540 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 541 | /// matrix_indexed_db_futures 0.7 ignores `blocked` on a delete, which then waits forever |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 542 | #[cfg(feature = "indexeddb")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 543 | async fn delete_database(name: &str) -> Result<(), DeleteError> { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 544 | use wasm_bindgen::{JsCast, JsValue}; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 545 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 546 | const BLOCKED: &str = "blocked"; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 547 | let global = js_sys::global(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 548 | let factory = if let Some(window) = global.dyn_ref::<web_sys::Window>() { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 549 | window.indexed_db() |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 550 | } else if let Some(worker) = global.dyn_ref::<web_sys::WorkerGlobalScope>() { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 551 | worker.indexed_db() |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 552 | } else { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 553 | return Err(DeleteError::Unavailable); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 554 | }; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 555 | let factory = factory.ok().flatten().ok_or(DeleteError::Unavailable)?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 556 | let request = factory |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 557 | .delete_database(name) |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 558 | .map_err(|error| DeleteError::Failed(format!("{error:?}")))?; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 559 | let settled = js_sys::Promise::new(&mut |resolve, reject| { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 560 | request.set_onsuccess(Some(&resolve)); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 561 | request.set_onerror(Some(&reject)); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 562 | let blocked = reject.bind1(&JsValue::NULL, &JsValue::from_str(BLOCKED)); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 563 | request.set_onblocked(Some(blocked.unchecked_ref())); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 564 | }); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 565 | match wasm_bindgen_futures::JsFuture::from(settled).await { |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 566 | Ok(_) => Ok(()), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 567 | Err(reason) if reason.as_string().as_deref() == Some(BLOCKED) => Err(DeleteError::Blocked), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 568 | Err(_) => Err(DeleteError::Failed(format!("{:?}", request.error()))), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 569 | } |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 570 | } |