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