62fe32759529 | atagen | 2026-09-26 08:50 UTC | 1 | use std::collections::HashMap; |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 2 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 3 | use std::path::PathBuf; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | use matrix_sdk::{ |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 6 | Client, ClientBuilder, SendOutsideWasm, 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}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | ruma::{OwnedDeviceId, 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 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 23 | use crate::{ClientHandle, Engine, EngineConfig, EngineError, EventStream, error::SourceError}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | pub struct Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | record: SessionRecord, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | pub struct PasswordLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | pub username: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | pub password: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | pub device_display_name: Option<String>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 36 | /// set after a soft logout so the store's keys still belong to us |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 37 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | impl PasswordLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | username: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | password: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | username: username.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | password: password.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | device_display_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | device_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | } |
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 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | pub fn with_device_display_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | self.device_display_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | } |
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 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | pub struct OAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | pub redirect_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | pub client_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | pub client_name: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | pub client_id: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | pub application_type: OAuthApplicationType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | impl OAuthLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | redirect_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | client_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | application_type: OAuthApplicationType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | redirect_uri: redirect_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | client_uri: client_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | client_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | client_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | device_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | application_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | pub fn with_client_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | self.client_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | /// a statically registered client id, skipping dynamic registration |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | self.client_id = Some(client_id.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | pub enum OAuthApplicationType { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | pub struct PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | authorization_url: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | state: Option<CsrfToken>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | impl PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | pub fn authorization_url(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | &self.authorization_url |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | pub fn registered_client_id(&self) -> String { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | self.client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | .client_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | .expect("OAuth client was registered before authorization") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | .as_str() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | .to_owned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | pub const fn is_pending(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | self.state.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | pub async fn finish(&mut self, callback_url: &str) -> Result<Session, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | let callback_url = Url::parse(callback_url) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | .map_err(|error| account_error("parsing the OAuth callback URL", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | let state = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | .state |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | let oauth = self.client.oauth(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | if let Err(error) = oauth.finish_login(callback_url.into()).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | oauth.abort_login(&state).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | return Err(account_error("OAuth login", error)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | Session::from_client(self.client.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | pub async fn abort(mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | if let Some(state) = self.state.take() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | self.client.oauth().abort_login(&state).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | pub struct StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 176 | location: StoreLocation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | passphrase: String, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 178 | lock_holder: Option<String>, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 179 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 180 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 181 | enum StoreLocation { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 182 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 183 | Sqlite(PathBuf), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 184 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 185 | IndexedDb(String), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 186 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | impl StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 189 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 190 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 191 | pub fn sqlite(path: impl Into<PathBuf>, passphrase: impl Into<String>) -> Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 192 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 193 | location: StoreLocation::Sqlite(path.into()), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 194 | passphrase: passphrase.into(), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 195 | lock_holder: None, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 196 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 197 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 198 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 199 | #[cfg(feature = "indexeddb")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 201 | pub fn indexeddb(name: impl Into<String>, passphrase: impl Into<String>) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 203 | location: StoreLocation::IndexedDb(name.into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | passphrase: passphrase.into(), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 205 | lock_holder: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 209 | /// 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 | 210 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 211 | pub fn with_lock_holder(mut self, holder: impl Into<String>) -> Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 212 | self.lock_holder = Some(holder.into()); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 213 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | /// Serializable authentication secrets. Store this separately from the |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 218 | /// encrypted store in protected, preferably encrypted storage. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | /// A saved record must be replaced when `Event::SessionTokensChanged` arrives. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | pub struct SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | authentication: AuthenticationRecord, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | #[serde(tag = "type", content = "session", rename_all = "snake_case")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | enum AuthenticationRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | Matrix(MatrixSession), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | OAuth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | client_id: ClientId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | user: UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | impl SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | pub fn homeserver(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 239 | &self.homeserver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | pub enum AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | #[error("{context} failed")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | context: &'static str, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | #[source] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 249 | source: Box<SourceError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | #[error("the homeserver returned no authenticated session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | MissingSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | #[error("the SDK returned an unsupported authentication session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | UnsupportedAuthentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | #[error("the OAuth callback was already submitted")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | OAuthCallbackAlreadySubmitted, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | #[error(transparent)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | Engine(#[from] EngineError), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | impl Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | pub async fn login_password( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | login: PasswordLogin, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | store: StoreConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | ) -> Result<Self, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | let client = matrix_client_builder(&store) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | .map_err(|error| account_error("building the Matrix client", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | let mut request = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | .matrix_auth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | .login_username(login.username.trim(), &login.password) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | .request_refresh_token(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | if let Some(name) = login.device_display_name.as_deref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | request = request.initial_device_display_name(name); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 278 | if let Some(device_id) = login.device_id.as_deref() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 279 | request = request.device_id(device_id.as_str()); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 280 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | .send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | .map_err(|error| account_error("password login", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | Self::from_client(client) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | pub async fn begin_oauth( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | login: OAuthLogin, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | store: StoreConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | ) -> Result<PendingOAuthLogin, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | let redirect_uri = Url::parse(login.redirect_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 293 | .map_err(|error| account_error("parsing the OAuth redirect URI", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 294 | let client_uri = Url::parse(login.client_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 295 | .map_err(|error| account_error("parsing the OAuth client URI", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | let client = matrix_client_builder(&store) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 298 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | .map_err(|error| account_error("building the Matrix client", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | let application_type = match login.application_type { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | OAuthApplicationType::Native => ApplicationType::Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | OAuthApplicationType::Web => ApplicationType::Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 305 | let mut metadata = ClientMetadata::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 306 | application_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | vec![OAuthGrantType::AuthorizationCode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | redirect_uris: vec![redirect_uri.clone()], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | }], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | Localized::new(client_uri, []), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 311 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | metadata.client_name = login.client_name.map(|name| Localized::new(name, [])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | let metadata = Raw::new(&metadata) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | .map_err(|error| account_error("serializing OAuth client metadata", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | let mut registration = ClientRegistrationData::new(metadata); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | if let Some(client_id) = login.client_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | registration.static_registrations = Some(HashMap::from([( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | client.homeserver(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | ClientId::new(client_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | )])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | let authorization = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | .login(redirect_uri, login.device_id, Some(registration), None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | .map_err(|error| account_error("starting OAuth login", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | Ok(PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | authorization_url: authorization.url.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | state: Some(authorization.state), |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | /// Restore a saved record with the same store location and passphrase. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | /// The caller must also handle refresh, rejection, and explicit logout. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | pub async fn restore(record: SessionRecord, store: StoreConfig) -> Result<Self, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | let client = matrix_client_builder(&store) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | .homeserver_url(&record.homeserver) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | .map_err(|error| account_error("reopening the Matrix store", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | let authentication = match record.authentication.clone() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | AuthenticationRecord::Matrix(session) => AuthSession::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | AuthenticationRecord::OAuth { client_id, user } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | OAuthSession { client_id, user }.into() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | .restore_session(authentication) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | .map_err(|error| account_error("restoring the authenticated session", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | Ok(Self { client, record }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | /// Initial login credentials only. This snapshot does not track later |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | /// token rotations; use `ClientHandle::session_record` after building. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | pub fn record(&self) -> SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | self.record.clone() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | pub async fn build( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | config: EngineConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | ) -> Result<(Engine, ClientHandle, EventStream), AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | Engine::from_client(self.client, config) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 369 | .map_err(AccountError::from) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | fn from_client(client: Client) -> Result<Self, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | let record = session_record(&client)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | Ok(Self { client, record }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | pub(crate) fn session_record(client: &Client) -> Result<SessionRecord, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | let authentication = match client.session().ok_or(AccountError::MissingSession)? { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | AuthSession::Matrix(session) => AuthenticationRecord::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | AuthSession::OAuth(session) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | let OAuthSession { client_id, user } = *session; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | AuthenticationRecord::OAuth { client_id, user } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | _ => return Err(AccountError::UnsupportedAuthentication), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | Ok(SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | homeserver: client.homeserver().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | authentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | fn encryption_settings() -> EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | auto_enable_cross_signing: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | backup_download_strategy: BackupDownloadStrategy::OneShot, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | auto_enable_backups: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | fn matrix_client_builder(store: &StoreConfig) -> ClientBuilder { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 402 | let mut builder = Client::builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | .with_encryption_settings(encryption_settings()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | .with_threading_support(ThreadingSupport::Enabled { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | with_subscriptions: false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | }) |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 407 | .handle_refresh_tokens(); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 408 | if let Some(holder) = &store.lock_holder { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 409 | builder = builder.cross_process_store_config(CrossProcessLockConfig::multi_process(holder)); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 410 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 411 | match &store.location { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 412 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 413 | StoreLocation::Sqlite(path) => builder.sqlite_store(path, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 414 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 415 | StoreLocation::IndexedDb(name) => builder.indexeddb_store(name, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 416 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | fn account_error( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | context: &'static str, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 421 | source: impl std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | ) -> AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | AccountError::Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | context, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 425 | source: Box::new(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 426 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 427 | } |