Skip to content

Commit 4ab3609

Browse files
authored
Add Account::microsoft_with_opts (#306)
* Refactor Microsoft account auth options handling also some cache stuff Read PR * Make MicrosoftAccountOpts public and clarify usage
1 parent f92df14 commit 4ab3609

File tree

1 file changed

+88
-23
lines changed

1 file changed

+88
-23
lines changed

azalea-client/src/account/microsoft.rs

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use std::path::PathBuf;
2+
13
use azalea_auth::{
24
AccessTokenResponse,
5+
AuthOpts,
36
certs::Certificates,
47
sessionserver::{self, ClientSessionServerError, SessionServerJoinOpts},
58
};
@@ -8,13 +11,68 @@ use uuid::Uuid;
811

912
use crate::account::{Account, AccountTrait, BoxFuture};
1013

14+
fn default_cache_file() -> PathBuf {
15+
let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| {
16+
panic!(
17+
"No {} environment variable found",
18+
minecraft_folder_path::home_env_var()
19+
)
20+
});
21+
minecraft_dir.join("azalea-auth.json")
22+
}
23+
24+
/// Options for Microsoft authentication in Azalea.
25+
///
26+
/// This is used by [`Account::microsoft_with_opts`].
27+
#[derive(Clone, Debug, Default)]
28+
pub struct MicrosoftAccountOpts {
29+
/// Whether we should check if the user owns the game.
30+
pub check_ownership: bool,
31+
/// The cache file to use for the auth cache.
32+
///
33+
/// If this is `None`, Azalea will default to its standard cache file
34+
/// (`~/.minecraft/azalea-auth.json`).
35+
pub cache_file: Option<PathBuf>,
36+
/// An override for the Microsoft Client ID to authenticate with.
37+
pub client_id: Option<String>,
38+
/// An override for the OAuth2 scope to authenticate with.
39+
pub scope: Option<String>,
40+
}
41+
42+
impl MicrosoftAccountOpts {
43+
fn to_auth_opts(&self) -> AuthOpts<'_> {
44+
let cache_file = self
45+
.cache_file
46+
.clone()
47+
.or_else(|| Some(default_cache_file()));
48+
49+
AuthOpts {
50+
check_ownership: self.check_ownership,
51+
cache_file,
52+
client_id: self.client_id.as_deref(),
53+
scope: self.scope.as_deref(),
54+
}
55+
}
56+
}
57+
58+
fn default_account_opts(client_id: Option<&str>, scope: Option<&str>) -> MicrosoftAccountOpts {
59+
MicrosoftAccountOpts {
60+
check_ownership: false,
61+
cache_file: Some(default_cache_file()),
62+
client_id: client_id.map(str::to_owned),
63+
scope: scope.map(str::to_owned),
64+
}
65+
}
66+
1167
/// A type of account that authenticates with Microsoft using Azalea's cache.
1268
///
1369
/// This type is not intended to be used directly by the user. To actually make
14-
/// an account that authenticates with Microsoft, see [`Account::microsoft`].
70+
/// an account that authenticates with Microsoft, see [`Account::microsoft`] or
71+
/// [`Account::microsoft_with_opts`].
1572
#[derive(Debug)]
1673
pub struct MicrosoftAccount {
1774
cache_key: String,
75+
auth_opts: MicrosoftAccountOpts,
1876

1977
username: String,
2078
uuid: Uuid,
@@ -24,31 +82,16 @@ pub struct MicrosoftAccount {
2482
}
2583
impl MicrosoftAccount {
2684
// deliberately private, use `Account::microsoft` or
27-
// `Account::microsoft_with_custom_client_id_and_scope` instead.
85+
// `Account::microsoft_with_opts` instead.
2886
async fn new(
2987
cache_key: &str,
30-
client_id: Option<&str>,
31-
scope: Option<&str>,
88+
auth_opts: MicrosoftAccountOpts,
3289
) -> Result<Self, azalea_auth::AuthError> {
33-
let minecraft_dir = minecraft_folder_path::minecraft_dir().unwrap_or_else(|| {
34-
panic!(
35-
"No {} environment variable found",
36-
minecraft_folder_path::home_env_var()
37-
)
38-
});
39-
let auth_result = azalea_auth::auth(
40-
cache_key,
41-
azalea_auth::AuthOpts {
42-
cache_file: Some(minecraft_dir.join("azalea-auth.json")),
43-
client_id,
44-
scope,
45-
..Default::default()
46-
},
47-
)
48-
.await?;
90+
let auth_result = azalea_auth::auth(cache_key, auth_opts.to_auth_opts()).await?;
4991

5092
Ok(Self {
5193
cache_key: cache_key.to_owned(),
94+
auth_opts,
5295
username: auth_result.profile.name,
5396
uuid: auth_result.profile.id,
5497
access_token: Mutex::new(auth_result.access_token),
@@ -74,7 +117,7 @@ impl AccountTrait for MicrosoftAccount {
74117
}
75118
fn refresh(&self) -> BoxFuture<'_, Result<(), azalea_auth::AuthError>> {
76119
Box::pin(async {
77-
let new_account = MicrosoftAccount::new(&self.cache_key, None, None).await?;
120+
let new_account = MicrosoftAccount::new(&self.cache_key, self.auth_opts.clone()).await?;
78121
let new_access_token = new_account.access_token().unwrap();
79122
*self.access_token.lock() = new_access_token;
80123
Ok(())
@@ -215,20 +258,42 @@ impl Account {
215258
/// typically set to the account email, but it can be any string.
216259
#[cfg(feature = "online-mode")]
217260
pub async fn microsoft(cache_key: &str) -> Result<Self, azalea_auth::AuthError> {
218-
Self::microsoft_with_custom_client_id_and_scope(cache_key, None, None).await
261+
MicrosoftAccount::new(cache_key, default_account_opts(None, None))
262+
.await
263+
.map(Account::from)
264+
}
265+
266+
/// Similar to [`Account::microsoft`] but you can pass custom auth options
267+
/// (including the cache file location).
268+
///
269+
/// For a custom cache directory, set
270+
/// `auth_opts.cache_file = Some(custom_dir.join("azalea-auth.json"))`.
271+
///
272+
/// If `auth_opts.cache_file` is `None`, it will default to Azalea's
273+
/// standard cache file (`~/.minecraft/azalea-auth.json`) to match
274+
/// [`Account::microsoft`].
275+
#[cfg(feature = "online-mode")]
276+
pub async fn microsoft_with_opts(
277+
cache_key: &str,
278+
auth_opts: MicrosoftAccountOpts,
279+
) -> Result<Self, azalea_auth::AuthError> {
280+
MicrosoftAccount::new(cache_key, auth_opts)
281+
.await
282+
.map(Account::from)
219283
}
220284

221285
/// Similar to [`Account::microsoft`] but you can use your own `client_id`
222286
/// and `scope`.
223287
///
224288
/// Pass `None` if you want to use default ones.
225289
#[cfg(feature = "online-mode")]
290+
#[deprecated(note = "Use `Account::microsoft_with_opts` instead.")]
226291
pub async fn microsoft_with_custom_client_id_and_scope(
227292
cache_key: &str,
228293
client_id: Option<&str>,
229294
scope: Option<&str>,
230295
) -> Result<Self, azalea_auth::AuthError> {
231-
MicrosoftAccount::new(cache_key, client_id, scope)
296+
MicrosoftAccount::new(cache_key, default_account_opts(client_id, scope))
232297
.await
233298
.map(Account::from)
234299
}

0 commit comments

Comments
 (0)