Auth return Err when user close the window, temporary remove download function
This commit is contained in:
parent
463e66bd97
commit
a2ca88b9cd
@ -3,6 +3,7 @@ use std::{fmt, net::TcpListener, sync::Arc};
|
|||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use reqwest::{header::{CONTENT_TYPE, CONNECTION, ACCEPT, AUTHORIZATION}, Client};
|
use reqwest::{header::{CONTENT_TYPE, CONNECTION, ACCEPT, AUTHORIZATION}, Client};
|
||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
|
use tauri::{WindowEvent, async_runtime::{spawn_blocking}};
|
||||||
use tokio::{sync::mpsc, join};
|
use tokio::{sync::mpsc, join};
|
||||||
use urlencoding::encode;
|
use urlencoding::encode;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -33,6 +34,7 @@ pub struct OauthToken {
|
|||||||
prompt: Arc<Prompt>
|
prompt: Arc<Prompt>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
struct AccessRefreshToken {
|
struct AccessRefreshToken {
|
||||||
access_token: String,
|
access_token: String,
|
||||||
refresh_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)
|
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)> {
|
async fn fetch_oauth2_token(prompt: Prompt, app: tauri::AppHandle) -> Result<(ReceivedCode, OauthToken)> {
|
||||||
let state: String = thread_rng()
|
let state: String = thread_rng()
|
||||||
.sample_iter(&rand::distributions::Alphanumeric)
|
.sample_iter(&rand::distributions::Alphanumeric)
|
||||||
@ -101,14 +108,39 @@ impl Authentification {
|
|||||||
"externam",
|
"externam",
|
||||||
tauri::WindowUrl::External(link.parse().unwrap())
|
tauri::WindowUrl::External(link.parse().unwrap())
|
||||||
).build().expect("Failed to build window");
|
).build().expect("Failed to build window");
|
||||||
let received = Self::listen(port_holder.unwrap()).await?;
|
let (sender, mut receiver) = mpsc::channel::<bool>(2);
|
||||||
second_window.close()?;
|
second_window.on_window_event(move|e| {
|
||||||
|
match e {
|
||||||
if received.state != state {
|
WindowEvent::Destroyed => {
|
||||||
bail!("CSRF check fail")
|
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 {
|
// fn create_link_from_prompt(prompt: Prompt) -> String {
|
||||||
|
@ -36,7 +36,6 @@ async fn login(app: tauri::AppHandle, _window: tauri::Window, state: tauri::Stat
|
|||||||
Err(err.to_string())
|
Err(err.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
Err(err) => 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) => {
|
Ok(game_profile) => {
|
||||||
match &game_profile.0 {
|
match &game_profile.0 {
|
||||||
Some(game_profile) => {
|
Some(game_profile) => {
|
||||||
let java_path = root_path.join("java");
|
// let java_path = root_path.join("java");
|
||||||
let opts = ClientOptions {
|
// let opts = ClientOptions {
|
||||||
authorization: game_profile,
|
// authorization: game_profile,
|
||||||
root_path,
|
// root_path,
|
||||||
java_path: &java_path.as_path(),
|
// java_path: &java_path.as_path(),
|
||||||
version_number: "1.19.4".to_string(),
|
// version_number: "1.19.4".to_string(),
|
||||||
version_type: launcher::VersionType::Release,
|
// version_type: launcher::VersionType::Release,
|
||||||
memory_min: "2G".to_string(),
|
// memory_min: "2G".to_string(),
|
||||||
memory_max: "4G".to_string(),
|
// memory_max: "4G".to_string(),
|
||||||
};
|
// };
|
||||||
let client = MinecraftClient::new(&opts);
|
// let client = MinecraftClient::new(&opts);
|
||||||
match client {
|
// match client {
|
||||||
Ok(mut client) => {
|
// Ok(mut client) => {
|
||||||
match client.download_assets() {
|
// match client.download_assets() {
|
||||||
Ok(_) => {
|
// Ok(_) => {
|
||||||
Ok("Content downloaded".to_string())
|
// Ok("Content downloaded".to_string())
|
||||||
},
|
// },
|
||||||
Err(err) => {
|
// Err(err) => {
|
||||||
Err(err.to_string())
|
// Err(err.to_string())
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
},
|
// },
|
||||||
Err(err) => {
|
// Err(err) => {
|
||||||
Err(err.to_string())
|
// Err(err.to_string())
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
Ok("Client created".to_string())
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
Err("You're not connected".to_string())
|
Err("You're not connected".to_string())
|
||||||
|
Reference in New Issue
Block a user