atagen

macltok

public
Star0
Watch0
5d2974de5826bd24a1e122358050983ec5eb0644
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git

Blame: src/account.rs

62fe32759529atagen2026-09-26 08:50 UTC1
#[cfg(feature = "sqlite")]
62fe32759529atagen2026-09-26 08:50 UTC2
use std::path::PathBuf;
f12377bce9b9atagen2026-09-26 12:32 UTC3
#[cfg(not(target_family = "wasm"))]
f12377bce9b9atagen2026-09-26 12:32 UTC4
use std::sync::atomic::{AtomicBool, Ordering};
713205a95af2atagen2026-09-26 09:24 UTC5
use std::{collections::HashMap, sync::Arc};
96fdf90aacd6atagen2026-09-08 08:24 UTC6
96fdf90aacd6atagen2026-09-08 08:24 UTC7
use matrix_sdk::{
f12377bce9b9atagen2026-09-26 12:32 UTC8
    Client, ClientBuilder, SendOutsideWasm, SessionChange, SessionMeta, SyncOutsideWasm,
f12377bce9b9atagen2026-09-26 12:32 UTC9
    ThreadingSupport,
96fdf90aacd6atagen2026-09-08 08:24 UTC10
    authentication::{
96fdf90aacd6atagen2026-09-08 08:24 UTC11
        AuthSession,
96fdf90aacd6atagen2026-09-08 08:24 UTC12
        matrix::MatrixSession,
96fdf90aacd6atagen2026-09-08 08:24 UTC13
        oauth::{
96fdf90aacd6atagen2026-09-08 08:24 UTC14
            ClientId, ClientRegistrationData, CsrfToken, OAuthSession, UserSession,
96fdf90aacd6atagen2026-09-08 08:24 UTC15
            registration::{ApplicationType, ClientMetadata, Localized, OAuthGrantType},
96fdf90aacd6atagen2026-09-08 08:24 UTC16
        },
96fdf90aacd6atagen2026-09-08 08:24 UTC17
    },
62fe32759529atagen2026-09-26 08:50 UTC18
    cross_process_lock::CrossProcessLockConfig,
96fdf90aacd6atagen2026-09-08 08:24 UTC19
    encryption::{BackupDownloadStrategy, EncryptionSettings},
6f1d6591c10datagen2026-09-26 09:52 UTC20
    ruma::{OwnedDeviceId, OwnedUserId, serde::Raw},
96fdf90aacd6atagen2026-09-08 08:24 UTC21
};
96fdf90aacd6atagen2026-09-08 08:24 UTC22
use serde::{Deserialize, Serialize};
96fdf90aacd6atagen2026-09-08 08:24 UTC23
use thiserror::Error;
f12377bce9b9atagen2026-09-26 12:32 UTC24
use tokio::sync::{broadcast, mpsc};
96fdf90aacd6atagen2026-09-08 08:24 UTC25
use url::Url;
96fdf90aacd6atagen2026-09-08 08:24 UTC26
713205a95af2atagen2026-09-26 09:24 UTC27
use crate::{
9a25b6ce66f4atagen2026-10-08 11:15 UTC28
    ClientHandle, Engine, EngineConfig, EngineError, ErrorKind, EventStream, ServerError,
9a25b6ce66f4atagen2026-10-08 11:15 UTC29
    SessionStore,
a250c7fa43fbatagen2026-09-26 13:35 UTC30
    engine::{Persistence, StoreLock},
a250c7fa43fbatagen2026-09-26 13:35 UTC31
    error::SourceError,
713205a95af2atagen2026-09-26 09:24 UTC32
};
96fdf90aacd6atagen2026-09-08 08:24 UTC33
f12377bce9b9atagen2026-09-26 12:32 UTC34
/// a logged in client and the `SessionStore` its record lives in. the engine it builds
f12377bce9b9atagen2026-09-26 12:32 UTC35
/// saves every token rotation there, unless told `without_persistence`
96fdf90aacd6atagen2026-09-08 08:24 UTC36
pub struct Session {
96fdf90aacd6atagen2026-09-08 08:24 UTC37
    client: Client,
f12377bce9b9atagen2026-09-26 12:32 UTC38
    tracking: Tracking,
a250c7fa43fbatagen2026-09-26 13:35 UTC39
    store_lock: StoreLock,
f12377bce9b9atagen2026-09-26 12:32 UTC40
}
f12377bce9b9atagen2026-09-26 12:32 UTC41
f12377bce9b9atagen2026-09-26 12:32 UTC42
/// taken as soon as the client exists, since the sdk can refresh before there's an engine
f12377bce9b9atagen2026-09-26 12:32 UTC43
struct Tracking {
f12377bce9b9atagen2026-09-26 12:32 UTC44
    session_changes: broadcast::Receiver<SessionChange>,
f12377bce9b9atagen2026-09-26 12:32 UTC45
    persistence: Option<Persistence>,
f12377bce9b9atagen2026-09-26 12:32 UTC46
    #[cfg(not(target_family = "wasm"))]
f12377bce9b9atagen2026-09-26 12:32 UTC47
    inline: Arc<AtomicBool>,
96fdf90aacd6atagen2026-09-08 08:24 UTC48
}
96fdf90aacd6atagen2026-09-08 08:24 UTC49
31ce2453c82fatagen2026-09-24 13:07 UTC50
#[non_exhaustive]
96fdf90aacd6atagen2026-09-08 08:24 UTC51
pub struct PasswordLogin {
96fdf90aacd6atagen2026-09-08 08:24 UTC52
    pub homeserver: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC53
    pub username: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC54
    pub password: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC55
    pub device_display_name: Option<String>,
fa9114fcf75fatagen2026-09-24 12:51 UTC56
    /// set after a soft logout so the store's keys still belong to us
fa9114fcf75fatagen2026-09-24 12:51 UTC57
    pub device_id: Option<OwnedDeviceId>,
9c05b1dd327aatagen2026-10-08 11:27 UTC58
    /// let plain http through, for loopback hosts only
9c05b1dd327aatagen2026-10-08 11:27 UTC59
    pub allow_insecure_loopback: bool,
96fdf90aacd6atagen2026-09-08 08:24 UTC60
}
96fdf90aacd6atagen2026-09-08 08:24 UTC61
31ce2453c82fatagen2026-09-24 13:07 UTC62
impl PasswordLogin {
31ce2453c82fatagen2026-09-24 13:07 UTC63
    #[must_use]
31ce2453c82fatagen2026-09-24 13:07 UTC64
    pub fn new(
31ce2453c82fatagen2026-09-24 13:07 UTC65
        homeserver: impl Into<String>,
31ce2453c82fatagen2026-09-24 13:07 UTC66
        username: impl Into<String>,
31ce2453c82fatagen2026-09-24 13:07 UTC67
        password: impl Into<String>,
31ce2453c82fatagen2026-09-24 13:07 UTC68
    ) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC69
        Self {
31ce2453c82fatagen2026-09-24 13:07 UTC70
            homeserver: homeserver.into(),
31ce2453c82fatagen2026-09-24 13:07 UTC71
            username: username.into(),
31ce2453c82fatagen2026-09-24 13:07 UTC72
            password: password.into(),
31ce2453c82fatagen2026-09-24 13:07 UTC73
            device_display_name: None,
31ce2453c82fatagen2026-09-24 13:07 UTC74
            device_id: None,
9c05b1dd327aatagen2026-10-08 11:27 UTC75
            allow_insecure_loopback: false,
31ce2453c82fatagen2026-09-24 13:07 UTC76
        }
31ce2453c82fatagen2026-09-24 13:07 UTC77
    }
31ce2453c82fatagen2026-09-24 13:07 UTC78
31ce2453c82fatagen2026-09-24 13:07 UTC79
    #[must_use]
31ce2453c82fatagen2026-09-24 13:07 UTC80
    pub fn with_device_display_name(mut self, name: impl Into<String>) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC81
        self.device_display_name = Some(name.into());
31ce2453c82fatagen2026-09-24 13:07 UTC82
        self
31ce2453c82fatagen2026-09-24 13:07 UTC83
    }
31ce2453c82fatagen2026-09-24 13:07 UTC84
31ce2453c82fatagen2026-09-24 13:07 UTC85
    #[must_use]
31ce2453c82fatagen2026-09-24 13:07 UTC86
    pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC87
        self.device_id = Some(device_id);
31ce2453c82fatagen2026-09-24 13:07 UTC88
        self
31ce2453c82fatagen2026-09-24 13:07 UTC89
    }
9c05b1dd327aatagen2026-10-08 11:27 UTC90
9c05b1dd327aatagen2026-10-08 11:27 UTC91
    #[must_use]
9c05b1dd327aatagen2026-10-08 11:27 UTC92
    pub fn allow_insecure_loopback(mut self, allow: bool) -> Self {
9c05b1dd327aatagen2026-10-08 11:27 UTC93
        self.allow_insecure_loopback = allow;
9c05b1dd327aatagen2026-10-08 11:27 UTC94
        self
9c05b1dd327aatagen2026-10-08 11:27 UTC95
    }
