From 6c110f2731c3893af397cc6a660f374ff705c99b Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sun, 30 Nov 2025 21:04:12 -0600 Subject: Add `online-mode` Cargo feature (#281) * Add `online-mode` cargo feature * fix bad formatting in Cargo.toml --- azalea-client/src/plugins/login.rs | 94 +++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 42 deletions(-) (limited to 'azalea-client/src/plugins/login.rs') diff --git a/azalea-client/src/plugins/login.rs b/azalea-client/src/plugins/login.rs index 25919c8c..46ff8ea9 100644 --- a/azalea-client/src/plugins/login.rs +++ b/azalea-client/src/plugins/login.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "online-mode")] use azalea_auth::sessionserver::ClientSessionServerError; use azalea_protocol::packets::login::{ ClientboundHello, ServerboundCustomQueryAnswer, ServerboundKey, @@ -80,8 +81,10 @@ pub struct AuthTask(Task= 2 { - // if this is the second attempt and we failed - // both times, give up - return Err(err.into()); - } - if matches!( - err, - ClientSessionServerError::InvalidSession | ClientSessionServerError::ForbiddenOperation - ) { - // uh oh, we got an invalid session and have - // to reauthenticate now - async_compat::Compat::new(account.refresh()).await?; - } else { - return Err(err.into()); + #[cfg(not(feature = "online-mode"))] + let _ = account; + + #[cfg(feature = "online-mode")] + if packet.should_authenticate { + let Some(access_token) = &account.access_token else { + // offline mode account, no need to do auth + return Ok((key_packet, private_key)); + }; + + // keep track of the number of times we tried authenticating so we can give up + // after too many + let mut attempts: usize = 1; + + while let Err(err) = { + let access_token = access_token.lock().clone(); + + let uuid = &account + .uuid + .expect("Uuid must be present if access token is present."); + + // this is necessary since reqwest usually depends on tokio and we're using + // `futures` here + async_compat::Compat::new(azalea_auth::sessionserver::join( + &access_token, + &packet.public_key, + &private_key, + uuid, + &packet.server_id, + )) + .await + } { + if attempts >= 2 { + // if this is the second attempt and we failed + // both times, give up + return Err(err.into()); + } + if matches!( + err, + ClientSessionServerError::InvalidSession + | ClientSessionServerError::ForbiddenOperation + ) { + // uh oh, we got an invalid session and have + // to reauthenticate now + async_compat::Compat::new(account.refresh()).await?; + } else { + return Err(err.into()); + } + attempts += 1; } - attempts += 1; } Ok((key_packet, private_key)) -- cgit v1.2.3