Switch to official launcher auth (#1118)

* Switch to official launcher auth

* add debug info

* Fix build
This commit is contained in:
Geometrically
2024-04-15 13:58:20 -07:00
committed by GitHub
parent 76447019c0
commit 2877919639
65 changed files with 1674 additions and 5349 deletions

View File

@@ -1,16 +1,15 @@
use crate::api::Result;
use chrono::{Duration, Utc};
use tauri::plugin::TauriPlugin;
use theseus::{hydra::init::DeviceLoginSuccess, prelude::*};
use tauri::Manager;
use theseus::prelude::*;
pub fn init<R: tauri::Runtime>() -> TauriPlugin<R> {
tauri::plugin::Builder::new("auth")
.invoke_handler(tauri::generate_handler![
auth_authenticate_begin_flow,
auth_authenticate_await_completion,
auth_cancel_flow,
auth_refresh,
auth_get_default_user,
auth_set_default_user,
auth_remove_user,
auth_has_user,
auth_users,
auth_get_user,
])
@@ -20,47 +19,73 @@ pub fn init<R: tauri::Runtime>() -> TauriPlugin<R> {
/// 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]
pub async fn auth_authenticate_begin_flow() -> Result<DeviceLoginSuccess> {
Ok(auth::authenticate_begin_flow().await?)
}
pub async fn auth_login(app: tauri::AppHandle) -> Result<Option<Credentials>> {
let flow = minecraft_auth::begin_login().await?;
/// Authenticate a user with Hydra - part 2
/// This completes the authentication flow quasi-synchronously, returning the sign-in credentials
/// (and also adding the credentials to the state)
#[tauri::command]
pub async fn auth_authenticate_await_completion() -> Result<Credentials> {
Ok(auth::authenticate_await_complete_flow().await?)
}
let start = Utc::now();
#[tauri::command]
pub async fn auth_cancel_flow() -> Result<()> {
Ok(auth::cancel_flow().await?)
}
if let Some(window) = app.get_window("signin") {
window.close()?;
}
/// Refresh some credentials using Hydra, if needed
// invoke('plugin:auth|auth_refresh',user)
#[tauri::command]
pub async fn auth_refresh(user: uuid::Uuid) -> Result<Credentials> {
Ok(auth::refresh(user).await?)
}
let window = tauri::WindowBuilder::new(
&app,
"signin",
tauri::WindowUrl::External(flow.redirect_uri.parse().map_err(
|_| {
theseus::ErrorKind::OtherError(
"Error parsing auth redirect URL".to_string(),
)
.as_error()
},
)?),
)
.title("Sign into Modrinth")
.build()?;
while (Utc::now() - start) < Duration::minutes(10) {
if window
.url()
.as_str()
.starts_with("https://login.live.com/oauth20_desktop.srf")
{
if let Some((_, code)) =
window.url().query_pairs().find(|x| x.0 == "code")
{
window.close()?;
let val =
minecraft_auth::finish_login(&code.clone(), flow).await?;
return Ok(Some(val));
}
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
window.close()?;
Ok(None)
}
#[tauri::command]
pub async fn auth_remove_user(user: uuid::Uuid) -> Result<()> {
Ok(auth::remove_user(user).await?)
Ok(minecraft_auth::remove_user(user).await?)
}
/// Check if a user exists in Theseus
// invoke('plugin:auth|auth_has_user',user)
#[tauri::command]
pub async fn auth_has_user(user: uuid::Uuid) -> Result<bool> {
Ok(auth::has_user(user).await?)
pub async fn auth_get_default_user() -> Result<Option<uuid::Uuid>> {
Ok(minecraft_auth::get_default_user().await?)
}
#[tauri::command]
pub async fn auth_set_default_user(user: uuid::Uuid) -> Result<()> {
Ok(minecraft_auth::set_default_user(user).await?)
}
/// Get a copy of the list of all user credentials
// invoke('plugin:auth|auth_users',user)
#[tauri::command]
pub async fn auth_users() -> Result<Vec<Credentials>> {
Ok(auth::users().await?)
Ok(minecraft_auth::users().await?)
}
/// Get a user from the UUID
@@ -68,5 +93,5 @@ pub async fn auth_users() -> Result<Vec<Credentials>> {
// invoke('plugin:auth|auth_users',user)
#[tauri::command]
pub async fn auth_get_user(user: uuid::Uuid) -> Result<Credentials> {
Ok(auth::get_user(user).await?)
Ok(minecraft_auth::get_user(user).await?)
}

View File

@@ -35,6 +35,9 @@ pub enum TheseusSerializableError {
#[error("IO error: {0}")]
IO(#[from] std::io::Error),
#[error("Tauri error: {0}")]
Tauri(#[from] tauri::Error),
#[cfg(target_os = "macos")]
#[error("Callback error: {0}")]
Callback(String),
@@ -88,9 +91,12 @@ macro_rules! impl_serialize {
#[cfg(target_os = "macos")]
impl_serialize! {
IO,
Tauri,
Callback
}
#[cfg(not(target_os = "macos"))]
impl_serialize! {
IO,
Tauri,
}

View File

@@ -146,6 +146,7 @@ fn main() {
initialize_state,
is_dev,
toggle_decorations,
api::auth::auth_login,
]);
builder