From a73f4b039721397cf1f7a0c44395200884617e6c Mon Sep 17 00:00:00 2001 From: Pauline Date: Sun, 28 Jul 2024 20:39:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20chore:=20explain=20mc=20auth=20f?= =?UTF-8?q?low?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATTRIBUTION.md | 4 +- apps/desktop/build.rs | 3 +- .../permissions/onelauncher/default.toml | 3 +- apps/desktop/src/api/commands.rs | 20 ++--- apps/testing/src/main.rs | 10 +++ packages/core/src/constants.rs | 2 - packages/core/src/store/auth_server.html | 33 ------- packages/core/src/store/minecraft.rs | 87 +------------------ 8 files changed, 27 insertions(+), 135 deletions(-) delete mode 100644 packages/core/src/store/auth_server.html diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 4102e35c..87033b3f 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -26,8 +26,8 @@ All rights reserved. Copyright (C) 2022~2024 Polyfrost This includes, but may not be limited to, the following files: -* [`apps/desktop/.icons/*`](./apps/desktop/.icons/) -* [`apps/desktop/src/assets/licensed/*`](./apps/desktop/src/assets/licensed/) +* [`packages/distribution/icons/*`](./packages/distribution/icons/) +* [`apps/frontend/src/assets/licensed/*`](./apps/frontend/src/assets/licensed/) ## Additional Libraries diff --git a/apps/desktop/build.rs b/apps/desktop/build.rs index eef73914..05964c47 100644 --- a/apps/desktop/build.rs +++ b/apps/desktop/build.rs @@ -6,7 +6,8 @@ fn main() { "onelauncher", tauri_build::InlinedPlugin::new().commands(&[ // User - "msa_auth", + "begin_msa", + "finish_msa", "get_users", "get_user", "remove_user", diff --git a/apps/desktop/permissions/onelauncher/default.toml b/apps/desktop/permissions/onelauncher/default.toml index ee8b0046..c9fd51d7 100644 --- a/apps/desktop/permissions/onelauncher/default.toml +++ b/apps/desktop/permissions/onelauncher/default.toml @@ -2,7 +2,8 @@ description = "Default permissions for the plugin" permissions = [ # User - "allow-msa-auth", + "allow-begin-msa", + "allow-finish-msa", "allow-get-users", "allow-get-user", "allow-remove-user", diff --git a/apps/desktop/src/api/commands.rs b/apps/desktop/src/api/commands.rs index 7366c0b4..1d5fb72d 100644 --- a/apps/desktop/src/api/commands.rs +++ b/apps/desktop/src/api/commands.rs @@ -4,12 +4,10 @@ use interpulse::api::minecraft::Version; use onelauncher::constants::{NATIVE_ARCH, TARGET_OS, VERSION}; use onelauncher::data::{Loader, ManagedPackage, MinecraftCredentials, PackageData, Settings}; use onelauncher::package::content; -use onelauncher::store::{Cluster, ClusterPath}; +use onelauncher::store::{Cluster, ClusterPath, MinecraftLogin}; use onelauncher::{cluster, minecraft, processor, settings}; use serde::{Deserialize, Serialize}; use specta::Type; -use tauri::AppHandle; -use tauri_plugin_shell::ShellExt; use uuid::Uuid; #[macro_export] @@ -23,7 +21,8 @@ macro_rules! collect_commands { ) .commands(tauri_specta::collect_commands![ // User - msa_auth, + begin_msa, + finish_msa, get_users, get_user, remove_user, @@ -215,13 +214,14 @@ pub async fn get_user(uuid: Uuid) -> Result { #[specta::specta] #[tauri::command] -pub async fn msa_auth(handle: AppHandle) -> Result { - let login = minecraft::begin().await?; - handle.shell().open(login.redirect_uri.clone(), None).map_err(|err| err.to_string())?; +pub async fn begin_msa() -> Result { + Ok(minecraft::begin().await?) +} - Err("not implemented".to_string()) - - // Ok(minecraft::finish(code.as_str(), login).await?) +#[specta::specta] +#[tauri::command] +pub async fn finish_msa(code: String, login: MinecraftLogin) -> Result { + Ok(minecraft::finish(code.as_str(), login).await?) } #[specta::specta] diff --git a/apps/testing/src/main.rs b/apps/testing/src/main.rs index 61530679..0fbc7b1d 100644 --- a/apps/testing/src/main.rs +++ b/apps/testing/src/main.rs @@ -17,13 +17,21 @@ async fn main() -> onelauncher::Result<()> { Ok(()) } +/// pauline's epic gamer minecraft auth flow +/// opens a browser tab in which the user sees xbox login and logs in with microsoft and gets redirected +/// to a new url. this new url contains the ?code=.... parameter that we need to finish authentication. +/// in this testing environment we can just copy the final url from our browser and into the testing console +/// and it will parse the url and find the code and complete the authenticaiton process. +/// in production/frontend, it works the same way but we get the url and code parameter automatically. pub async fn authenticate_mc() -> onelauncher::Result { + // begins login flow, opens browser println!("a browser will open, follow login flow"); let login = minecraft::begin().await?; println!("url is {}", login.redirect_uri.as_str()); webbrowser::open(login.redirect_uri.as_str())?; + // after user is done with auth, paste the final code into console below this line println!("enter flow url: "); let mut input = String::new(); std::io::stdin() @@ -32,6 +40,7 @@ pub async fn authenticate_mc() -> onelauncher::Result { println!("{}", input.trim()); + // parses the url code query param using the same logic we should use in frontend let parsed = url::Url::parse(input.trim()).expect("idk"); let code = if let Some((_, code)) = parsed.query_pairs().find(|x| x.0 == "code") { let code = code.clone(); @@ -41,6 +50,7 @@ pub async fn authenticate_mc() -> onelauncher::Result { }; let creds = minecraft::finish(code.as_str(), login).await?; + // mc auth flow is complete and added to state println!("logged in {}", creds.username); Ok(creds) } diff --git a/packages/core/src/constants.rs b/packages/core/src/constants.rs index 6d5760bd..1234209b 100644 --- a/packages/core/src/constants.rs +++ b/packages/core/src/constants.rs @@ -23,8 +23,6 @@ pub const DUMMY_REPLACE_NEWLINE: &str = "\n"; pub const DISCORD_RPC_CLIENT_ID: &str = "1234567890000000"; // todo /// Our Microsoft client ID. pub const MICROSOFT_CLIENT_ID: &str = "9eac3a4e-8cdd-43ef-863e-49cd601b1f03"; -/// The port to run the localhost server on. -pub const MSA_SERVER_PORT: u16 = 13523; /// Mojang/Microsoft client ID. pub const MINECRAFT_CLIENT_ID: &str = "00000000402b5328"; /// Mojang/Microsoft login redirect URI. diff --git a/packages/core/src/store/auth_server.html b/packages/core/src/store/auth_server.html deleted file mode 100644 index fa7c9a16..00000000 --- a/packages/core/src/store/auth_server.html +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - OneLauncher - - - -

