From c811dc471a31175d985e2d448a772c628dcad5f6 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 26 Sep 2025 18:28:42 +0300 Subject: update azalea-auth docs and variable names to make it clear that the cache key doesn't need to be an email --- CHANGELOG.md | 3 +-- azalea-auth/README.md | 3 ++- azalea-auth/src/auth.rs | 35 ++++++++++++++++++++--------------- azalea-auth/src/cache.rs | 18 +++++++++--------- azalea/README.md | 2 +- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8886e956..10948e27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,9 +57,8 @@ is breaking anyways, semantic versioning is not followed. - Improved matchers on the `ChatPacket` functions to work on more servers. (@ShayBox) - Bevy's `AppExit` Event is now handled by Azalea's ECS runner. - Pathfinding now works over farmland blocks. -- `HitResultComponent` no longer treats non-pickable entities like dropped items as pickable. - Refreshing the account token no longer results in a panic. -- Fix `::is_valid_id` on registries incorrectly returning true for values equal to the length. +- Fix `is_valid_id` on registries incorrectly returning true for values equal to the length. - Fix outdated implementation for the `ClientboundMerchantOffers` packet. - Fix compilation with new dependency versions. (@ShayBox) diff --git a/azalea-auth/README.md b/azalea-auth/README.md index 8f7cd46b..d7026254 100644 --- a/azalea-auth/README.md +++ b/azalea-auth/README.md @@ -2,7 +2,8 @@ A port of Mojang's Authlib and launcher authentication. -The default location of Azalea's cache is at `~/.minecraft/azalea-auth.json`. You can delete or modify this file if you'd like to associate an email with a different account. +The default location of Azalea's cache is at `~/.minecraft/azalea-auth.json`. +You can delete or modify this file if you'd like to associate a cache key (usually an email) with a different account. # Examples diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index f079f2c8..d54194c9 100644 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -62,16 +62,16 @@ pub enum AuthError { /// Authenticate with Microsoft. If the data isn't cached, /// they'll be asked to go to log into Microsoft in a web page. /// -/// The email is technically only used as a cache key, so it *could* be -/// anything. You should just have it be the actual email so it's not confusing -/// though, and in case the Microsoft API does start providing the real email. +/// The cache key is an arbitrary string that's used to identify the account in +/// the future. As a convention, we usually prefer to put the account email +/// here. /// /// If you want to use your own code to cache or show the auth code to the user /// in a different way, use [`get_ms_link_code`], [`get_ms_auth_token`], /// [`get_minecraft_token`] and [`get_profile`] instead. -pub async fn auth(email: &str, opts: AuthOpts<'_>) -> Result { +pub async fn auth(cache_key: &str, opts: AuthOpts<'_>) -> Result { let cached_account = if let Some(cache_file) = &opts.cache_file { - cache::get_account_in_cache(cache_file, email).await + cache::get_account_in_cache(cache_file, cache_key).await } else { None }; @@ -94,7 +94,7 @@ pub async fn auth(email: &str, opts: AuthOpts<'_>) -> Result) -> Result { // can't refresh, ask the user to auth again error!("Error refreshing Microsoft auth token: {}", e); - msa = - interactive_get_ms_auth_token(&client, email, Some(client_id), Some(scope)) - .await?; + msa = interactive_get_ms_auth_token( + &client, + cache_key, + Some(client_id), + Some(scope), + ) + .await?; } } } @@ -134,9 +138,9 @@ pub async fn auth(email: &str, opts: AuthOpts<'_>) -> Result, scope: Option<&str>, ) -> Result, GetMicrosoftAuthTokenError> { @@ -405,7 +410,7 @@ pub async fn interactive_get_ms_auth_token( let verification_uri = &res.verification_uri; let user_code = &res.user_code; println!( - "Go to \x1b[1m{verification_uri}?otc={user_code}\x1b[m and enter the code \x1b[1m{user_code}\x1b[m for \x1b[1m{email}\x1b[m", + "Go to \x1b[1m{verification_uri}?otc={user_code}\x1b[m and enter the code \x1b[1m{user_code}\x1b[m for \x1b[1m{cache_key}\x1b[m", ); get_ms_auth_token(client, res, client_id).await diff --git a/azalea-auth/src/cache.rs b/azalea-auth/src/cache.rs index 5b230f7d..41a578c2 100644 --- a/azalea-auth/src/cache.rs +++ b/azalea-auth/src/cache.rs @@ -28,7 +28,7 @@ pub enum CacheError { #[derive(Deserialize, Serialize, Debug)] pub struct CachedAccount { - pub email: String, + pub cache_key: String, /// Microsoft auth pub msa: ExpiringValue, /// Xbox Live auth @@ -113,23 +113,23 @@ async fn set_entire_cache(cache_file: &Path, cache: Vec) -> Resul Ok(()) } -/// Gets cached data for the given email. +/// Gets cached data for the given cache key. /// -/// Technically it doesn't actually have to be an email since it's only the -/// cache key. I considered using usernames or UUIDs as the cache key, but -/// usernames change and no one has their UUID memorized. -pub async fn get_account_in_cache(cache_file: &Path, email: &str) -> Option { +/// As a convention, the cache key is usually the email of the account. +pub async fn get_account_in_cache(cache_file: &Path, cache_key: &str) -> Option { let cache = get_entire_cache(cache_file).await.unwrap_or_default(); - cache.into_iter().find(|account| account.email == email) + cache + .into_iter() + .find(|account| account.cache_key == cache_key) } pub async fn set_account_in_cache( cache_file: &Path, - email: &str, + cache_key: &str, account: CachedAccount, ) -> Result<(), CacheError> { let mut cache = get_entire_cache(cache_file).await.unwrap_or_default(); - cache.retain(|account| account.email != email); + cache.retain(|account| account.cache_key != cache_key); cache.push(account); set_entire_cache(cache_file, cache).await } diff --git a/azalea/README.md b/azalea/README.md index 255e7139..ec25bfd0 100644 --- a/azalea/README.md +++ b/azalea/README.md @@ -35,7 +35,7 @@ The documentation for the latest Azalea crates.io release is available at [docs. # Examples ```rust,no_run -//! A bot that logs chat messages and the number that we've received to the console.\ +//! A bot that logs chat messages and the number that we've received to the console. use std::sync::Arc; -- cgit v1.2.3