mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-10 00:48:44 -04:00
chore: move the refresh_token_lock into the AuthCtx
This commit is contained in:
@@ -14,10 +14,14 @@
|
||||
|
||||
use matrix_sdk_base::SessionMeta;
|
||||
use ruma::api::client::discovery::discover_homeserver::AuthenticationServerInfo;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::matrix_auth::{self, MatrixAuth, MatrixAuthData};
|
||||
#[cfg(feature = "experimental-oidc")]
|
||||
use crate::oidc::{self, Oidc, OidcAuthData};
|
||||
use crate::{
|
||||
matrix_auth::{self, MatrixAuth, MatrixAuthData},
|
||||
RefreshTokenError,
|
||||
};
|
||||
|
||||
/// All the data relative to authentication, and that must be shared between a client and all its
|
||||
/// children.
|
||||
@@ -29,6 +33,9 @@ pub(crate) struct AuthCtx {
|
||||
/// Whether to try to refresh the access token automatically when an
|
||||
/// `M_UNKNOWN_TOKEN` error is encountered.
|
||||
pub(crate) handle_refresh_tokens: bool,
|
||||
|
||||
/// Lock making sure we're only doing one token refresh at a time.
|
||||
pub(crate) refresh_token_lock: Mutex<Result<(), RefreshTokenError>>,
|
||||
}
|
||||
|
||||
/// An enum over all the possible authentication APIs.
|
||||
|
||||
@@ -21,6 +21,7 @@ use ruma::{
|
||||
OwnedServerName, ServerName,
|
||||
};
|
||||
use thiserror::Error;
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::{debug, field::debug, instrument, Span};
|
||||
use url::Url;
|
||||
|
||||
@@ -425,6 +426,7 @@ impl ClientBuilder {
|
||||
let auth_ctx = Arc::new(AuthCtx {
|
||||
authentication_server_info,
|
||||
handle_refresh_tokens: self.handle_refresh_tokens,
|
||||
refresh_token_lock: Mutex::new(Ok(())),
|
||||
});
|
||||
|
||||
let inner = Arc::new(ClientInner::new(
|
||||
|
||||
@@ -186,8 +186,6 @@ pub(crate) struct ClientInner {
|
||||
/// Whether the client should update its homeserver URL with the discovery
|
||||
/// information present in the login response.
|
||||
respect_login_well_known: bool,
|
||||
/// Lock making sure we're only doing one token refresh at a time.
|
||||
pub(crate) refresh_token_lock: Mutex<Result<(), RefreshTokenError>>,
|
||||
/// An event that can be listened on to wait for a successful sync. The
|
||||
/// event will only be fired if a sync loop is running. Can be used for
|
||||
/// synchronization, e.g. if we send out a request to create a room, we can
|
||||
@@ -259,7 +257,6 @@ impl ClientInner {
|
||||
sync_gap_broadcast_txs: Default::default(),
|
||||
respect_login_well_known,
|
||||
sync_beat: event_listener::Event::new(),
|
||||
refresh_token_lock: Mutex::new(Ok(())),
|
||||
session_change_sender,
|
||||
auth_data: Default::default(),
|
||||
#[cfg(feature = "e2e-encryption")]
|
||||
|
||||
@@ -450,7 +450,7 @@ impl MatrixAuth {
|
||||
&self,
|
||||
) -> Result<Option<refresh_token::v3::Response>, RefreshTokenError> {
|
||||
let client = &self.client;
|
||||
let lock = client.inner.refresh_token_lock.try_lock();
|
||||
let lock = client.inner.auth_ctx.refresh_token_lock.try_lock();
|
||||
|
||||
if let Ok(mut guard) = lock {
|
||||
let Some(mut session_tokens) = self.session_tokens() else {
|
||||
@@ -489,7 +489,7 @@ impl MatrixAuth {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match client.inner.refresh_token_lock.lock().await.as_ref() {
|
||||
match client.inner.auth_ctx.refresh_token_lock.lock().await.as_ref() {
|
||||
Ok(_) => Ok(None),
|
||||
Err(error) => Err(error.clone()),
|
||||
}
|
||||
|
||||
@@ -926,7 +926,7 @@ impl Oidc {
|
||||
&self,
|
||||
) -> Result<Option<AccessTokenResponse>, RefreshTokenError> {
|
||||
let client = &self.client;
|
||||
let lock = client.inner.refresh_token_lock.try_lock();
|
||||
let lock = client.inner.auth_ctx.refresh_token_lock.try_lock();
|
||||
|
||||
macro_rules! fail {
|
||||
($lock:expr, $error:expr) => {
|
||||
@@ -957,7 +957,7 @@ impl Oidc {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match client.inner.refresh_token_lock.lock().await.as_ref() {
|
||||
match client.inner.auth_ctx.refresh_token_lock.lock().await.as_ref() {
|
||||
Ok(_) => Ok(None),
|
||||
Err(error) => Err(error.clone()),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user