aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/login.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2025-11-30 21:04:12 -0600
committerGitHub <noreply@github.com>2025-11-30 21:04:12 -0600
commit6c110f2731c3893af397cc6a660f374ff705c99b (patch)
treed3441af29cf40b49d474fc04aa1f426ad4292fa0 /azalea-client/src/plugins/login.rs
parent6930966aabf9b49fb6a0dc8b61076fa3f1abc298 (diff)
downloadazalea-drasl-6c110f2731c3893af397cc6a660f374ff705c99b.tar.xz
Add `online-mode` Cargo feature (#281)
* Add `online-mode` cargo feature * fix bad formatting in Cargo.toml
Diffstat (limited to 'azalea-client/src/plugins/login.rs')
-rw-r--r--azalea-client/src/plugins/login.rs94
1 files changed, 52 insertions, 42 deletions
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<Result<(ServerboundKey, PrivateKey), AuthWithAccountErr
pub enum AuthWithAccountError {
#[error("Failed to encrypt the challenge from the server for {0:?}")]
Encryption(ClientboundHello),
+ #[cfg(feature = "online-mode")]
#[error("{0}")]
SessionServer(#[from] ClientSessionServerError),
+ #[cfg(feature = "online-mode")]
#[error("Couldn't refresh access token: {0}")]
Auth(#[from] azalea_auth::AuthError),
}
@@ -99,49 +102,56 @@ pub async fn auth_with_account(
};
let private_key = encrypt_res.secret_key;
- 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());
+ #[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))