31ce2453c82fatagen2026-09-24 13:07 UTC96
}
31ce2453c82fatagen2026-09-24 13:07 UTC97
31ce2453c82fatagen2026-09-24 13:07 UTC98
#[non_exhaustive]
96fdf90aacd6atagen2026-09-08 08:24 UTC99
pub struct OAuthLogin {
96fdf90aacd6atagen2026-09-08 08:24 UTC100
    pub homeserver: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC101
    pub redirect_uri: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC102
    pub client_uri: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC103
    pub client_name: Option<String>,
96fdf90aacd6atagen2026-09-08 08:24 UTC104
    pub client_id: Option<String>,
96fdf90aacd6atagen2026-09-08 08:24 UTC105
    pub device_id: Option<OwnedDeviceId>,
96fdf90aacd6atagen2026-09-08 08:24 UTC106
    pub application_type: OAuthApplicationType,
9c05b1dd327aatagen2026-10-08 11:27 UTC107
    /// let plain http through, for loopback hosts only
9c05b1dd327aatagen2026-10-08 11:27 UTC108
    pub allow_insecure_loopback: bool,
96fdf90aacd6atagen2026-09-08 08:24 UTC109
}
96fdf90aacd6atagen2026-09-08 08:24 UTC110
31ce2453c82fatagen2026-09-24 13:07 UTC111
impl OAuthLogin {
31ce2453c82fatagen2026-09-24 13:07 UTC112
    #[must_use]
31ce2453c82fatagen2026-09-24 13:07 UTC113
    pub fn new(
31ce2453c82fatagen2026-09-24 13:07 UTC114
        homeserver: impl Into<String>,
31ce2453c82fatagen2026-09-24 13:07 UTC115
        redirect_uri: impl Into<String>,
31ce2453c82fatagen2026-09-24 13:07 UTC116
        client_uri: impl Into<String>,
31ce2453c82fatagen2026-09-24 13:07 UTC117
        application_type: OAuthApplicationType,
31ce2453c82fatagen2026-09-24 13:07 UTC118
    ) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC119
        Self {
31ce2453c82fatagen2026-09-24 13:07 UTC120
            homeserver: homeserver.into(),
31ce2453c82fatagen2026-09-24 13:07 UTC121
            redirect_uri: redirect_uri.into(),
31ce2453c82fatagen2026-09-24 13:07 UTC122
            client_uri: client_uri.into(),
31ce2453c82fatagen2026-09-24 13:07 UTC123
            client_name: None,
31ce2453c82fatagen2026-09-24 13:07 UTC124
            client_id: None,
31ce2453c82fatagen2026-09-24 13:07 UTC125
            device_id: None,
31ce2453c82fatagen2026-09-24 13:07 UTC126
            application_type,
9c05b1dd327aatagen2026-10-08 11:27 UTC127
            allow_insecure_loopback: false,
31ce2453c82fatagen2026-09-24 13:07 UTC128
        }
31ce2453c82fatagen2026-09-24 13:07 UTC129
    }
31ce2453c82fatagen2026-09-24 13:07 UTC130
31ce2453c82fatagen2026-09-24 13:07 UTC131
    #[must_use]
31ce2453c82fatagen2026-09-24 13:07 UTC132
    pub fn with_client_name(mut self, name: impl Into<String>) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC133
        self.client_name = Some(name.into());
31ce2453c82fatagen2026-09-24 13:07 UTC134
        self
31ce2453c82fatagen2026-09-24 13:07 UTC135
    }
31ce2453c82fatagen2026-09-24 13:07 UTC136
31ce2453c82fatagen2026-09-24 13:07 UTC137
    /// a statically registered client id, skipping dynamic registration
31ce2453c82fatagen2026-09-24 13:07 UTC138
    #[must_use]
31ce2453c82fatagen2026-09-24 13:07 UTC139
    pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC140
        self.client_id = Some(client_id.into());
31ce2453c82fatagen2026-09-24 13:07 UTC141
        self
31ce2453c82fatagen2026-09-24 13:07 UTC142
    }
31ce2453c82fatagen2026-09-24 13:07 UTC143
31ce2453c82fatagen2026-09-24 13:07 UTC144
    #[must_use]
31ce2453c82fatagen2026-09-24 13:07 UTC145
    pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC146
        self.device_id = Some(device_id);
31ce2453c82fatagen2026-09-24 13:07 UTC147
        self
31ce2453c82fatagen2026-09-24 13:07 UTC148
    }
9c05b1dd327aatagen2026-10-08 11:27 UTC149
9c05b1dd327aatagen2026-10-08 11:27 UTC150
    #[must_use]
9c05b1dd327aatagen2026-10-08 11:27 UTC151
    pub fn allow_insecure_loopback(mut self, allow: bool) -> Self {
9c05b1dd327aatagen2026-10-08 11:27 UTC152
        self.allow_insecure_loopback = allow;
9c05b1dd327aatagen2026-10-08 11:27 UTC153
        self
9c05b1dd327aatagen2026-10-08 11:27 UTC154
    }
