Skip to content

Commit a73f4b0

Browse files
committed
🔨 chore: explain mc auth flow
1 parent 4539cb5 commit a73f4b0

File tree

8 files changed

+27
-135
lines changed

8 files changed

+27
-135
lines changed

ATTRIBUTION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ All rights reserved. Copyright (C) 2022~2024 Polyfrost
2626

2727
This includes, but may not be limited to, the following files:
2828

29-
* [`apps/desktop/.icons/*`](./apps/desktop/.icons/)
30-
* [`apps/desktop/src/assets/licensed/*`](./apps/desktop/src/assets/licensed/)
29+
* [`packages/distribution/icons/*`](./packages/distribution/icons/)
30+
* [`apps/frontend/src/assets/licensed/*`](./apps/frontend/src/assets/licensed/)
3131

3232
## Additional Libraries
3333

apps/desktop/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ fn main() {
66
"onelauncher",
77
tauri_build::InlinedPlugin::new().commands(&[
88
// User
9-
"msa_auth",
9+
"begin_msa",
10+
"finish_msa",
1011
"get_users",
1112
"get_user",
1213
"remove_user",

apps/desktop/permissions/onelauncher/default.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
description = "Default permissions for the plugin"
33
permissions = [
44
# User
5-
"allow-msa-auth",
5+
"allow-begin-msa",
6+
"allow-finish-msa",
67
"allow-get-users",
78
"allow-get-user",
89
"allow-remove-user",

apps/desktop/src/api/commands.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use interpulse::api::minecraft::Version;
44
use onelauncher::constants::{NATIVE_ARCH, TARGET_OS, VERSION};
55
use onelauncher::data::{Loader, ManagedPackage, MinecraftCredentials, PackageData, Settings};
66
use onelauncher::package::content;
7-
use onelauncher::store::{Cluster, ClusterPath};
7+
use onelauncher::store::{Cluster, ClusterPath, MinecraftLogin};
88
use onelauncher::{cluster, minecraft, processor, settings};
99
use serde::{Deserialize, Serialize};
1010
use specta::Type;
11-
use tauri::AppHandle;
12-
use tauri_plugin_shell::ShellExt;
1311
use uuid::Uuid;
1412

1513
#[macro_export]
@@ -23,7 +21,8 @@ macro_rules! collect_commands {
2321
)
2422
.commands(tauri_specta::collect_commands![
2523
// User
26-
msa_auth,
24+
begin_msa,
25+
finish_msa,
2726
get_users,
2827
get_user,
2928
remove_user,
@@ -215,13 +214,14 @@ pub async fn get_user(uuid: Uuid) -> Result<MinecraftCredentials, String> {
215214

216215
#[specta::specta]
217216
#[tauri::command]
218-
pub async fn msa_auth(handle: AppHandle) -> Result<MinecraftCredentials, String> {
219-
let login = minecraft::begin().await?;
220-
handle.shell().open(login.redirect_uri.clone(), None).map_err(|err| err.to_string())?;
221-
222-
Err("not implemented".to_string())
217+
pub async fn begin_msa() -> Result<MinecraftLogin, String> {
218+
Ok(minecraft::begin().await?)
219+
}
223220

224-
// Ok(minecraft::finish(code.as_str(), login).await?)
221+
#[specta::specta]
222+
#[tauri::command]
223+
pub async fn finish_msa(code: String, login: MinecraftLogin) -> Result<MinecraftCredentials, String> {
224+
Ok(minecraft::finish(code.as_str(), login).await?)
225225
}
226226

227227
#[specta::specta]

apps/testing/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ async fn main() -> onelauncher::Result<()> {
1717
Ok(())
1818
}
1919

20+
/// pauline's epic gamer minecraft auth flow
21+
/// opens a browser tab in which the user sees xbox login and logs in with microsoft and gets redirected
22+
/// to a new url. this new url contains the ?code=.... parameter that we need to finish authentication.
23+
/// in this testing environment we can just copy the final url from our browser and into the testing console
24+
/// and it will parse the url and find the code and complete the authenticaiton process.
25+
/// in production/frontend, it works the same way but we get the url and code parameter automatically.
2026
pub async fn authenticate_mc() -> onelauncher::Result<MinecraftCredentials> {
27+
// begins login flow, opens browser
2128
println!("a browser will open, follow login flow");
2229
let login = minecraft::begin().await?;
2330

2431
println!("url is {}", login.redirect_uri.as_str());
2532
webbrowser::open(login.redirect_uri.as_str())?;
2633

34+
// after user is done with auth, paste the final code into console below this line
2735
println!("enter flow url: ");
2836
let mut input = String::new();
2937
std::io::stdin()
@@ -32,6 +40,7 @@ pub async fn authenticate_mc() -> onelauncher::Result<MinecraftCredentials> {
3240

3341
println!("{}", input.trim());
3442

43+
// parses the url code query param using the same logic we should use in frontend
3544
let parsed = url::Url::parse(input.trim()).expect("idk");
3645
let code = if let Some((_, code)) = parsed.query_pairs().find(|x| x.0 == "code") {
3746
let code = code.clone();
@@ -41,6 +50,7 @@ pub async fn authenticate_mc() -> onelauncher::Result<MinecraftCredentials> {
4150
};
4251
let creds = minecraft::finish(code.as_str(), login).await?;
4352

53+
// mc auth flow is complete and added to state
4454
println!("logged in {}", creds.username);
4555
Ok(creds)
4656
}

packages/core/src/constants.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ pub const DUMMY_REPLACE_NEWLINE: &str = "\n";
2323
pub const DISCORD_RPC_CLIENT_ID: &str = "1234567890000000"; // todo
2424
/// Our Microsoft client ID.
2525
pub const MICROSOFT_CLIENT_ID: &str = "9eac3a4e-8cdd-43ef-863e-49cd601b1f03";
26-
/// The port to run the localhost server on.
27-
pub const MSA_SERVER_PORT: u16 = 13523;
2826
/// Mojang/Microsoft client ID.
2927
pub const MINECRAFT_CLIENT_ID: &str = "00000000402b5328";
3028
/// Mojang/Microsoft login redirect URI.

packages/core/src/store/auth_server.html

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/core/src/store/minecraft.rs

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -310,89 +310,6 @@ impl MinecraftState {
310310
}
311311
}
312312

313-
// pub struct LocalServer {
314-
// listener: TcpListener,
315-
// sender: tokio::sync::mpsc::Sender<bool>,
316-
// receiver: tokio::sync::mpsc::Receiver<bool>,
317-
// }
318-
319-
// impl LocalServer {
320-
// pub async fn new() -> crate::Result<Self> {
321-
// let listener = TcpListener::bind(format!("127.0.0.1:{}", constants::MSA_SERVER_PORT)).await?;
322-
// let (sender, receiver) = tokio::sync::mpsc::channel::<bool>(1);
323-
324-
// Ok(Self {
325-
// listener,
326-
// sender,
327-
// receiver,
328-
// })
329-
// }
330-
331-
// pub async fn wait_for_code(&self) -> crate::Result<String> {
332-
333-
// }
334-
335-
// async fn handle_request(&self, stream: &mut TcpStream) -> crate::Result<String> {
336-
// use tokio::io::{AsyncReadExt, AsyncWriteExt};
337-
// let code: Option<String>;
338-
339-
// let code_result = {
340-
// let mut buf = vec![0; 1024];
341-
// let size = stream.read(&mut buf).await?;
342-
// if size == 0 {
343-
// return Err(MinecraftAuthError::MSACodeFetchError.into());
344-
// }
345-
346-
// let request = String::from_utf8_lossy(&buf[..size]);
347-
// code = Some(
348-
// request
349-
// .split(' ')
350-
// .find(|x| x.starts_with("code="))
351-
// .ok_or(anyhow::anyhow!("No code found"))?
352-
// .split('=')
353-
// .nth(1)
354-
// .ok_or(anyhow::anyhow!("No code found"))?
355-
// .to_string()
356-
// );
357-
358-
// Ok::<(), String>(())
359-
// };
360-
361-
// let errored = code_result.is_err();
362-
// let html = include_str!("./auth_server.html");
363-
// let result = match code_result {
364-
// Ok(()) => {
365-
// html
366-
// .replace("%TITLE%", "Successfully Authenticated")
367-
// .replace("%CONTENT%", "You can now close this tab.")
368-
// }
369-
// Err(e) => {
370-
// html
371-
// .replace("%TITLE%", "Error")
372-
// .replace("%CONTENT%", &format!("An error has occurred: {}", e.as_str()))
373-
// }
374-
// };
375-
376-
// let response = format!(
377-
// "HTTP/1.1 {}\r\nContent-Length: {}\r\n\r\n{}",
378-
// if errored { "400 Bad Request" } else { "200 OK" },
379-
// result.len(),
380-
// result
381-
// );
382-
383-
// stream.write_all(response.as_bytes()).await?;
384-
385-
// if errored {
386-
// return Err(MinecraftAuthError::MSACodeFetchError.into());
387-
// }
388-
389-
// match code {
390-
// Some(code) => Ok(code),
391-
// None => Err(MinecraftAuthError::MSACodeFetchError.into()),
392-
// }
393-
// }
394-
// }
395-
396313
/// A [`reqwest::Request`] with a [`DateTime<Utc>`] attached to it.
397314
struct RequestWithDate<T> {
398315
pub date: DateTime<Utc>,
@@ -1117,7 +1034,5 @@ pub enum MinecraftAuthError {
11171034
#[error("failed to read user hash")]
11181035
HashError,
11191036
#[error("failed to read user xbox session ID")]
1120-
SessionIdError,
1121-
#[error("failed to read user MSA code")]
1122-
MSACodeFetchError,
1037+
SessionIdError
11231038
}

0 commit comments

Comments
 (0)