973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | use std::{ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | collections::HashMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | path::{Path, PathBuf}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | sync::Arc, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | time::Duration, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | use futures_util::StreamExt; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | use macltok::{ |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 10 | AccountError, ClientHandle, EncryptedSessionStore, EngineConfig, EngineError, ErrorKind, |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 11 | Mirror, MirrorHandle, PasswordLogin, Restore, Retain, Session, SessionStatus, SessionStore, |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 12 | SoftLogout, StateChange, StoreConfig, Termination, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | use rand_core::{OsRng, RngCore}; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | use sha2::{Digest, Sha256}; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | use tokio::{sync::Mutex, task::JoinHandle, time::timeout}; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 18 | use malice_core::{ |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 19 | error::{CoreError, Failure}, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 20 | login::login_error, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 21 | }; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 22 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | use crate::csrf::valid_sid; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | const AUTH_TIMEOUT: Duration = Duration::from_secs(20); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | pub struct UserSession { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | pub sid: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | pub user_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | pub handle: ClientHandle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | pub mirror: MirrorHandle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | sessions: Arc<dyn SessionStore>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | engine: Mutex<Option<JoinHandle<Result<Termination, EngineError>>>>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | mirror_task: Mutex<Option<JoinHandle<()>>>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | struct Inner { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | sessions: tokio::sync::RwLock<HashMap<String, Arc<UserSession>>>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | /// Serialises concurrent restores of the same sid so two engines never |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | /// open the same SQLite store at once. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | init_lock: Mutex<()>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | data_dir: PathBuf, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | master_key: [u8; 32], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | cookie_secure: bool, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | allow_http_homeserver: bool, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | #[derive(Clone)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | pub struct AppState { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | inner: Arc<Inner>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | #[derive(Debug)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | pub enum RestoreError { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | /// No cookie session and nothing on disk: plain guest. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | NotFound, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | /// Credential on disk is unusable: caller should clear the cookie. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | Stale(String), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | /// Credential looks fine but the engine would not start. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | Unavailable(String), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | /// The server logged this device out; it can log back in as the same user. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | SoftLoggedOut { user_id: String, homeserver: String }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | impl AppState { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | pub fn new( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | data_dir: PathBuf, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | master_key: [u8; 32], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | cookie_secure: bool, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | allow_http_homeserver: bool, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | ) -> Self { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | Self { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | inner: Arc::new(Inner { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | sessions: tokio::sync::RwLock::new(HashMap::new()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | init_lock: Mutex::new(()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | data_dir, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | master_key, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | cookie_secure, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | allow_http_homeserver, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | }), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | pub fn allow_http_homeserver(&self) -> bool { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | self.inner.allow_http_homeserver |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | pub fn cookie_secure(&self) -> bool { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | self.inner.cookie_secure |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | fn sessions_dir(&self) -> PathBuf { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | self.inner.data_dir.join("sessions") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | fn session_file(&self, sid: &str) -> PathBuf { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | self.sessions_dir().join(format!("{sid}.bin")) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 99 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 100 | fn store_dir(&self, sid: &str) -> PathBuf { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 101 | self.inner.data_dir.join("stores").join(sid) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 102 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 103 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 104 | fn store_passphrase(&self, sid: &str) -> String { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 105 | let mut hasher = Sha256::new(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 106 | hasher.update(self.inner.master_key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 107 | hasher.update(b"malice-store-v1:"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 108 | hasher.update(sid.as_bytes()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 109 | hex::encode(hasher.finalize()) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 110 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 111 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 112 | fn store_config(&self, sid: &str) -> StoreConfig { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 113 | StoreConfig::sqlite(self.store_dir(sid), self.store_passphrase(sid)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 114 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 115 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 116 | /// A fresh handle onto a sid's record and store. Cheap: nothing touches |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 117 | /// disk until the handle is actually used. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 118 | fn session_store(&self, sid: &str) -> Arc<dyn SessionStore> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 119 | Arc::new(EncryptedSessionStore::file( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 120 | self.session_file(sid), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 121 | self.store_config(sid), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 122 | )) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 123 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 124 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 125 | async fn apply_retention(&self, sessions: &Arc<dyn SessionStore>, retain: Retain) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 126 | let result = match retain { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | Retain::RecordAndStore => Ok(()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | Retain::Store => sessions.soft_log_out().await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | Retain::Nothing => sessions.discard().await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 130 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 131 | if let Err(error) = result { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 132 | tracing::warn!(%error, "applying session retention failed"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 133 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 134 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 135 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 136 | /// Resolve a cookie sid to a live session, restoring from disk on demand. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 137 | /// The shared map is never held across network or engine work: the fast |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 138 | /// path clones an `Arc` under a read lock, and restores serialise on a |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 139 | /// short init lock with a double-checked insert. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 140 | pub async fn get_or_restore(&self, sid: &str) -> Result<Arc<UserSession>, RestoreError> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 141 | if !valid_sid(sid) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 142 | return Err(RestoreError::NotFound); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 143 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 144 | if let Some(user) = self.inner.sessions.read().await.get(sid).cloned() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 145 | return Ok(user); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 146 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 147 | let _init = self.inner.init_lock.lock().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 148 | if let Some(user) = self.inner.sessions.read().await.get(sid).cloned() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 149 | return Ok(user); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 150 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 151 | let sessions = self.session_store(sid); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 152 | let restore = Session::restore(sessions.clone()).await.map_err(|error| { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 153 | tracing::warn!("restoring persisted session failed: {error}"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 154 | restore_error(error) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 155 | })?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 156 | match restore { |
ecb8598b38af | atagen | 2026-10-22 09:19 UTC | 157 | None | Some(Restore::OAuthPending) => Err(RestoreError::NotFound), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 158 | Some(Restore::SoftLoggedOut(soft)) => Err(RestoreError::SoftLoggedOut { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 159 | user_id: soft.user_id.to_string(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 160 | homeserver: soft.homeserver, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 161 | }), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 162 | Some(Restore::Resumed(session)) => self |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 163 | .start_user_session(sid.to_owned(), session, sessions) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 164 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 165 | .map_err(|error| { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 166 | tracing::warn!("starting restored session failed: {error}"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 167 | RestoreError::Unavailable(error) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 168 | }), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 169 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 170 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 171 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 172 | /// Restore just far enough to tell whether `sid`'s record is a soft |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 173 | /// logout, without disturbing anything else. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 174 | async fn soft_logged_out(&self, sid: &str) -> Option<SoftLogout> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 175 | match Session::restore(self.session_store(sid)).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 176 | Ok(Some(Restore::SoftLoggedOut(soft))) => Some(soft), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 177 | _ => None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 178 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 179 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 180 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 181 | /// Password login: creates a fresh opaque sid and starts the engine, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 182 | /// which saves the record itself. The password never leaves this call. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 183 | /// When the cookie sid names the same user's soft-logged-out device, logs |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 184 | /// back in as that device against the same sid instead, keeping its keys. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 185 | pub async fn login( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 186 | &self, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 187 | cookie_sid: Option<&str>, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 188 | login: PasswordLogin, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 189 | username: &str, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 190 | password: String, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 191 | ) -> Result<Arc<UserSession>, CoreError> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 192 | if let Some(sid) = cookie_sid.filter(|sid| valid_sid(sid)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 193 | && let Some(soft) = self.soft_logged_out(sid).await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 194 | { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 195 | if username_matches(username, &soft.user_id) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 196 | let sessions = self.session_store(sid); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 197 | let session = login_with( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 198 | soft.password_login(password.clone()), |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 199 | &password, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 200 | sessions.clone(), |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 201 | ) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 202 | .await?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 203 | let _init = self.inner.init_lock.lock().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 204 | return self |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 205 | .start_user_session(sid.to_owned(), session, sessions) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 206 | .await |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 207 | .map_err(|error| CoreError::new(Failure::Upstream, error)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 208 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 209 | self.apply_retention(&self.session_store(sid), Retain::Nothing) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 210 | .await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 211 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 212 | let sid = new_sid(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 213 | let sessions = self.session_store(&sid); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 214 | let session = login_with(login, &password, sessions.clone()).await?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 215 | let _init = self.inner.init_lock.lock().await; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 216 | self.start_user_session(sid, session, sessions) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 217 | .await |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 218 | .map_err(|error| CoreError::new(Failure::Upstream, error)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 219 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 220 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 221 | async fn start_user_session( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 222 | &self, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 223 | sid: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 224 | session: Session, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 225 | sessions: Arc<dyn SessionStore>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 226 | ) -> Result<Arc<UserSession>, String> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 227 | let (engine, handle, events) = session |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 228 | .build(EngineConfig::default()) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 229 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 230 | .map_err(|error| format!("starting sync engine: {error}"))?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 231 | let engine_task = tokio::spawn(engine.run()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 232 | let (mirror, mirror_handle) = Mirror::new(handle.clone(), events); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 233 | let mirror_task = tokio::spawn(mirror.run()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 234 | let Some(user_id) = wait_authenticated(&mirror_handle).await else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 235 | engine_task.abort(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 236 | let _ = engine_task.await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 237 | mirror_task.abort(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 238 | let _ = mirror_task.await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 239 | return Err("sync engine stopped before authenticating".to_owned()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 240 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 241 | let user = Arc::new(UserSession { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 242 | sid: sid.clone(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 243 | user_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 244 | handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 245 | mirror: mirror_handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 246 | sessions, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 247 | engine: Mutex::new(Some(engine_task)), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 248 | mirror_task: Mutex::new(Some(mirror_task)), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 249 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 250 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 251 | let mut sessions_map = self.inner.sessions.write().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 252 | sessions_map.insert(sid, user.clone()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 253 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 254 | let supervisor_state = self.clone(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 255 | let supervisor_user = user.clone(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 256 | tokio::spawn(async move { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 257 | supervise(supervisor_state, supervisor_user).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 258 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 259 | Ok(user) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 260 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 261 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 262 | /// Logout: best-effort server-side logout, then drop all local state and |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 263 | /// delete persisted secrets. Never holds the map lock across the network |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 264 | /// call. Forces `Retain::Nothing` regardless of what the engine's own |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 265 | /// termination said, so an explicit logout always ends in a full discard |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 266 | /// even if it raced the supervisor over a server-initiated soft logout. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 267 | pub async fn logout(&self, sid: &str) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 268 | let user = { self.inner.sessions.read().await.get(sid).cloned() }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 269 | if let Some(user) = user { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 270 | // answers once the server has let go of the device, and the engine stops itself |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 271 | let _ = timeout(Duration::from_secs(5), user.handle.logout()).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 272 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 273 | self.remove_session(sid, Retain::Nothing).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 274 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 275 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 276 | /// Forget an in-memory session, abort its engine and mirror tasks, and |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 277 | /// apply the given retention to its persisted secrets. Called both for |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 278 | /// explicit logout (always `Retain::Nothing`) and from the supervisor |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 279 | /// once an engine's `Termination` says what its record and store are |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 280 | /// still worth. Retention always runs, even if the session was already |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 281 | /// gone from the map, so a racing logout's `Nothing` still lands. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 282 | pub async fn remove_session(&self, sid: &str, retain: Retain) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 283 | // A restore must not reopen the store between removing the map entry |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 284 | // and deleting its persisted credentials. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 285 | let _init = self.inner.init_lock.lock().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 286 | let user = self.inner.sessions.write().await.remove(sid); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 287 | let sessions = match &user { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 288 | Some(user) => user.sessions.clone(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 289 | None => self.session_store(sid), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 290 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 291 | if let Some(user) = &user { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 292 | if let Some(task) = user.engine.lock().await.take() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 293 | task.abort(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 294 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 295 | if let Some(task) = user.mirror_task.lock().await.take() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 296 | task.abort(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 297 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 298 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 299 | self.apply_retention(&sessions, retain).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 300 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 301 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 302 | /// Graceful shutdown: ask every engine to stop, keep persisted records so |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 303 | /// the next boot restores them. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 304 | pub async fn shutdown_gracefully(&self) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 305 | let users: Vec<Arc<UserSession>> = |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 306 | self.inner.sessions.read().await.values().cloned().collect(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 307 | for user in &users { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 308 | // the supervisor keeps waiting, so a shutdown can answer once `Stopped` is out |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 309 | let handle = user.handle.clone(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 310 | tokio::spawn(async move { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 311 | let _ = timeout(Duration::from_secs(5), handle.shutdown()).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 312 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 313 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 314 | // Give engines a moment to emit `Stopped`; the supervisor then drops |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 315 | // each in-memory session while leaving persisted records behind. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 316 | timeout(Duration::from_secs(5), async { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 317 | loop { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 318 | if self.inner.sessions.read().await.is_empty() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 319 | break; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 320 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 321 | tokio::time::sleep(Duration::from_millis(50)).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 322 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 323 | }) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 324 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 325 | .ok(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 326 | for user in &users { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 327 | if let Some(task) = user.engine.lock().await.take() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 328 | task.abort(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 329 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 330 | if let Some(task) = user.mirror_task.lock().await.take() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 331 | task.abort(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 332 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 333 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 334 | self.inner.sessions.write().await.clear(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 335 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 336 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 337 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 338 | fn restore_error(error: AccountError) -> RestoreError { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 339 | match error { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 340 | AccountError::StoreInUse => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 341 | RestoreError::Unavailable("another connection is using this session".to_owned()) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 342 | } |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 343 | AccountError::Operation { |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 344 | kind: ErrorKind::Network | ErrorKind::Server | ErrorKind::RateLimited, |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 345 | .. |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 346 | } => RestoreError::Unavailable(format!("restoring persisted session failed: {error}")), |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 347 | error @ (AccountError::Operation { .. } |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 348 | | AccountError::MissingSession |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 349 | | AccountError::UnsupportedAuthentication |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 350 | | AccountError::OAuthCallbackAlreadySubmitted |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 351 | | AccountError::NoPendingOAuth |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 352 | | AccountError::InvalidHomeserver(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 353 | | AccountError::AlreadyLoggedIn(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 354 | | AccountError::StoreDeletePending |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 355 | | AccountError::Unsaved { .. } |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 356 | | AccountError::Engine(_)) => { |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 357 | RestoreError::Stale(format!("restoring persisted session failed: {error}")) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 358 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 359 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 360 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 361 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 362 | async fn login_with( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 363 | login: PasswordLogin, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 364 | password: &str, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 365 | sessions: Arc<dyn SessionStore>, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 366 | ) -> Result<Session, CoreError> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 367 | match Session::login_password(login, sessions).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 368 | Ok(session) => Ok(session), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 369 | Err(AccountError::Unsaved { session, .. }) => session |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 370 | .save() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 371 | .await |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 372 | .map_err(|_| CoreError::new(Failure::Upstream, "could not save the session")), |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 373 | Err(error) => Err(login_error(&error, password)), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 374 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 375 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 376 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 377 | fn username_matches(submitted: &str, user_id: &ruma::OwnedUserId) -> bool { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 378 | let submitted = submitted.trim(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 379 | submitted == user_id.as_str() || submitted == user_id.localpart() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 380 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 381 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 382 | /// Wait for the mirror to learn the user id. Fails once the subscription |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 383 | /// ends or the session leaves `Active` before that happens. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 384 | async fn wait_authenticated(mirror: &MirrorHandle) -> Option<String> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 385 | timeout(AUTH_TIMEOUT, async { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 386 | let mut updates = mirror.subscribe(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 387 | loop { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 388 | let update = updates.next().await?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 389 | match update.change { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 390 | StateChange::State(state) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 391 | if let Some(user_id) = &state.user_id { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 392 | return Some(user_id.to_string()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 393 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 394 | if state.session != SessionStatus::Active { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 395 | return None; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 396 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 397 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 398 | StateChange::Authenticated(user_id) => return Some(user_id.to_string()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 399 | StateChange::Session(status) if status != SessionStatus::Active => return None, |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 400 | StateChange::Session(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 401 | | StateChange::SessionSaveFailed(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 402 | | StateChange::SessionSaved |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 403 | | StateChange::Connection(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 404 | | StateChange::Encryption(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 405 | | StateChange::Verification(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 406 | | StateChange::VerificationTarget(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 407 | | StateChange::NotificationSettings |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 408 | | StateChange::Rooms(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 409 | | StateChange::ImagePacks(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 410 | | StateChange::Timeline { .. } |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 411 | | StateChange::History { .. } |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 412 | | StateChange::Typing(_) |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 413 | | StateChange::Upload { .. } |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 414 | | StateChange::UploadFinished { .. } |
35a820422568 | atagen | 2026-10-24 10:15 UTC | 415 | | StateChange::Failed(_) => {} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 416 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 417 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 418 | }) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 419 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 420 | .ok() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 421 | .flatten() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 422 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 423 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 424 | /// Awaits a session's engine, then its mirror, removes it from the map, and |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 425 | /// applies the retention its `Termination` earns. Taking the engine |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 426 | /// `JoinHandle` is the single point two racing teardowns (this task and an |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 427 | /// explicit logout) arbitrate over: whichever finds it empty does nothing |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 428 | /// further, so the engine and mirror are never aborted twice. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 429 | async fn supervise(state: AppState, user: Arc<UserSession>) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 430 | let Some(engine_task) = user.engine.lock().await.take() else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 431 | return; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 432 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 433 | let retain = match engine_task.await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 434 | Ok(Ok(termination)) => termination.retain(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 435 | Ok(Err(error)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 436 | tracing::warn!(%error, "sync engine ended with an error; keeping its session"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 437 | Retain::RecordAndStore |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 438 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 439 | Err(error) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 440 | tracing::warn!(%error, "sync engine task did not finish cleanly; keeping its session"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 441 | Retain::RecordAndStore |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 442 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 443 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 444 | if let Some(mirror_task) = user.mirror_task.lock().await.take() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 445 | let _ = mirror_task.await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 446 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 447 | state.remove_session(&user.sid, retain).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 448 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 449 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 450 | fn new_sid() -> String { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 451 | let mut bytes = [0u8; 32]; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 452 | OsRng.fill_bytes(&mut bytes); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 453 | hex::encode(bytes) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 454 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 455 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 456 | /// Load the 32-byte master key, creating it with owner-only permissions when |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 457 | /// absent. Secrets must never be world-readable. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 458 | pub fn load_or_create_master_key(data_dir: &Path) -> Result<[u8; 32], String> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 459 | create_private_dir(data_dir).map_err(|error| format!("creating data dir: {error}"))?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 460 | restrict_dir(data_dir); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 461 | let path = data_dir.join("master.key"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 462 | if path.exists() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 463 | let bytes = std::fs::read(&path).map_err(|error| format!("reading master key: {error}"))?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 464 | restrict_file(&path); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 465 | if bytes.len() != 32 { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 466 | return Err("master key has an unexpected length".to_owned()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 467 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 468 | let mut key = [0u8; 32]; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 469 | key.copy_from_slice(&bytes); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 470 | return Ok(key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 471 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 472 | let mut key = [0u8; 32]; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 473 | OsRng.fill_bytes(&mut key); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 474 | let mut file = |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 475 | create_private_file(&path).map_err(|error| format!("creating master key: {error}"))?; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 476 | if let Err(error) = std::io::Write::write_all(&mut file, &key).and_then(|_| file.sync_all()) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 477 | let _ = std::fs::remove_file(&path); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 478 | return Err(format!("writing master key: {error}")); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 479 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 480 | Ok(key) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 481 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 482 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 483 | fn create_private_dir(path: &Path) -> std::io::Result<()> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 484 | let mut builder = std::fs::DirBuilder::new(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 485 | builder.recursive(true); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 486 | #[cfg(unix)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 487 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 488 | use std::os::unix::fs::DirBuilderExt; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 489 | builder.mode(0o700); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 490 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 491 | builder.create(path) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 492 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 493 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 494 | fn create_private_file(path: &Path) -> std::io::Result<std::fs::File> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 495 | let mut options = std::fs::OpenOptions::new(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 496 | options.write(true).create_new(true); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 497 | #[cfg(unix)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 498 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 499 | use std::os::unix::fs::OpenOptionsExt; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 500 | options.mode(0o600); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 501 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 502 | options.open(path) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 503 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 504 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 505 | pub fn ensure_dirs(data_dir: &Path) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 506 | for dir in [ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 507 | data_dir.to_path_buf(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 508 | data_dir.join("sessions"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 509 | data_dir.join("stores"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 510 | ] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 511 | let _ = create_private_dir(&dir); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 512 | restrict_dir(&dir); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 513 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 514 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 515 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 516 | #[cfg(unix)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 517 | fn restrict_dir(path: &Path) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 518 | use std::os::unix::fs::PermissionsExt; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 519 | let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 520 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 521 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 522 | #[cfg(not(unix))] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 523 | fn restrict_dir(_path: &Path) {} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 524 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 525 | #[cfg(unix)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 526 | fn restrict_file(path: &Path) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 527 | use std::os::unix::fs::PermissionsExt; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 528 | let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 529 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 530 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 531 | #[cfg(not(unix))] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 532 | fn restrict_file(_path: &Path) {} |