31ce2453c82fatagen2026-09-24 13:07 UTC155
}
31ce2453c82fatagen2026-09-24 13:07 UTC156
96fdf90aacd6atagen2026-09-08 08:24 UTC157
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
31ce2453c82fatagen2026-09-24 13:07 UTC158
#[non_exhaustive]
96fdf90aacd6atagen2026-09-08 08:24 UTC159
pub enum OAuthApplicationType {
96fdf90aacd6atagen2026-09-08 08:24 UTC160
    Native,
96fdf90aacd6atagen2026-09-08 08:24 UTC161
    Web,
96fdf90aacd6atagen2026-09-08 08:24 UTC162
}
96fdf90aacd6atagen2026-09-08 08:24 UTC163
96fdf90aacd6atagen2026-09-08 08:24 UTC164
pub struct PendingOAuthLogin {
96fdf90aacd6atagen2026-09-08 08:24 UTC165
    client: Client,
713205a95af2atagen2026-09-26 09:24 UTC166
    sessions: Arc<dyn SessionStore>,
a250c7fa43fbatagen2026-09-26 13:35 UTC167
    tracking: Option<(Tracking, StoreLock)>,
96fdf90aacd6atagen2026-09-08 08:24 UTC168
    authorization_url: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC169
    state: Option<CsrfToken>,
96fdf90aacd6atagen2026-09-08 08:24 UTC170
}
96fdf90aacd6atagen2026-09-08 08:24 UTC171
96fdf90aacd6atagen2026-09-08 08:24 UTC172
impl PendingOAuthLogin {
96fdf90aacd6atagen2026-09-08 08:24 UTC173
    #[must_use]
96fdf90aacd6atagen2026-09-08 08:24 UTC174
    pub fn authorization_url(&self) -> &str {
96fdf90aacd6atagen2026-09-08 08:24 UTC175
        &self.authorization_url
96fdf90aacd6atagen2026-09-08 08:24 UTC176
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC177
96fdf90aacd6atagen2026-09-08 08:24 UTC178
    #[must_use]
96fdf90aacd6atagen2026-09-08 08:24 UTC179
    pub fn registered_client_id(&self) -> String {
96fdf90aacd6atagen2026-09-08 08:24 UTC180
        self.client
96fdf90aacd6atagen2026-09-08 08:24 UTC181
            .oauth()
96fdf90aacd6atagen2026-09-08 08:24 UTC182
            .client_id()
96fdf90aacd6atagen2026-09-08 08:24 UTC183
            .expect("OAuth client was registered before authorization")
96fdf90aacd6atagen2026-09-08 08:24 UTC184
            .as_str()
96fdf90aacd6atagen2026-09-08 08:24 UTC185
            .to_owned()
96fdf90aacd6atagen2026-09-08 08:24 UTC186
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC187
96fdf90aacd6atagen2026-09-08 08:24 UTC188
    #[must_use]
96fdf90aacd6atagen2026-09-08 08:24 UTC189
    pub const fn is_pending(&self) -> bool {
96fdf90aacd6atagen2026-09-08 08:24 UTC190
        self.state.is_some()
96fdf90aacd6atagen2026-09-08 08:24 UTC191
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC192
96fdf90aacd6atagen2026-09-08 08:24 UTC193
    pub async fn finish(&mut self, callback_url: &str) -> Result<Session, AccountError> {
96fdf90aacd6atagen2026-09-08 08:24 UTC194
        let callback_url = Url::parse(callback_url)
96fdf90aacd6atagen2026-09-08 08:24 UTC195
            .map_err(|error| account_error("parsing the OAuth callback URL", error))?;
96fdf90aacd6atagen2026-09-08 08:24 UTC196
        let state = self
96fdf90aacd6atagen2026-09-08 08:24 UTC197
            .state
96fdf90aacd6atagen2026-09-08 08:24 UTC198
            .take()
96fdf90aacd6atagen2026-09-08 08:24 UTC199
            .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?;
96fdf90aacd6atagen2026-09-08 08:24 UTC200
        let oauth = self.client.oauth();
96fdf90aacd6atagen2026-09-08 08:24 UTC201
        if let Err(error) = oauth.finish_login(callback_url.into()).await {
96fdf90aacd6atagen2026-09-08 08:24 UTC202
            oauth.abort_login(&state).await;
96fdf90aacd6atagen2026-09-08 08:24 UTC203
            return Err(account_error("OAuth login", error));
96fdf90aacd6atagen2026-09-08 08:24 UTC204
        }
a250c7fa43fbatagen2026-09-26 13:35 UTC205
        let (tracking, store_lock) = self
f12377bce9b9atagen2026-09-26 12:32 UTC206
            .tracking
f12377bce9b9atagen2026-09-26 12:32 UTC207
            .take()
f12377bce9b9atagen2026-09-26 12:32 UTC208
            .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?;
a250c7fa43fbatagen2026-09-26 13:35 UTC209
        Session::saved(
a250c7fa43fbatagen2026-09-26 13:35 UTC210
            self.client.clone(),
a250c7fa43fbatagen2026-09-26 13:35 UTC211
            self.sessions.clone(),
a250c7fa43fbatagen2026-09-26 13:35 UTC212
            tracking,
a250c7fa43fbatagen2026-09-26 13:35 UTC213
            store_lock,
a250c7fa43fbatagen2026-09-26 13:35 UTC214
        )
a250c7fa43fbatagen2026-09-26 13:35 UTC215
        .await
96fdf90aacd6atagen2026-09-08 08:24 UTC216
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC217
96fdf90aacd6atagen2026-09-08 08:24 UTC218
    pub async fn abort(mut self) {
96fdf90aacd6atagen2026-09-08 08:24 UTC219
        if let Some(state) = self.state.take() {
96fdf90aacd6atagen2026-09-08 08:24 UTC220
            self.client.oauth().abort_login(&state).await;
96fdf90aacd6atagen2026-09-08 08:24 UTC221
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC222
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC223
}
96fdf90aacd6atagen2026-09-08 08:24 UTC224
96fdf90aacd6atagen2026-09-08 08:24 UTC225
pub struct StoreConfig {
62fe32759529atagen2026-09-26 08:50 UTC226
    location: StoreLocation,
96fdf90aacd6atagen2026-09-08 08:24 UTC227
    passphrase: String,
62fe32759529atagen2026-09-26 08:50 UTC228
}
62fe32759529atagen2026-09-26 08:50 UTC229
62fe32759529atagen2026-09-26 08:50 UTC230
enum StoreLocation {
62fe32759529atagen2026-09-26 08:50 UTC231
    #[cfg(feature = "sqlite")]
62fe32759529atagen2026-09-26 08:50 UTC232
    Sqlite(PathBuf),
62fe32759529atagen2026-09-26 08:50 UTC233
    #[cfg(feature = "indexeddb")]
62fe32759529atagen2026-09-26 08:50 UTC234
    IndexedDb(String),
96fdf90aacd6atagen2026-09-08 08:24 UTC235
}
96fdf90aacd6atagen2026-09-08 08:24 UTC236
96fdf90aacd6atagen2026-09-08 08:24 UTC237
impl StoreConfig {
62fe32759529atagen2026-09-26 08:50 UTC238
    #[cfg(feature = "sqlite")]
62fe32759529atagen2026-09-26 08:50 UTC239
    #[must_use]
62fe32759529atagen2026-09-26 08:50 UTC240
    pub fn sqlite(path: impl Into<PathBuf>, passphrase: impl Into<String>) -> Self {
62fe32759529atagen2026-09-26 08:50 UTC241
        Self {
62fe32759529atagen2026-09-26 08:50 UTC242
            location: StoreLocation::Sqlite(path.into()),
62fe32759529atagen2026-09-26 08:50 UTC243
            passphrase: passphrase.into(),
62fe32759529atagen2026-09-26 08:50 UTC244
        }
62fe32759529atagen2026-09-26 08:50 UTC245
    }
62fe32759529atagen2026-09-26 08:50 UTC246
62fe32759529atagen2026-09-26 08:50 UTC247
    #[cfg(feature = "indexeddb")]
96fdf90aacd6atagen2026-09-08 08:24 UTC248
    #[must_use]
62fe32759529atagen2026-09-26 08:50 UTC249
    pub fn indexeddb(name: impl Into<String>, passphrase: impl Into<String>) -> Self {
96fdf90aacd6atagen2026-09-08 08:24 UTC250
        Self {
62fe32759529atagen2026-09-26 08:50 UTC251
            location: StoreLocation::IndexedDb(name.into()),
96fdf90aacd6atagen2026-09-08 08:24 UTC252
            passphrase: passphrase.into(),
96fdf90aacd6atagen2026-09-08 08:24 UTC253
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC254
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC255
713205a95af2atagen2026-09-26 09:24 UTC256
    /// delete the store's databases, once nothing has them open
713205a95af2atagen2026-09-26 09:24 UTC257
    pub async fn delete(&self) -> Result<(), AccountError> {
713205a95af2atagen2026-09-26 09:24 UTC258
        match &self.location {
713205a95af2atagen2026-09-26 09:24 UTC259
            #[cfg(feature = "sqlite")]
713205a95af2atagen2026-09-26 09:24 UTC260
            StoreLocation::Sqlite(path) => crate::session_store::delete_sqlite_store(path)
713205a95af2atagen2026-09-26 09:24 UTC261
                .map_err(|error| account_error("deleting the Matrix store", error)),
713205a95af2atagen2026-09-26 09:24 UTC262
            #[cfg(feature = "indexeddb")]
713205a95af2atagen2026-09-26 09:24 UTC263
            StoreLocation::IndexedDb(name) => crate::session_store::delete_indexeddb_store(name)
713205a95af2atagen2026-09-26 09:24 UTC264
                .await
6db49c0ecf71atagen2026-09-26 12:41 UTC265
                .map_err(|error| error.into_account_error("deleting the Matrix store")),
713205a95af2atagen2026-09-26 09:24 UTC266
        }
713205a95af2atagen2026-09-26 09:24 UTC267
    }
713205a95af2atagen2026-09-26 09:24 UTC268
a250c7fa43fbatagen2026-09-26 13:35 UTC269
    /// taken before the client opens the store, and held until the engine and every
a250c7fa43fbatagen2026-09-26 13:35 UTC270
    /// `ClientHandle` are gone
a250c7fa43fbatagen2026-09-26 13:35 UTC271
    pub(crate) async fn lock(&self) -> Result<StoreLock, AccountError> {
a250c7fa43fbatagen2026-09-26 13:35 UTC272
        match &self.location {
a250c7fa43fbatagen2026-09-26 13:35 UTC273
            #[cfg(feature = "sqlite")]
a250c7fa43fbatagen2026-09-26 13:35 UTC274
            StoreLocation::Sqlite(path) => StoreLock::file(path),
a250c7fa43fbatagen2026-09-26 13:35 UTC275
            #[cfg(feature = "indexeddb")]
a250c7fa43fbatagen2026-09-26 13:35 UTC276
            StoreLocation::IndexedDb(name) => StoreLock::web(name).await,
a250c7fa43fbatagen2026-09-26 13:35 UTC277
        }
a250c7fa43fbatagen2026-09-26 13:35 UTC278
    }
a250c7fa43fbatagen2026-09-26 13:35 UTC279
713205a95af2atagen2026-09-26 09:24 UTC280
    pub(crate) fn passphrase(&self) -> &str {
713205a95af2atagen2026-09-26 09:24 UTC281
        &self.passphrase
713205a95af2atagen2026-09-26 09:24 UTC282
    }
713205a95af2atagen2026-09-26 09:24 UTC283
713205a95af2atagen2026-09-26 09:24 UTC284
    #[cfg(feature = "indexeddb")]
713205a95af2atagen2026-09-26 09:24 UTC285
    pub(crate) fn indexeddb_name(&self) -> &str {
713205a95af2atagen2026-09-26 09:24 UTC286
        let StoreLocation::IndexedDb(name) = &self.location;
713205a95af2atagen2026-09-26 09:24 UTC287
        name
713205a95af2atagen2026-09-26 09:24 UTC288
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC289
}
96fdf90aacd6atagen2026-09-08 08:24 UTC290
f12377bce9b9atagen2026-09-26 12:32 UTC291
/// Serializable authentication secrets. A `SessionStore` keeps it, and the engine
f12377bce9b9atagen2026-09-26 12:32 UTC292
/// replaces it there on every token rotation.
96fdf90aacd6atagen2026-09-08 08:24 UTC293
#[derive(Clone, Deserialize, Serialize)]
96fdf90aacd6atagen2026-09-08 08:24 UTC294
pub struct SessionRecord {
96fdf90aacd6atagen2026-09-08 08:24 UTC295
    homeserver: String,
96fdf90aacd6atagen2026-09-08 08:24 UTC296
    authentication: AuthenticationRecord,
96fdf90aacd6atagen2026-09-08 08:24 UTC297
}
96fdf90aacd6atagen2026-09-08 08:24 UTC298
96fdf90aacd6atagen2026-09-08 08:24 UTC299
#[derive(Clone, Deserialize, Serialize)]
96fdf90aacd6atagen2026-09-08 08:24 UTC300
#[serde(tag = "type", content = "session", rename_all = "snake_case")]
96fdf90aacd6atagen2026-09-08 08:24 UTC301
enum AuthenticationRecord {
96fdf90aacd6atagen2026-09-08 08:24 UTC302
    Matrix(MatrixSession),
96fdf90aacd6atagen2026-09-08 08:24 UTC303
    OAuth {
96fdf90aacd6atagen2026-09-08 08:24 UTC304
        client_id: ClientId,
96fdf90aacd6atagen2026-09-08 08:24 UTC305
        user: UserSession,
96fdf90aacd6atagen2026-09-08 08:24 UTC306
    },
6f1d6591c10datagen2026-09-26 09:52 UTC307
    SoftLoggedOut {
6f1d6591c10datagen2026-09-26 09:52 UTC308
        meta: SessionMeta,
6f1d6591c10datagen2026-09-26 09:52 UTC309
        client_id: Option<ClientId>,
6f1d6591c10datagen2026-09-26 09:52 UTC310
    },
96fdf90aacd6atagen2026-09-08 08:24 UTC311
}
96fdf90aacd6atagen2026-09-08 08:24 UTC312
96fdf90aacd6atagen2026-09-08 08:24 UTC313
impl SessionRecord {
96fdf90aacd6atagen2026-09-08 08:24 UTC314
    #[must_use]
96fdf90aacd6atagen2026-09-08 08:24 UTC315
    pub fn homeserver(&self) -> &str {
96fdf90aacd6atagen2026-09-08 08:24 UTC316
        &self.homeserver
96fdf90aacd6atagen2026-09-08 08:24 UTC317
    }
6f1d6591c10datagen2026-09-26 09:52 UTC318
6f1d6591c10datagen2026-09-26 09:52 UTC319
    pub(crate) fn soft_logged_out(self) -> Self {
6f1d6591c10datagen2026-09-26 09:52 UTC320
        let authentication = match self.authentication {
6f1d6591c10datagen2026-09-26 09:52 UTC321
            AuthenticationRecord::Matrix(session) => AuthenticationRecord::SoftLoggedOut {
6f1d6591c10datagen2026-09-26 09:52 UTC322
                meta: session.meta,
6f1d6591c10datagen2026-09-26 09:52 UTC323
                client_id: None,
6f1d6591c10datagen2026-09-26 09:52 UTC324
            },
6f1d6591c10datagen2026-09-26 09:52 UTC325
            AuthenticationRecord::OAuth { client_id, user } => {
6f1d6591c10datagen2026-09-26 09:52 UTC326
                AuthenticationRecord::SoftLoggedOut {
6f1d6591c10datagen2026-09-26 09:52 UTC327
                    meta: user.meta,
6f1d6591c10datagen2026-09-26 09:52 UTC328
                    client_id: Some(client_id),
6f1d6591c10datagen2026-09-26 09:52 UTC329
                }
6f1d6591c10datagen2026-09-26 09:52 UTC330
            }
6f1d6591c10datagen2026-09-26 09:52 UTC331
            soft @ AuthenticationRecord::SoftLoggedOut { .. } => soft,
6f1d6591c10datagen2026-09-26 09:52 UTC332
        };
6f1d6591c10datagen2026-09-26 09:52 UTC333
        Self {
6f1d6591c10datagen2026-09-26 09:52 UTC334
            homeserver: self.homeserver,
6f1d6591c10datagen2026-09-26 09:52 UTC335
            authentication,
6f1d6591c10datagen2026-09-26 09:52 UTC336
        }
6f1d6591c10datagen2026-09-26 09:52 UTC337
    }
6f1d6591c10datagen2026-09-26 09:52 UTC338
}
6f1d6591c10datagen2026-09-26 09:52 UTC339
6f1d6591c10datagen2026-09-26 09:52 UTC340
pub enum Restore {
6f1d6591c10datagen2026-09-26 09:52 UTC341
    Resumed(Session),
6f1d6591c10datagen2026-09-26 09:52 UTC342
    SoftLoggedOut(SoftLogout),
6f1d6591c10datagen2026-09-26 09:52 UTC343
}
6f1d6591c10datagen2026-09-26 09:52 UTC344
6f1d6591c10datagen2026-09-26 09:52 UTC345
/// who to log in again as. the store keeps this device's e2ee keys, so a login
6f1d6591c10datagen2026-09-26 09:52 UTC346
/// as any other device would orphan them
6f1d6591c10datagen2026-09-26 09:52 UTC347
#[derive(Clone, Debug)]
6f1d6591c10datagen2026-09-26 09:52 UTC348
#[non_exhaustive]
6f1d6591c10datagen2026-09-26 09:52 UTC349
pub struct SoftLogout {
6f1d6591c10datagen2026-09-26 09:52 UTC350
    pub homeserver: String,
6f1d6591c10datagen2026-09-26 09:52 UTC351
    pub user_id: OwnedUserId,
6f1d6591c10datagen2026-09-26 09:52 UTC352
    pub device_id: OwnedDeviceId,
6f1d6591c10datagen2026-09-26 09:52 UTC353
    /// set when the login was OAuth, for `OAuthLogin::with_client_id`
6f1d6591c10datagen2026-09-26 09:52 UTC354
    pub client_id: Option<String>,
6f1d6591c10datagen2026-09-26 09:52 UTC355
}
6f1d6591c10datagen2026-09-26 09:52 UTC356
6f1d6591c10datagen2026-09-26 09:52 UTC357
impl SoftLogout {
6f1d6591c10datagen2026-09-26 09:52 UTC358
    #[must_use]
6f1d6591c10datagen2026-09-26 09:52 UTC359
    pub fn password_login(&self, password: impl Into<String>) -> PasswordLogin {
6f1d6591c10datagen2026-09-26 09:52 UTC360
        PasswordLogin::new(self.homeserver.clone(), self.user_id.as_str(), password)
6f1d6591c10datagen2026-09-26 09:52 UTC361
            .with_device_id(self.device_id.clone())
9c05b1dd327aatagen2026-10-08 11:27 UTC362
            .allow_insecure_loopback(true)
6f1d6591c10datagen2026-09-26 09:52 UTC363
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC364
}
96fdf90aacd6atagen2026-09-08 08:24 UTC365
96fdf90aacd6atagen2026-09-08 08:24 UTC366
#[derive(Debug, Error)]
96fdf90aacd6atagen2026-09-08 08:24 UTC367
pub enum AccountError {
96fdf90aacd6atagen2026-09-08 08:24 UTC368
    #[error("{context} failed")]
96fdf90aacd6atagen2026-09-08 08:24 UTC369
    Operation {
96fdf90aacd6atagen2026-09-08 08:24 UTC370
        context: &'static str,
9a25b6ce66f4atagen2026-10-08 11:15 UTC371
        kind: ErrorKind,
9a25b6ce66f4atagen2026-10-08 11:15 UTC372
        server: Option<ServerError>,
96fdf90aacd6atagen2026-09-08 08:24 UTC373
        #[source]
62fe32759529atagen2026-09-26 08:50 UTC374
        source: Box<SourceError>,
96fdf90aacd6atagen2026-09-08 08:24 UTC375
    },
96fdf90aacd6atagen2026-09-08 08:24 UTC376
    #[error("the homeserver returned no authenticated session")]
96fdf90aacd6atagen2026-09-08 08:24 UTC377
    MissingSession,
96fdf90aacd6atagen2026-09-08 08:24 UTC378
    #[error("the SDK returned an unsupported authentication session")]
96fdf90aacd6atagen2026-09-08 08:24 UTC379
    UnsupportedAuthentication,
96fdf90aacd6atagen2026-09-08 08:24 UTC380
    #[error("the OAuth callback was already submitted")]
96fdf90aacd6atagen2026-09-08 08:24 UTC381
    OAuthCallbackAlreadySubmitted,
9c05b1dd327aatagen2026-10-08 11:27 UTC382
    /// what was typed can't name a homeserver, so nothing was sent anywhere
9c05b1dd327aatagen2026-10-08 11:27 UTC383
    #[error("that isn't a usable homeserver, {0}")]
9c05b1dd327aatagen2026-10-08 11:27 UTC384
    InvalidHomeserver(&'static str),
6db49c0ecf71atagen2026-09-26 12:41 UTC385
    /// an IndexedDB delete is waiting on the store's other connections. it goes through
6db49c0ecf71atagen2026-09-26 12:41 UTC386
    /// once they close, and opening the store again waits behind it
6db49c0ecf71atagen2026-09-26 12:41 UTC387
    #[error("the store is still open elsewhere, it will be deleted once that closes")]
6db49c0ecf71atagen2026-09-26 12:41 UTC388
    StoreDeletePending,
a250c7fa43fbatagen2026-09-26 13:35 UTC389
    /// another engine, in this process or another, has the store. only one runs per store
a250c7fa43fbatagen2026-09-26 13:35 UTC390
    #[error("another engine is using this store")]
a250c7fa43fbatagen2026-09-26 13:35 UTC391
    StoreInUse,
a250c7fa43fbatagen2026-09-26 13:35 UTC392
    /// the login worked but its first record didn't save. the server already has the
a250c7fa43fbatagen2026-09-26 13:35 UTC393
    /// device, so retry the save rather than log in again
a250c7fa43fbatagen2026-09-26 13:35 UTC394
    #[error("saving the new login's record failed")]
a250c7fa43fbatagen2026-09-26 13:35 UTC395
    Unsaved {
a250c7fa43fbatagen2026-09-26 13:35 UTC396
        session: Box<UnsavedSession>,
a250c7fa43fbatagen2026-09-26 13:35 UTC397
        #[source]
a250c7fa43fbatagen2026-09-26 13:35 UTC398
        source: Box<AccountError>,
a250c7fa43fbatagen2026-09-26 13:35 UTC399
    },
96fdf90aacd6atagen2026-09-08 08:24 UTC400
    #[error(transparent)]
96fdf90aacd6atagen2026-09-08 08:24 UTC401
    Engine(#[from] EngineError),
96fdf90aacd6atagen2026-09-08 08:24 UTC402
}
96fdf90aacd6atagen2026-09-08 08:24 UTC403
9a25b6ce66f4atagen2026-10-08 11:15 UTC404
impl AccountError {
9a25b6ce66f4atagen2026-10-08 11:15 UTC405
    /// what the homeserver said, when it refused
9a25b6ce66f4atagen2026-10-08 11:15 UTC406
    #[must_use]
9a25b6ce66f4atagen2026-10-08 11:15 UTC407
    pub fn server(&self) -> Option<&ServerError> {
9a25b6ce66f4atagen2026-10-08 11:15 UTC408
        match self {
9a25b6ce66f4atagen2026-10-08 11:15 UTC409
            Self::Operation { server, .. } => server.as_ref(),
9a25b6ce66f4atagen2026-10-08 11:15 UTC410
            Self::Unsaved { source, .. } => source.server(),
9a25b6ce66f4atagen2026-10-08 11:15 UTC411
            _ => None,
9a25b6ce66f4atagen2026-10-08 11:15 UTC412
        }
9a25b6ce66f4atagen2026-10-08 11:15 UTC413
    }
9a25b6ce66f4atagen2026-10-08 11:15 UTC414
}
9a25b6ce66f4atagen2026-10-08 11:15 UTC415
a250c7fa43fbatagen2026-09-26 13:35 UTC416
/// a live login whose record never reached its `SessionStore`
a250c7fa43fbatagen2026-09-26 13:35 UTC417
pub struct UnsavedSession {
a250c7fa43fbatagen2026-09-26 13:35 UTC418
    session: Session,
a250c7fa43fbatagen2026-09-26 13:35 UTC419
    sessions: Arc<dyn SessionStore>,
a250c7fa43fbatagen2026-09-26 13:35 UTC420
}
a250c7fa43fbatagen2026-09-26 13:35 UTC421
a250c7fa43fbatagen2026-09-26 13:35 UTC422
impl UnsavedSession {
a250c7fa43fbatagen2026-09-26 13:35 UTC423
    pub async fn save(self) -> Result<Session, AccountError> {
a250c7fa43fbatagen2026-09-26 13:35 UTC424
        let saved = match session_record(&self.session.client) {
a250c7fa43fbatagen2026-09-26 13:35 UTC425
            Ok(record) => self.sessions.save(&record).await,
a250c7fa43fbatagen2026-09-26 13:35 UTC426
            Err(error) => Err(error),
a250c7fa43fbatagen2026-09-26 13:35 UTC427
        };
a250c7fa43fbatagen2026-09-26 13:35 UTC428
        match saved {
a250c7fa43fbatagen2026-09-26 13:35 UTC429
            Ok(()) => Ok(self.session),
a250c7fa43fbatagen2026-09-26 13:35 UTC430
            Err(source) => Err(AccountError::Unsaved {
a250c7fa43fbatagen2026-09-26 13:35 UTC431
                session: Box::new(self),
a250c7fa43fbatagen2026-09-26 13:35 UTC432
                source: Box::new(source),
a250c7fa43fbatagen2026-09-26 13:35 UTC433
            }),
a250c7fa43fbatagen2026-09-26 13:35 UTC434
        }
a250c7fa43fbatagen2026-09-26 13:35 UTC435
    }
a250c7fa43fbatagen2026-09-26 13:35 UTC436
a250c7fa43fbatagen2026-09-26 13:35 UTC437
    /// the record to keep somewhere else, if the store won't take it
a250c7fa43fbatagen2026-09-26 13:35 UTC438
    pub fn record(&self) -> Result<SessionRecord, AccountError> {
a250c7fa43fbatagen2026-09-26 13:35 UTC439
        session_record(&self.session.client)
a250c7fa43fbatagen2026-09-26 13:35 UTC440
    }
a250c7fa43fbatagen2026-09-26 13:35 UTC441
}
a250c7fa43fbatagen2026-09-26 13:35 UTC442
a250c7fa43fbatagen2026-09-26 13:35 UTC443
impl std::fmt::Debug for UnsavedSession {
a250c7fa43fbatagen2026-09-26 13:35 UTC444
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
a250c7fa43fbatagen2026-09-26 13:35 UTC445
        formatter
a250c7fa43fbatagen2026-09-26 13:35 UTC446
            .debug_struct("UnsavedSession")
a250c7fa43fbatagen2026-09-26 13:35 UTC447
            .finish_non_exhaustive()
a250c7fa43fbatagen2026-09-26 13:35 UTC448
    }
a250c7fa43fbatagen2026-09-26 13:35 UTC449
}
a250c7fa43fbatagen2026-09-26 13:35 UTC450
96fdf90aacd6atagen2026-09-08 08:24 UTC451
impl Session {
96fdf90aacd6atagen2026-09-08 08:24 UTC452
    pub async fn login_password(
96fdf90aacd6atagen2026-09-08 08:24 UTC453
        login: PasswordLogin,
f12377bce9b9atagen2026-09-26 12:32 UTC454
        sessions: Arc<dyn SessionStore>,
96fdf90aacd6atagen2026-09-08 08:24 UTC455
    ) -> Result<Self, AccountError> {
9c05b1dd327aatagen2026-10-08 11:27 UTC456
        let homeserver = homeserver_input(&login.homeserver, login.allow_insecure_loopback)?;
a250c7fa43fbatagen2026-09-26 13:35 UTC457
        let store_lock = sessions.store().lock().await?;
713205a95af2atagen2026-09-26 09:24 UTC458
        let client = matrix_client_builder(sessions.store())
9c05b1dd327aatagen2026-10-08 11:27 UTC459
            .server_name_or_homeserver_url(homeserver)
96fdf90aacd6atagen2026-09-08 08:24 UTC460
            .build()
96fdf90aacd6atagen2026-09-08 08:24 UTC461
            .await
96fdf90aacd6atagen2026-09-08 08:24 UTC462
            .map_err(|error| account_error("building the Matrix client", error))?;
f12377bce9b9atagen2026-09-26 12:32 UTC463
        let tracking = Tracking::start(&client, sessions.clone())?;
96fdf90aacd6atagen2026-09-08 08:24 UTC464
        let mut request = client
96fdf90aacd6atagen2026-09-08 08:24 UTC465
            .matrix_auth()
96fdf90aacd6atagen2026-09-08 08:24 UTC466
            .login_username(login.username.trim(), &login.password)
96fdf90aacd6atagen2026-09-08 08:24 UTC467
            .request_refresh_token();
96fdf90aacd6atagen2026-09-08 08:24 UTC468
        if let Some(name) = login.device_display_name.as_deref() {
96fdf90aacd6atagen2026-09-08 08:24 UTC469
            request = request.initial_device_display_name(name);
96fdf90aacd6atagen2026-09-08 08:24 UTC470
        }
fa9114fcf75fatagen2026-09-24 12:51 UTC471
        if let Some(device_id) = login.device_id.as_deref() {
fa9114fcf75fatagen2026-09-24 12:51 UTC472
            request = request.device_id(device_id.as_str());
fa9114fcf75fatagen2026-09-24 12:51 UTC473
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC474
        request
96fdf90aacd6atagen2026-09-08 08:24 UTC475
            .send()
96fdf90aacd6atagen2026-09-08 08:24 UTC476
            .await
96fdf90aacd6atagen2026-09-08 08:24 UTC477
            .map_err(|error| account_error("password login", error))?;
a250c7fa43fbatagen2026-09-26 13:35 UTC478
        Self::saved(client, sessions, tracking, store_lock).await
96fdf90aacd6atagen2026-09-08 08:24 UTC479
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC480
a250c7fa43fbatagen2026-09-26 13:35 UTC481
    /// the pending login lives only in this process, so the one that begins it has to finish it
96fdf90aacd6atagen2026-09-08 08:24 UTC482
    pub async fn begin_oauth(
96fdf90aacd6atagen2026-09-08 08:24 UTC483
        login: OAuthLogin,
713205a95af2atagen2026-09-26 09:24 UTC484
        sessions: Arc<dyn SessionStore>,
96fdf90aacd6atagen2026-09-08 08:24 UTC485
    ) -> Result<PendingOAuthLogin, AccountError> {
96fdf90aacd6atagen2026-09-08 08:24 UTC486
        let redirect_uri = Url::parse(login.redirect_uri.trim())
96fdf90aacd6atagen2026-09-08 08:24 UTC487
            .map_err(|error| account_error("parsing the OAuth redirect URI", error))?;
96fdf90aacd6atagen2026-09-08 08:24 UTC488
        let client_uri = Url::parse(login.client_uri.trim())
96fdf90aacd6atagen2026-09-08 08:24 UTC489
            .map_err(|error| account_error("parsing the OAuth client URI", error))?;
9c05b1dd327aatagen2026-10-08 11:27 UTC490
        let homeserver = homeserver_input(&login.homeserver, login.allow_insecure_loopback)?;
a250c7fa43fbatagen2026-09-26 13:35 UTC491
        let store_lock = sessions.store().lock().await?;
713205a95af2atagen2026-09-26 09:24 UTC492
        let client = matrix_client_builder(sessions.store())
9c05b1dd327aatagen2026-10-08 11:27 UTC493
            .server_name_or_homeserver_url(homeserver)
96fdf90aacd6atagen2026-09-08 08:24 UTC494
            .build()
96fdf90aacd6atagen2026-09-08 08:24 UTC495
            .await
96fdf90aacd6atagen2026-09-08 08:24 UTC496
            .map_err(|error| account_error("building the Matrix client", error))?;
f12377bce9b9atagen2026-09-26 12:32 UTC497
        let tracking = Tracking::start(&client, sessions.clone())?;
96fdf90aacd6atagen2026-09-08 08:24 UTC498
        let application_type = match login.application_type {
96fdf90aacd6atagen2026-09-08 08:24 UTC499
            OAuthApplicationType::Native => ApplicationType::Native,
96fdf90aacd6atagen2026-09-08 08:24 UTC500
            OAuthApplicationType::Web => ApplicationType::Web,
96fdf90aacd6atagen2026-09-08 08:24 UTC501
        };
96fdf90aacd6atagen2026-09-08 08:24 UTC502
        let mut metadata = ClientMetadata::new(
96fdf90aacd6atagen2026-09-08 08:24 UTC503
            application_type,
96fdf90aacd6atagen2026-09-08 08:24 UTC504
            vec![OAuthGrantType::AuthorizationCode {
96fdf90aacd6atagen2026-09-08 08:24 UTC505
                redirect_uris: vec![redirect_uri.clone()],
96fdf90aacd6atagen2026-09-08 08:24 UTC506
            }],
96fdf90aacd6atagen2026-09-08 08:24 UTC507
            Localized::new(client_uri, []),
96fdf90aacd6atagen2026-09-08 08:24 UTC508
        );
96fdf90aacd6atagen2026-09-08 08:24 UTC509
        metadata.client_name = login.client_name.map(|name| Localized::new(name, []));
96fdf90aacd6atagen2026-09-08 08:24 UTC510
        let metadata = Raw::new(&metadata)
96fdf90aacd6atagen2026-09-08 08:24 UTC511
            .map_err(|error| account_error("serializing OAuth client metadata", error))?;
96fdf90aacd6atagen2026-09-08 08:24 UTC512
        let mut registration = ClientRegistrationData::new(metadata);
96fdf90aacd6atagen2026-09-08 08:24 UTC513
        if let Some(client_id) = login.client_id {
96fdf90aacd6atagen2026-09-08 08:24 UTC514
            registration.static_registrations = Some(HashMap::from([(
96fdf90aacd6atagen2026-09-08 08:24 UTC515
                client.homeserver(),
96fdf90aacd6atagen2026-09-08 08:24 UTC516
                ClientId::new(client_id),
96fdf90aacd6atagen2026-09-08 08:24 UTC517
            )]));
96fdf90aacd6atagen2026-09-08 08:24 UTC518
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC519
        let authorization = client
96fdf90aacd6atagen2026-09-08 08:24 UTC520
            .oauth()
96fdf90aacd6atagen2026-09-08 08:24 UTC521
            .login(redirect_uri, login.device_id, Some(registration), None)
96fdf90aacd6atagen2026-09-08 08:24 UTC522
            .build()
96fdf90aacd6atagen2026-09-08 08:24 UTC523
            .await
96fdf90aacd6atagen2026-09-08 08:24 UTC524
            .map_err(|error| account_error("starting OAuth login", error))?;
96fdf90aacd6atagen2026-09-08 08:24 UTC525
        Ok(PendingOAuthLogin {
96fdf90aacd6atagen2026-09-08 08:24 UTC526
            client,
713205a95af2atagen2026-09-26 09:24 UTC527
            sessions,
a250c7fa43fbatagen2026-09-26 13:35 UTC528
            tracking: Some((tracking, store_lock)),
96fdf90aacd6atagen2026-09-08 08:24 UTC529
            authorization_url: authorization.url.to_string(),
96fdf90aacd6atagen2026-09-08 08:24 UTC530
            state: Some(authorization.state),
96fdf90aacd6atagen2026-09-08 08:24 UTC531
        })
96fdf90aacd6atagen2026-09-08 08:24 UTC532
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC533
713205a95af2atagen2026-09-26 09:24 UTC534
    /// Restore the record `sessions` holds, or `None` when it holds none.
f12377bce9b9atagen2026-09-26 12:32 UTC535
    /// The caller must also handle rejection and explicit logout.
f12377bce9b9atagen2026-09-26 12:32 UTC536
    pub async fn restore(sessions: Arc<dyn SessionStore>) -> Result<Option<Restore>, AccountError> {
713205a95af2atagen2026-09-26 09:24 UTC537
        let Some(record) = sessions.load().await? else {
713205a95af2atagen2026-09-26 09:24 UTC538
            return Ok(None);
713205a95af2atagen2026-09-26 09:24 UTC539
        };
713205a95af2atagen2026-09-26 09:24 UTC540
        let authentication = match record.authentication {
96fdf90aacd6atagen2026-09-08 08:24 UTC541
            AuthenticationRecord::Matrix(session) => AuthSession::Matrix(session),
96fdf90aacd6atagen2026-09-08 08:24 UTC542
            AuthenticationRecord::OAuth { client_id, user } => {
96fdf90aacd6atagen2026-09-08 08:24 UTC543
                OAuthSession { client_id, user }.into()
96fdf90aacd6atagen2026-09-08 08:24 UTC544
            }
6f1d6591c10datagen2026-09-26 09:52 UTC545
            AuthenticationRecord::SoftLoggedOut { meta, client_id } => {
6f1d6591c10datagen2026-09-26 09:52 UTC546
                return Ok(Some(Restore::SoftLoggedOut(SoftLogout {
6f1d6591c10datagen2026-09-26 09:52 UTC547
                    homeserver: record.homeserver,
6f1d6591c10datagen2026-09-26 09:52 UTC548
                    user_id: meta.user_id,
6f1d6591c10datagen2026-09-26 09:52 UTC549
                    device_id: meta.device_id,
6f1d6591c10datagen2026-09-26 09:52 UTC550
                    client_id: client_id.map(|client_id| client_id.as_str().to_owned()),
6f1d6591c10datagen2026-09-26 09:52 UTC551
                })));
6f1d6591c10datagen2026-09-26 09:52 UTC552
            }
96fdf90aacd6atagen2026-09-08 08:24 UTC553
        };
a250c7fa43fbatagen2026-09-26 13:35 UTC554
        let store_lock = sessions.store().lock().await?;
6f1d6591c10datagen2026-09-26 09:52 UTC555
        let client = matrix_client_builder(sessions.store())
6f1d6591c10datagen2026-09-26 09:52 UTC556
            .homeserver_url(&record.homeserver)
6f1d6591c10datagen2026-09-26 09:52 UTC557
            .build()
6f1d6591c10datagen2026-09-26 09:52 UTC558
            .await
6f1d6591c10datagen2026-09-26 09:52 UTC559
            .map_err(|error| account_error("reopening the Matrix store", error))?;
f12377bce9b9atagen2026-09-26 12:32 UTC560
        let tracking = Tracking::start(&client, sessions)?;
96fdf90aacd6atagen2026-09-08 08:24 UTC561
        client
96fdf90aacd6atagen2026-09-08 08:24 UTC562
            .restore_session(authentication)
96fdf90aacd6atagen2026-09-08 08:24 UTC563
            .await
96fdf90aacd6atagen2026-09-08 08:24 UTC564
            .map_err(|error| account_error("restoring the authenticated session", error))?;
a250c7fa43fbatagen2026-09-26 13:35 UTC565
        Ok(Some(Restore::Resumed(Self {
a250c7fa43fbatagen2026-09-26 13:35 UTC566
            client,
a250c7fa43fbatagen2026-09-26 13:35 UTC567
            tracking,
a250c7fa43fbatagen2026-09-26 13:35 UTC568
            store_lock,
a250c7fa43fbatagen2026-09-26 13:35 UTC569
        })))
f12377bce9b9atagen2026-09-26 12:32 UTC570
    }
f12377bce9b9atagen2026-09-26 12:32 UTC571
f12377bce9b9atagen2026-09-26 12:32 UTC572
    /// leave the record to the host, which then saves `ClientHandle::session_record()`
f12377bce9b9atagen2026-09-26 12:32 UTC573
    /// on every `SessionTokensChanged`
f12377bce9b9atagen2026-09-26 12:32 UTC574
    #[must_use]
f12377bce9b9atagen2026-09-26 12:32 UTC575
    pub fn without_persistence(mut self) -> Self {
f12377bce9b9atagen2026-09-26 12:32 UTC576
        self.tracking.persistence = None;
f12377bce9b9atagen2026-09-26 12:32 UTC577
        #[cfg(not(target_family = "wasm"))]
f12377bce9b9atagen2026-09-26 12:32 UTC578
        self.tracking.inline.store(false, Ordering::Release);
f12377bce9b9atagen2026-09-26 12:32 UTC579
        self
96fdf90aacd6atagen2026-09-08 08:24 UTC580
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC581
96fdf90aacd6atagen2026-09-08 08:24 UTC582
    pub async fn build(
96fdf90aacd6atagen2026-09-08 08:24 UTC583
        self,
96fdf90aacd6atagen2026-09-08 08:24 UTC584
        config: EngineConfig,
96fdf90aacd6atagen2026-09-08 08:24 UTC585
    ) -> Result<(Engine, ClientHandle, EventStream), AccountError> {
f12377bce9b9atagen2026-09-26 12:32 UTC586
        Engine::with_persistence(
f12377bce9b9atagen2026-09-26 12:32 UTC587
            self.client,
f12377bce9b9atagen2026-09-26 12:32 UTC588
            config,
f12377bce9b9atagen2026-09-26 12:32 UTC589
            self.tracking.session_changes,
f12377bce9b9atagen2026-09-26 12:32 UTC590
            self.tracking.persistence,
a250c7fa43fbatagen2026-09-26 13:35 UTC591
            Some(self.store_lock),
f12377bce9b9atagen2026-09-26 12:32 UTC592
        )
f12377bce9b9atagen2026-09-26 12:32 UTC593
        .await
f12377bce9b9atagen2026-09-26 12:32 UTC594
        .map_err(AccountError::from)
96fdf90aacd6atagen2026-09-08 08:24 UTC595
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC596
f12377bce9b9atagen2026-09-26 12:32 UTC597
    async fn saved(
f12377bce9b9atagen2026-09-26 12:32 UTC598
        client: Client,
a250c7fa43fbatagen2026-09-26 13:35 UTC599
        sessions: Arc<dyn SessionStore>,
f12377bce9b9atagen2026-09-26 12:32 UTC600
        tracking: Tracking,
a250c7fa43fbatagen2026-09-26 13:35 UTC601
        store_lock: StoreLock,
f12377bce9b9atagen2026-09-26 12:32 UTC602
    ) -> Result<Self, AccountError> {
a250c7fa43fbatagen2026-09-26 13:35 UTC603
        UnsavedSession {
a250c7fa43fbatagen2026-09-26 13:35 UTC604
            session: Self {
a250c7fa43fbatagen2026-09-26 13:35 UTC605
                client,
a250c7fa43fbatagen2026-09-26 13:35 UTC606
                tracking,
a250c7fa43fbatagen2026-09-26 13:35 UTC607
                store_lock,
a250c7fa43fbatagen2026-09-26 13:35 UTC608
            },
a250c7fa43fbatagen2026-09-26 13:35 UTC609
            sessions,
a250c7fa43fbatagen2026-09-26 13:35 UTC610
        }
a250c7fa43fbatagen2026-09-26 13:35 UTC611
        .save()
a250c7fa43fbatagen2026-09-26 13:35 UTC612
        .await
f12377bce9b9atagen2026-09-26 12:32 UTC613
    }
f12377bce9b9atagen2026-09-26 12:32 UTC614
}
f12377bce9b9atagen2026-09-26 12:32 UTC615
f12377bce9b9atagen2026-09-26 12:32 UTC616
impl Tracking {
f12377bce9b9atagen2026-09-26 12:32 UTC617
    fn start(client: &Client, sessions: Arc<dyn SessionStore>) -> Result<Self, AccountError> {
f12377bce9b9atagen2026-09-26 12:32 UTC618
        let session_changes = client.subscribe_to_session_changes();
f12377bce9b9atagen2026-09-26 12:32 UTC619
        #[cfg_attr(target_family = "wasm", allow(unused_variables))]
f12377bce9b9atagen2026-09-26 12:32 UTC620
        let (results, inline_results) = mpsc::unbounded_channel();
f12377bce9b9atagen2026-09-26 12:32 UTC621
        #[cfg(not(target_family = "wasm"))]
f12377bce9b9atagen2026-09-26 12:32 UTC622
        let inline = save_inline(client, sessions.clone(), results)?;
f12377bce9b9atagen2026-09-26 12:32 UTC623
        Ok(Self {
f12377bce9b9atagen2026-09-26 12:32 UTC624
            session_changes,
f12377bce9b9atagen2026-09-26 12:32 UTC625
            persistence: Some(Persistence {
f12377bce9b9atagen2026-09-26 12:32 UTC626
                save: Box::new(move |client| {
f12377bce9b9atagen2026-09-26 12:32 UTC627
                    let sessions = sessions.clone();
f12377bce9b9atagen2026-09-26 12:32 UTC628
                    Box::pin(async move {
f12377bce9b9atagen2026-09-26 12:32 UTC629
                        let record = session_record(&client).map_err(save_failed)?;
f12377bce9b9atagen2026-09-26 12:32 UTC630
                        sessions.save(&record).await.map_err(save_failed)
f12377bce9b9atagen2026-09-26 12:32 UTC631
                    })
f12377bce9b9atagen2026-09-26 12:32 UTC632
                }),
f12377bce9b9atagen2026-09-26 12:32 UTC633
                inline: inline_results,
f12377bce9b9atagen2026-09-26 12:32 UTC634
            }),
f12377bce9b9atagen2026-09-26 12:32 UTC635
            #[cfg(not(target_family = "wasm"))]
f12377bce9b9atagen2026-09-26 12:32 UTC636
            inline,
f12377bce9b9atagen2026-09-26 12:32 UTC637
        })
96fdf90aacd6atagen2026-09-08 08:24 UTC638
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC639
}
96fdf90aacd6atagen2026-09-08 08:24 UTC640
f12377bce9b9atagen2026-09-26 12:32 UTC641
/// the sdk calls this inside its refresh, after it has the new tokens and before it uses
f12377bce9b9atagen2026-09-26 12:32 UTC642
/// them, so a store that writes there and then can't lose a refresh token to a crash
f12377bce9b9atagen2026-09-26 12:32 UTC643
#[cfg(not(target_family = "wasm"))]
f12377bce9b9atagen2026-09-26 12:32 UTC644
fn save_inline(
f12377bce9b9atagen2026-09-26 12:32 UTC645
    client: &Client,
f12377bce9b9atagen2026-09-26 12:32 UTC646
    sessions: Arc<dyn SessionStore>,
f12377bce9b9atagen2026-09-26 12:32 UTC647
    results: mpsc::UnboundedSender<Result<(), crate::Error>>,
f12377bce9b9atagen2026-09-26 12:32 UTC648
) -> Result<Arc<AtomicBool>, AccountError> {
f12377bce9b9atagen2026-09-26 12:32 UTC649
    let enabled = Arc::new(AtomicBool::new(true));
f12377bce9b9atagen2026-09-26 12:32 UTC650
    let saving = enabled.clone();
f12377bce9b9atagen2026-09-26 12:32 UTC651
    client
f12377bce9b9atagen2026-09-26 12:32 UTC652
        .set_session_callbacks(
f12377bce9b9atagen2026-09-26 12:32 UTC653
            // only asked for with the cross process refresh lock, which we never enable
f12377bce9b9atagen2026-09-26 12:32 UTC654
            Box::new(|client| {
f12377bce9b9atagen2026-09-26 12:32 UTC655
                client
f12377bce9b9atagen2026-09-26 12:32 UTC656
                    .session_tokens()
f12377bce9b9atagen2026-09-26 12:32 UTC657
                    .ok_or_else(|| AccountError::MissingSession.into())
f12377bce9b9atagen2026-09-26 12:32 UTC658
            }),
f12377bce9b9atagen2026-09-26 12:32 UTC659
            Box::new(move |client| {
f12377bce9b9atagen2026-09-26 12:32 UTC660
                if !saving.load(Ordering::Acquire) {
f12377bce9b9atagen2026-09-26 12:32 UTC661
                    return Ok(());
f12377bce9b9atagen2026-09-26 12:32 UTC662
                }
f12377bce9b9atagen2026-09-26 12:32 UTC663
                let record = session_record(&client)?;
f12377bce9b9atagen2026-09-26 12:32 UTC664
                if let Some(result) = sessions.save_blocking(&record) {
f12377bce9b9atagen2026-09-26 12:32 UTC665
                    let _ = results.send(result.map_err(save_failed));
f12377bce9b9atagen2026-09-26 12:32 UTC666
                }
f12377bce9b9atagen2026-09-26 12:32 UTC667
                Ok(())
f12377bce9b9atagen2026-09-26 12:32 UTC668
            }),
f12377bce9b9atagen2026-09-26 12:32 UTC669
        )
f12377bce9b9atagen2026-09-26 12:32 UTC670
        .map_err(|error| account_error("registering the session callbacks", error))?;
f12377bce9b9atagen2026-09-26 12:32 UTC671
    Ok(enabled)
f12377bce9b9atagen2026-09-26 12:32 UTC672
}
f12377bce9b9atagen2026-09-26 12:32 UTC673
f12377bce9b9atagen2026-09-26 12:32 UTC674
fn save_failed(error: AccountError) -> crate::Error {
f12377bce9b9atagen2026-09-26 12:32 UTC675
    let message = format!("saving the session record failed, {error}");
f12377bce9b9atagen2026-09-26 12:32 UTC676
    crate::Error::with_source(ErrorKind::Store, message, error)
f12377bce9b9atagen2026-09-26 12:32 UTC677
}
f12377bce9b9atagen2026-09-26 12:32 UTC678
96fdf90aacd6atagen2026-09-08 08:24 UTC679
pub(crate) fn session_record(client: &Client) -> Result<SessionRecord, AccountError> {
96fdf90aacd6atagen2026-09-08 08:24 UTC680
    let authentication = match client.session().ok_or(AccountError::MissingSession)? {
96fdf90aacd6atagen2026-09-08 08:24 UTC681
        AuthSession::Matrix(session) => AuthenticationRecord::Matrix(session),
96fdf90aacd6atagen2026-09-08 08:24 UTC682
        AuthSession::OAuth(session) => {
96fdf90aacd6atagen2026-09-08 08:24 UTC683
            let OAuthSession { client_id, user } = *session;
96fdf90aacd6atagen2026-09-08 08:24 UTC684
            AuthenticationRecord::OAuth { client_id, user }
96fdf90aacd6atagen2026-09-08 08:24 UTC685
        }
96fdf90aacd6atagen2026-09-08 08:24 UTC686
        _ => return Err(AccountError::UnsupportedAuthentication),
96fdf90aacd6atagen2026-09-08 08:24 UTC687
    };
96fdf90aacd6atagen2026-09-08 08:24 UTC688
    Ok(SessionRecord {
96fdf90aacd6atagen2026-09-08 08:24 UTC689
        homeserver: client.homeserver().to_string(),
96fdf90aacd6atagen2026-09-08 08:24 UTC690
        authentication,
96fdf90aacd6atagen2026-09-08 08:24 UTC691
    })
96fdf90aacd6atagen2026-09-08 08:24 UTC692
}
96fdf90aacd6atagen2026-09-08 08:24 UTC693
96fdf90aacd6atagen2026-09-08 08:24 UTC694
fn encryption_settings() -> EncryptionSettings {
96fdf90aacd6atagen2026-09-08 08:24 UTC695
    EncryptionSettings {
96fdf90aacd6atagen2026-09-08 08:24 UTC696
        auto_enable_cross_signing: true,
96fdf90aacd6atagen2026-09-08 08:24 UTC697
        backup_download_strategy: BackupDownloadStrategy::OneShot,
96fdf90aacd6atagen2026-09-08 08:24 UTC698
        auto_enable_backups: true,
96fdf90aacd6atagen2026-09-08 08:24 UTC699
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC700
}
96fdf90aacd6atagen2026-09-08 08:24 UTC701
a250c7fa43fbatagen2026-09-26 13:35 UTC702
/// `StoreLock` already keeps every other client off the store, and the sdk's lease would cost
a250c7fa43fbatagen2026-09-26 13:35 UTC703
/// a store write per locked section and a task renewing it every 50ms
96fdf90aacd6atagen2026-09-08 08:24 UTC704
fn matrix_client_builder(store: &StoreConfig) -> ClientBuilder {
a250c7fa43fbatagen2026-09-26 13:35 UTC705
    let builder = Client::builder()
96fdf90aacd6atagen2026-09-08 08:24 UTC706
        .with_encryption_settings(encryption_settings())
96fdf90aacd6atagen2026-09-08 08:24 UTC707
        .with_threading_support(ThreadingSupport::Enabled {
96fdf90aacd6atagen2026-09-08 08:24 UTC708
            with_subscriptions: false,
96fdf90aacd6atagen2026-09-08 08:24 UTC709
        })
a250c7fa43fbatagen2026-09-26 13:35 UTC710
        .handle_refresh_tokens()
a250c7fa43fbatagen2026-09-26 13:35 UTC711
        .cross_process_store_config(CrossProcessLockConfig::SingleProcess);
62fe32759529atagen2026-09-26 08:50 UTC712
    match &store.location {
62fe32759529atagen2026-09-26 08:50 UTC713
        #[cfg(feature = "sqlite")]
62fe32759529atagen2026-09-26 08:50 UTC714
        StoreLocation::Sqlite(path) => builder.sqlite_store(path, Some(&store.passphrase)),
62fe32759529atagen2026-09-26 08:50 UTC715
        #[cfg(feature = "indexeddb")]
62fe32759529atagen2026-09-26 08:50 UTC716
        StoreLocation::IndexedDb(name) => builder.indexeddb_store(name, Some(&store.passphrase)),
62fe32759529atagen2026-09-26 08:50 UTC717
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC718
}
96fdf90aacd6atagen2026-09-08 08:24 UTC719
9c05b1dd327aatagen2026-10-08 11:27 UTC720
/// a bare server name goes through untouched, since that's what makes the sdk try `.well-known`
9c05b1dd327aatagen2026-10-08 11:27 UTC721
fn homeserver_input(input: &str, allow_insecure_loopback: bool) -> Result<&str, AccountError> {
9c05b1dd327aatagen2026-10-08 11:27 UTC722
    let input = input.trim();
9c05b1dd327aatagen2026-10-08 11:27 UTC723
    let invalid = |reason| Err(AccountError::InvalidHomeserver(reason));
9c05b1dd327aatagen2026-10-08 11:27 UTC724
    if input.is_empty() {
9c05b1dd327aatagen2026-10-08 11:27 UTC725
        return invalid("it's empty");
9c05b1dd327aatagen2026-10-08 11:27 UTC726
    }
9c05b1dd327aatagen2026-10-08 11:27 UTC727
    if input.contains(char::is_whitespace) {
9c05b1dd327aatagen2026-10-08 11:27 UTC728
        return invalid("it has spaces in it");
9c05b1dd327aatagen2026-10-08 11:27 UTC729
    }
9c05b1dd327aatagen2026-10-08 11:27 UTC730
    if !input.contains("://") {
9c05b1dd327aatagen2026-10-08 11:27 UTC731
        return match <&ruma::ServerName>::try_from(input) {
9c05b1dd327aatagen2026-10-08 11:27 UTC732
            Ok(_) => Ok(input),
9c05b1dd327aatagen2026-10-08 11:27 UTC733
            Err(_) => invalid("it's neither a server name nor a url"),
9c05b1dd327aatagen2026-10-08 11:27 UTC734
        };
9c05b1dd327aatagen2026-10-08 11:27 UTC735
    }
9c05b1dd327aatagen2026-10-08 11:27 UTC736
    let Ok(url) = Url::parse(input) else {
9c05b1dd327aatagen2026-10-08 11:27 UTC737
        return invalid("it isn't a valid url");
9c05b1dd327aatagen2026-10-08 11:27 UTC738
    };
9c05b1dd327aatagen2026-10-08 11:27 UTC739
    if !url.username().is_empty() || url.password().is_some() {
9c05b1dd327aatagen2026-10-08 11:27 UTC740
        return invalid("it has a username in it");
9c05b1dd327aatagen2026-10-08 11:27 UTC741
    }
9c05b1dd327aatagen2026-10-08 11:27 UTC742
    let loopback = match url.host() {
9c05b1dd327aatagen2026-10-08 11:27 UTC743
        None => return invalid("it has no host"),
9c05b1dd327aatagen2026-10-08 11:27 UTC744
        Some(url::Host::Domain(domain)) => domain.eq_ignore_ascii_case("localhost"),
9c05b1dd327aatagen2026-10-08 11:27 UTC745
        Some(url::Host::Ipv4(address)) => address.is_loopback(),
9c05b1dd327aatagen2026-10-08 11:27 UTC746
        Some(url::Host::Ipv6(address)) => address.is_loopback(),
9c05b1dd327aatagen2026-10-08 11:27 UTC747
    };
9c05b1dd327aatagen2026-10-08 11:27 UTC748
    match url.scheme() {
9c05b1dd327aatagen2026-10-08 11:27 UTC749
        "https" => Ok(input),
9c05b1dd327aatagen2026-10-08 11:27 UTC750
        "http" if allow_insecure_loopback && loopback => Ok(input),
9c05b1dd327aatagen2026-10-08 11:27 UTC751
        "http" if allow_insecure_loopback => invalid("plain http only reaches loopback hosts"),
9c05b1dd327aatagen2026-10-08 11:27 UTC752
        "http" => invalid("it isn't https"),
9c05b1dd327aatagen2026-10-08 11:27 UTC753
        _ => invalid("only http and https are spoken"),
9c05b1dd327aatagen2026-10-08 11:27 UTC754
    }
9c05b1dd327aatagen2026-10-08 11:27 UTC755
}
9c05b1dd327aatagen2026-10-08 11:27 UTC756
713205a95af2atagen2026-09-26 09:24 UTC757
pub(crate) fn account_error(
96fdf90aacd6atagen2026-09-08 08:24 UTC758
    context: &'static str,
62fe32759529atagen2026-09-26 08:50 UTC759
    source: impl std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static,
96fdf90aacd6atagen2026-09-08 08:24 UTC760
) -> AccountError {
96fdf90aacd6atagen2026-09-08 08:24 UTC761
    AccountError::Operation {
96fdf90aacd6atagen2026-09-08 08:24 UTC762
        context,
9a25b6ce66f4atagen2026-10-08 11:15 UTC763
        kind: crate::error::classify(&source),
9a25b6ce66f4atagen2026-10-08 11:15 UTC764
        server: ServerError::find(&source),
96fdf90aacd6atagen2026-09-08 08:24 UTC765
        source: Box::new(source),
96fdf90aacd6atagen2026-09-08 08:24 UTC766
    }
96fdf90aacd6atagen2026-09-08 08:24 UTC767
}