Auth return Err when user close the window, temporary remove download function

This commit is contained in:
Quentin Legot 2023-04-22 19:15:49 +02:00
parent 463e66bd97
commit a2ca88b9cd
2 changed files with 66 additions and 34 deletions

View File

@ -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<Prompt>
}
#[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<bool>) {
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::<bool>(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 {

View File

@ -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<CustomState>>) -> Result<String,
Ok(game_profile) => {
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())