WIP: Migrate to matrix-rust-sdk #111
@@ -3,7 +3,9 @@ use crate::users::UserEmail;
|
||||
use crate::utils::rand_utils::rand_string;
|
||||
use anyhow::Context;
|
||||
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::{Client, ClientBuildError};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -40,6 +42,12 @@ enum MatrixClientError {
|
||||
ClearDbPassphrase(std::io::Error),
|
||||
#[error("Failed to fetch server metadata! {0}")]
|
||||
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}")]
|
||||
ParseAuthRedirectURL(url::ParseError),
|
||||
#[error("Failed to build auth request! {0}")]
|
||||
@@ -66,9 +74,8 @@ impl MatrixClient {
|
||||
/// Start to build Matrix client to initiate user authentication
|
||||
pub async fn build_client(email: &UserEmail) -> anyhow::Result<Self> {
|
||||
// Check if we are restoring a previous state
|
||||
let is_restoring = AppConfig::get()
|
||||
.user_matrix_session_file_path(email)
|
||||
.is_file();
|
||||
let session_file_path = AppConfig::get().user_matrix_session_file_path(email);
|
||||
let is_restoring = session_file_path.is_file();
|
||||
if !is_restoring {
|
||||
Self::destroy_data(email).map_err(MatrixClientError::DestroyPreviousData)?;
|
||||
}
|
||||
@@ -103,8 +110,23 @@ impl MatrixClient {
|
||||
.map_err(MatrixClientError::FetchServerMetadata)?;
|
||||
log::info!("OAuth2 server issuer: {:?}", server_metadata.issuer);
|
||||
|
||||
// TODO : restore client ID to oauth if needed
|
||||
// TODO : restore client if client already existed
|
||||
if is_restoring {
|
||||
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 {
|
||||
email: email.clone(),
|
||||
|
||||
Reference in New Issue
Block a user