From 13afc1d6a42d28f01087fb59ca868696be4a2635 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Mon, 12 Aug 2024 03:24:45 +0530 Subject: Auth Customization Options (#159) * Added Support for Custom Auth using `client_id` and `scope` * fix: `Account::microsoft` and added lifetime to `Account::microsoft_with_custom_client_id` * Added function `with_microsoft_access_token_and_custom_client_id` * Removed Custom Scope. * I got carried away, and made scope also customizable, later realized no customization is needed. * Better Documentation and Minor fixes * Added Custom Scope * Added RpsTicket format for custom `client_id` * Moved to non-static str * fix example Co-authored-by: mat <27899617+mat-1@users.noreply.github.com> * fix doc grammer * changed function signature * fmt * fixed example * removed dead code * Removed `d=` insertion in `client_id` * removed unnecessary `mut` --------- Co-authored-by: mat <27899617+mat-1@users.noreply.github.com> --- azalea-client/src/account.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'azalea-client/src') diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 741a07d4..c007e01a 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -90,6 +90,18 @@ impl Account { /// a key for the cache, but it's recommended to use the real email to /// avoid confusion. pub async fn microsoft(email: &str) -> Result { + Self::microsoft_with_custom_client_id_and_scope(email, None, None).await + } + + /// Similar to [`account.microsoft()`](Self::microsoft) but you can use your + /// own `client_id` and `scope`. + /// + /// Pass `None` if you want to use default ones. + pub async fn microsoft_with_custom_client_id_and_scope( + email: &str, + client_id: Option<&str>, + scope: Option<&str>, + ) -> Result { let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| { panic!( "No {} environment variable found", @@ -100,6 +112,8 @@ impl Account { email, azalea_auth::AuthOpts { cache_file: Some(minecraft_dir.join("azalea-auth.json")), + client_id, + scope, ..Default::default() }, ) @@ -128,24 +142,42 @@ impl Account { /// # async fn example() -> Result<(), Box> { /// let client = reqwest::Client::new(); /// - /// let res = azalea_auth::get_ms_link_code(&client).await?; + /// let res = azalea_auth::get_ms_link_code(&client, None, None).await?; + /// // Or, `azalea_auth::get_ms_link_code(&client, Some(client_id), None).await?` + /// // if you want to use your own client_id /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code /// ); - /// let msa = azalea_auth::get_ms_auth_token(&client, res).await?; + /// let msa = azalea_auth::get_ms_auth_token(&client, res, None).await?; /// Account::with_microsoft_access_token(msa).await?; /// # Ok(()) /// # } /// ``` pub async fn with_microsoft_access_token( + msa: azalea_auth::cache::ExpiringValue, + ) -> Result { + Self::with_microsoft_access_token_and_custom_client_id_and_scope(msa, None, None).await + } + + /// Similar to [`Account::with_microsoft_access_token`] but you can use + /// custom `client_id` and `scope`. + pub async fn with_microsoft_access_token_and_custom_client_id_and_scope( mut msa: azalea_auth::cache::ExpiringValue, + client_id: Option<&str>, + scope: Option<&str>, ) -> Result { let client = reqwest::Client::new(); if msa.is_expired() { tracing::trace!("refreshing Microsoft auth token"); - msa = azalea_auth::refresh_ms_auth_token(&client, &msa.data.refresh_token).await?; + msa = azalea_auth::refresh_ms_auth_token( + &client, + &msa.data.refresh_token, + client_id, + scope, + ) + .await?; } let msa_token = &msa.data.access_token; -- cgit v1.2.3