Restore user session on restart

This commit is contained in:
2025-11-06 18:58:43 +01:00
parent 1438e2de0e
commit 1ba5372468

View File

@@ -3,7 +3,9 @@ use crate::users::UserEmail;
use crate::utils::rand_utils::rand_string; use crate::utils::rand_utils::rand_string;
use anyhow::Context; use anyhow::Context;
use matrix_sdk::authentication::oauth::error::OAuthDiscoveryError; use matrix_sdk::authentication::oauth::error::OAuthDiscoveryError;
use matrix_sdk::authentication::oauth::{ClientId, OAuthError, UrlOrQuery, UserSession}; use matrix_sdk::authentication::oauth::{
ClientId, OAuthError, OAuthSession, UrlOrQuery, UserSession,
};
use matrix_sdk::ruma::serde::Raw; use matrix_sdk::ruma::serde::Raw;
use matrix_sdk::{Client, ClientBuildError}; use matrix_sdk::{Client, ClientBuildError};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -40,6 +42,12 @@ enum MatrixClientError {
ClearDbPassphrase(std::io::Error), ClearDbPassphrase(std::io::Error),
#[error("Failed to fetch server metadata! {0}")] #[error("Failed to fetch server metadata! {0}")]
FetchServerMetadata(OAuthDiscoveryError), FetchServerMetadata(OAuthDiscoveryError),
#[error("Failed to load stored session! {0}")]
LoadStoredSession(std::io::Error),
#[error("Failed to decode stored session! {0}")]
DecodeStoredSession(serde_json::Error),
#[error("Failed to restore stored session! {0}")]
RestoreSession(matrix_sdk::Error),
#[error("Failed to parse auth redirect URL! {0}")] #[error("Failed to parse auth redirect URL! {0}")]
ParseAuthRedirectURL(url::ParseError), ParseAuthRedirectURL(url::ParseError),
#[error("Failed to build auth request! {0}")] #[error("Failed to build auth request! {0}")]
@@ -66,9 +74,8 @@ impl MatrixClient {
/// Start to build Matrix client to initiate user authentication /// Start to build Matrix client to initiate user authentication
pub async fn build_client(email: &UserEmail) -> anyhow::Result<Self> { pub async fn build_client(email: &UserEmail) -> anyhow::Result<Self> {
// Check if we are restoring a previous state // Check if we are restoring a previous state
let is_restoring = AppConfig::get() let session_file_path = AppConfig::get().user_matrix_session_file_path(email);
.user_matrix_session_file_path(email) let is_restoring = session_file_path.is_file();
.is_file();
if !is_restoring { if !is_restoring {
Self::destroy_data(email).map_err(MatrixClientError::DestroyPreviousData)?; Self::destroy_data(email).map_err(MatrixClientError::DestroyPreviousData)?;
} }
@@ -103,8 +110,23 @@ impl MatrixClient {
.map_err(MatrixClientError::FetchServerMetadata)?; .map_err(MatrixClientError::FetchServerMetadata)?;
log::info!("OAuth2 server issuer: {:?}", server_metadata.issuer); log::info!("OAuth2 server issuer: {:?}", server_metadata.issuer);
// TODO : restore client ID to oauth if needed if is_restoring {
// TODO : restore client if client already existed let session: StoredSession = serde_json::from_str(
std::fs::read_to_string(session_file_path)
.map_err(MatrixClientError::LoadStoredSession)?
.as_str(),
)
.map_err(MatrixClientError::DecodeStoredSession)?;
// Restore data
client
.restore_session(OAuthSession {
client_id: session.client_id,
user: session.user_session,
})
.await
.map_err(MatrixClientError::RestoreSession)?;
}
let client = Self { let client = Self {
email: email.clone(), email: email.clone(),