From a434b97c54c4c81b83ea1636268863a8f07dfd15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Thu, 3 Aug 2023 14:32:36 +0200 Subject: [PATCH] sdk: Don't derive (De)serialize for AuthSession MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out not all authentication API sessions (i.e. OIDC) are fully (de)serializable. Signed-off-by: Kévin Commaille --- crates/matrix-sdk/src/authentication.rs | 3 +-- examples/persist_session/src/main.rs | 13 +++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/matrix-sdk/src/authentication.rs b/crates/matrix-sdk/src/authentication.rs index 2dc142d68..73ff7c9b4 100644 --- a/crates/matrix-sdk/src/authentication.rs +++ b/crates/matrix-sdk/src/authentication.rs @@ -13,7 +13,6 @@ // limitations under the License. use matrix_sdk_base::SessionMeta; -use serde::{Deserialize, Serialize}; use crate::matrix_auth::{self, MatrixAuth, MatrixAuthData}; @@ -26,7 +25,7 @@ pub enum AuthApi { } /// A user session using one of the available authentication APIs. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone)] #[non_exhaustive] pub enum AuthSession { /// A session using the native Matrix authentication API. diff --git a/examples/persist_session/src/main.rs b/examples/persist_session/src/main.rs index 4cb5d07b1..7bcb9387f 100644 --- a/examples/persist_session/src/main.rs +++ b/examples/persist_session/src/main.rs @@ -5,11 +5,12 @@ use std::{ use matrix_sdk::{ config::SyncSettings, + matrix_auth::Session, ruma::{ api::client::filter::FilterDefinition, events::room::message::{MessageType, OriginalSyncRoomMessageEvent}, }, - AuthSession, Client, Error, LoopCtrl, Room, RoomState, + Client, Error, LoopCtrl, Room, RoomState, }; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use serde::{Deserialize, Serialize}; @@ -35,7 +36,7 @@ struct FullSession { client_session: ClientSession, /// The Matrix user session. - user_session: AuthSession, + user_session: Session, /// The latest sync token. /// @@ -95,7 +96,7 @@ async fn restore_session(session_file: &Path) -> anyhow::Result<(Client, Option< .build() .await?; - println!("Restoring session for {}…", user_session.meta().user_id); + println!("Restoring session for {}…", user_session.meta.user_id); // Restore the Matrix user session. client.restore_session(user_session).await?; @@ -108,6 +109,7 @@ async fn login(data_dir: &Path, session_file: &Path) -> anyhow::Result { println!("No previous session found, logging in…"); let (client, client_session) = build_client(data_dir).await?; + let matrix_auth = client.matrix_auth(); loop { print!("\nUsername: "); @@ -122,8 +124,7 @@ async fn login(data_dir: &Path, session_file: &Path) -> anyhow::Result { io::stdin().read_line(&mut password).expect("Unable to read user input"); password = password.trim().to_owned(); - match client - .matrix_auth() + match matrix_auth .login_username(&username, &password) .initial_device_display_name("persist-session client") .await @@ -143,7 +144,7 @@ async fn login(data_dir: &Path, session_file: &Path) -> anyhow::Result { // This is not very secure, for simplicity. If the system provides a way of // storing secrets securely, it should be used instead. // Note that we could also build the user session from the login response. - let user_session = client.session().expect("A logged-in client should have a session"); + let user_session = matrix_auth.session().expect("A logged-in client should have a session"); let serialized_session = serde_json::to_string(&FullSession { client_session, user_session, sync_token: None })?; fs::write(session_file, serialized_session).await?;