From 6c55767c73e4e7ec484c37e0117ccd73449ff465 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 5 Sep 2023 16:11:54 +0200 Subject: [PATCH] chore: move the `session_changer_sender` field into `AuthCtx` --- crates/matrix-sdk/src/authentication.rs | 9 +++++++-- crates/matrix-sdk/src/client/builder.rs | 3 ++- crates/matrix-sdk/src/client/mod.rs | 10 ++-------- crates/matrix-sdk/src/matrix_auth/mod.rs | 1 + crates/matrix-sdk/src/oidc/mod.rs | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/crates/matrix-sdk/src/authentication.rs b/crates/matrix-sdk/src/authentication.rs index 44d9da652..2e7c63766 100644 --- a/crates/matrix-sdk/src/authentication.rs +++ b/crates/matrix-sdk/src/authentication.rs @@ -14,13 +14,13 @@ use matrix_sdk_base::SessionMeta; use ruma::api::client::discovery::discover_homeserver::AuthenticationServerInfo; -use tokio::sync::Mutex; +use tokio::sync::{broadcast, Mutex}; #[cfg(feature = "experimental-oidc")] use crate::oidc::{self, Oidc, OidcAuthData}; use crate::{ matrix_auth::{self, MatrixAuth, MatrixAuthData}, - RefreshTokenError, + RefreshTokenError, SessionChange, }; /// All the data relative to authentication, and that must be shared between a client and all its @@ -36,6 +36,11 @@ pub(crate) struct AuthCtx { /// Lock making sure we're only doing one token refresh at a time. pub(crate) refresh_token_lock: Mutex>, + + /// Session change publisher. Allows the subscriber to handle changes to the + /// session such as logging out when the access token is invalid or + /// persisting updates to the access/refresh tokens. + pub(crate) session_change_sender: broadcast::Sender, } /// An enum over all the possible authentication APIs. diff --git a/crates/matrix-sdk/src/client/builder.rs b/crates/matrix-sdk/src/client/builder.rs index 855c777a0..98851496b 100644 --- a/crates/matrix-sdk/src/client/builder.rs +++ b/crates/matrix-sdk/src/client/builder.rs @@ -21,7 +21,7 @@ use ruma::{ OwnedServerName, ServerName, }; use thiserror::Error; -use tokio::sync::Mutex; +use tokio::sync::{broadcast, Mutex}; use tracing::{debug, field::debug, instrument, Span}; use url::Url; @@ -427,6 +427,7 @@ impl ClientBuilder { authentication_server_info, handle_refresh_tokens: self.handle_refresh_tokens, refresh_token_lock: Mutex::new(Ok(())), + session_change_sender: broadcast::Sender::new(1), }); let inner = Arc::new(ClientInner::new( diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index 8a1d2568d..28353ad68 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -192,10 +192,6 @@ pub(crate) struct ClientInner { /// wait for the sync to get the data to fetch a room object from the state /// store. pub(crate) sync_beat: event_listener::Event, - /// Session change publisher. Allows the subscriber to handle changes to the - /// session such as logging out when the access token is invalid or - /// persisting updates to the access/refresh tokens. - pub(crate) session_change_sender: broadcast::Sender, /// Authentication data to keep in memory. pub(crate) auth_data: OnceCell, @@ -234,8 +230,6 @@ impl ClientInner { server_versions: Option>, respect_login_well_known: bool, ) -> Self { - let session_change_sender = broadcast::Sender::new(1); - Self { homeserver: RwLock::new(homeserver), auth_ctx, @@ -257,7 +251,6 @@ impl ClientInner { sync_gap_broadcast_txs: Default::default(), respect_login_well_known, sync_beat: event_listener::Event::new(), - session_change_sender, auth_data: Default::default(), #[cfg(feature = "e2e-encryption")] cross_process_crypto_store_lock: OnceCell::new(), @@ -1360,6 +1353,7 @@ impl Client { info!("An unknown token error has been encountered."); _ = self .inner + .auth_ctx .session_change_sender .send(SessionChange::UnknownToken { soft_logout: *soft_logout }); } @@ -1935,7 +1929,7 @@ impl Client { /// Subscribes a new receiver to client SessionChange broadcasts. pub fn subscribe_to_session_changes(&self) -> broadcast::Receiver { - let broadcast = &self.inner.session_change_sender; + let broadcast = &self.inner.auth_ctx.session_change_sender; broadcast.subscribe() } diff --git a/crates/matrix-sdk/src/matrix_auth/mod.rs b/crates/matrix-sdk/src/matrix_auth/mod.rs index ebfb7cf2f..abac4658e 100644 --- a/crates/matrix-sdk/src/matrix_auth/mod.rs +++ b/crates/matrix-sdk/src/matrix_auth/mod.rs @@ -476,6 +476,7 @@ impl MatrixAuth { _ = self .client .inner + .auth_ctx .session_change_sender .send(SessionChange::TokensRefreshed); diff --git a/crates/matrix-sdk/src/oidc/mod.rs b/crates/matrix-sdk/src/oidc/mod.rs index dfef6f148..1a0ff78cd 100644 --- a/crates/matrix-sdk/src/oidc/mod.rs +++ b/crates/matrix-sdk/src/oidc/mod.rs @@ -903,7 +903,7 @@ impl Oidc { latest_id_token, }); - _ = self.client.inner.session_change_sender.send(SessionChange::TokensRefreshed); + _ = self.client.inner.auth_ctx.session_change_sender.send(SessionChange::TokensRefreshed); Ok(response) }