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