96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | use std::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2 | collections::HashMap, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | path::{Path, PathBuf}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | Client, ClientBuilder, ThreadingSupport, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | authentication::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | AuthSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | matrix::MatrixSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | oauth::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | ClientId, ClientRegistrationData, CsrfToken, OAuthSession, UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | registration::{ApplicationType, ClientMetadata, Localized, OAuthGrantType}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | }, |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | use crate::{ClientHandle, Engine, EngineConfig, EngineError, EventStream}; |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | pub struct PasswordLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | pub username: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | pub password: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | pub device_display_name: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | pub struct OAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | pub redirect_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | pub client_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | pub client_name: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | pub client_id: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | pub application_type: OAuthApplicationType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | pub enum OAuthApplicationType { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | pub struct PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | authorization_url: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | state: Option<CsrfToken>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | impl PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | pub fn authorization_url(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | &self.authorization_url |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | pub fn registered_client_id(&self) -> String { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | self.client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | .client_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | .expect("OAuth client was registered before authorization") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | .as_str() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | .to_owned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | pub const fn is_pending(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | self.state.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | pub async fn finish(&mut self, callback_url: &str) -> Result<Session, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | let callback_url = Url::parse(callback_url) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | .map_err(|error| account_error("parsing the OAuth callback URL", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | let state = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | .state |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | let oauth = self.client.oauth(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | if let Err(error) = oauth.finish_login(callback_url.into()).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | oauth.abort_login(&state).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | return Err(account_error("OAuth login", error)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | Session::from_client(self.client.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | pub async fn abort(mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | if let Some(state) = self.state.take() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | self.client.oauth().abort_login(&state).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | pub struct StoreConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | path: PathBuf, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | passphrase: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | impl StoreConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | pub fn new(path: impl Into<PathBuf>, passphrase: impl Into<String>) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | path: path.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | passphrase: passphrase.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | pub fn path(&self) -> &Path { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | &self.path |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | /// Serializable authentication secrets. Store this separately from the |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | /// encrypted SQLite store in protected, preferably encrypted storage. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | /// A saved record must be replaced when `Event::SessionTokensChanged` arrives. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | pub struct SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | authentication: AuthenticationRecord, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | #[serde(tag = "type", content = "session", rename_all = "snake_case")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | enum AuthenticationRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | Matrix(MatrixSession), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | OAuth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | client_id: ClientId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | user: UserSession, |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | impl SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | pub fn homeserver(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | &self.homeserver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | } |
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 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | pub enum AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | #[error("{context} failed")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | context: &'static str, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | #[source] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | source: Box<dyn std::error::Error + Send + Sync>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | #[error("the homeserver returned no authenticated session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | MissingSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | #[error("the SDK returned an unsupported authentication session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | UnsupportedAuthentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | #[error("the OAuth callback was already submitted")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | OAuthCallbackAlreadySubmitted, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | #[error(transparent)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | Engine(#[from] EngineError), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | impl Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | pub async fn login_password( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | login: PasswordLogin, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | store: StoreConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | ) -> Result<Self, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | let client = matrix_client_builder(&store) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | .map_err(|error| account_error("building the Matrix client", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | let mut request = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | .matrix_auth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | .login_username(login.username.trim(), &login.password) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | .request_refresh_token(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | if let Some(name) = login.device_display_name.as_deref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | request = request.initial_device_display_name(name); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | .send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 186 | .map_err(|error| account_error("password login", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | Self::from_client(client) |
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 | pub async fn begin_oauth( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | login: OAuthLogin, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | store: StoreConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | ) -> Result<PendingOAuthLogin, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | let redirect_uri = Url::parse(login.redirect_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | .map_err(|error| account_error("parsing the OAuth redirect URI", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | let client_uri = Url::parse(login.client_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | .map_err(|error| account_error("parsing the OAuth client URI", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | let client = matrix_client_builder(&store) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | .server_name_or_homeserver_url(login.homeserver.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | .map_err(|error| account_error("building the Matrix client", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | let application_type = match login.application_type { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | OAuthApplicationType::Native => ApplicationType::Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | OAuthApplicationType::Web => ApplicationType::Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | let mut metadata = ClientMetadata::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | application_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | vec![OAuthGrantType::AuthorizationCode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | redirect_uris: vec![redirect_uri.clone()], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | }], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | Localized::new(client_uri, []), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | metadata.client_name = login.client_name.map(|name| Localized::new(name, [])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | let metadata = Raw::new(&metadata) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | .map_err(|error| account_error("serializing OAuth client metadata", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | let mut registration = ClientRegistrationData::new(metadata); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | if let Some(client_id) = login.client_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | registration.static_registrations = Some(HashMap::from([( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | client.homeserver(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | ClientId::new(client_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | )])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | let authorization = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | .login(redirect_uri, login.device_id, Some(registration), None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | .map_err(|error| account_error("starting OAuth login", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | Ok(PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | authorization_url: authorization.url.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | state: Some(authorization.state), |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | /// Restore a saved record with the same store location and passphrase. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | /// The caller must also handle refresh, rejection, and explicit logout. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 239 | pub async fn restore(record: SessionRecord, store: StoreConfig) -> Result<Self, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | let client = matrix_client_builder(&store) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | .homeserver_url(&record.homeserver) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | .map_err(|error| account_error("reopening the Matrix store", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | let authentication = match record.authentication.clone() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | AuthenticationRecord::Matrix(session) => AuthSession::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | AuthenticationRecord::OAuth { client_id, user } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | OAuthSession { client_id, user }.into() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | .restore_session(authentication) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | .map_err(|error| account_error("restoring the authenticated session", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | Ok(Self { client, record }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | /// Initial login credentials only. This snapshot does not track later |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | /// token rotations; use `ClientHandle::session_record` after building. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | pub fn record(&self) -> SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | self.record.clone() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | pub async fn build( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | config: EngineConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | ) -> Result<(Engine, ClientHandle, EventStream), AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | Engine::from_client(self.client, config) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | .map_err(AccountError::from) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | fn from_client(client: Client) -> Result<Self, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | let record = session_record(&client)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | Ok(Self { client, record }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 279 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 280 | pub(crate) fn session_record(client: &Client) -> Result<SessionRecord, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | let authentication = match client.session().ok_or(AccountError::MissingSession)? { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | AuthSession::Matrix(session) => AuthenticationRecord::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | AuthSession::OAuth(session) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | let OAuthSession { client_id, user } = *session; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | AuthenticationRecord::OAuth { client_id, user } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | _ => return Err(AccountError::UnsupportedAuthentication), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | Ok(SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | homeserver: client.homeserver().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | authentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 293 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 294 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 295 | fn encryption_settings() -> EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | auto_enable_cross_signing: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 298 | backup_download_strategy: BackupDownloadStrategy::OneShot, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | auto_enable_backups: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | fn matrix_client_builder(store: &StoreConfig) -> ClientBuilder { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | Client::builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 305 | .with_encryption_settings(encryption_settings()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 306 | .with_threading_support(ThreadingSupport::Enabled { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | with_subscriptions: false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | .handle_refresh_tokens() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | .sqlite_store(&store.path, Some(&store.passphrase)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 311 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | fn account_error( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | context: &'static str, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | source: impl std::error::Error + Send + Sync + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | ) -> AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | AccountError::Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | context, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | source: Box::new(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | } |