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