2023-03-31 18:44:26 -07:00
|
|
|
use crate::api::Result;
|
2024-04-15 13:58:20 -07:00
|
|
|
use chrono::{Duration, Utc};
|
2023-06-28 09:58:58 -07:00
|
|
|
use tauri::plugin::TauriPlugin;
|
2024-08-27 21:49:36 +02:00
|
|
|
use tauri::{Manager, Runtime, UserAttentionType};
|
2024-04-15 13:58:20 -07:00
|
|
|
use theseus::prelude::*;
|
2023-03-31 18:44:26 -07:00
|
|
|
|
2024-08-27 21:49:36 +02:00
|
|
|
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
|
|
|
|
tauri::plugin::Builder::<R>::new("auth")
|
2023-06-28 09:58:58 -07:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
2025-11-17 18:41:52 +00:00
|
|
|
check_reachable,
|
2024-08-27 21:49:36 +02:00
|
|
|
login,
|
|
|
|
|
remove_user,
|
|
|
|
|
get_default_user,
|
|
|
|
|
set_default_user,
|
|
|
|
|
get_users,
|
2023-06-28 09:58:58 -07:00
|
|
|
])
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 18:41:52 +00:00
|
|
|
/// Checks if the authentication servers are reachable.
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn check_reachable() -> Result<()> {
|
|
|
|
|
minecraft_auth::check_reachable().await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 18:44:26 -07:00
|
|
|
/// Authenticate a user with Hydra - part 1
|
|
|
|
|
/// This begins the authentication flow quasi-synchronously, returning a URL to visit (that the user will sign in at)
|
|
|
|
|
#[tauri::command]
|
2024-08-27 21:49:36 +02:00
|
|
|
pub async fn login<R: Runtime>(
|
|
|
|
|
app: tauri::AppHandle<R>,
|
|
|
|
|
) -> Result<Option<Credentials>> {
|
2024-04-15 13:58:20 -07:00
|
|
|
let flow = minecraft_auth::begin_login().await?;
|
2023-03-31 18:44:26 -07:00
|
|
|
|
2024-04-15 13:58:20 -07:00
|
|
|
let start = Utc::now();
|
2023-03-31 18:44:26 -07:00
|
|
|
|
2024-08-27 21:49:36 +02:00
|
|
|
if let Some(window) = app.get_webview_window("signin") {
|
2024-04-15 13:58:20 -07:00
|
|
|
window.close()?;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 21:49:36 +02:00
|
|
|
let window = tauri::WebviewWindowBuilder::new(
|
2024-04-15 13:58:20 -07:00
|
|
|
&app,
|
|
|
|
|
"signin",
|
2025-07-23 00:55:18 +02:00
|
|
|
tauri::WebviewUrl::External(flow.auth_request_uri.parse().map_err(
|
2024-04-15 13:58:20 -07:00
|
|
|
|_| {
|
|
|
|
|
theseus::ErrorKind::OtherError(
|
|
|
|
|
"Error parsing auth redirect URL".to_string(),
|
|
|
|
|
)
|
|
|
|
|
.as_error()
|
|
|
|
|
},
|
|
|
|
|
)?),
|
|
|
|
|
)
|
|
|
|
|
.title("Sign into Modrinth")
|
2024-04-23 12:05:28 -07:00
|
|
|
.always_on_top(true)
|
|
|
|
|
.center()
|
2024-04-15 13:58:20 -07:00
|
|
|
.build()?;
|
2023-06-02 07:09:46 -07:00
|
|
|
|
2024-04-23 12:05:28 -07:00
|
|
|
window.request_user_attention(Some(UserAttentionType::Critical))?;
|
|
|
|
|
|
2024-04-15 13:58:20 -07:00
|
|
|
while (Utc::now() - start) < Duration::minutes(10) {
|
2024-04-23 12:05:28 -07:00
|
|
|
if window.title().is_err() {
|
|
|
|
|
// user closed window, cancelling flow
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 13:58:20 -07:00
|
|
|
if window
|
2024-08-27 21:49:36 +02:00
|
|
|
.url()?
|
2024-04-15 13:58:20 -07:00
|
|
|
.as_str()
|
|
|
|
|
.starts_with("https://login.live.com/oauth20_desktop.srf")
|
2025-08-08 15:50:44 -07:00
|
|
|
&& let Some((_, code)) =
|
2024-08-27 21:49:36 +02:00
|
|
|
window.url()?.query_pairs().find(|x| x.0 == "code")
|
2025-08-08 15:50:44 -07:00
|
|
|
{
|
|
|
|
|
window.close()?;
|
|
|
|
|
let val = minecraft_auth::finish_login(&code.clone(), flow).await?;
|
2024-04-15 13:58:20 -07:00
|
|
|
|
2025-08-08 15:50:44 -07:00
|
|
|
return Ok(Some(val));
|
2024-04-15 13:58:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.close()?;
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
2025-07-23 00:55:18 +02:00
|
|
|
|
2023-03-31 18:44:26 -07:00
|
|
|
#[tauri::command]
|
2024-08-27 21:49:36 +02:00
|
|
|
pub async fn remove_user(user: uuid::Uuid) -> Result<()> {
|
2024-04-15 13:58:20 -07:00
|
|
|
Ok(minecraft_auth::remove_user(user).await?)
|
2023-03-31 18:44:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2024-08-27 21:49:36 +02:00
|
|
|
pub async fn get_default_user() -> Result<Option<uuid::Uuid>> {
|
2024-04-15 13:58:20 -07:00
|
|
|
Ok(minecraft_auth::get_default_user().await?)
|
2023-03-31 18:44:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2024-08-27 21:49:36 +02:00
|
|
|
pub async fn set_default_user(user: uuid::Uuid) -> Result<()> {
|
2024-04-15 13:58:20 -07:00
|
|
|
Ok(minecraft_auth::set_default_user(user).await?)
|
2023-03-31 18:44:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a copy of the list of all user credentials
|
|
|
|
|
#[tauri::command]
|
2024-08-27 21:49:36 +02:00
|
|
|
pub async fn get_users() -> Result<Vec<Credentials>> {
|
2024-04-15 13:58:20 -07:00
|
|
|
Ok(minecraft_auth::users().await?)
|
2023-03-31 18:44:26 -07:00
|
|
|
}
|