%TITLE%

-

%CONTENT%

- - diff --git a/packages/core/src/store/minecraft.rs b/packages/core/src/store/minecraft.rs index 8eb3b229..d8085e12 100644 --- a/packages/core/src/store/minecraft.rs +++ b/packages/core/src/store/minecraft.rs @@ -310,89 +310,6 @@ impl MinecraftState { } } -// pub struct LocalServer { -// listener: TcpListener, -// sender: tokio::sync::mpsc::Sender, -// receiver: tokio::sync::mpsc::Receiver, -// } - -// impl LocalServer { -// pub async fn new() -> crate::Result { -// let listener = TcpListener::bind(format!("127.0.0.1:{}", constants::MSA_SERVER_PORT)).await?; -// let (sender, receiver) = tokio::sync::mpsc::channel::(1); - -// Ok(Self { -// listener, -// sender, -// receiver, -// }) -// } - -// pub async fn wait_for_code(&self) -> crate::Result { - -// } - -// async fn handle_request(&self, stream: &mut TcpStream) -> crate::Result { -// use tokio::io::{AsyncReadExt, AsyncWriteExt}; -// let code: Option; - -// let code_result = { -// let mut buf = vec![0; 1024]; -// let size = stream.read(&mut buf).await?; -// if size == 0 { -// return Err(MinecraftAuthError::MSACodeFetchError.into()); -// } - -// let request = String::from_utf8_lossy(&buf[..size]); -// code = Some( -// request -// .split(' ') -// .find(|x| x.starts_with("code=")) -// .ok_or(anyhow::anyhow!("No code found"))? -// .split('=') -// .nth(1) -// .ok_or(anyhow::anyhow!("No code found"))? -// .to_string() -// ); - -// Ok::<(), String>(()) -// }; - -// let errored = code_result.is_err(); -// let html = include_str!("./auth_server.html"); -// let result = match code_result { -// Ok(()) => { -// html -// .replace("%TITLE%", "Successfully Authenticated") -// .replace("%CONTENT%", "You can now close this tab.") -// } -// Err(e) => { -// html -// .replace("%TITLE%", "Error") -// .replace("%CONTENT%", &format!("An error has occurred: {}", e.as_str())) -// } -// }; - -// let response = format!( -// "HTTP/1.1 {}\r\nContent-Length: {}\r\n\r\n{}", -// if errored { "400 Bad Request" } else { "200 OK" }, -// result.len(), -// result -// ); - -// stream.write_all(response.as_bytes()).await?; - -// if errored { -// return Err(MinecraftAuthError::MSACodeFetchError.into()); -// } - -// match code { -// Some(code) => Ok(code), -// None => Err(MinecraftAuthError::MSACodeFetchError.into()), -// } -// } -// } - /// A [`reqwest::Request`] with a [`DateTime`] attached to it. struct RequestWithDate { pub date: DateTime, @@ -1117,7 +1034,5 @@ pub enum MinecraftAuthError { #[error("failed to read user hash")] HashError, #[error("failed to read user xbox session ID")] - SessionIdError, - #[error("failed to read user MSA code")] - MSACodeFetchError, + SessionIdError }