diff options
| author | Aditya Kumar <117935160+AS1100K@users.noreply.github.com> | 2024-08-12 03:24:45 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-11 16:54:45 -0500 |
| commit | 13afc1d6a42d28f01087fb59ca868696be4a2635 (patch) | |
| tree | a3b77e5a7529c764a9f384407b580e273b0b3b55 /azalea-client/src | |
| parent | 92c90753eac774add5d033a567f9af115dad4ea0 (diff) | |
| download | azalea-drasl-13afc1d6a42d28f01087fb59ca868696be4a2635.tar.xz | |
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>
Diffstat (limited to 'azalea-client/src')
| -rwxr-xr-x | azalea-client/src/account.rs | 38 |
1 files changed, 35 insertions, 3 deletions
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, azalea_auth::AuthError> { + 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<Self, azalea_auth::AuthError> { 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<dyn std::error::Error>> { /// 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<AccessTokenResponse>, + ) -> Result<Self, azalea_auth::AuthError> { + 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<AccessTokenResponse>, + client_id: Option<&str>, + scope: Option<&str>, ) -> Result<Self, azalea_auth::AuthError> { 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; |
