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