From 02390235c7ae1eff6b4cb500f28b9eb6b0bceaff Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Wed, 19 Jun 2024 15:05:06 +0530 Subject: [PATCH 01/16] Added Support for Custom Auth using `client_id` and `scope` --- azalea-auth/src/auth.rs | 71 ++++++++++++++++++++++++++---------- azalea-client/src/account.rs | 11 +++++- 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index e0a75adff..505f4c99f 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -24,6 +24,8 @@ pub struct AuthOpts { /// The directory to store the cache in. If this is not set, caching is not /// done. pub cache_file: Option, + pub scope: Option<&'static str>, + pub client_id: Option<&'static str> } #[derive(Debug, Error)] @@ -60,6 +62,10 @@ pub enum AuthError { /// 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 { + if !((opts.scope.is_some() && opts.client_id.is_some()) || (opts.scope.is_none() && opts.client_id.is_none())) { + panic!("Either provide both `scope` and `client_id` or nether.") + } + let cached_account = if let Some(cache_file) = &opts.cache_file { cache::get_account_in_cache(cache_file, email).await } else { @@ -80,16 +86,16 @@ pub async fn auth(email: &str, opts: AuthOpts) -> Result let mut msa = if let Some(account) = cached_account { account.msa } else { - interactive_get_ms_auth_token(&client, email).await? + interactive_get_ms_auth_token(&client, email, opts.scope, opts.client_id).await? }; if msa.is_expired() { tracing::trace!("refreshing Microsoft auth token"); - match refresh_ms_auth_token(&client, &msa.data.refresh_token).await { + match refresh_ms_auth_token(&client, &msa.data.refresh_token, opts.scope, opts.client_id).await { Ok(new_msa) => msa = new_msa, Err(e) => { // can't refresh, ask the user to auth again tracing::error!("Error refreshing Microsoft auth token: {}", e); - msa = interactive_get_ms_auth_token(&client, email).await?; + msa = interactive_get_ms_auth_token(&client, email, opts.scope, opts.client_id).await?; } } } @@ -280,7 +286,7 @@ pub enum GetMicrosoftAuthTokenError { /// /// ``` /// # async fn example(client: &reqwest::Client) -> Result<(), Box> { -/// let res = azalea_auth::get_ms_link_code(&client).await?; +/// let res = azalea_auth::get_ms_link_code(&client, None, None).await?; /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code @@ -293,12 +299,20 @@ pub enum GetMicrosoftAuthTokenError { /// ``` pub async fn get_ms_link_code( client: &reqwest::Client, + scope: Option<&str>, + client_id: Option<&str> ) -> Result { + let (scope, client_id) = if scope.is_some() && client_id.is_some() { + (scope.unwrap(), client_id.unwrap()) + } else { + ("service::user.auth.xboxlive.com::MBI_SSL", CLIENT_ID) + }; + Ok(client .post("https://login.live.com/oauth20_connect.srf") .form(&vec![ - ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), - ("client_id", CLIENT_ID), + ("scope", scope), + ("client_id", client_id), ("response_type", "device_code"), ]) .send() @@ -357,8 +371,10 @@ pub async fn get_ms_auth_token( pub async fn interactive_get_ms_auth_token( client: &reqwest::Client, email: &str, + scope: Option<&str>, + client_id: Option<&str> ) -> Result, GetMicrosoftAuthTokenError> { - let res = get_ms_link_code(client).await?; + let res = get_ms_link_code(client, scope, client_id).await?; tracing::trace!("Device code response: {:?}", res); println!( "Go to \x1b[1m{}\x1b[m and enter the code \x1b[1m{}\x1b[m for \x1b[1m{}\x1b[m", @@ -379,19 +395,36 @@ pub enum RefreshMicrosoftAuthTokenError { pub async fn refresh_ms_auth_token( client: &reqwest::Client, refresh_token: &str, + scope: Option<&str>, + client_id: Option<&str> ) -> Result, RefreshMicrosoftAuthTokenError> { - let access_token_response_text = client - .post("https://login.live.com/oauth20_token.srf") - .form(&vec![ - ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), - ("client_id", CLIENT_ID), - ("grant_type", "refresh_token"), - ("refresh_token", refresh_token), - ]) - .send() - .await? - .text() - .await?; + let access_token_response_text = if scope.is_some() && client_id.is_some() { + client + .post("https://login.live.com/oauth20_token.srf") + .form(&vec![ + ("scope", scope.unwrap()), + ("client_id", client_id.unwrap()), + ("grant_type", "refresh_token"), + ("refresh_token", refresh_token), + ]) + .send() + .await? + .text() + .await? + } else { + client + .post("https://login.live.com/oauth20_token.srf") + .form(&vec![ + ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), + ("client_id", CLIENT_ID), + ("grant_type", "refresh_token"), + ("refresh_token", refresh_token), + ]) + .send() + .await? + .text() + .await? + }; let access_token_response: AccessTokenResponse = serde_json::from_str(&access_token_response_text)?; diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 741a07d45..b030cdb47 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -90,6 +90,11 @@ 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(email, None, None) + } + + /// Similar to [`account.microsoft()`](Self::microsoft) but you can use your own `client_id` and `scope`. + pub async fn microsoft_with_custom_client_id(email: &str, scope: Option<&str>, client_id: Option<&str>) -> Result { let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| { panic!( "No {} environment variable found", @@ -100,6 +105,8 @@ impl Account { email, azalea_auth::AuthOpts { cache_file: Some(minecraft_dir.join("azalea-auth.json")), + scope, + client_id, ..Default::default() }, ) @@ -128,7 +135,7 @@ 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?; /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code @@ -145,7 +152,7 @@ impl Account { 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, None, None).await?; } let msa_token = &msa.data.access_token; From c28319200bb5178f3ba4c6aca0cb20e9c98a26e8 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Wed, 19 Jun 2024 15:09:30 +0530 Subject: [PATCH 02/16] fix: `Account::microsoft` and added lifetime to `Account::microsoft_with_custom_client_id` --- azalea-client/src/account.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index b030cdb47..24df01341 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -90,11 +90,11 @@ 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(email, None, None) + Self::microsoft_with_custom_client_id(email, None, None).await } /// Similar to [`account.microsoft()`](Self::microsoft) but you can use your own `client_id` and `scope`. - pub async fn microsoft_with_custom_client_id(email: &str, scope: Option<&str>, client_id: Option<&str>) -> Result { + pub async fn microsoft_with_custom_client_id(email: &str, scope: Option<&'static str>, client_id: Option<&'static str>) -> Result { let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| { panic!( "No {} environment variable found", From bc491bf8200f76425048080fb64d9599db1dd364 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Wed, 19 Jun 2024 15:13:45 +0530 Subject: [PATCH 03/16] Added function `with_microsoft_access_token_and_custom_client_id` --- azalea-client/src/account.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 24df01341..631e3189e 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -146,13 +146,22 @@ impl Account { /// # } /// ``` pub async fn with_microsoft_access_token( + msa: azalea_auth::cache::ExpiringValue + ) -> Result { + Self::with_microsoft_access_token_and_custom_client_id(msa, None, None).await + } + + /// Similar to [`Account::with_microsoft_access_token`] but you can you custom `client_id` and `scope` + pub async fn with_microsoft_access_token_and_custom_client_id( mut msa: azalea_auth::cache::ExpiringValue, + scope: Option<&'static str>, + client_id: Option<&'static 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, None, None).await?; + msa = azalea_auth::refresh_ms_auth_token(&client, &msa.data.refresh_token, scope, client_id).await?; } let msa_token = &msa.data.access_token; From d2bb6f4b6d1350b5973cde7c0eeed2570c0dfe7f Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Wed, 19 Jun 2024 15:24:53 +0530 Subject: [PATCH 04/16] Removed Custom Scope. * I got carried away, and made scope also customizable, later realized no customization is needed. --- azalea-auth/src/auth.rs | 68 ++++++++++++++---------------------- azalea-client/src/account.rs | 12 +++---- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 505f4c99f..6d181225d 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -24,7 +24,7 @@ pub struct AuthOpts { /// The directory to store the cache in. If this is not set, caching is not /// done. pub cache_file: Option, - pub scope: Option<&'static str>, + /// If you choose to use your own microsoft authentication instead of using nintendo switch, just put your client_id here pub client_id: Option<&'static str> } @@ -62,10 +62,6 @@ pub enum AuthError { /// 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 { - if !((opts.scope.is_some() && opts.client_id.is_some()) || (opts.scope.is_none() && opts.client_id.is_none())) { - panic!("Either provide both `scope` and `client_id` or nether.") - } - let cached_account = if let Some(cache_file) = &opts.cache_file { cache::get_account_in_cache(cache_file, email).await } else { @@ -86,16 +82,16 @@ pub async fn auth(email: &str, opts: AuthOpts) -> Result let mut msa = if let Some(account) = cached_account { account.msa } else { - interactive_get_ms_auth_token(&client, email, opts.scope, opts.client_id).await? + interactive_get_ms_auth_token(&client, email, opts.client_id).await? }; if msa.is_expired() { tracing::trace!("refreshing Microsoft auth token"); - match refresh_ms_auth_token(&client, &msa.data.refresh_token, opts.scope, opts.client_id).await { + match refresh_ms_auth_token(&client, &msa.data.refresh_token, opts.client_id).await { Ok(new_msa) => msa = new_msa, Err(e) => { // can't refresh, ask the user to auth again tracing::error!("Error refreshing Microsoft auth token: {}", e); - msa = interactive_get_ms_auth_token(&client, email, opts.scope, opts.client_id).await?; + msa = interactive_get_ms_auth_token(&client, email, opts.client_id).await?; } } } @@ -286,7 +282,7 @@ pub enum GetMicrosoftAuthTokenError { /// /// ``` /// # async fn example(client: &reqwest::Client) -> Result<(), Box> { -/// let res = azalea_auth::get_ms_link_code(&client, None, None).await?; +/// let res = azalea_auth::get_ms_link_code(&client, None).await?; /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code @@ -299,19 +295,18 @@ pub enum GetMicrosoftAuthTokenError { /// ``` pub async fn get_ms_link_code( client: &reqwest::Client, - scope: Option<&str>, client_id: Option<&str> ) -> Result { - let (scope, client_id) = if scope.is_some() && client_id.is_some() { - (scope.unwrap(), client_id.unwrap()) + let client_id = if client_id.is_some() { + client_id.unwrap() } else { - ("service::user.auth.xboxlive.com::MBI_SSL", CLIENT_ID) + CLIENT_ID }; Ok(client .post("https://login.live.com/oauth20_connect.srf") .form(&vec![ - ("scope", scope), + ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), ("client_id", client_id), ("response_type", "device_code"), ]) @@ -371,10 +366,9 @@ pub async fn get_ms_auth_token( pub async fn interactive_get_ms_auth_token( client: &reqwest::Client, email: &str, - scope: Option<&str>, client_id: Option<&str> ) -> Result, GetMicrosoftAuthTokenError> { - let res = get_ms_link_code(client, scope, client_id).await?; + let res = get_ms_link_code(client, client_id).await?; tracing::trace!("Device code response: {:?}", res); println!( "Go to \x1b[1m{}\x1b[m and enter the code \x1b[1m{}\x1b[m for \x1b[1m{}\x1b[m", @@ -395,36 +389,26 @@ pub enum RefreshMicrosoftAuthTokenError { pub async fn refresh_ms_auth_token( client: &reqwest::Client, refresh_token: &str, - scope: Option<&str>, client_id: Option<&str> ) -> Result, RefreshMicrosoftAuthTokenError> { - let access_token_response_text = if scope.is_some() && client_id.is_some() { - client - .post("https://login.live.com/oauth20_token.srf") - .form(&vec![ - ("scope", scope.unwrap()), - ("client_id", client_id.unwrap()), - ("grant_type", "refresh_token"), - ("refresh_token", refresh_token), - ]) - .send() - .await? - .text() - .await? + let client_id = if client_id.is_some() { + client_id.unwrap() } else { - client - .post("https://login.live.com/oauth20_token.srf") - .form(&vec![ - ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), - ("client_id", CLIENT_ID), - ("grant_type", "refresh_token"), - ("refresh_token", refresh_token), - ]) - .send() - .await? - .text() - .await? + CLIENT_ID }; + + let access_token_response_text = client + .post("https://login.live.com/oauth20_token.srf") + .form(&vec![ + ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), + ("client_id", client_id.unwrap()), + ("grant_type", "refresh_token"), + ("refresh_token", refresh_token), + ]) + .send() + .await? + .text() + .await?; let access_token_response: AccessTokenResponse = serde_json::from_str(&access_token_response_text)?; diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 631e3189e..ac06408db 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -90,11 +90,11 @@ 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(email, None, None).await + Self::microsoft_with_custom_client_id(email, None).await } /// Similar to [`account.microsoft()`](Self::microsoft) but you can use your own `client_id` and `scope`. - pub async fn microsoft_with_custom_client_id(email: &str, scope: Option<&'static str>, client_id: Option<&'static str>) -> Result { + pub async fn microsoft_with_custom_client_id(email: &str, client_id: Option<&'static str>) -> Result { let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| { panic!( "No {} environment variable found", @@ -105,7 +105,6 @@ impl Account { email, azalea_auth::AuthOpts { cache_file: Some(minecraft_dir.join("azalea-auth.json")), - scope, client_id, ..Default::default() }, @@ -135,7 +134,7 @@ impl Account { /// # async fn example() -> Result<(), Box> { /// let client = reqwest::Client::new(); /// - /// let res = azalea_auth::get_ms_link_code(&client, None, None).await?; + /// let res = azalea_auth::get_ms_link_code(&client, None).await?; /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code @@ -148,20 +147,19 @@ impl Account { pub async fn with_microsoft_access_token( msa: azalea_auth::cache::ExpiringValue ) -> Result { - Self::with_microsoft_access_token_and_custom_client_id(msa, None, None).await + Self::with_microsoft_access_token_and_custom_client_id(msa, None).await } /// Similar to [`Account::with_microsoft_access_token`] but you can you custom `client_id` and `scope` pub async fn with_microsoft_access_token_and_custom_client_id( mut msa: azalea_auth::cache::ExpiringValue, - scope: Option<&'static str>, client_id: Option<&'static 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, scope, client_id).await?; + msa = azalea_auth::refresh_ms_auth_token(&client, &msa.data.refresh_token, client_id).await?; } let msa_token = &msa.data.access_token; From a53a9c4f4a9baebbf960d7200c34005fc8e26eb4 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Wed, 19 Jun 2024 15:32:51 +0530 Subject: [PATCH 05/16] Better Documentation and Minor fixes --- azalea-auth/src/auth.rs | 4 ++-- azalea-client/src/account.rs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 6d181225d..82169e9ac 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -389,7 +389,7 @@ pub enum RefreshMicrosoftAuthTokenError { pub async fn refresh_ms_auth_token( client: &reqwest::Client, refresh_token: &str, - client_id: Option<&str> + client_id: Option<&'static str> ) -> Result, RefreshMicrosoftAuthTokenError> { let client_id = if client_id.is_some() { client_id.unwrap() @@ -401,7 +401,7 @@ pub async fn refresh_ms_auth_token( .post("https://login.live.com/oauth20_token.srf") .form(&vec![ ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), - ("client_id", client_id.unwrap()), + ("client_id", client_id), ("grant_type", "refresh_token"), ("refresh_token", refresh_token), ]) diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index ac06408db..5e047283d 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -93,7 +93,7 @@ impl Account { Self::microsoft_with_custom_client_id(email, None).await } - /// Similar to [`account.microsoft()`](Self::microsoft) but you can use your own `client_id` and `scope`. + /// Similar to [`account.microsoft()`](Self::microsoft) but you can use your own `client_id`. pub async fn microsoft_with_custom_client_id(email: &str, client_id: Option<&'static str>) -> Result { let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| { panic!( @@ -135,6 +135,8 @@ impl Account { /// let client = reqwest::Client::new(); /// /// let res = azalea_auth::get_ms_link_code(&client, None).await?; + /// // Or, `azalea_auth::get_ms_link_code(&client, Some(client_id)).await?` + /// // if you want to use your own client_id /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code @@ -150,7 +152,7 @@ impl Account { Self::with_microsoft_access_token_and_custom_client_id(msa, None).await } - /// Similar to [`Account::with_microsoft_access_token`] but you can you custom `client_id` and `scope` + /// Similar to [`Account::with_microsoft_access_token`] but you can you custom `client_id`. pub async fn with_microsoft_access_token_and_custom_client_id( mut msa: azalea_auth::cache::ExpiringValue, client_id: Option<&'static str> From 32ff19095923d2976b8382689e38b8f8efd5fb6a Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Thu, 27 Jun 2024 17:14:12 +0530 Subject: [PATCH 06/16] Added Custom Scope --- azalea-auth/src/auth.rs | 44 ++++++++++++++++++------------------ azalea-client/src/account.rs | 22 ++++++++++-------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 82169e9ac..09681b0ca 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -25,7 +25,9 @@ pub struct AuthOpts { /// done. pub cache_file: Option, /// If you choose to use your own microsoft authentication instead of using nintendo switch, just put your client_id here - pub client_id: Option<&'static str> + pub client_id: Option<&'static str>, + /// If you want to use custom scope instead of default one + pub scope: Option<&'static str> } #[derive(Debug, Error)] @@ -78,20 +80,23 @@ pub async fn auth(email: &str, opts: AuthOpts) -> Result profile: account.profile.clone(), }) } else { + let client_id= opts.client_id.unwrap_or(CLIENT_ID); + let scope = opts.scope.unwrap_or(SCOPE); + let client = reqwest::Client::new(); let mut msa = if let Some(account) = cached_account { account.msa } else { - interactive_get_ms_auth_token(&client, email, opts.client_id).await? + interactive_get_ms_auth_token(&client, email, client_id, scope).await? }; if msa.is_expired() { tracing::trace!("refreshing Microsoft auth token"); - match refresh_ms_auth_token(&client, &msa.data.refresh_token, opts.client_id).await { + match refresh_ms_auth_token(&client, &msa.data.refresh_token, opts.client_id, opts.scope).await { Ok(new_msa) => msa = new_msa, Err(e) => { // can't refresh, ask the user to auth again tracing::error!("Error refreshing Microsoft auth token: {}", e); - msa = interactive_get_ms_auth_token(&client, email, opts.client_id).await?; + msa = interactive_get_ms_auth_token(&client, email, client_id, scope).await?; } } } @@ -261,6 +266,7 @@ pub struct ProfileResponse { // nintendo switch (so it works for accounts that are under 18 years old) const CLIENT_ID: &str = "00000000441cc96b"; +const SCOPE: &str = "service::user.auth.xboxlive.com::MBI_SSL"; #[derive(Debug, Error)] pub enum GetMicrosoftAuthTokenError { @@ -282,7 +288,7 @@ pub enum GetMicrosoftAuthTokenError { /// /// ``` /// # async fn example(client: &reqwest::Client) -> Result<(), Box> { -/// let res = azalea_auth::get_ms_link_code(&client, None).await?; +/// let res = azalea_auth::get_ms_link_code(&client, "00000000441cc96b", "service::user.auth.xboxlive.com::MBI_SSL").await?; /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code @@ -295,18 +301,13 @@ pub enum GetMicrosoftAuthTokenError { /// ``` pub async fn get_ms_link_code( client: &reqwest::Client, - client_id: Option<&str> + client_id: &str, + scope: &str ) -> Result { - let client_id = if client_id.is_some() { - client_id.unwrap() - } else { - CLIENT_ID - }; - Ok(client .post("https://login.live.com/oauth20_connect.srf") .form(&vec![ - ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), + ("scope", scope), ("client_id", client_id), ("response_type", "device_code"), ]) @@ -366,9 +367,10 @@ pub async fn get_ms_auth_token( pub async fn interactive_get_ms_auth_token( client: &reqwest::Client, email: &str, - client_id: Option<&str> + client_id: &str, + scope: &str ) -> Result, GetMicrosoftAuthTokenError> { - let res = get_ms_link_code(client, client_id).await?; + let res = get_ms_link_code(client, client_id, scope).await?; tracing::trace!("Device code response: {:?}", res); println!( "Go to \x1b[1m{}\x1b[m and enter the code \x1b[1m{}\x1b[m for \x1b[1m{}\x1b[m", @@ -389,18 +391,16 @@ pub enum RefreshMicrosoftAuthTokenError { pub async fn refresh_ms_auth_token( client: &reqwest::Client, refresh_token: &str, - client_id: Option<&'static str> + client_id: Option<&str>, + scope: Option<&str> ) -> Result, RefreshMicrosoftAuthTokenError> { - let client_id = if client_id.is_some() { - client_id.unwrap() - } else { - CLIENT_ID - }; + let client_id= client_id.unwrap_or(CLIENT_ID); + let scope = scope.unwrap_or(SCOPE); let access_token_response_text = client .post("https://login.live.com/oauth20_token.srf") .form(&vec![ - ("scope", "service::user.auth.xboxlive.com::MBI_SSL"), + ("scope", scope), ("client_id", client_id), ("grant_type", "refresh_token"), ("refresh_token", refresh_token), diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 5e047283d..ac1b74160 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -90,11 +90,13 @@ 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(email, None).await + 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`. - pub async fn microsoft_with_custom_client_id(email: &str, client_id: Option<&'static str>) -> Result { + /// 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<&'static str>, scope: Option<&'static str>) -> Result { let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| { panic!( "No {} environment variable found", @@ -106,6 +108,7 @@ impl Account { azalea_auth::AuthOpts { cache_file: Some(minecraft_dir.join("azalea-auth.json")), client_id, + scope, ..Default::default() }, ) @@ -134,7 +137,7 @@ impl Account { /// # async fn example() -> Result<(), Box> { /// let client = reqwest::Client::new(); /// - /// let res = azalea_auth::get_ms_link_code(&client, None).await?; + /// let res = azalea_auth::get_ms_link_code(&client, "00000000441cc96b", "service::user. auth. xboxlive. com::MBI_SSL").await?; /// // Or, `azalea_auth::get_ms_link_code(&client, Some(client_id)).await?` /// // if you want to use your own client_id /// println!( @@ -149,19 +152,20 @@ impl Account { pub async fn with_microsoft_access_token( msa: azalea_auth::cache::ExpiringValue ) -> Result { - Self::with_microsoft_access_token_and_custom_client_id(msa, None).await + 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 you custom `client_id`. - pub async fn with_microsoft_access_token_and_custom_client_id( + /// 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<&'static str> + client_id: Option<&'static str>, + scope: Option<&'static 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, client_id).await?; + msa = azalea_auth::refresh_ms_auth_token(&client, &msa.data.refresh_token, client_id, scope).await?; } let msa_token = &msa.data.access_token; From a5985dd502ae83615e98b9a318be3338072c4636 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:01:00 +0530 Subject: [PATCH 07/16] Added RpsTicket format for custom `client_id` --- azalea-auth/examples/auth_manual.rs | 9 +++++++-- azalea-auth/src/auth.rs | 17 +++++++++++------ azalea-client/src/account.rs | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/azalea-auth/examples/auth_manual.rs b/azalea-auth/examples/auth_manual.rs index 6a9510a8f..5984a829f 100755 --- a/azalea-auth/examples/auth_manual.rs +++ b/azalea-auth/examples/auth_manual.rs @@ -18,15 +18,20 @@ async fn main() -> Result<(), Box> { Ok(()) } +// We will be using default `client_id` and `scope` +// If you want to use your own replace `CLIENT_ID` and `SCOPE` with your own. async fn auth() -> Result> { let client = reqwest::Client::new(); - let res = azalea_auth::get_ms_link_code(&client).await?; + let res = azalea_auth::get_ms_link_code(&client, CLIENT_ID, SCOPE).await?; 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, CLIENT_ID).await?; let auth_result = azalea_auth::get_minecraft_token(&client, &msa.data.access_token).await?; Ok(azalea_auth::get_profile(&client, &auth_result.minecraft_access_token).await?) } + +const CLIENT_ID: &str = "00000000441cc96b"; +const SCOPE: &str = "service::user.auth.xboxlive.com::MBI_SSL"; diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 09681b0ca..404e54af4 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -288,12 +288,12 @@ pub enum GetMicrosoftAuthTokenError { /// /// ``` /// # async fn example(client: &reqwest::Client) -> Result<(), Box> { -/// let res = azalea_auth::get_ms_link_code(&client, "00000000441cc96b", "service::user.auth.xboxlive.com::MBI_SSL").await?; +/// let res = azalea_auth::get_ms_link_code(&client, "client_id", "scope").await?; /// 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, "client_id").await?; /// let minecraft = azalea_auth::get_minecraft_token(client, &msa.data.access_token).await?; /// let profile = azalea_auth::get_profile(&client, &minecraft.minecraft_access_token).await?; /// # Ok(()) @@ -324,6 +324,7 @@ pub async fn get_ms_link_code( pub async fn get_ms_auth_token( client: &reqwest::Client, res: DeviceCodeResponse, + client_id: &str ) -> Result, GetMicrosoftAuthTokenError> { let login_expires_at = Instant::now() + std::time::Duration::from_secs(res.expires_in); @@ -331,12 +332,12 @@ pub async fn get_ms_auth_token( tokio::time::sleep(std::time::Duration::from_secs(res.interval)).await; tracing::trace!("Polling to check if user has logged in..."); - if let Ok(access_token_response) = client + if let Ok(mut access_token_response) = client .post(format!( - "https://login.live.com/oauth20_token.srf?client_id={CLIENT_ID}" + "https://login.live.com/oauth20_token.srf?client_id={client_id}" )) .form(&vec![ - ("client_id", CLIENT_ID), + ("client_id", client_id), ("device_code", &res.device_code), ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"), ]) @@ -345,6 +346,10 @@ pub async fn get_ms_auth_token( .json::() .await { + if client_id != CLIENT_ID { + access_token_response.access_token.insert_str(0, "d="); + } + tracing::trace!("access_token_response: {:?}", access_token_response); let expires_at = SystemTime::now() + std::time::Duration::from_secs(access_token_response.expires_in); @@ -377,7 +382,7 @@ pub async fn interactive_get_ms_auth_token( res.verification_uri, res.user_code, email ); - get_ms_auth_token(client, res).await + get_ms_auth_token(client, res, client_id).await } #[derive(Debug, Error)] diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index ac1b74160..0367e0f53 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -137,14 +137,14 @@ impl Account { /// # async fn example() -> Result<(), Box> { /// let client = reqwest::Client::new(); /// - /// let res = azalea_auth::get_ms_link_code(&client, "00000000441cc96b", "service::user. auth. xboxlive. com::MBI_SSL").await?; + /// let res = azalea_auth::get_ms_link_code(&client, "client_id", "scope").await?; /// // Or, `azalea_auth::get_ms_link_code(&client, Some(client_id)).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, "client_id").await?; /// Account::with_microsoft_access_token(msa).await?; /// # Ok(()) /// # } From c3d4e7fb4111218db22ab52cc5b48c0c3d9592d8 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 7 Jul 2024 19:40:00 +0530 Subject: [PATCH 08/16] Moved to non-static str --- azalea-auth/src/auth.rs | 8 ++++---- azalea-client/src/account.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 404e54af4..9deb845a0 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -13,7 +13,7 @@ use thiserror::Error; use uuid::Uuid; #[derive(Default)] -pub struct AuthOpts { +pub struct AuthOpts<'a> { /// Whether we should check if the user actually owns the game. This will /// fail if the user has Xbox Game Pass! Note that this isn't really /// necessary, since getting the user profile will check this anyways. @@ -25,9 +25,9 @@ pub struct AuthOpts { /// done. pub cache_file: Option, /// If you choose to use your own microsoft authentication instead of using nintendo switch, just put your client_id here - pub client_id: Option<&'static str>, + pub client_id: Option<&'a str>, /// If you want to use custom scope instead of default one - pub scope: Option<&'static str> + pub scope: Option<&'a str> } #[derive(Debug, Error)] @@ -63,7 +63,7 @@ pub enum AuthError { /// 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<'a>(email: &str, opts: AuthOpts<'a>) -> Result { let cached_account = if let Some(cache_file) = &opts.cache_file { cache::get_account_in_cache(cache_file, email).await } else { diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 0367e0f53..65520f4b6 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -96,7 +96,7 @@ impl Account { /// 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<&'static str>, scope: Option<&'static str>) -> Result { + 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", @@ -158,8 +158,8 @@ impl Account { /// 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<&'static str>, - scope: Option<&'static str> + client_id: Option<&str>, + scope: Option<&str> ) -> Result { let client = reqwest::Client::new(); From 4dabc1441d0c85d19b710dd621995ab39cf8eb3c Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:12:09 +0530 Subject: [PATCH 09/16] fix example Co-authored-by: mat <27899617+mat-1@users.noreply.github.com> --- azalea-client/src/account.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 65520f4b6..8717d28e4 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -137,8 +137,8 @@ impl Account { /// # async fn example() -> Result<(), Box> { /// let client = reqwest::Client::new(); /// - /// let res = azalea_auth::get_ms_link_code(&client, "client_id", "scope").await?; - /// // Or, `azalea_auth::get_ms_link_code(&client, Some(client_id)).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 {}", From ab94c396978526b8eec29d0310bd31a737963904 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:18:33 +0530 Subject: [PATCH 10/16] fix doc grammer --- azalea-auth/src/auth.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 9deb845a0..29ed1706c 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -24,9 +24,9 @@ pub struct AuthOpts<'a> { /// The directory to store the cache in. If this is not set, caching is not /// done. pub cache_file: Option, - /// If you choose to use your own microsoft authentication instead of using nintendo switch, just put your client_id here + /// If you choose to use your own Microsoft authentication instead of using Nintendo Switch, just put your client_id here. pub client_id: Option<&'a str>, - /// If you want to use custom scope instead of default one + /// If you want to use custom scope instead of default one, just put your scope here. pub scope: Option<&'a str> } From de33ea22cf30ac7a2130efa65e6217928135eb98 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:31:03 +0530 Subject: [PATCH 11/16] changed function signature --- azalea-auth/src/auth.rs | 57 ++++++++++++++++++++++++++---------- azalea-client/src/account.rs | 2 +- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index 29ed1706c..ba28bff25 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -24,10 +24,12 @@ pub struct AuthOpts<'a> { /// The directory to store the cache in. If this is not set, caching is not /// done. pub cache_file: Option, - /// If you choose to use your own Microsoft authentication instead of using Nintendo Switch, just put your client_id here. + /// If you choose to use your own Microsoft authentication instead of using + /// Nintendo Switch, just put your client_id here. pub client_id: Option<&'a str>, - /// If you want to use custom scope instead of default one, just put your scope here. - pub scope: Option<&'a str> + /// If you want to use custom scope instead of default one, just put your + /// scope here. + pub scope: Option<&'a str>, } #[derive(Debug, Error)] @@ -80,23 +82,32 @@ pub async fn auth<'a>(email: &str, opts: AuthOpts<'a>) -> Result msa = new_msa, Err(e) => { // can't refresh, ask the user to auth again tracing::error!("Error refreshing Microsoft auth token: {}", e); - msa = interactive_get_ms_auth_token(&client, email, client_id, scope).await?; + msa = + interactive_get_ms_auth_token(&client, email, Some(client_id), Some(scope)) + .await?; } } } @@ -288,12 +299,12 @@ pub enum GetMicrosoftAuthTokenError { /// /// ``` /// # async fn example(client: &reqwest::Client) -> Result<(), Box> { -/// let res = azalea_auth::get_ms_link_code(&client, "client_id", "scope").await?; +/// let res = azalea_auth::get_ms_link_code(&client, None, None).await?; /// println!( /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code /// ); -/// let msa = azalea_auth::get_ms_auth_token(client, res, "client_id").await?; +/// let msa = azalea_auth::get_ms_auth_token(client, res, None).await?; /// let minecraft = azalea_auth::get_minecraft_token(client, &msa.data.access_token).await?; /// let profile = azalea_auth::get_profile(&client, &minecraft.minecraft_access_token).await?; /// # Ok(()) @@ -301,9 +312,17 @@ pub enum GetMicrosoftAuthTokenError { /// ``` pub async fn get_ms_link_code( client: &reqwest::Client, - client_id: &str, - scope: &str + client_id: Option<&str>, + scope: Option<&str>, ) -> Result { + let client_id = if let Some(c) = client_id { + c + } else { + CLIENT_ID + }; + + let scope = if let Some(c) = scope { c } else { SCOPE }; + Ok(client .post("https://login.live.com/oauth20_connect.srf") .form(&vec![ @@ -324,8 +343,14 @@ pub async fn get_ms_link_code( pub async fn get_ms_auth_token( client: &reqwest::Client, res: DeviceCodeResponse, - client_id: &str + client_id: Option<&str>, ) -> Result, GetMicrosoftAuthTokenError> { + let client_id = if let Some(c) = client_id { + c + } else { + CLIENT_ID + }; + let login_expires_at = Instant::now() + std::time::Duration::from_secs(res.expires_in); while Instant::now() < login_expires_at { @@ -372,8 +397,8 @@ pub async fn get_ms_auth_token( pub async fn interactive_get_ms_auth_token( client: &reqwest::Client, email: &str, - client_id: &str, - scope: &str + client_id: Option<&str>, + scope: Option<&str>, ) -> Result, GetMicrosoftAuthTokenError> { let res = get_ms_link_code(client, client_id, scope).await?; tracing::trace!("Device code response: {:?}", res); @@ -397,9 +422,9 @@ pub async fn refresh_ms_auth_token( client: &reqwest::Client, refresh_token: &str, client_id: Option<&str>, - scope: Option<&str> + scope: Option<&str>, ) -> Result, RefreshMicrosoftAuthTokenError> { - let client_id= client_id.unwrap_or(CLIENT_ID); + let client_id = client_id.unwrap_or(CLIENT_ID); let scope = scope.unwrap_or(SCOPE); let access_token_response_text = client diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 8717d28e4..02f2d63c0 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -144,7 +144,7 @@ impl Account { /// "Go to {} and enter the code {}", /// res.verification_uri, res.user_code /// ); - /// let msa = azalea_auth::get_ms_auth_token(&client, res, "client_id").await?; + /// let msa = azalea_auth::get_ms_auth_token(&client, res, None).await?; /// Account::with_microsoft_access_token(msa).await?; /// # Ok(()) /// # } From 8826ceeafc7c3258e634e76a202a611d4adb487a Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:31:13 +0530 Subject: [PATCH 12/16] fmt --- azalea-client/src/account.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 02f2d63c0..c007e01ab 100755 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -93,10 +93,15 @@ impl Account { 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`. + /// 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 { + 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", @@ -150,22 +155,29 @@ impl Account { /// # } /// ``` pub async fn with_microsoft_access_token( - msa: azalea_auth::cache::ExpiringValue + 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`. + /// 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> + 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, client_id, scope).await?; + msa = azalea_auth::refresh_ms_auth_token( + &client, + &msa.data.refresh_token, + client_id, + scope, + ) + .await?; } let msa_token = &msa.data.access_token; From 824f06743cbe28525810b29a9e5638147171aac9 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:34:24 +0530 Subject: [PATCH 13/16] fixed example --- azalea-auth/examples/auth_manual.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azalea-auth/examples/auth_manual.rs b/azalea-auth/examples/auth_manual.rs index 5984a829f..e8d6f7300 100755 --- a/azalea-auth/examples/auth_manual.rs +++ b/azalea-auth/examples/auth_manual.rs @@ -23,12 +23,12 @@ async fn main() -> Result<(), Box> { async fn auth() -> Result> { let client = reqwest::Client::new(); - let res = azalea_auth::get_ms_link_code(&client, CLIENT_ID, SCOPE).await?; + let res = azalea_auth::get_ms_link_code(&client, None, None).await?; println!( "Go to {} and enter the code {}", res.verification_uri, res.user_code ); - let msa = azalea_auth::get_ms_auth_token(&client, res, CLIENT_ID).await?; + let msa = azalea_auth::get_ms_auth_token(&client, res, None).await?; let auth_result = azalea_auth::get_minecraft_token(&client, &msa.data.access_token).await?; Ok(azalea_auth::get_profile(&client, &auth_result.minecraft_access_token).await?) } From 580a0584f9ce16c2a64e3ef4ea4c54ba8bb61663 Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:35:30 +0530 Subject: [PATCH 14/16] removed dead code --- azalea-auth/examples/auth_manual.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/azalea-auth/examples/auth_manual.rs b/azalea-auth/examples/auth_manual.rs index e8d6f7300..05803c875 100755 --- a/azalea-auth/examples/auth_manual.rs +++ b/azalea-auth/examples/auth_manual.rs @@ -19,7 +19,6 @@ async fn main() -> Result<(), Box> { } // We will be using default `client_id` and `scope` -// If you want to use your own replace `CLIENT_ID` and `SCOPE` with your own. async fn auth() -> Result> { let client = reqwest::Client::new(); @@ -32,6 +31,3 @@ async fn auth() -> Result> { let auth_result = azalea_auth::get_minecraft_token(&client, &msa.data.access_token).await?; Ok(azalea_auth::get_profile(&client, &auth_result.minecraft_access_token).await?) } - -const CLIENT_ID: &str = "00000000441cc96b"; -const SCOPE: &str = "service::user.auth.xboxlive.com::MBI_SSL"; From 9f30d2f14af6293b203d100ec6f7168917f5282a Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:37:09 +0530 Subject: [PATCH 15/16] Removed `d=` insertion in `client_id` --- azalea-auth/src/auth.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index ba28bff25..ba4be7edd 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -371,10 +371,6 @@ pub async fn get_ms_auth_token( .json::() .await { - if client_id != CLIENT_ID { - access_token_response.access_token.insert_str(0, "d="); - } - tracing::trace!("access_token_response: {:?}", access_token_response); let expires_at = SystemTime::now() + std::time::Duration::from_secs(access_token_response.expires_in); From 6b1ec2814db9e03391da627bb51dd536d7bb053e Mon Sep 17 00:00:00 2001 From: Aditya Kumar <117935160+AS1100K@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:42:53 +0530 Subject: [PATCH 16/16] removed unnecessary `mut` --- azalea-auth/src/auth.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azalea-auth/src/auth.rs b/azalea-auth/src/auth.rs index ba4be7edd..08d2d3a84 100755 --- a/azalea-auth/src/auth.rs +++ b/azalea-auth/src/auth.rs @@ -357,7 +357,7 @@ pub async fn get_ms_auth_token( tokio::time::sleep(std::time::Duration::from_secs(res.interval)).await; tracing::trace!("Polling to check if user has logged in..."); - if let Ok(mut access_token_response) = client + if let Ok(access_token_response) = client .post(format!( "https://login.live.com/oauth20_token.srf?client_id={client_id}" ))