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; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 3 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 4 | use std::sync::atomic::{AtomicBool, Ordering}; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 5 | use std::{collections::HashMap, sync::Arc}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | use matrix_sdk::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 8 | Client, ClientBuilder, SendOutsideWasm, SessionChange, SessionMeta, SyncOutsideWasm, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 9 | ThreadingSupport, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | authentication::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | AuthSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | matrix::MatrixSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | oauth::{ |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 14 | ClientId, ClientRegistrationData, CsrfToken, OAuthSession, PendingAuthorization, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 15 | UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | registration::{ApplicationType, ClientMetadata, Localized, OAuthGrantType}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | }, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 19 | cross_process_lock::CrossProcessLockConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | encryption::{BackupDownloadStrategy, EncryptionSettings}, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 21 | ruma::{MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId, serde::Raw}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | use serde::{Deserialize, Serialize}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | use thiserror::Error; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 25 | use tokio::sync::{broadcast, mpsc}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | use url::Url; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 28 | use crate::{ |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 29 | ClientHandle, Engine, EngineConfig, EngineError, ErrorKind, EventStream, ServerError, |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 30 | SessionStore, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 31 | engine::{Persistence, StoreLock}, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 32 | error::SourceError, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 33 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 35 | /// a logged in client and the `SessionStore` its record lives in. the engine it builds |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 36 | /// saves every token rotation there, unless told `without_persistence` |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | pub struct Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | client: Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 39 | tracking: Tracking, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 40 | store_lock: StoreLock, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 41 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 42 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 43 | /// taken as soon as the client exists, since the sdk can refresh before there's an engine |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 44 | struct Tracking { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 45 | session_changes: broadcast::Receiver<SessionChange>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 46 | persistence: Option<Persistence>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 47 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 48 | inline: Arc<AtomicBool>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | pub struct PasswordLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | pub username: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | pub password: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | pub device_display_name: Option<String>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 57 | /// set after a soft logout so the store's keys still belong to us |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 58 | pub device_id: Option<OwnedDeviceId>, |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 59 | /// let plain http through, for loopback hosts only |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 60 | pub allow_insecure_loopback: bool, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | impl PasswordLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | username: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | password: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | username: username.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | password: password.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | device_display_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | device_id: None, |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 76 | allow_insecure_loopback: false, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | pub fn with_device_display_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | self.device_display_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 91 | |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 92 | #[must_use] |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 93 | pub fn allow_insecure_loopback(mut self, allow: bool) -> Self { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 94 | self.allow_insecure_loopback = allow; |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 95 | self |
9c05b1dd327a | atagen | 2026-10-08 11:27 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 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | pub struct OAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | pub homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | pub redirect_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | pub client_uri: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | pub client_name: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | pub client_id: Option<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | pub device_id: Option<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | pub application_type: OAuthApplicationType, |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 108 | /// let plain http through, for loopback hosts only |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 109 | pub allow_insecure_loopback: bool, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | impl OAuthLogin { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | pub fn new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | homeserver: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | redirect_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | client_uri: impl Into<String>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | application_type: OAuthApplicationType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | ) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | homeserver: homeserver.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | redirect_uri: redirect_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | client_uri: client_uri.into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | client_name: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | client_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | device_id: None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | application_type, |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 128 | allow_insecure_loopback: false, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | pub fn with_client_name(mut self, name: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | self.client_name = Some(name.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | /// a statically registered client id, skipping dynamic registration |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | self.client_id = Some(client_id.into()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | pub fn with_device_id(mut self, device_id: OwnedDeviceId) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | self.device_id = Some(device_id); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 150 | |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 151 | #[must_use] |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 152 | pub fn allow_insecure_loopback(mut self, allow: bool) -> Self { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 153 | self.allow_insecure_loopback = allow; |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 154 | self |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 155 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | pub enum OAuthApplicationType { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | pub struct PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | client: Client, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 167 | sessions: Arc<dyn SessionStore>, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 168 | tracking: Option<(Tracking, StoreLock)>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | authorization_url: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | state: Option<CsrfToken>, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 171 | previous: Option<SessionRecord>, |
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 | impl PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | pub fn authorization_url(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | &self.authorization_url |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | pub fn registered_client_id(&self) -> String { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | self.client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | .client_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | .expect("OAuth client was registered before authorization") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 186 | .as_str() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | .to_owned() |
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 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | pub const fn is_pending(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | self.state.is_some() |
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 finish(&mut self, callback_url: &str) -> Result<Session, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | let callback_url = Url::parse(callback_url) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | .map_err(|error| account_error("parsing the OAuth callback URL", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | let state = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | .state |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | let oauth = self.client.oauth(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | if let Err(error) = oauth.finish_login(callback_url.into()).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | oauth.abort_login(&state).await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 205 | if let Some(previous) = self.previous.take() { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 206 | put_back(self.sessions.as_ref(), previous).await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 207 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | return Err(account_error("OAuth login", error)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 210 | let (tracking, store_lock) = self |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 211 | .tracking |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 212 | .take() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 213 | .ok_or(AccountError::OAuthCallbackAlreadySubmitted)?; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 214 | Session::saved( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 215 | self.client.clone(), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 216 | self.sessions.clone(), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 217 | tracking, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 218 | store_lock, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 219 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 220 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 223 | /// puts back what the store held before, or deletes the store if it held nothing |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 224 | pub async fn abort(self) { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 225 | let Self { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 226 | client, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 227 | sessions, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 228 | tracking, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 229 | state, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 230 | previous, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 231 | .. |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 232 | } = self; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 233 | let Some(state) = state else { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 234 | return; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 235 | }; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 236 | client.oauth().abort_login(&state).await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 237 | drop(client); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 238 | match previous { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 239 | Some(previous) => put_back(sessions.as_ref(), previous).await, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 240 | None => { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 241 | if let Err(error) = sessions.discard().await { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 242 | tracing::warn!(%error, "deleting an abandoned OAuth login's store failed"); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 243 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 244 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 246 | drop(tracking); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 247 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 248 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 249 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 250 | async fn put_back(sessions: &dyn SessionStore, previous: SessionRecord) { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 251 | if let Err(error) = sessions.save(&previous).await { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 252 | tracing::warn!(%error, "putting back the record an OAuth login replaced failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | } |
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 | pub struct StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 257 | location: StoreLocation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | passphrase: String, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 259 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 260 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 261 | enum StoreLocation { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 262 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 263 | Sqlite(PathBuf), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 264 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 265 | IndexedDb(String), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | impl StoreConfig { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 269 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 270 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 271 | pub fn sqlite(path: impl Into<PathBuf>, passphrase: impl Into<String>) -> Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 272 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 273 | location: StoreLocation::Sqlite(path.into()), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 274 | passphrase: passphrase.into(), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 275 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 276 | } |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 277 | |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 278 | #[cfg(feature = "indexeddb")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 279 | #[must_use] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 280 | pub fn indexeddb(name: impl Into<String>, passphrase: impl Into<String>) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | Self { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 282 | location: StoreLocation::IndexedDb(name.into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | passphrase: passphrase.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 287 | /// delete the store's databases, once nothing has them open |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 288 | pub async fn delete(&self) -> Result<(), AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 289 | match &self.location { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 290 | #[cfg(feature = "sqlite")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 291 | StoreLocation::Sqlite(path) => crate::session_store::delete_sqlite_store(path) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 292 | .map_err(|error| account_error("deleting the Matrix store", error)), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 293 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 294 | StoreLocation::IndexedDb(name) => crate::session_store::delete_indexeddb_store(name) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 295 | .await |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 296 | .map_err(|error| error.into_account_error("deleting the Matrix store")), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 297 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 298 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 299 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 300 | /// taken before the client opens the store, and held until the engine and every |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 301 | /// `ClientHandle` are gone |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 302 | pub(crate) async fn lock(&self) -> Result<StoreLock, AccountError> { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 303 | match &self.location { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 304 | #[cfg(feature = "sqlite")] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 305 | StoreLocation::Sqlite(path) => StoreLock::file(path), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 306 | #[cfg(feature = "indexeddb")] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 307 | StoreLocation::IndexedDb(name) => StoreLock::web(name).await, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 308 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 309 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 310 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 311 | pub(crate) fn passphrase(&self) -> &str { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 312 | &self.passphrase |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 313 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 314 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 315 | #[cfg(feature = "indexeddb")] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 316 | pub(crate) fn indexeddb_name(&self) -> &str { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 317 | let StoreLocation::IndexedDb(name) = &self.location; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 318 | name |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 319 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 322 | /// Serializable authentication secrets. A `SessionStore` keeps it, and the engine |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 323 | /// replaces it there on every token rotation. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | pub struct SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | homeserver: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | authentication: AuthenticationRecord, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | #[derive(Clone, Deserialize, Serialize)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | #[serde(tag = "type", content = "session", rename_all = "snake_case")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | enum AuthenticationRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | Matrix(MatrixSession), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | OAuth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | client_id: ClientId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | user: UserSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | }, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 338 | SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 339 | meta: SessionMeta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 340 | client_id: Option<ClientId>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 341 | }, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 342 | /// an oauth login waiting on its redirect. `authorization` holds the pkce verifier |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 343 | PendingOAuth { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 344 | client_id: ClientId, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 345 | state: CsrfToken, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 346 | authorization: Box<PendingAuthorization>, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 347 | started_ms: u64, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 348 | /// what the store held before, put back if the login never finishes |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 349 | previous: Option<Box<SessionRecord>>, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 350 | }, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 351 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 352 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 353 | /// authorization codes live for minutes, so a login this old was abandoned |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 354 | const OAUTH_PENDING_FOR_MS: u64 = 60 * 60 * 1000; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 355 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 356 | /// the authorization server answers in the query, or the fragment when asked to |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 357 | pub(crate) fn callback_state(callback: &Url) -> Option<String> { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 358 | let query = callback.query_pairs(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 359 | let fragment = url::form_urlencoded::parse(callback.fragment().unwrap_or_default().as_bytes()); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 360 | query |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 361 | .chain(fragment) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 362 | .find(|(key, _)| key == "state") |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 363 | .map(|(_, state)| state.into_owned()) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 364 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 365 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 366 | fn now_ms() -> u64 { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 367 | MilliSecondsSinceUnixEpoch::now().get().into() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 369 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | impl SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | pub fn homeserver(&self) -> &str { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | &self.homeserver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 375 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 376 | /// a pending oauth login is nobody yet, unless it resumes someone |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 377 | pub(crate) fn user_id(&self) -> Option<&OwnedUserId> { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 378 | match &self.authentication { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 379 | AuthenticationRecord::Matrix(session) => Some(&session.meta.user_id), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 380 | AuthenticationRecord::OAuth { user, .. } => Some(&user.meta.user_id), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 381 | AuthenticationRecord::SoftLoggedOut { meta, .. } => Some(&meta.user_id), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 382 | AuthenticationRecord::PendingOAuth { previous, .. } => previous.as_ref()?.user_id(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 383 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 384 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 385 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 386 | /// what a login should resume. a pending oauth login stands in for the record before it |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 387 | pub(crate) fn resumable(self) -> Option<Self> { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 388 | match self.authentication { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 389 | AuthenticationRecord::PendingOAuth { previous, .. } => { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 390 | previous.and_then(|previous| previous.resumable()) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 391 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 392 | _ => Some(self), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 393 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 394 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 395 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 396 | pub(crate) fn pending_oauth_state(&self) -> Option<&str> { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 397 | match &self.authentication { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 398 | AuthenticationRecord::PendingOAuth { state, .. } => Some(state.secret()), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 399 | _ => None, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 400 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 401 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 402 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 403 | pub(crate) fn oauth_expired(&self) -> bool { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 404 | let AuthenticationRecord::PendingOAuth { started_ms, .. } = &self.authentication else { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 405 | return false; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 406 | }; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 407 | now_ms().saturating_sub(*started_ms) > OAUTH_PENDING_FOR_MS |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 408 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 409 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 410 | pub(crate) fn soft_logout(&self) -> Option<SoftLogout> { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 411 | let (meta, client_id) = match &self.authentication { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 412 | AuthenticationRecord::SoftLoggedOut { meta, client_id } => (meta, client_id), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 413 | AuthenticationRecord::PendingOAuth { previous, .. } => { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 414 | return previous.as_ref()?.soft_logout(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 415 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 416 | _ => return None, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 417 | }; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 418 | Some(SoftLogout { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 419 | homeserver: self.homeserver.clone(), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 420 | user_id: meta.user_id.clone(), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 421 | device_id: meta.device_id.clone(), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 422 | client_id: client_id |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 423 | .as_ref() |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 424 | .map(|client_id| client_id.as_str().to_owned()), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 425 | }) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 426 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 427 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 428 | pub(crate) fn soft_logged_out(self) -> Self { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 429 | let authentication = match self.authentication { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 430 | AuthenticationRecord::Matrix(session) => AuthenticationRecord::SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 431 | meta: session.meta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 432 | client_id: None, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 433 | }, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 434 | AuthenticationRecord::OAuth { client_id, user } => { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 435 | AuthenticationRecord::SoftLoggedOut { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 436 | meta: user.meta, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 437 | client_id: Some(client_id), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 438 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 439 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 440 | soft @ AuthenticationRecord::SoftLoggedOut { .. } => soft, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 441 | pending @ AuthenticationRecord::PendingOAuth { .. } => pending, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 442 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 443 | Self { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 444 | homeserver: self.homeserver, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 445 | authentication, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 446 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 447 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 448 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 449 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 450 | pub enum Restore { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 451 | Resumed(Session), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 452 | SoftLoggedOut(SoftLogout), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 453 | /// an oauth login is waiting on its redirect. `Session::finish_oauth` takes the callback |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 454 | OAuthPending, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 455 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 456 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 457 | /// 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 | 458 | /// as any other device would orphan them |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 459 | #[derive(Clone, Debug)] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 460 | #[non_exhaustive] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 461 | pub struct SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 462 | pub homeserver: String, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 463 | pub user_id: OwnedUserId, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 464 | pub device_id: OwnedDeviceId, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 465 | /// set when the login was OAuth, for `OAuthLogin::with_client_id` |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 466 | pub client_id: Option<String>, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 467 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 468 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 469 | impl SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 470 | #[must_use] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 471 | pub fn password_login(&self, password: impl Into<String>) -> PasswordLogin { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 472 | PasswordLogin::new(self.homeserver.clone(), self.user_id.as_str(), password) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 473 | .with_device_id(self.device_id.clone()) |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 474 | .allow_insecure_loopback(true) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 475 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 476 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 477 | #[must_use] |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 478 | pub fn oauth_login( |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 479 | &self, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 480 | redirect_uri: impl Into<String>, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 481 | client_uri: impl Into<String>, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 482 | application_type: OAuthApplicationType, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 483 | ) -> OAuthLogin { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 484 | let login = OAuthLogin::new( |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 485 | self.homeserver.clone(), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 486 | redirect_uri, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 487 | client_uri, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 488 | application_type, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 489 | ) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 490 | .with_device_id(self.device_id.clone()) |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 491 | .allow_insecure_loopback(true); |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 492 | match &self.client_id { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 493 | Some(client_id) => login.with_client_id(client_id.clone()), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 494 | None => login, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 495 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 496 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 497 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 499 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 500 | pub enum AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | #[error("{context} failed")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | context: &'static str, |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 504 | kind: ErrorKind, |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 505 | server: Option<ServerError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 506 | #[source] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 507 | source: Box<SourceError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | #[error("the homeserver returned no authenticated session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | MissingSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 511 | #[error("the SDK returned an unsupported authentication session")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 512 | UnsupportedAuthentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 513 | #[error("the OAuth callback was already submitted")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 514 | OAuthCallbackAlreadySubmitted, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 515 | /// nothing is waiting for this callback. it was finished, abandoned, expired, or begun |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 516 | /// somewhere else |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 517 | #[error("no OAuth login is waiting for this callback")] |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 518 | NoPendingOAuth, |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 519 | /// what was typed can't name a homeserver, so nothing was sent anywhere |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 520 | #[error("that isn't a usable homeserver, {0}")] |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 521 | InvalidHomeserver(&'static str), |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 522 | /// `Accounts` already has a live login for them. a second would be another device |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 523 | #[error("{0} is already logged in")] |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 524 | AlreadyLoggedIn(OwnedUserId), |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 525 | /// an IndexedDB delete is waiting on the store's other connections. it goes through |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 526 | /// once they close, and opening the store again waits behind it |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 527 | #[error("the store is still open elsewhere, it will be deleted once that closes")] |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 528 | StoreDeletePending, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 529 | /// another engine, in this process or another, has the store. only one runs per store |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 530 | #[error("another engine is using this store")] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 531 | StoreInUse, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 532 | /// the login worked but its first record didn't save. the server already has the |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 533 | /// device, so retry the save rather than log in again |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 534 | #[error("saving the new login's record failed")] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 535 | Unsaved { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 536 | session: Box<UnsavedSession>, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 537 | #[source] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 538 | source: Box<AccountError>, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 539 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 540 | #[error(transparent)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 541 | Engine(#[from] EngineError), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 542 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 543 | |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 544 | impl AccountError { |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 545 | /// what the homeserver said, when it refused |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 546 | #[must_use] |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 547 | pub fn server(&self) -> Option<&ServerError> { |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 548 | match self { |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 549 | Self::Operation { server, .. } => server.as_ref(), |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 550 | Self::Unsaved { source, .. } => source.server(), |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 551 | _ => None, |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 552 | } |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 553 | } |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 554 | } |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 555 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 556 | /// a live login whose record never reached its `SessionStore` |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 557 | pub struct UnsavedSession { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 558 | session: Session, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 559 | sessions: Arc<dyn SessionStore>, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 560 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 561 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 562 | impl UnsavedSession { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 563 | pub async fn save(self) -> Result<Session, AccountError> { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 564 | let saved = match session_record(&self.session.client) { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 565 | Ok(record) => self.sessions.save(&record).await, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 566 | Err(error) => Err(error), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 567 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 568 | match saved { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 569 | Ok(()) => Ok(self.session), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 570 | Err(source) => Err(AccountError::Unsaved { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 571 | session: Box::new(self), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 572 | source: Box::new(source), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 573 | }), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 574 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 575 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 576 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 577 | /// the record to keep somewhere else, if the store won't take it |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 578 | pub fn record(&self) -> Result<SessionRecord, AccountError> { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 579 | session_record(&self.session.client) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 580 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 581 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 582 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 583 | impl std::fmt::Debug for UnsavedSession { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 584 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 585 | formatter |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 586 | .debug_struct("UnsavedSession") |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 587 | .finish_non_exhaustive() |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 588 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 589 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 590 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | impl Session { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | pub async fn login_password( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | login: PasswordLogin, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 594 | sessions: Arc<dyn SessionStore>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | ) -> Result<Self, AccountError> { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 596 | homeserver_input(&login.homeserver, login.allow_insecure_loopback)?; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 597 | let store_lock = sessions.store().lock().await?; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 598 | Self::login_password_locked(login, sessions, store_lock).await |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 599 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 600 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 601 | pub(crate) async fn login_password_locked( |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 602 | login: PasswordLogin, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 603 | sessions: Arc<dyn SessionStore>, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 604 | store_lock: StoreLock, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 605 | ) -> Result<Self, AccountError> { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 606 | let homeserver = homeserver_input(&login.homeserver, login.allow_insecure_loopback)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 607 | let client = matrix_client_builder(sessions.store()) |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 608 | .server_name_or_homeserver_url(homeserver) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 610 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 611 | .map_err(|error| account_error("building the Matrix client", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 612 | let tracking = Tracking::start(&client, sessions.clone())?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | let mut request = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | .matrix_auth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | .login_username(login.username.trim(), &login.password) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 616 | .request_refresh_token(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 617 | if let Some(name) = login.device_display_name.as_deref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 618 | request = request.initial_device_display_name(name); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 619 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 620 | if let Some(device_id) = login.device_id.as_deref() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 621 | request = request.device_id(device_id.as_str()); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 622 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 623 | request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 624 | .send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 625 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 626 | .map_err(|error| account_error("password login", error))?; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 627 | Self::saved(client, sessions, tracking, store_lock).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 628 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 629 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 630 | /// the pending login lives only in this process, so the one that begins it has to finish it |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 631 | pub async fn begin_oauth( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 632 | login: OAuthLogin, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 633 | sessions: Arc<dyn SessionStore>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 634 | ) -> Result<PendingOAuthLogin, AccountError> { |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 635 | homeserver_input(&login.homeserver, login.allow_insecure_loopback)?; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 636 | let store_lock = sessions.store().lock().await?; |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 637 | Self::begin_oauth_locked(login, sessions, store_lock).await |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 638 | } |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 639 | |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 640 | pub(crate) async fn begin_oauth_locked( |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 641 | login: OAuthLogin, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 642 | sessions: Arc<dyn SessionStore>, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 643 | store_lock: StoreLock, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 644 | ) -> Result<PendingOAuthLogin, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 645 | let redirect_uri = Url::parse(login.redirect_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 646 | .map_err(|error| account_error("parsing the OAuth redirect URI", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 647 | let client_uri = Url::parse(login.client_uri.trim()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 648 | .map_err(|error| account_error("parsing the OAuth client URI", error))?; |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 649 | let homeserver = homeserver_input(&login.homeserver, login.allow_insecure_loopback)?; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 650 | let client = matrix_client_builder(sessions.store()) |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 651 | .server_name_or_homeserver_url(homeserver) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 652 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 653 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 654 | .map_err(|error| account_error("building the Matrix client", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 655 | let tracking = Tracking::start(&client, sessions.clone())?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 656 | let application_type = match login.application_type { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 657 | OAuthApplicationType::Native => ApplicationType::Native, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 658 | OAuthApplicationType::Web => ApplicationType::Web, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 659 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 660 | let mut metadata = ClientMetadata::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 661 | application_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 662 | vec![OAuthGrantType::AuthorizationCode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 663 | redirect_uris: vec![redirect_uri.clone()], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 664 | }], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 665 | Localized::new(client_uri, []), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 666 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 667 | metadata.client_name = login.client_name.map(|name| Localized::new(name, [])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 668 | let metadata = Raw::new(&metadata) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 669 | .map_err(|error| account_error("serializing OAuth client metadata", error))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 670 | let mut registration = ClientRegistrationData::new(metadata); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 671 | if let Some(client_id) = login.client_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 672 | registration.static_registrations = Some(HashMap::from([( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 673 | client.homeserver(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 674 | ClientId::new(client_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 675 | )])); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 676 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 677 | let authorization = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 678 | .oauth() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 679 | .login(redirect_uri, login.device_id, Some(registration), None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 680 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 681 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 682 | .map_err(|error| account_error("starting OAuth login", error))?; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 683 | let previous = sessions.load().await?.and_then(SessionRecord::resumable); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 684 | let pending = SessionRecord { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 685 | homeserver: client.homeserver().to_string(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 686 | authentication: AuthenticationRecord::PendingOAuth { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 687 | client_id: client |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 688 | .oauth() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 689 | .client_id() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 690 | .ok_or(AccountError::MissingSession)? |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 691 | .clone(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 692 | state: authorization.state.clone(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 693 | authorization: Box::new( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 694 | client |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 695 | .oauth() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 696 | .pending_authorization(&authorization.state) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 697 | .await |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 698 | .ok_or(AccountError::MissingSession)?, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 699 | ), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 700 | started_ms: now_ms(), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 701 | previous: previous.clone().map(Box::new), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 702 | }, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 703 | }; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 704 | sessions.save(&pending).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 705 | Ok(PendingOAuthLogin { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 706 | client, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 707 | sessions, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 708 | tracking: Some((tracking, store_lock)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 709 | authorization_url: authorization.url.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 710 | state: Some(authorization.state), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 711 | previous, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 712 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 713 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 714 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 715 | /// finishes a login `begin_oauth` started in another process, from the callback the |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 716 | /// redirect brought back. the pending login is in `sessions`, so nothing else is needed |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 717 | pub async fn finish_oauth( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 718 | sessions: Arc<dyn SessionStore>, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 719 | callback_url: &str, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 720 | ) -> Result<Self, AccountError> { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 721 | let store_lock = sessions.store().lock().await?; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 722 | Self::finish_oauth_locked(sessions, store_lock, callback_url).await |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 723 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 724 | |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 725 | pub(crate) async fn finish_oauth_locked( |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 726 | sessions: Arc<dyn SessionStore>, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 727 | store_lock: StoreLock, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 728 | callback_url: &str, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 729 | ) -> Result<Self, AccountError> { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 730 | let callback = Url::parse(callback_url) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 731 | .map_err(|error| account_error("parsing the OAuth callback URL", error))?; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 732 | let record = sessions.load().await?.ok_or(AccountError::NoPendingOAuth)?; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 733 | if record.pending_oauth_state() != callback_state(&callback).as_deref() { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 734 | return Err(AccountError::NoPendingOAuth); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 735 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 736 | let AuthenticationRecord::PendingOAuth { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 737 | client_id, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 738 | state, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 739 | authorization, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 740 | previous, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 741 | .. |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 742 | } = record.authentication |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 743 | else { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 744 | return Err(AccountError::NoPendingOAuth); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 745 | }; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 746 | let client = matrix_client_builder(sessions.store()) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 747 | .homeserver_url(&record.homeserver) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 748 | .build() |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 749 | .await |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 750 | .map_err(|error| account_error("reopening the Matrix store", error))?; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 751 | let tracking = Tracking::start(&client, sessions.clone())?; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 752 | let oauth = client.oauth(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 753 | oauth.restore_registered_client(client_id); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 754 | let finished = match oauth |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 755 | .restore_pending_authorization(state, *authorization) |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 756 | .await |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 757 | { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 758 | Ok(()) => oauth.finish_login(callback.into()).await, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 759 | Err(error) => Err(error.into()), |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 760 | }; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 761 | if let Err(error) = finished { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 762 | match previous { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 763 | Some(previous) => put_back(sessions.as_ref(), *previous).await, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 764 | None => { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 765 | drop((tracking, client)); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 766 | if let Err(error) = sessions.discard().await { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 767 | tracing::warn!(%error, "deleting a failed OAuth login's store failed"); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 768 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 769 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 770 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 771 | return Err(account_error("OAuth login", error)); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 772 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 773 | Self::saved(client, sessions, tracking, store_lock).await |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 774 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 775 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 776 | /// Restore the record `sessions` holds, or `None` when it holds none. |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 777 | /// The caller must also handle rejection and explicit logout. |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 778 | pub async fn restore(sessions: Arc<dyn SessionStore>) -> Result<Option<Restore>, AccountError> { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 779 | let Some(record) = sessions.load().await? else { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 780 | return Ok(None); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 781 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 782 | let authentication = match record.authentication { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 783 | AuthenticationRecord::Matrix(session) => AuthSession::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 784 | AuthenticationRecord::OAuth { client_id, user } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 785 | OAuthSession { client_id, user }.into() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 786 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 787 | AuthenticationRecord::PendingOAuth { .. } if record.oauth_expired() => { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 788 | let previous = record.resumable(); |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 789 | match previous { |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 790 | Some(previous) => put_back(sessions.as_ref(), previous).await, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 791 | None => sessions.discard().await?, |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 792 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 793 | return Box::pin(Self::restore(sessions)).await; |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 794 | } |
d8d8a9fe770a | atagen | 2026-10-22 07:44 UTC | 795 | AuthenticationRecord::PendingOAuth { .. } => return Ok(Some(Restore::OAuthPending)), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 796 | AuthenticationRecord::SoftLoggedOut { meta, client_id } => { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 797 | return Ok(Some(Restore::SoftLoggedOut(SoftLogout { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 798 | homeserver: record.homeserver, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 799 | user_id: meta.user_id, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 800 | device_id: meta.device_id, |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 801 | client_id: client_id.map(|client_id| client_id.as_str().to_owned()), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 802 | }))); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 803 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 804 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 805 | let store_lock = sessions.store().lock().await?; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 806 | let client = matrix_client_builder(sessions.store()) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 807 | .homeserver_url(&record.homeserver) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 808 | .build() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 809 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 810 | .map_err(|error| account_error("reopening the Matrix store", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 811 | let tracking = Tracking::start(&client, sessions)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 812 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 813 | .restore_session(authentication) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 814 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 815 | .map_err(|error| account_error("restoring the authenticated session", error))?; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 816 | Ok(Some(Restore::Resumed(Self { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 817 | client, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 818 | tracking, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 819 | store_lock, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 820 | }))) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 821 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 822 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 823 | /// leave the record to the host, which then saves `ClientHandle::session_record()` |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 824 | /// on every `SessionTokensChanged` |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 825 | #[must_use] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 826 | pub fn without_persistence(mut self) -> Self { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 827 | self.tracking.persistence = None; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 828 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 829 | self.tracking.inline.store(false, Ordering::Release); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 830 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 831 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 832 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 833 | pub async fn build( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 834 | self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 835 | config: EngineConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 836 | ) -> Result<(Engine, ClientHandle, EventStream), AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 837 | Engine::with_persistence( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 838 | self.client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 839 | config, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 840 | self.tracking.session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 841 | self.tracking.persistence, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 842 | Some(self.store_lock), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 843 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 844 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 845 | .map_err(AccountError::from) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 846 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 847 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 848 | async fn saved( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 849 | client: Client, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 850 | sessions: Arc<dyn SessionStore>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 851 | tracking: Tracking, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 852 | store_lock: StoreLock, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 853 | ) -> Result<Self, AccountError> { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 854 | UnsavedSession { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 855 | session: Self { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 856 | client, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 857 | tracking, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 858 | store_lock, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 859 | }, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 860 | sessions, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 861 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 862 | .save() |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 863 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 864 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 865 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 866 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 867 | impl Tracking { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 868 | fn start(client: &Client, sessions: Arc<dyn SessionStore>) -> Result<Self, AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 869 | let session_changes = client.subscribe_to_session_changes(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 870 | #[cfg_attr(target_family = "wasm", allow(unused_variables))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 871 | let (results, inline_results) = mpsc::unbounded_channel(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 872 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 873 | let inline = save_inline(client, sessions.clone(), results)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 874 | Ok(Self { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 875 | session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 876 | persistence: Some(Persistence { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 877 | save: Box::new(move |client| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 878 | let sessions = sessions.clone(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 879 | Box::pin(async move { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 880 | let record = session_record(&client).map_err(save_failed)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 881 | sessions.save(&record).await.map_err(save_failed) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 882 | }) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 883 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 884 | inline: inline_results, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 885 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 886 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 887 | inline, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 888 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 889 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 890 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 891 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 892 | /// the sdk calls this inside its refresh, after it has the new tokens and before it uses |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 893 | /// them, so a store that writes there and then can't lose a refresh token to a crash |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 894 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 895 | fn save_inline( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 896 | client: &Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 897 | sessions: Arc<dyn SessionStore>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 898 | results: mpsc::UnboundedSender<Result<(), crate::Error>>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 899 | ) -> Result<Arc<AtomicBool>, AccountError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 900 | let enabled = Arc::new(AtomicBool::new(true)); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 901 | let saving = enabled.clone(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 902 | client |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 903 | .set_session_callbacks( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 904 | // only asked for with the cross process refresh lock, which we never enable |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 905 | Box::new(|client| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 906 | client |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 907 | .session_tokens() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 908 | .ok_or_else(|| AccountError::MissingSession.into()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 909 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 910 | Box::new(move |client| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 911 | if !saving.load(Ordering::Acquire) { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 912 | return Ok(()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 913 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 914 | let record = session_record(&client)?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 915 | if let Some(result) = sessions.save_blocking(&record) { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 916 | let _ = results.send(result.map_err(save_failed)); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 917 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 918 | Ok(()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 919 | }), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 920 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 921 | .map_err(|error| account_error("registering the session callbacks", error))?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 922 | Ok(enabled) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 923 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 924 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 925 | fn save_failed(error: AccountError) -> crate::Error { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 926 | let message = format!("saving the session record failed, {error}"); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 927 | crate::Error::with_source(ErrorKind::Store, message, error) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 928 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 929 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 930 | pub(crate) fn session_record(client: &Client) -> Result<SessionRecord, AccountError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 931 | let authentication = match client.session().ok_or(AccountError::MissingSession)? { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 932 | AuthSession::Matrix(session) => AuthenticationRecord::Matrix(session), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 933 | AuthSession::OAuth(session) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 934 | let OAuthSession { client_id, user } = *session; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 935 | AuthenticationRecord::OAuth { client_id, user } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 936 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 937 | _ => return Err(AccountError::UnsupportedAuthentication), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 938 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 939 | Ok(SessionRecord { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 940 | homeserver: client.homeserver().to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 941 | authentication, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 942 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 943 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 944 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 945 | fn encryption_settings() -> EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 946 | EncryptionSettings { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 947 | auto_enable_cross_signing: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 948 | backup_download_strategy: BackupDownloadStrategy::OneShot, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 949 | auto_enable_backups: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 950 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 951 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 952 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 953 | /// `StoreLock` already keeps every other client off the store, and the sdk's lease would cost |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 954 | /// a store write per locked section and a task renewing it every 50ms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 955 | fn matrix_client_builder(store: &StoreConfig) -> ClientBuilder { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 956 | let builder = Client::builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 957 | .with_encryption_settings(encryption_settings()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 958 | .with_threading_support(ThreadingSupport::Enabled { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 959 | with_subscriptions: false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 960 | }) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 961 | .handle_refresh_tokens() |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 962 | .cross_process_store_config(CrossProcessLockConfig::SingleProcess); |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 963 | match &store.location { |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 964 | #[cfg(feature = "sqlite")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 965 | StoreLocation::Sqlite(path) => builder.sqlite_store(path, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 966 | #[cfg(feature = "indexeddb")] |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 967 | StoreLocation::IndexedDb(name) => builder.indexeddb_store(name, Some(&store.passphrase)), |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 968 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 969 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 970 | |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 971 | /// a bare server name goes through untouched, since that's what makes the sdk try `.well-known` |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 972 | pub(crate) fn homeserver_input( |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 973 | input: &str, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 974 | allow_insecure_loopback: bool, |
cb0668d30c14 | atagen | 2026-10-08 11:34 UTC | 975 | ) -> Result<&str, AccountError> { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 976 | let input = input.trim(); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 977 | let invalid = |reason| Err(AccountError::InvalidHomeserver(reason)); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 978 | if input.is_empty() { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 979 | return invalid("it's empty"); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 980 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 981 | if input.contains(char::is_whitespace) { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 982 | return invalid("it has spaces in it"); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 983 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 984 | if !input.contains("://") { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 985 | return match <&ruma::ServerName>::try_from(input) { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 986 | Ok(_) => Ok(input), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 987 | Err(_) => invalid("it's neither a server name nor a url"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 988 | }; |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 989 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 990 | let Ok(url) = Url::parse(input) else { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 991 | return invalid("it isn't a valid url"); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 992 | }; |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 993 | if !url.username().is_empty() || url.password().is_some() { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 994 | return invalid("it has a username in it"); |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 995 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 996 | let loopback = match url.host() { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 997 | None => return invalid("it has no host"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 998 | Some(url::Host::Domain(domain)) => domain.eq_ignore_ascii_case("localhost"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 999 | Some(url::Host::Ipv4(address)) => address.is_loopback(), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1000 | Some(url::Host::Ipv6(address)) => address.is_loopback(), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1001 | }; |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1002 | match url.scheme() { |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1003 | "https" => Ok(input), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1004 | "http" if allow_insecure_loopback && loopback => Ok(input), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1005 | "http" if allow_insecure_loopback => invalid("plain http only reaches loopback hosts"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1006 | "http" => invalid("it isn't https"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1007 | _ => invalid("only http and https are spoken"), |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1008 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1009 | } |
9c05b1dd327a | atagen | 2026-10-08 11:27 UTC | 1010 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 1011 | pub(crate) fn account_error( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1012 | context: &'static str, |
62fe32759529 | atagen | 2026-09-26 08:50 UTC | 1013 | source: impl std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1014 | ) -> AccountError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1015 | AccountError::Operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1016 | context, |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 1017 | kind: crate::error::classify(&source), |
9a25b6ce66f4 | atagen | 2026-10-08 11:15 UTC | 1018 | server: ServerError::find(&source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1019 | source: Box::new(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1020 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1021 | } |