62fe32759529 | atagen | 2026-09-26 08:50 UTC | 1 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 2 | use std::path::PathBuf; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 3 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 4 | use std::sync::atomic::{AtomicBool, Ordering}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 5 | use std::{collections::HashMap, sync::Arc}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | use matrix_sdk::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 8 | Client, ClientBuilder, SendOutsideWasm, SessionChange, SessionMeta, SyncOutsideWasm, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 9 | ThreadingSupport, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | authentication::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | AuthSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | matrix::MatrixSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | oauth::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | ClientId, ClientRegistrationData, CsrfToken, OAuthSession, UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | registration::{ApplicationType, ClientMetadata, Localized, OAuthGrantType}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | }, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 18 | cross_process_lock::CrossProcessLockConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | encryption::{BackupDownloadStrategy, EncryptionSettings}, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 20 | ruma::{OwnedDeviceId, OwnedUserId, serde::Raw}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | use serde::{Deserialize, Serialize}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | use thiserror::Error; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 24 | use tokio::sync::{broadcast, mpsc}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | use url::Url; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 27 | use crate::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 28 | ClientHandle, Engine, EngineConfig, EngineError, ErrorKind, EventStream, SessionStore, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 29 | engine::Persistence, error::SourceError, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 30 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 32 | /// a logged in client and the `SessionStore` its record lives in. the engine it builds |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 33 | /// saves every token rotation there, unless told `without_persistence` |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | pub struct Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | client: Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 36 | tracking: Tracking, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 37 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 38 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 39 | /// taken as soon as the client exists, since the sdk can refresh before there's an engine |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 40 | struct Tracking { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 41 | session_changes: broadcast::Receiver<SessionChange>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 42 | persistence: Option<Persistence>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 43 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 44 | inline: Arc<AtomicBool>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | pub struct PasswordLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | pub username: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | pub password: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | pub device_display_name: Option<String>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 53 | /// set after a soft logout so the store's keys still belong to us |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 54 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | impl PasswordLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | username: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | password: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | username: username.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | password: password.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | device_display_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | device_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | pub fn with_device_display_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | self.device_display_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | pub struct OAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | pub redirect_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | pub client_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | pub client_name: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | pub client_id: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | pub application_type: OAuthApplicationType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | impl OAuthLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | redirect_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | client_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | application_type: OAuthApplicationType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | redirect_uri: redirect_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | client_uri: client_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | client_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | client_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | device_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | application_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | pub fn with_client_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | self.client_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | /// a statically registered client id, skipping dynamic registration |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | self.client_id = Some(client_id.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | pub enum OAuthApplicationType { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | pub struct PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | client: Client, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 145 | sessions: Arc<dyn SessionStore>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 146 | tracking: Option<Tracking>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | authorization_url: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | state: Option<CsrfToken>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | impl PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | pub fn authorization_url(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | &self.authorization_url |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | pub fn registered_client_id(&self) -> String { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | self.client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | .client_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | .expect("OAuth client was registered before authorization") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | .as_str() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | .to_owned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | pub const fn is_pending(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | self.state.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | pub async fn finish(&mut self, callback_url: &str) -> Result<Session, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | let callback_url = Url::parse(callback_url) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | .map_err(|error| account_error("parsing the OAuth callback URL", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | let state = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | .state |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | let oauth = self.client.oauth(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | if let Err(error) = oauth.finish_login(callback_url.into()).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | oauth.abort_login(&state).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | return Err(account_error("OAuth login", error)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 184 | let tracking = self |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 185 | .tracking |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 186 | .take() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 187 | .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 188 | Session::saved(self.client.clone(), &*self.sessions, tracking).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | pub async fn abort(mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | if let Some(state) = self.state.take() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | self.client.oauth().abort_login(&state).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | pub struct StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 199 | location: StoreLocation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | passphrase: String, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 201 | lock_holder: Option<String>, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 202 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 203 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 204 | enum StoreLocation { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 205 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 206 | Sqlite(PathBuf), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 207 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 208 | IndexedDb(String), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | impl StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 212 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 213 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 214 | pub fn sqlite(path: impl Into<PathBuf>, passphrase: impl Into<String>) -> Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 215 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 216 | location: StoreLocation::Sqlite(path.into()), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 217 | passphrase: passphrase.into(), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 218 | lock_holder: None, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 219 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 220 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 221 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 222 | #[cfg(feature = "indexeddb")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 224 | pub fn indexeddb(name: impl Into<String>, passphrase: impl Into<String>) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 226 | location: StoreLocation::IndexedDb(name.into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | passphrase: passphrase.into(), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 228 | lock_holder: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 232 | /// the sdk names every client `main`, so two tabs on one store would both think they hold its lock |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 234 | pub fn with_lock_holder(mut self, holder: impl Into<String>) -> Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 235 | self.lock_holder = Some(holder.into()); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 236 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 238 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 239 | /// delete the store's databases, once nothing has them open |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 240 | pub async fn delete(&self) -> Result<(), AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 241 | match &self.location { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 242 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 243 | StoreLocation::Sqlite(path) => crate::session_store::delete_sqlite_store(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 244 | .map_err(|error| account_error("deleting the Matrix store", error)), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 245 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 246 | StoreLocation::IndexedDb(name) => crate::session_store::delete_indexeddb_store(name) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 247 | .await |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 248 | .map_err(|error| error.into_account_error("deleting the Matrix store")), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 249 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 250 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 251 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 252 | pub(crate) fn passphrase(&self) -> &str { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 253 | &self.passphrase |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 254 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 255 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 256 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 257 | pub(crate) fn indexeddb_name(&self) -> &str { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 258 | let StoreLocation::IndexedDb(name) = &self.location; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 259 | name |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 260 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 263 | /// Serializable authentication secrets. A `SessionStore` keeps it, and the engine |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 264 | /// replaces it there on every token rotation. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | pub struct SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | authentication: AuthenticationRecord, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | #[serde(tag = "type", content = "session", rename_all = "snake_case")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | enum AuthenticationRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | Matrix(MatrixSession), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | OAuth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | client_id: ClientId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | user: UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | }, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 279 | SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 280 | meta: SessionMeta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 281 | client_id: Option<ClientId>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 282 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | impl SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | pub fn homeserver(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | &self.homeserver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 290 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 291 | pub(crate) fn soft_logged_out(self) -> Self { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 292 | let authentication = match self.authentication { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 293 | AuthenticationRecord::Matrix(session) => AuthenticationRecord::SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 294 | meta: session.meta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 295 | client_id: None, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 296 | }, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 297 | AuthenticationRecord::OAuth { client_id, user } => { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 298 | AuthenticationRecord::SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 299 | meta: user.meta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 300 | client_id: Some(client_id), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 301 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 302 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 303 | soft @ AuthenticationRecord::SoftLoggedOut { .. } => soft, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 304 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 305 | Self { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 306 | homeserver: self.homeserver, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 307 | authentication, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 308 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 309 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 310 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 311 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 312 | pub enum Restore { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 313 | Resumed(Session), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 314 | SoftLoggedOut(SoftLogout), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 315 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 316 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 317 | /// who to log in again as. the store keeps this device's e2ee keys, so a login |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 318 | /// as any other device would orphan them |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 319 | #[derive(Clone, Debug)] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 320 | #[non_exhaustive] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 321 | pub struct SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 322 | pub homeserver: String, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 323 | pub user_id: OwnedUserId, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 324 | pub device_id: OwnedDeviceId, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 325 | /// set when the login was OAuth, for `OAuthLogin::with_client_id` |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 326 | pub client_id: Option<String>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 327 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 328 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 329 | impl SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 330 | #[must_use] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 331 | pub fn password_login(&self, password: impl Into<String>) -> PasswordLogin { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 332 | PasswordLogin::new(self.homeserver.clone(), self.user_id.as_str(), password) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 333 | .with_device_id(self.device_id.clone()) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 334 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | pub enum AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | #[error("{context} failed")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | context: &'static str, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | #[source] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 343 | source: Box<SourceError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | #[error("the homeserver returned no authenticated session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | MissingSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | #[error("the SDK returned an unsupported authentication session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | UnsupportedAuthentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | #[error("the OAuth callback was already submitted")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | OAuthCallbackAlreadySubmitted, |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 351 | /// an IndexedDB delete is waiting on the store's other connections. it goes through |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 352 | /// once they close, and opening the store again waits behind it |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 353 | #[error("the store is still open elsewhere, it will be deleted once that closes")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 354 | StoreDeletePending, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | #[error(transparent)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | Engine(#[from] EngineError), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | impl Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | pub async fn login_password( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | login: PasswordLogin, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 362 | sessions: Arc<dyn SessionStore>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | ) -> Result<Self, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 364 | let client = matrix_client_builder(sessions.store()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | .map_err(|error| account_error("building the Matrix client", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 369 | let tracking = Tracking::start(&client, sessions.clone())?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | let mut request = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | .matrix_auth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | .login_username(login.username.trim(), &login.password) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | .request_refresh_token(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | if let Some(name) = login.device_display_name.as_deref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | request = request.initial_device_display_name(name); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 377 | if let Some(device_id) = login.device_id.as_deref() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 378 | request = request.device_id(device_id.as_str()); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 379 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | .send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | .map_err(|error| account_error("password login", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 384 | Self::saved(client, &*sessions, tracking).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | pub async fn begin_oauth( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | login: OAuthLogin, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 389 | sessions: Arc<dyn SessionStore>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | ) -> Result<PendingOAuthLogin, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | let redirect_uri = Url::parse(login.redirect_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | .map_err(|error| account_error("parsing the OAuth redirect URI", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | let client_uri = Url::parse(login.client_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | .map_err(|error| account_error("parsing the OAuth client URI", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 395 | let client = matrix_client_builder(sessions.store()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | .map_err(|error| account_error("building the Matrix client", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 400 | let tracking = Tracking::start(&client, sessions.clone())?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | let application_type = match login.application_type { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | OAuthApplicationType::Native => ApplicationType::Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | OAuthApplicationType::Web => ApplicationType::Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | let mut metadata = ClientMetadata::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | application_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | vec![OAuthGrantType::AuthorizationCode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 408 | redirect_uris: vec![redirect_uri.clone()], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 409 | }], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | Localized::new(client_uri, []), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 411 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 412 | metadata.client_name = login.client_name.map(|name| Localized::new(name, [])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 413 | let metadata = Raw::new(&metadata) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 414 | .map_err(|error| account_error("serializing OAuth client metadata", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 415 | let mut registration = ClientRegistrationData::new(metadata); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | if let Some(client_id) = login.client_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | registration.static_registrations = Some(HashMap::from([( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | client.homeserver(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | ClientId::new(client_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | )])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | let authorization = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | .login(redirect_uri, login.device_id, Some(registration), None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 425 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 426 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 427 | .map_err(|error| account_error("starting OAuth login", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 428 | Ok(PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 429 | client, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 430 | sessions, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 431 | tracking: Some(tracking), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 432 | authorization_url: authorization.url.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 433 | state: Some(authorization.state), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 437 | /// Restore the record `sessions` holds, or `None` when it holds none. |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 438 | /// The caller must also handle rejection and explicit logout. |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 439 | pub async fn restore(sessions: Arc<dyn SessionStore>) -> Result<Option<Restore>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 440 | let Some(record) = sessions.load().await? else { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 441 | return Ok(None); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 442 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 443 | let authentication = match record.authentication { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | AuthenticationRecord::Matrix(session) => AuthSession::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | AuthenticationRecord::OAuth { client_id, user } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | OAuthSession { client_id, user }.into() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 448 | AuthenticationRecord::SoftLoggedOut { meta, client_id } => { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 449 | return Ok(Some(Restore::SoftLoggedOut(SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 450 | homeserver: record.homeserver, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 451 | user_id: meta.user_id, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 452 | device_id: meta.device_id, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 453 | client_id: client_id.map(|client_id| client_id.as_str().to_owned()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 454 | }))); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 455 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 456 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 457 | let client = matrix_client_builder(sessions.store()) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 458 | .homeserver_url(&record.homeserver) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 459 | .build() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 460 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 461 | .map_err(|error| account_error("reopening the Matrix store", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 462 | let tracking = Tracking::start(&client, sessions)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | .restore_session(authentication) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | .map_err(|error| account_error("restoring the authenticated session", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 467 | Ok(Some(Restore::Resumed(Self { client, tracking }))) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 468 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 469 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 470 | /// leave the record to the host, which then saves `ClientHandle::session_record()` |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 471 | /// on every `SessionTokensChanged` |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 472 | #[must_use] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 473 | pub fn without_persistence(mut self) -> Self { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 474 | self.tracking.persistence = None; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 475 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 476 | self.tracking.inline.store(false, Ordering::Release); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 477 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | pub async fn build( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | config: EngineConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | ) -> Result<(Engine, ClientHandle, EventStream), AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 484 | Engine::with_persistence( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 485 | self.client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 486 | config, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 487 | self.tracking.session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 488 | self.tracking.persistence, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 489 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 490 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 491 | .map_err(AccountError::from) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 494 | async fn saved( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 495 | client: Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 496 | sessions: &dyn SessionStore, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 497 | tracking: Tracking, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 498 | ) -> Result<Self, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 499 | sessions.save(&session_record(&client)?).await?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 500 | Ok(Self { client, tracking }) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 501 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 502 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 503 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 504 | impl Tracking { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 505 | fn start(client: &Client, sessions: Arc<dyn SessionStore>) -> Result<Self, AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 506 | let session_changes = client.subscribe_to_session_changes(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 507 | #[cfg_attr(target_family = "wasm", allow(unused_variables))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 508 | let (results, inline_results) = mpsc::unbounded_channel(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 509 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 510 | let inline = save_inline(client, sessions.clone(), results)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 511 | Ok(Self { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 512 | session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 513 | persistence: Some(Persistence { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 514 | save: Box::new(move |client| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 515 | let sessions = sessions.clone(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 516 | Box::pin(async move { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 517 | let record = session_record(&client).map_err(save_failed)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 518 | sessions.save(&record).await.map_err(save_failed) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 519 | }) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 520 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 521 | inline: inline_results, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 522 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 523 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 524 | inline, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 525 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 526 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 527 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 528 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 529 | /// the sdk calls this inside its refresh, after it has the new tokens and before it uses |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 530 | /// them, so a store that writes there and then can't lose a refresh token to a crash |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 531 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 532 | fn save_inline( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 533 | client: &Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 534 | sessions: Arc<dyn SessionStore>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 535 | results: mpsc::UnboundedSender<Result<(), crate::Error>>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 536 | ) -> Result<Arc<AtomicBool>, AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 537 | let enabled = Arc::new(AtomicBool::new(true)); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 538 | let saving = enabled.clone(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 539 | client |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 540 | .set_session_callbacks( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 541 | // only asked for with the cross process refresh lock, which we never enable |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 542 | Box::new(|client| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 543 | client |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 544 | .session_tokens() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 545 | .ok_or_else(|| AccountError::MissingSession.into()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 546 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 547 | Box::new(move |client| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 548 | if !saving.load(Ordering::Acquire) { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 549 | return Ok(()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 550 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 551 | let record = session_record(&client)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 552 | if let Some(result) = sessions.save_blocking(&record) { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 553 | let _ = results.send(result.map_err(save_failed)); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 554 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 555 | Ok(()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 556 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 557 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 558 | .map_err(|error| account_error("registering the session callbacks", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 559 | Ok(enabled) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 560 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 561 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 562 | fn save_failed(error: AccountError) -> crate::Error { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 563 | let message = format!("saving the session record failed, {error}"); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 564 | crate::Error::with_source(ErrorKind::Store, message, error) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 565 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 566 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | pub(crate) fn session_record(client: &Client) -> Result<SessionRecord, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | let authentication = match client.session().ok_or(AccountError::MissingSession)? { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | AuthSession::Matrix(session) => AuthenticationRecord::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 570 | AuthSession::OAuth(session) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 571 | let OAuthSession { client_id, user } = *session; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | AuthenticationRecord::OAuth { client_id, user } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 573 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 574 | _ => return Err(AccountError::UnsupportedAuthentication), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 576 | Ok(SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 577 | homeserver: client.homeserver().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | authentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 580 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | fn encryption_settings() -> EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 583 | EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | auto_enable_cross_signing: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | backup_download_strategy: BackupDownloadStrategy::OneShot, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 586 | auto_enable_backups: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 587 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 589 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 590 | fn matrix_client_builder(store: &StoreConfig) -> ClientBuilder { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 591 | let mut builder = Client::builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | .with_encryption_settings(encryption_settings()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | .with_threading_support(ThreadingSupport::Enabled { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | with_subscriptions: false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | }) |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 596 | .handle_refresh_tokens(); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 597 | if let Some(holder) = &store.lock_holder { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 598 | builder = builder.cross_process_store_config(CrossProcessLockConfig::multi_process(holder)); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 599 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 600 | match &store.location { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 601 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 602 | StoreLocation::Sqlite(path) => builder.sqlite_store(path, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 603 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 604 | StoreLocation::IndexedDb(name) => builder.indexeddb_store(name, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 605 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 606 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 607 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 608 | pub(crate) fn account_error( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | context: &'static str, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 610 | source: impl std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 611 | ) -> AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 612 | AccountError::Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | context, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | source: Box::new(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 616 | } |