From a2ca88b9cd7d54648750603ce32fc1bcd6f82417 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sat, 22 Apr 2023 19:15:49 +0200 Subject: [PATCH] Auth return Err when user close the window, temporary remove download function --- src-tauri/src/authentification/mod.rs | 46 +++++++++++++++++++---- src-tauri/src/main.rs | 54 +++++++++++++-------------- 2 files changed, 66 insertions(+), 34 deletions(-) diff --git a/src-tauri/src/authentification/mod.rs b/src-tauri/src/authentification/mod.rs index 90c06d0..c7d015b 100644 --- a/src-tauri/src/authentification/mod.rs +++ b/src-tauri/src/authentification/mod.rs @@ -3,6 +3,7 @@ use std::{fmt, net::TcpListener, sync::Arc}; use rand::{thread_rng, Rng}; use reqwest::{header::{CONTENT_TYPE, CONNECTION, ACCEPT, AUTHORIZATION}, Client}; use serde_json::{Value, json}; +use tauri::{WindowEvent, async_runtime::{spawn_blocking}}; use tokio::{sync::mpsc, join}; use urlencoding::encode; use serde::{Deserialize, Serialize}; @@ -33,6 +34,7 @@ pub struct OauthToken { prompt: Arc } +#[allow(dead_code)] struct AccessRefreshToken { access_token: String, refresh_token: String @@ -74,6 +76,11 @@ impl Authentification { format!("https://login.live.com/oauth20_authorize.srf?client_id={}&response_type=code&redirect_uri={}&scope=Xboxlive.signin+Xboxlive.offline_access&prompt={}&state={}", token.client_id, encode(token.redirect.as_str()), token.prompt, state) } + #[allow(unused_must_use)] + fn send_blocking(sender: tokio::sync::mpsc::Sender) { + sender.blocking_send(true); + } + async fn fetch_oauth2_token(prompt: Prompt, app: tauri::AppHandle) -> Result<(ReceivedCode, OauthToken)> { let state: String = thread_rng() .sample_iter(&rand::distributions::Alphanumeric) @@ -101,14 +108,39 @@ impl Authentification { "externam", tauri::WindowUrl::External(link.parse().unwrap()) ).build().expect("Failed to build window"); - let received = Self::listen(port_holder.unwrap()).await?; - second_window.close()?; - - if received.state != state { - bail!("CSRF check fail") + let (sender, mut receiver) = mpsc::channel::(2); + second_window.on_window_event(move|e| { + match e { + WindowEvent::Destroyed => { + let sender = sender.clone(); + spawn_blocking(|| Self::send_blocking(sender)); + }, + _ => {} + } + }); + let listener = Self::listen(port_holder.unwrap()); + tokio::select! { + received = listener => { + match received { + Ok(received) => { + println!("received code"); + second_window.close()?; + + if received.state != state { + bail!("CSRF check fail") + } + Ok((received, token_data)) + }, + Err(err) => { + bail!(err) + } + } + }, + _ = receiver.recv() => { + println!("window closed"); + bail!("You closed the window before completion") + } } - - Ok((received, token_data)) } // fn create_link_from_prompt(prompt: Prompt) -> String { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index dc611ad..bd25ddd 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -36,7 +36,6 @@ async fn login(app: tauri::AppHandle, _window: tauri::Window, state: tauri::Stat Err(err.to_string()) } } - }, Err(err) => Err(err.to_string()) } @@ -51,32 +50,33 @@ async fn download(state: tauri::State<'_, Mutex>) -> Result { match &game_profile.0 { Some(game_profile) => { - let java_path = root_path.join("java"); - let opts = ClientOptions { - authorization: game_profile, - root_path, - java_path: &java_path.as_path(), - version_number: "1.19.4".to_string(), - version_type: launcher::VersionType::Release, - memory_min: "2G".to_string(), - memory_max: "4G".to_string(), - }; - let client = MinecraftClient::new(&opts); - match client { - Ok(mut client) => { - match client.download_assets() { - Ok(_) => { - Ok("Content downloaded".to_string()) - }, - Err(err) => { - Err(err.to_string()) - } - } - }, - Err(err) => { - Err(err.to_string()) - } - } + // let java_path = root_path.join("java"); + // let opts = ClientOptions { + // authorization: game_profile, + // root_path, + // java_path: &java_path.as_path(), + // version_number: "1.19.4".to_string(), + // version_type: launcher::VersionType::Release, + // memory_min: "2G".to_string(), + // memory_max: "4G".to_string(), + // }; + // let client = MinecraftClient::new(&opts); + // match client { + // Ok(mut client) => { + // match client.download_assets() { + // Ok(_) => { + // Ok("Content downloaded".to_string()) + // }, + // Err(err) => { + // Err(err.to_string()) + // } + // } + // }, + // Err(err) => { + // Err(err.to_string()) + // } + // } + Ok("Client created".to_string()) }, None => { Err("You're not connected".to_string())