Skip to content

Commit 856e401

Browse files
committed
feat(api): session token
1 parent 595eb0f commit 856e401

6 files changed

Lines changed: 24 additions & 7 deletions

File tree

src-tauri/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ sysinfo = "0.29"
4747
regex = "1.7.0"
4848
uuid = { version = "1.2", features = ["serde", "v4"] }
4949
chrono = { version = "0.4", features = ["serde"] }
50+
rand = "0.9"
5051

5152
sha1 = "0.10"
5253
base16ct = {version = "0.2.0", features = ["alloc"] }

src-tauri/src/app/client_api.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,23 @@ pub struct Client {
4949
// To show a warning to the user when using a non-secure connection,
5050
// we need to pass this information to the frontend.
5151
is_secure: bool,
52+
session_token: String
5253
}
5354

5455
impl Client {
55-
pub fn new(host: &str) -> Self {
56+
pub fn new(host: &str, session_token: String) -> Self {
5657
Self {
5758
url: host.to_string(),
5859
is_secure: host.starts_with("https://"),
60+
session_token
5961
}
6062
}
6163

6264
/// Finds the first available API endpoint
6365
/// and returns a [Client] instance with the endpoint set.
6466
///
6567
/// Returns [String] as error with technical information if no API endpoint is reachable.
66-
pub async fn lookup() -> Result<Self, String> {
68+
pub async fn lookup(session_token: String) -> Result<Self, String> {
6769
let span = debug_span!("api_lookup");
6870
let _guard = span.enter();
6971

@@ -127,7 +129,7 @@ impl Client {
127129

128130
if is_success {
129131
debug!(parent: &span, "API endpoint '{}' is available", endpoint);
130-
return Ok(Self::new(endpoint));
132+
return Ok(Self::new(endpoint, session_token));
131133
}
132134
}
133135

@@ -205,6 +207,7 @@ impl Client {
205207
pub async fn request_from_endpoint<T: DeserializeOwned>(&self, api_version: &str, endpoint: &str) -> Result<T> {
206208
Ok(HTTP_CLIENT
207209
.get(format!("{}/{}/{}", self.url, api_version, endpoint))
210+
.header("X-Session-Token", &self.session_token)
208211
.send()
209212
.await?
210213
.error_for_status()?
@@ -219,6 +222,7 @@ impl Client {
219222
) -> Result<T> {
220223
Ok(client_account
221224
.authenticate_request(HTTP_CLIENT.get(format!("{}/{}/{}", self.url, API_V3, endpoint)))?
225+
.header("X-Session-Token", &self.session_token)
222226
.send()
223227
.await?
224228
.error_for_status()?

src-tauri/src/app/gui/commands/system.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub(crate) async fn get_launcher_version() -> Result<String, String> {
2626
}
2727

2828
#[tauri::command]
29-
pub(crate) async fn setup_client() -> Result<Client, String> {
30-
Client::lookup()
29+
pub(crate) async fn setup_client(session_token: String) -> Result<Client, String> {
30+
Client::lookup(session_token)
3131
.await
3232
}
3333

src-tauri/src/app/options.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::{collections::HashMap, path::Path};
2222
use crate::minecraft::java::DistributionSelection;
2323
use crate::{auth::ClientAccount, minecraft::auth::MinecraftAccount};
2424
use anyhow::Result;
25+
use rand::distr::{Alphanumeric, SampleString};
2526
use serde::{Deserialize, Serialize};
2627
use tokio::fs;
2728
use tracing::info;
@@ -72,6 +73,8 @@ pub(crate) struct LauncherOptions {
7273
pub concurrent_downloads: u32,
7374
#[serde(rename = "keepLauncherOpen")]
7475
pub keep_launcher_open: bool,
76+
#[serde(rename = "sessionToken", default = "random_token")]
77+
pub session_token: String,
7578
}
7679

7780
#[derive(Serialize, Deserialize)]
@@ -126,6 +129,7 @@ impl Options {
126129
keep_launcher_open: legacy.keep_launcher_open,
127130
show_nightly_builds: legacy.show_nightly_builds,
128131
concurrent_downloads: legacy.concurrent_downloads as u32,
132+
session_token: random_token()
129133
},
130134
premium_options: PremiumOptions {
131135
account: legacy.client_account,
@@ -170,6 +174,7 @@ impl Default for LauncherOptions {
170174
show_nightly_builds: false,
171175
keep_launcher_open: false,
172176
concurrent_downloads: 10,
177+
session_token: random_token()
173178
}
174179
}
175180
}
@@ -198,6 +203,10 @@ fn default_memory() -> u64 {
198203
4096
199204
}
200205

206+
fn random_token() -> String {
207+
Alphanumeric.sample_string(&mut rand::rng(), 16)
208+
}
209+
201210
// Legacy format structure
202211
#[derive(Deserialize)]
203212
#[allow(unused)]

src/lib/Window.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
6767
async function setupClient() {
6868
try {
69-
client = await invoke("setup_client");
69+
client = await invoke("setup_client", {
70+
sessionToken: options.launcher.sessionToken
71+
});
7072
console.info("API Client has been set up", client);
7173
} catch (e) {
7274
console.error("Failed to set up API client:", e);
@@ -88,8 +90,8 @@
8890
}
8991
9092
onMount(async () => {
91-
await Promise.all([handleUpdate(), setupClient(), checkSystem()]);
9293
await setupOptions();
94+
await Promise.all([handleUpdate(), setupClient(), checkSystem()]);
9395
loading = false;
9496
});
9597
</script>

0 commit comments

Comments
 (0)