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; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 3 | use std::{collections::HashMap, sync::Arc}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | use matrix_sdk::{ |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 6 | Client, ClientBuilder, SendOutsideWasm, SessionMeta, SyncOutsideWasm, ThreadingSupport, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | authentication::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | AuthSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | matrix::MatrixSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | oauth::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | ClientId, ClientRegistrationData, CsrfToken, OAuthSession, UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | registration::{ApplicationType, ClientMetadata, Localized, OAuthGrantType}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | }, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 15 | cross_process_lock::CrossProcessLockConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | encryption::{BackupDownloadStrategy, EncryptionSettings}, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 17 | ruma::{OwnedDeviceId, OwnedUserId, serde::Raw}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | use serde::{Deserialize, Serialize}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | use thiserror::Error; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | use url::Url; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 23 | use crate::{ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 24 | ClientHandle, Engine, EngineConfig, EngineError, EventStream, SessionStore, error::SourceError, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 25 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | pub struct Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | pub struct PasswordLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | pub username: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | pub password: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | pub device_display_name: Option<String>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 37 | /// set after a soft logout so the store's keys still belong to us |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 38 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | impl PasswordLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | username: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | password: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | username: username.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | password: password.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | device_display_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | device_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | pub fn with_device_display_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | self.device_display_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | pub struct OAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | pub redirect_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | pub client_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | pub client_name: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | pub client_id: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | pub application_type: OAuthApplicationType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | impl OAuthLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | redirect_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | client_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | application_type: OAuthApplicationType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | redirect_uri: redirect_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | client_uri: client_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | client_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | client_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | device_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | application_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | pub fn with_client_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | self.client_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | /// a statically registered client id, skipping dynamic registration |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | self.client_id = Some(client_id.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | self |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | pub enum OAuthApplicationType { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | pub struct PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | client: Client, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 129 | sessions: Arc<dyn SessionStore>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | authorization_url: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | state: Option<CsrfToken>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | impl PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | pub fn authorization_url(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | &self.authorization_url |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | pub fn registered_client_id(&self) -> String { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | self.client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | .client_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | .expect("OAuth client was registered before authorization") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | .as_str() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | .to_owned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | pub const fn is_pending(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | self.state.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | pub async fn finish(&mut self, callback_url: &str) -> Result<Session, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | let callback_url = Url::parse(callback_url) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | .map_err(|error| account_error("parsing the OAuth callback URL", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | let state = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | .state |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | let oauth = self.client.oauth(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | if let Err(error) = oauth.finish_login(callback_url.into()).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | oauth.abort_login(&state).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | return Err(account_error("OAuth login", error)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 167 | Session::saved(self.client.clone(), &*self.sessions).await |
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 | pub async fn abort(mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | if let Some(state) = self.state.take() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | self.client.oauth().abort_login(&state).await; |
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 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | pub struct StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 178 | location: StoreLocation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | passphrase: String, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 180 | lock_holder: Option<String>, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 181 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 182 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 183 | enum StoreLocation { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 184 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 185 | Sqlite(PathBuf), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 186 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 187 | IndexedDb(String), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | impl StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 191 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 192 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 193 | pub fn sqlite(path: impl Into<PathBuf>, passphrase: impl Into<String>) -> Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 194 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 195 | location: StoreLocation::Sqlite(path.into()), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 196 | passphrase: passphrase.into(), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 197 | lock_holder: None, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 198 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 199 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 200 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 201 | #[cfg(feature = "indexeddb")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 203 | pub fn indexeddb(name: impl Into<String>, passphrase: impl Into<String>) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 205 | location: StoreLocation::IndexedDb(name.into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | passphrase: passphrase.into(), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 207 | lock_holder: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 211 | /// the sdk names every client `main`, so two tabs on one store would both think they hold its lock |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 213 | pub fn with_lock_holder(mut self, holder: impl Into<String>) -> Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 214 | self.lock_holder = Some(holder.into()); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 215 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 217 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 218 | /// delete the store's databases, once nothing has them open |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 219 | pub async fn delete(&self) -> Result<(), AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 220 | match &self.location { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 221 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 222 | StoreLocation::Sqlite(path) => crate::session_store::delete_sqlite_store(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 223 | .map_err(|error| account_error("deleting the Matrix store", error)), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 224 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 225 | StoreLocation::IndexedDb(name) => crate::session_store::delete_indexeddb_store(name) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 226 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 227 | .map_err(|error| account_error("deleting the Matrix store", error)), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 228 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 229 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 230 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 231 | pub(crate) fn passphrase(&self) -> &str { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 232 | &self.passphrase |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 233 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 234 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 235 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 236 | pub(crate) fn indexeddb_name(&self) -> &str { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 237 | let StoreLocation::IndexedDb(name) = &self.location; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 238 | name |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 239 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 242 | /// Serializable authentication secrets. A `SessionStore` keeps it, and it must |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 243 | /// be replaced when `Event::SessionTokensChanged` arrives. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | pub struct SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | authentication: AuthenticationRecord, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | #[serde(tag = "type", content = "session", rename_all = "snake_case")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | enum AuthenticationRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | Matrix(MatrixSession), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | OAuth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | client_id: ClientId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | user: UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | }, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 258 | SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 259 | meta: SessionMeta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 260 | client_id: Option<ClientId>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 261 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | impl SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | pub fn homeserver(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | &self.homeserver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 269 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 270 | pub(crate) fn soft_logged_out(self) -> Self { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 271 | let authentication = match self.authentication { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 272 | AuthenticationRecord::Matrix(session) => AuthenticationRecord::SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 273 | meta: session.meta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 274 | client_id: None, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 275 | }, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 276 | AuthenticationRecord::OAuth { client_id, user } => { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 277 | AuthenticationRecord::SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 278 | meta: user.meta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 279 | client_id: Some(client_id), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 280 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 281 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 282 | soft @ AuthenticationRecord::SoftLoggedOut { .. } => soft, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 283 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 284 | Self { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 285 | homeserver: self.homeserver, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 286 | authentication, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 287 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 288 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 289 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 290 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 291 | pub enum Restore { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 292 | Resumed(Session), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 293 | SoftLoggedOut(SoftLogout), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 294 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 295 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 296 | /// 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 | 297 | /// as any other device would orphan them |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 298 | #[derive(Clone, Debug)] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 299 | #[non_exhaustive] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 300 | pub struct SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 301 | pub homeserver: String, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 302 | pub user_id: OwnedUserId, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 303 | pub device_id: OwnedDeviceId, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 304 | /// set when the login was OAuth, for `OAuthLogin::with_client_id` |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 305 | pub client_id: Option<String>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 306 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 307 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 308 | impl SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 309 | #[must_use] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 310 | pub fn password_login(&self, password: impl Into<String>) -> PasswordLogin { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 311 | PasswordLogin::new(self.homeserver.clone(), self.user_id.as_str(), password) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 312 | .with_device_id(self.device_id.clone()) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 313 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | pub enum AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | #[error("{context} failed")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | context: &'static str, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | #[source] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 322 | source: Box<SourceError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | #[error("the homeserver returned no authenticated session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | MissingSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | #[error("the SDK returned an unsupported authentication session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | UnsupportedAuthentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | #[error("the OAuth callback was already submitted")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | OAuthCallbackAlreadySubmitted, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | #[error(transparent)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | Engine(#[from] EngineError), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | impl Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | pub async fn login_password( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | login: PasswordLogin, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 337 | sessions: &dyn SessionStore, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | ) -> Result<Self, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 339 | let client = matrix_client_builder(sessions.store()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | .map_err(|error| account_error("building the Matrix client", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | let mut request = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | .matrix_auth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | .login_username(login.username.trim(), &login.password) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | .request_refresh_token(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | if let Some(name) = login.device_display_name.as_deref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | request = request.initial_device_display_name(name); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 351 | if let Some(device_id) = login.device_id.as_deref() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 352 | request = request.device_id(device_id.as_str()); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 353 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | .send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | .map_err(|error| account_error("password login", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 358 | Self::saved(client, sessions).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | pub async fn begin_oauth( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | login: OAuthLogin, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 363 | sessions: Arc<dyn SessionStore>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | ) -> Result<PendingOAuthLogin, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | let redirect_uri = Url::parse(login.redirect_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | .map_err(|error| account_error("parsing the OAuth redirect URI", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | let client_uri = Url::parse(login.client_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | .map_err(|error| account_error("parsing the OAuth client URI", error))?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 369 | let client = matrix_client_builder(sessions.store()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | .map_err(|error| account_error("building the Matrix client", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | let application_type = match login.application_type { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | OAuthApplicationType::Native => ApplicationType::Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | OAuthApplicationType::Web => ApplicationType::Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | let mut metadata = ClientMetadata::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | application_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | vec![OAuthGrantType::AuthorizationCode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | redirect_uris: vec![redirect_uri.clone()], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | }], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | Localized::new(client_uri, []), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | metadata.client_name = login.client_name.map(|name| Localized::new(name, [])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | let metadata = Raw::new(&metadata) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | .map_err(|error| account_error("serializing OAuth client metadata", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | let mut registration = ClientRegistrationData::new(metadata); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | if let Some(client_id) = login.client_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | registration.static_registrations = Some(HashMap::from([( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | client.homeserver(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | ClientId::new(client_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | )])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | let authorization = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | .login(redirect_uri, login.device_id, Some(registration), None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | .map_err(|error| account_error("starting OAuth login", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | Ok(PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | client, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 403 | sessions, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | authorization_url: authorization.url.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | state: Some(authorization.state), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 408 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 409 | /// Restore the record `sessions` holds, or `None` when it holds none. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | /// The caller must also handle refresh, rejection, and explicit logout. |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 411 | pub async fn restore(sessions: &dyn SessionStore) -> Result<Option<Restore>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 412 | let Some(record) = sessions.load().await? else { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 413 | return Ok(None); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 414 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 415 | let authentication = match record.authentication { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | AuthenticationRecord::Matrix(session) => AuthSession::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | AuthenticationRecord::OAuth { client_id, user } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | OAuthSession { client_id, user }.into() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 420 | AuthenticationRecord::SoftLoggedOut { meta, client_id } => { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 421 | return Ok(Some(Restore::SoftLoggedOut(SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 422 | homeserver: record.homeserver, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 423 | user_id: meta.user_id, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 424 | device_id: meta.device_id, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 425 | client_id: client_id.map(|client_id| client_id.as_str().to_owned()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 426 | }))); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 427 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 428 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 429 | let client = matrix_client_builder(sessions.store()) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 430 | .homeserver_url(&record.homeserver) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 431 | .build() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 432 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 433 | .map_err(|error| account_error("reopening the Matrix store", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | .restore_session(authentication) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | .map_err(|error| account_error("restoring the authenticated session", error))?; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 438 | Ok(Some(Restore::Resumed(Self { client }))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | pub async fn build( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | config: EngineConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | ) -> Result<(Engine, ClientHandle, EventStream), AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | Engine::from_client(self.client, config) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | .map_err(AccountError::from) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 450 | async fn saved(client: Client, sessions: &dyn SessionStore) -> Result<Self, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 451 | sessions.save(&session_record(&client)?).await?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 452 | Ok(Self { client }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 453 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 454 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 455 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 456 | pub(crate) fn session_record(client: &Client) -> Result<SessionRecord, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 457 | let authentication = match client.session().ok_or(AccountError::MissingSession)? { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 458 | AuthSession::Matrix(session) => AuthenticationRecord::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 459 | AuthSession::OAuth(session) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 460 | let OAuthSession { client_id, user } = *session; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 461 | AuthenticationRecord::OAuth { client_id, user } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 462 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | _ => return Err(AccountError::UnsupportedAuthentication), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | Ok(SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | homeserver: client.homeserver().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | authentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 471 | fn encryption_settings() -> EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 472 | EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 473 | auto_enable_cross_signing: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | backup_download_strategy: BackupDownloadStrategy::OneShot, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | auto_enable_backups: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 477 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | fn matrix_client_builder(store: &StoreConfig) -> ClientBuilder { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 480 | let mut builder = Client::builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | .with_encryption_settings(encryption_settings()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | .with_threading_support(ThreadingSupport::Enabled { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | with_subscriptions: false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 484 | }) |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 485 | .handle_refresh_tokens(); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 486 | if let Some(holder) = &store.lock_holder { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 487 | builder = builder.cross_process_store_config(CrossProcessLockConfig::multi_process(holder)); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 488 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 489 | match &store.location { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 490 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 491 | StoreLocation::Sqlite(path) => builder.sqlite_store(path, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 492 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 493 | StoreLocation::IndexedDb(name) => builder.indexeddb_store(name, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 494 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 496 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 497 | pub(crate) fn account_error( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | context: &'static str, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 499 | source: impl std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 500 | ) -> AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | AccountError::Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | context, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | source: Box::new(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | } |