From 2095a00c95454441a3f6675b0c11dcf6a4b3d3ef Mon Sep 17 00:00:00 2001 From: objz Date: Sat, 10 Jan 2026 14:08:22 +0100 Subject: [PATCH] fix(auth): replace keyring with file-based token storage --- Cargo.lock | 11 -------- Cargo.toml | 1 - src/auth.rs | 61 +++++++++--------------------------------- src/instance/launch.rs | 22 ++++++++++----- 4 files changed, 28 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bffdb34..8bebac7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1714,16 +1714,6 @@ dependencies = [ "thiserror 2.0.12", ] -[[package]] -name = "keyring" -version = "3.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eebcc3aff044e5944a8fbaf69eb277d11986064cba30c468730e8b9909fb551c" -dependencies = [ - "log", - "zeroize", -] - [[package]] name = "kinded" version = "0.3.0" @@ -1901,7 +1891,6 @@ dependencies = [ "crossterm", "dirs-next", "image", - "keyring", "log", "minecraft-msa-auth", "oauth2", diff --git a/Cargo.toml b/Cargo.toml index eb5b295..4aa97ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,6 @@ ratatui-image = { version = "10.0.6", default-features = false, features = ["cro image = { version = "0.25", default-features = false, features = ["png"] } zip = "2" minecraft-msa-auth = "0.4.0" -keyring = "3.6.3" oauth2 = "5.0.0" diff --git a/src/auth.rs b/src/auth.rs index 419b2f7..f14c347 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -11,13 +11,10 @@ use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; const CLIENT_ID: &str = "708e91b5-99f8-4a1d-80ec-e746cbb24771"; -const DEVICE_CODE_URL: &str = - "https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode"; -const MSA_AUTHORIZE_URL: &str = - "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize"; +const DEVICE_CODE_URL: &str = "https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode"; +const MSA_AUTHORIZE_URL: &str = "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize"; const MSA_TOKEN_URL: &str = "https://login.microsoftonline.com/consumers/oauth2/v2.0/token"; const MC_PROFILE_URL: &str = "https://api.minecraftservices.com/minecraft/profile"; -const KEYRING_SERVICE: &str = "mcl-launcher"; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Account { @@ -25,6 +22,8 @@ pub struct Account { pub username: String, pub account_type: AccountType, pub active: bool, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub refresh_token: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -51,8 +50,6 @@ struct McProfile { name: String, } -// ── Account Store ────────────────────────────────────────────── - pub struct AccountStore { pub accounts: Vec, path: PathBuf, @@ -105,9 +102,6 @@ impl AccountStore { return; } let account = self.accounts.remove(index); - if account.account_type == AccountType::Microsoft { - let _ = delete_refresh_token(&account.uuid); - } if account.active && !self.accounts.is_empty() { self.accounts[0].active = true; } @@ -122,28 +116,6 @@ fn account_store_path() -> PathBuf { .join("accounts.json") } -// ── Keyring ──────────────────────────────────────────────────── - -fn store_refresh_token(uuid: &str, token: &str) -> Result<(), String> { - let entry = keyring::Entry::new(KEYRING_SERVICE, &format!("refresh:{uuid}")) - .map_err(|e| e.to_string())?; - entry.set_password(token).map_err(|e| e.to_string()) -} - -fn get_refresh_token(uuid: &str) -> Result { - let entry = keyring::Entry::new(KEYRING_SERVICE, &format!("refresh:{uuid}")) - .map_err(|e| e.to_string())?; - entry.get_password().map_err(|e| e.to_string()) -} - -fn delete_refresh_token(uuid: &str) -> Result<(), String> { - let entry = keyring::Entry::new(KEYRING_SERVICE, &format!("refresh:{uuid}")) - .map_err(|e| e.to_string())?; - entry.delete_credential().map_err(|e| e.to_string()) -} - -// ── Offline ──────────────────────────────────────────────────── - pub fn offline_uuid(username: &str) -> String { use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; @@ -166,11 +138,10 @@ pub fn create_offline_account(username: &str) -> Account { username: username.to_string(), account_type: AccountType::Offline, active: false, + refresh_token: None, } } -// ── Microsoft Auth (oauth2 + minecraft-msa-auth) ─────────────── - pub static DEVICE_CODE_DISPLAY: Lazy>>> = Lazy::new(|| Arc::new(Mutex::new(None))); @@ -280,25 +251,20 @@ async fn exchange_and_build_account( profile.id.clone() }; - if let Some(refresh) = ms_refresh_token { - let _ = store_refresh_token(&uuid, refresh); - } - AuthResult::Success(Account { uuid, username: profile.name, account_type: AccountType::Microsoft, active: false, + refresh_token: ms_refresh_token.map(|s| s.to_string()), }) } -// ── Token Refresh ────────────────────────────────────────────── - -pub async fn refresh_and_get_token(account: &Account) -> Result { +pub async fn refresh_and_get_token(account: &Account) -> Result<(String, Option), String> { match account.account_type { - AccountType::Offline => Ok("0".to_string()), + AccountType::Offline => Ok(("0".to_string(), None)), AccountType::Microsoft => { - let refresh = get_refresh_token(&account.uuid).map_err(|_| { + let refresh = account.refresh_token.as_deref().ok_or_else(|| { format!( "No saved credentials for '{}'. Please remove and re-add the account.", account.username @@ -316,7 +282,7 @@ pub async fn refresh_and_get_token(account: &Account) -> Result let http_client = reqwest::Client::new(); let token = oauth_client - .exchange_refresh_token(&RefreshToken::new(refresh)) + .exchange_refresh_token(&RefreshToken::new(refresh.to_string())) .add_scope(Scope::new("XboxLive.signin".to_string())) .add_scope(Scope::new("offline_access".to_string())) .request_async(&http_client) @@ -324,10 +290,7 @@ pub async fn refresh_and_get_token(account: &Account) -> Result .map_err(|e| format!("Token refresh failed: {e}"))?; let ms_access_token = token.access_token().secret().to_string(); - - if let Some(new_refresh) = token.refresh_token() { - let _ = store_refresh_token(&account.uuid, new_refresh.secret()); - } + let new_refresh = token.refresh_token().map(|r| r.secret().to_string()); let mc_flow = MinecraftAuthorizationFlow::new(reqwest::Client::new()); let mc_token = mc_flow @@ -335,7 +298,7 @@ pub async fn refresh_and_get_token(account: &Account) -> Result .await .map_err(|e| format!("Minecraft auth failed: {e}"))?; - Ok(mc_token.access_token().as_ref().to_string()) + Ok((mc_token.access_token().as_ref().to_string(), new_refresh)) } } } diff --git a/src/instance/launch.rs b/src/instance/launch.rs index 7e83e93..2324579 100644 --- a/src/instance/launch.rs +++ b/src/instance/launch.rs @@ -235,14 +235,14 @@ pub async fn launch( ]; jvm.extend(config.jvm_args.clone()); - let account_store = crate::auth::AccountStore::load(); + let mut account_store = crate::auth::AccountStore::load(); let (mc_username, mc_uuid, mc_token, mc_user_type) = - match account_store.active_account() { + match account_store.active_account().cloned() { Some(acc) => { - let token = match acc.account_type { + let (token, new_refresh) = match acc.account_type { crate::auth::AccountType::Microsoft => { - match crate::auth::refresh_and_get_token(acc).await { - Ok(t) => t, + match crate::auth::refresh_and_get_token(&acc).await { + Ok(pair) => pair, Err(e) => { return Err(LaunchError::Auth(format!( "Authentication failed: {e}" @@ -250,8 +250,18 @@ pub async fn launch( } } } - crate::auth::AccountType::Offline => "0".to_string(), + crate::auth::AccountType::Offline => ("0".to_string(), None), }; + if let Some(new_rt) = new_refresh { + if let Some(stored) = account_store + .accounts + .iter_mut() + .find(|a| a.uuid == acc.uuid) + { + stored.refresh_token = Some(new_rt); + account_store.save(); + } + } let user_type = match acc.account_type { crate::auth::AccountType::Microsoft => "msa", crate::auth::AccountType::Offline => "legacy",