From 65d15fe75106656a067f06a7c3111428b2a05d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Sat, 26 Apr 2025 22:00:12 +0200 Subject: [PATCH] fix: less racy auth token refresh logic This may help with issues reported by users where the access token is invalid and can't be used to join servers over long periods of time. --- packages/app-lib/src/state/minecraft_auth.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/app-lib/src/state/minecraft_auth.rs b/packages/app-lib/src/state/minecraft_auth.rs index 6cc464c5f..3a81040d8 100644 --- a/packages/app-lib/src/state/minecraft_auth.rs +++ b/packages/app-lib/src/state/minecraft_auth.rs @@ -253,12 +253,16 @@ pub(super) static PROFILE_CACHE: Mutex< > = Mutex::const_new(HashMap::with_hasher(BuildHasherDefault::new())); impl Credentials { - /// Refreshes the authentication tokens for this user if they are expired. + /// Refreshes the authentication tokens for this user if they are expired, or + /// very close to expiration. async fn refresh( &mut self, exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy, ) -> crate::Result<()> { - if self.expires > Utc::now() { + // Use a margin of 5 minutes to give e.g. Minecraft and potentially + // other operations that depend on a fresh token 5 minutes to complete + // from now, and deal with some classes of clock skew + if self.expires > Utc::now() + Duration::minutes(5) { return Ok(()); }