restructured auto-credentials (#74)

* restructured auto-credentials

* fix clone

---------

Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Wyatt Verchere
2023-04-07 14:43:21 -07:00
committed by GitHub
parent 34005dd2e2
commit 764d75181f
8 changed files with 82 additions and 48 deletions

View File

@@ -57,6 +57,7 @@ pub async fn authenticate(
Ok(credentials)
}
/// Refresh some credentials using Hydra, if needed
/// This is the primary desired way to get credentials, as it will also refresh them.
#[tracing::instrument]

View File

@@ -1,5 +1,9 @@
//! Theseus profile management interface
use crate::{launcher::download, state::MinecraftChild};
use crate::{
auth::{self, refresh},
launcher::download,
state::MinecraftChild,
};
pub use crate::{
state::{JavaSettings, Profile},
State,
@@ -91,10 +95,33 @@ pub async fn sync(path: &Path) -> crate::Result<()> {
}
}
/// Run Minecraft using a profile
/// Run Minecraft using a profile and the default credentials, logged in credentials,
/// failing with an error if no credentials are available
#[tracing::instrument(skip_all)]
pub async fn run(path: &Path) -> crate::Result<Arc<RwLock<MinecraftChild>>> {
let state = State::get().await?;
// Get default account and refresh credentials (preferred way to log in)
let default_account = state.settings.read().await.default_user;
let credentials = if let Some(default_account) = default_account {
refresh(default_account, false).await?
} else {
// If no default account, try to use a logged in account
let users = auth::users().await?;
let last_account = users.iter().next();
if let Some(last_account) = last_account {
refresh(last_account.id, false).await?
} else {
return Err(crate::ErrorKind::NoCredentialsError.as_error());
}
};
run_credentials(path, &credentials).await
}
/// Run Minecraft using a profile, and credentials for authentication
/// Returns Arc pointer to RwLock to Child
#[tracing::instrument(skip_all)]
pub async fn run(
pub async fn run_credentials(
path: &Path,
credentials: &crate::auth::Credentials,
) -> crate::Result<Arc<RwLock<MinecraftChild>>> {