You've already forked AstralRinth
forked from didirus/AstralRinth
Authentication (#37)
* Initial authentication implementation * Store user info in the database, improve encapsulation in profiles * Add user list, remove unused dependencies, add spantraces * Implement user remove, update UUID crate * Add user set-default * Revert submodule macro usage * Make tracing significantly less verbose
This commit is contained in:
100
theseus/src/api/auth.rs
Normal file
100
theseus/src/api/auth.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
//! Authentication flow interface
|
||||
use crate::{launcher::auth as inner, State};
|
||||
use futures::prelude::*;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
pub use inner::Credentials;
|
||||
|
||||
/// Authenticate a user with Hydra
|
||||
/// To run this, you need to first spawn this function as a task, then
|
||||
/// open a browser to the given URL and finally wait on the spawned future
|
||||
/// with the ability to cancel in case the browser is closed before finishing
|
||||
#[tracing::instrument]
|
||||
pub async fn authenticate(
|
||||
browser_url: oneshot::Sender<url::Url>,
|
||||
) -> crate::Result<Credentials> {
|
||||
let mut flow = inner::HydraAuthFlow::new().await?;
|
||||
let state = State::get().await?;
|
||||
let mut users = state.users.write().await;
|
||||
|
||||
let url = flow.prepare_login_url().await?;
|
||||
browser_url.send(url).map_err(|url| {
|
||||
crate::ErrorKind::OtherError(format!(
|
||||
"Error sending browser url to parent: {url}"
|
||||
))
|
||||
})?;
|
||||
|
||||
let credentials = flow.extract_credentials().await?;
|
||||
users.insert(&credentials)?;
|
||||
|
||||
if state.settings.read().await.default_user.is_none() {
|
||||
let mut settings = state.settings.write().await;
|
||||
settings.default_user = Some(credentials.id);
|
||||
}
|
||||
|
||||
Ok(credentials)
|
||||
}
|
||||
|
||||
/// Refresh some credentials using Hydra, if needed
|
||||
#[tracing::instrument]
|
||||
pub async fn refresh(
|
||||
user: uuid::Uuid,
|
||||
update_name: bool,
|
||||
) -> crate::Result<Credentials> {
|
||||
let state = State::get().await?;
|
||||
let mut users = state.users.write().await;
|
||||
|
||||
futures::future::ready(users.get(user)?.ok_or_else(|| {
|
||||
crate::ErrorKind::OtherError(format!(
|
||||
"Tried to refresh nonexistent user with ID {user}"
|
||||
))
|
||||
.as_error()
|
||||
}))
|
||||
.and_then(|mut credentials| async move {
|
||||
if chrono::offset::Utc::now() > credentials.expires {
|
||||
inner::refresh_credentials(&mut credentials).await?;
|
||||
if update_name {
|
||||
inner::refresh_username(&mut credentials).await?;
|
||||
}
|
||||
}
|
||||
users.insert(&credentials)?;
|
||||
Ok(credentials)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Remove a user account from the database
|
||||
#[tracing::instrument]
|
||||
pub async fn remove_user(user: uuid::Uuid) -> crate::Result<()> {
|
||||
let state = State::get().await?;
|
||||
let mut users = state.users.write().await;
|
||||
|
||||
if state.settings.read().await.default_user == Some(user) {
|
||||
let mut settings = state.settings.write().await;
|
||||
settings.default_user = users
|
||||
.0
|
||||
.first()?
|
||||
.map(|it| uuid::Uuid::from_slice(&it.0))
|
||||
.transpose()?;
|
||||
}
|
||||
|
||||
users.remove(user)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if a user exists in Theseus
|
||||
#[tracing::instrument]
|
||||
pub async fn has_user(user: uuid::Uuid) -> crate::Result<bool> {
|
||||
let state = State::get().await?;
|
||||
let users = state.users.read().await;
|
||||
|
||||
Ok(users.contains(user)?)
|
||||
}
|
||||
|
||||
/// Get a copy of the list of all user credentials
|
||||
#[tracing::instrument]
|
||||
pub async fn users() -> crate::Result<Box<[Credentials]>> {
|
||||
let state = State::get().await?;
|
||||
let users = state.users.read().await;
|
||||
users.iter().collect()
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
//! API for interacting with Theseus
|
||||
pub mod auth;
|
||||
pub mod profile;
|
||||
|
||||
pub mod data {
|
||||
pub use crate::{
|
||||
launcher::Credentials,
|
||||
state::{
|
||||
DirectoryInfo, Hooks, JavaSettings, MemorySettings, ModLoader,
|
||||
ProfileMetadata, Settings, WindowSize,
|
||||
},
|
||||
pub use crate::state::{
|
||||
DirectoryInfo, Hooks, JavaSettings, MemorySettings, ModLoader,
|
||||
ProfileMetadata, Settings, WindowSize,
|
||||
};
|
||||
}
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::{
|
||||
auth::{self, Credentials},
|
||||
data::*,
|
||||
profile::{self, Profile},
|
||||
State,
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
//! Theseus profile management interface
|
||||
|
||||
pub use crate::{
|
||||
state::{JavaSettings, Profile},
|
||||
State,
|
||||
};
|
||||
use daedalus as d;
|
||||
use std::{future::Future, path::Path};
|
||||
use std::{
|
||||
future::Future,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use tokio::process::{Child, Command};
|
||||
|
||||
/// Add a profile to the in-memory state
|
||||
#[tracing::instrument]
|
||||
pub async fn add(profile: Profile) -> crate::Result<()> {
|
||||
let state = State::get().await?;
|
||||
let mut profiles = state.profiles.write().await;
|
||||
@@ -18,6 +21,7 @@ pub async fn add(profile: Profile) -> crate::Result<()> {
|
||||
}
|
||||
|
||||
/// Add a path as a profile in-memory
|
||||
#[tracing::instrument]
|
||||
pub async fn add_path(path: &Path) -> crate::Result<()> {
|
||||
let state = State::get().await?;
|
||||
let mut profiles = state.profiles.write().await;
|
||||
@@ -27,6 +31,7 @@ pub async fn add_path(path: &Path) -> crate::Result<()> {
|
||||
}
|
||||
|
||||
/// Remove a profile
|
||||
#[tracing::instrument]
|
||||
pub async fn remove(path: &Path) -> crate::Result<()> {
|
||||
let state = State::get().await?;
|
||||
let mut profiles = state.profiles.write().await;
|
||||
@@ -36,19 +41,22 @@ pub async fn remove(path: &Path) -> crate::Result<()> {
|
||||
}
|
||||
|
||||
/// Get a profile by path,
|
||||
#[tracing::instrument]
|
||||
pub async fn get(path: &Path) -> crate::Result<Option<Profile>> {
|
||||
let state = State::get().await?;
|
||||
let profiles = state.profiles.read().await;
|
||||
|
||||
profiles.0.get(path).map_or(Ok(None), |prof| match prof {
|
||||
Some(prof) => Ok(Some(prof.clone())),
|
||||
None => Err(crate::Error::UnloadedProfileError(
|
||||
None => Err(crate::ErrorKind::UnloadedProfileError(
|
||||
path.display().to_string(),
|
||||
)),
|
||||
)
|
||||
.as_error()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if a profile is already managed by Theseus
|
||||
#[tracing::instrument]
|
||||
pub async fn is_managed(profile: &Path) -> crate::Result<bool> {
|
||||
let state = State::get().await?;
|
||||
let profiles = state.profiles.read().await;
|
||||
@@ -56,6 +64,7 @@ pub async fn is_managed(profile: &Path) -> crate::Result<bool> {
|
||||
}
|
||||
|
||||
/// Check if a profile is loaded
|
||||
#[tracing::instrument]
|
||||
pub async fn is_loaded(profile: &Path) -> crate::Result<bool> {
|
||||
let state = State::get().await?;
|
||||
let profiles = state.profiles.read().await;
|
||||
@@ -75,29 +84,41 @@ pub async fn edit<Fut>(
|
||||
where
|
||||
Fut: Future<Output = crate::Result<()>>,
|
||||
{
|
||||
let state = State::get().await.unwrap();
|
||||
let state = State::get().await?;
|
||||
let mut profiles = state.profiles.write().await;
|
||||
|
||||
match profiles.0.get_mut(path) {
|
||||
Some(&mut Some(ref mut profile)) => action(profile).await,
|
||||
Some(&mut None) => Err(crate::Error::UnloadedProfileError(
|
||||
Some(&mut None) => Err(crate::ErrorKind::UnloadedProfileError(
|
||||
path.display().to_string(),
|
||||
)),
|
||||
None => Err(crate::Error::UnmanagedProfileError(
|
||||
)
|
||||
.as_error()),
|
||||
None => Err(crate::ErrorKind::UnmanagedProfileError(
|
||||
path.display().to_string(),
|
||||
)),
|
||||
)
|
||||
.as_error()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a copy of the profile set
|
||||
#[tracing::instrument]
|
||||
pub async fn list(
|
||||
) -> crate::Result<std::collections::HashMap<PathBuf, Option<Profile>>> {
|
||||
let state = State::get().await?;
|
||||
let profiles = state.profiles.read().await;
|
||||
Ok(profiles.0.clone())
|
||||
}
|
||||
|
||||
/// Run Minecraft using a profile
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub async fn run(
|
||||
path: &Path,
|
||||
credentials: &crate::launcher::Credentials,
|
||||
credentials: &crate::auth::Credentials,
|
||||
) -> crate::Result<Child> {
|
||||
let state = State::get().await.unwrap();
|
||||
let settings = state.settings.read().await;
|
||||
let profile = get(path).await?.ok_or_else(|| {
|
||||
crate::Error::OtherError(format!(
|
||||
crate::ErrorKind::OtherError(format!(
|
||||
"Tried to run a nonexistent or unloaded profile at path {}!",
|
||||
path.display()
|
||||
))
|
||||
@@ -110,7 +131,7 @@ pub async fn run(
|
||||
.iter()
|
||||
.find(|it| it.id == profile.metadata.game_version.as_ref())
|
||||
.ok_or_else(|| {
|
||||
crate::Error::LauncherError(format!(
|
||||
crate::ErrorKind::LauncherError(format!(
|
||||
"Invalid or unknown Minecraft version: {}",
|
||||
profile.metadata.game_version
|
||||
))
|
||||
@@ -130,10 +151,11 @@ pub async fn run(
|
||||
.await?;
|
||||
|
||||
if !result.success() {
|
||||
return Err(crate::Error::LauncherError(format!(
|
||||
return Err(crate::ErrorKind::LauncherError(format!(
|
||||
"Non-zero exit code for pre-launch hook: {}",
|
||||
result.code().unwrap_or(-1)
|
||||
)));
|
||||
))
|
||||
.as_error());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +175,7 @@ pub async fn run(
|
||||
settings.java_8_path.as_ref()
|
||||
}
|
||||
.ok_or_else(|| {
|
||||
crate::Error::LauncherError(format!(
|
||||
crate::ErrorKind::LauncherError(format!(
|
||||
"No Java installed for version {}",
|
||||
version_info.java_version.map_or(8, |it| it.major_version),
|
||||
))
|
||||
@@ -161,10 +183,11 @@ pub async fn run(
|
||||
};
|
||||
|
||||
if !java_install.exists() {
|
||||
return Err(crate::Error::LauncherError(format!(
|
||||
return Err(crate::ErrorKind::LauncherError(format!(
|
||||
"Could not find Java install: {}",
|
||||
java_install.display()
|
||||
)));
|
||||
))
|
||||
.as_error());
|
||||
}
|
||||
|
||||
let ref java_args = profile
|
||||
@@ -195,21 +218,26 @@ pub async fn run(
|
||||
.await
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn kill(running: &mut Child) -> crate::Result<()> {
|
||||
running.kill().await?;
|
||||
wait_for(running).await
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub async fn wait_for(running: &mut Child) -> crate::Result<()> {
|
||||
let result = running.wait().await.map_err(|err| {
|
||||
crate::Error::LauncherError(format!("Error running minecraft: {err}"))
|
||||
crate::ErrorKind::LauncherError(format!(
|
||||
"Error running minecraft: {err}"
|
||||
))
|
||||
})?;
|
||||
|
||||
match result.success() {
|
||||
false => Err(crate::Error::LauncherError(format!(
|
||||
false => Err(crate::ErrorKind::LauncherError(format!(
|
||||
"Minecraft exited with non-zero code {}",
|
||||
result.code().unwrap_or(-1)
|
||||
))),
|
||||
))
|
||||
.as_error()),
|
||||
true => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user