fix: switch default profile to terminal and fixed some comments

This commit is contained in:
objz committed 2026-04-18 19:44:59 +02:00
1 parent 971375bf1f
commit 52787b57c7
71 files changed
+671 -188

No files matched your search

+12 -5
View File
@@ -1,3 +1,5 @@
// account management: persistence, switching active accounts, and offline uuid generation
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
@@ -41,10 +43,11 @@ impl AccountStore {
pub fn save(&self) {
if let Some(parent) = self.path.parent()
&& let Err(e) = std::fs::create_dir_all(parent) {
tracing::error!("Failed to create accounts directory: {}", e);
return;
}
&& let Err(e) = std::fs::create_dir_all(parent)
{
tracing::error!("Failed to create accounts directory: {}", e);
return;
}
match serde_json::to_string_pretty(&self.accounts) {
Ok(json) => {
if let Err(e) = std::fs::write(&self.path, json) {
@@ -66,6 +69,8 @@ impl AccountStore {
self.save();
}
// if an account with the same uuid already exists, replace it.
// first account added auto-becomes active so there's always a selection.
pub fn add(&mut self, account: Account) {
let uuid = &account.uuid;
self.accounts.retain(|a| a.uuid != *uuid);
@@ -93,7 +98,9 @@ pub fn account_store_path() -> PathBuf {
crate::config::get_config_path().join("accounts.json")
}
/// Generate a deterministic offline-mode UUID for a username.
// deterministic fake uuid from a username, formatted as uuid v3 with the proper
// version and variant bits set. not cryptographically meaningful, just needs to
// be consistent so the same offline name always maps to the same uuid.
pub fn offline_uuid(username: &str) -> String {
use std::hash::{DefaultHasher, Hash, Hasher};
let mut hasher = DefaultHasher::new();
+1
View File
@@ -1,3 +1,4 @@
// auth module: account storage (offline + microsoft) and the oauth device code flow
mod accounts;
mod oauth;
+10
View File
@@ -1,3 +1,7 @@
// microsoft oauth device code flow for minecraft authentication.
// the flow goes: MSA device code -> MSA token -> xbox/mc token exchange -> mc profile fetch.
// device code auth is used because it works without a redirect URI, which is nice for a TUI.
use std::sync::{Arc, LazyLock, Mutex};
use minecraft_msa_auth::MinecraftAuthorizationFlow;
@@ -28,6 +32,7 @@ struct McProfile {
name: String,
}
// shared slot so the TUI can poll for the device code to show the user
pub static DEVICE_CODE_DISPLAY: LazyLock<Arc<Mutex<Option<DeviceCodeInfo>>>> =
LazyLock::new(|| Arc::new(Mutex::new(None)));
@@ -68,6 +73,8 @@ async fn run_full_oauth_flow() -> Result<(String, Option<String>), String> {
Ok((ms_access_token, ms_refresh_token))
}
// kicks off auth on a background task, returns a mutex the caller can poll for the result.
// the TUI checks DEVICE_CODE_DISPLAY for the code to show, and this mutex for completion.
pub fn start_microsoft_auth() -> Arc<Mutex<Option<AuthResult>>> {
let result: Arc<Mutex<Option<AuthResult>>> = Arc::new(Mutex::new(None));
let result_clone = result.clone();
@@ -124,6 +131,7 @@ async fn exchange_and_build_account(
Err(e) => return AuthResult::Error(format!("Profile parse failed: {e}")),
};
// mojang returns uuids without dashes because of course they do
let uuid = if profile.id.len() == 32 {
format!(
"{}-{}-{}-{}-{}",
@@ -146,6 +154,8 @@ async fn exchange_and_build_account(
})
}
// returns (mc_access_token, new_refresh_token). for offline accounts it just
// hands back a dummy token since the server doesn't care.
pub async fn refresh_and_get_token(account: &Account) -> Result<(String, Option<String>), String> {
match account.account_type {
AccountType::Offline => Ok(("0".to_owned(), None)),