Skip to content

Commit e6e2768

Browse files
committed
refactor(sdk): Add constructor for AuthCtx
Allows to have stricter visibility for the fields and put less code in ClientBuilder. Signed-off-by: Kévin Commaille <[email protected]>
1 parent ab98028 commit e6e2768

File tree

2 files changed

+22
-16
lines changed
  • crates/matrix-sdk/src

2 files changed

+22
-16
lines changed

crates/matrix-sdk/src/authentication/mod.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ pub(crate) type ReloadSessionCallback =
6666
/// All the data relative to authentication, and that must be shared between a
6767
/// client and all its children.
6868
pub(crate) struct AuthCtx {
69-
pub(crate) oauth: OAuthCtx,
69+
oauth: OAuthCtx,
7070

7171
/// Whether to try to refresh the access token automatically when an
7272
/// `M_UNKNOWN_TOKEN` error is encountered.
7373
pub(crate) handle_refresh_tokens: bool,
7474

7575
/// Lock making sure we're only doing one token refresh at a time.
76-
pub(crate) refresh_token_lock: Arc<AsyncMutex<Result<(), RefreshTokenError>>>,
76+
refresh_token_lock: Arc<AsyncMutex<Result<(), RefreshTokenError>>>,
7777

7878
/// Session change publisher. Allows the subscriber to handle changes to the
7979
/// session such as logging out when the access token is invalid or
@@ -84,7 +84,7 @@ pub(crate) struct AuthCtx {
8484
pub(crate) auth_data: OnceCell<AuthData>,
8585

8686
/// The current session tokens.
87-
pub(crate) tokens: OnceCell<Mutex<SessionTokens>>,
87+
tokens: OnceCell<Mutex<SessionTokens>>,
8888

8989
/// A callback called whenever we need an absolute source of truth for the
9090
/// current session tokens.
@@ -103,6 +103,20 @@ pub(crate) struct AuthCtx {
103103
}
104104

105105
impl AuthCtx {
106+
/// Construct a new `AuthCtx` with the given settings.
107+
pub(crate) fn new(handle_refresh_tokens: bool, allow_insecure_oauth: bool) -> Self {
108+
Self {
109+
handle_refresh_tokens,
110+
refresh_token_lock: Arc::new(AsyncMutex::new(Ok(()))),
111+
session_change_sender: broadcast::Sender::new(1),
112+
auth_data: OnceCell::default(),
113+
tokens: OnceCell::default(),
114+
reload_session_callback: OnceCell::default(),
115+
save_session_callback: OnceCell::default(),
116+
oauth: OAuthCtx::new(allow_insecure_oauth),
117+
}
118+
}
119+
106120
/// The current session tokens.
107121
pub(crate) fn session_tokens(&self) -> Option<SessionTokens> {
108122
Some(self.tokens.get()?.lock().clone())

crates/matrix-sdk/src/client/builder/mod.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ use ruma::{
3636
api::{MatrixVersion, SupportedVersions, error::FromHttpResponseError},
3737
};
3838
use thiserror::Error;
39-
use tokio::sync::{Mutex, OnceCell, broadcast};
39+
#[cfg(feature = "experimental-search")]
40+
use tokio::sync::Mutex;
41+
use tokio::sync::OnceCell;
4042
use tracing::{Span, debug, field::debug, instrument};
4143

4244
use super::{Client, ClientInner};
@@ -50,7 +52,7 @@ use crate::search_index::SearchIndex;
5052
use crate::search_index::SearchIndexStoreKind;
5153
use crate::{
5254
HttpError, IdParseError,
53-
authentication::{AuthCtx, oauth::OAuthCtx},
55+
authentication::AuthCtx,
5456
client::caches::CachedValue::{Cached, NotSet},
5557
config::RequestConfig,
5658
error::RumaApiError,
@@ -578,17 +580,7 @@ impl ClientBuilder {
578580
};
579581

580582
let allow_insecure_oauth = homeserver.scheme() == "http";
581-
582-
let auth_ctx = Arc::new(AuthCtx {
583-
handle_refresh_tokens: self.handle_refresh_tokens,
584-
refresh_token_lock: Arc::new(Mutex::new(Ok(()))),
585-
session_change_sender: broadcast::Sender::new(1),
586-
auth_data: OnceCell::default(),
587-
tokens: OnceCell::default(),
588-
reload_session_callback: OnceCell::default(),
589-
save_session_callback: OnceCell::default(),
590-
oauth: OAuthCtx::new(allow_insecure_oauth),
591-
});
583+
let auth_ctx = Arc::new(AuthCtx::new(self.handle_refresh_tokens, allow_insecure_oauth));
592584

593585
// Enable the send queue by default.
594586
let send_queue = Arc::new(SendQueueData::new(true));

0 commit comments

Comments
 (0)