From 962cad459daca81571d3bfe74fc99cf9c2e5f8a2 Mon Sep 17 00:00:00 2001 From: Abel Date: Tue, 11 Jun 2024 23:02:16 +0200 Subject: [PATCH] Fixed auth and added summary/description to ModIOMod definition --- Cargo.lock | 2 -- Cargo.toml | 4 +-- README.md | 10 ++---- src/lib.rs | 93 ++++++++++++------------------------------------------ 4 files changed, 25 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 47f940f..306bb93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -737,8 +737,6 @@ dependencies = [ "image", "modio", "reqwest", - "serde", - "serde_json", "tokio", "zip", ] diff --git a/Cargo.toml b/Cargo.toml index 3ad9d2c..f981e12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,4 @@ modio = { git = "https://github.com/nickelc/modio-rs", branch = "master" } tokio = { version = "1.32.0", features = ["full"] } zip = "0.5" reqwest = { version = "0.11", features = ["json"] } -image = "0.23" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +image = "0.23" \ No newline at end of file diff --git a/README.md b/README.md index 27aaa8e..0937b91 100644 --- a/README.md +++ b/README.md @@ -49,12 +49,8 @@ Returns the mod dictionary. ## Auth -These functions returns a Dictionary with an api key located in dictionary["api_key"] +These functions returns a String with an api key on success or an empty String otherwise -### login_with_email(email: String, password: String) +### login_with_steam(ticket: String) -Uses email and password to login to mod.io - -### login_with_steam(app_id: int, ticket: String) - -Uses the Steam AppID and user auth ticket to login to mod.io +Uses the Steam user auth ticket to login to mod.io diff --git a/src/lib.rs b/src/lib.rs index 6bf3890..a22b2f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,8 +22,6 @@ use tokio::fs::read; use image::GenericImageView; -use serde::Deserialize; - struct ModIOAddon; #[gdextension] @@ -33,6 +31,8 @@ pub struct ModIOMod { pub id: u64, pub name: GString, pub submitter: GString, + pub summary: GString, + pub description: GString, pub date_updated: i64, pub date_live: i64, pub thumb_url: GString, @@ -55,6 +55,8 @@ impl ToGodot for ModIOMod { dictionary.insert("id", self.id); dictionary.insert("name", self.name.clone()); dictionary.insert("submitter", self.submitter.clone()); + dictionary.insert("summary", self.summary.clone()); + dictionary.insert("description", self.description.clone()); dictionary.insert("date_updated", self.date_updated); dictionary.insert("date_live", self.date_live); dictionary.insert("thumb_url", self.thumb_url.clone()); @@ -73,6 +75,8 @@ impl ToGodot for ModIOMod { dictionary.insert("id", self.id); dictionary.insert("name", self.name.clone()); dictionary.insert("submitter", self.submitter.clone()); + dictionary.insert("summary", self.summary.clone()); + dictionary.insert("description", self.description.clone()); dictionary.insert("date_updated", self.date_updated); dictionary.insert("date_live", self.date_live); dictionary.insert("thumb_url", self.thumb_url.clone()); @@ -90,6 +94,8 @@ impl ToGodot for ModIOMod { dictionary.insert("id", self.id); dictionary.insert("name", self.name.clone()); dictionary.insert("submitter", self.submitter.clone()); + dictionary.insert("summary", self.summary.clone()); + dictionary.insert("description", self.description.clone()); dictionary.insert("date_updated", self.date_updated); dictionary.insert("date_live", self.date_live); dictionary.insert("thumb_url", self.thumb_url.clone()); @@ -122,10 +128,14 @@ impl ModIOMod { .map(|tag| tag.name.as_str().into()) .collect(); + + Self { id: mod_info.id.get(), name: mod_info.name.as_str().into(), submitter: mod_info.submitted_by.username.as_str().into(), + summary: mod_info.summary.as_str().into(), + description: mod_info.description_plaintext.into(), date_updated: mod_info.date_updated as i64, date_live: mod_info.date_live as i64, thumb_url: mod_info.logo.thumb_1280x720.as_str().into_godot(), @@ -138,12 +148,6 @@ impl ModIOMod { } } -#[derive(Deserialize)] -struct AuthResponse { - access_token: String, - expires_in: u64, -} - struct ModIOClient { client: Modio, id: u64 @@ -221,39 +225,12 @@ impl ModIOClient { Ok(mod_info) } - pub async fn login_with_email(&self, email: &str, password: &str) -> Result> { - let client = Client::new(); - let response = client.post("https://api.mod.io/v1/oauth/email") - .form(&[ - ("email", email), - ("password", password), - ("grant_type", "password"), - ]) - .send() - .await?; + pub async fn login_with_steam(&self, ticket: &str) -> Result> { - if response.status().is_success() { - let auth: AuthResponse = response.json().await?; - Ok(auth.access_token) - } else { - Err("Failed to login with email and password".into()) - } - } - - pub async fn login_with_steam(&self, app_id: &str, ticket: &str) -> Result> { - let client = Client::new(); - let response = client.post("https://api.mod.io/v1/oauth/steam") - .form(&[ - ("appdata", app_id), - ("ticket", ticket), - ("grant_type", "steam"), - ]) - .send() - .await?; + let auth = self.client.auth().external(modio::auth::SteamOptions::new(ticket)).await?; - if response.status().is_success() { - let auth: AuthResponse = response.json().await?; - Ok(auth.access_token) + if !auth.api_key.is_empty() { + Ok(auth.api_key) } else { Err("Failed to login with Steam".into()) } @@ -393,44 +370,16 @@ impl ModIO { } #[func] - fn login_with_email(&self, email: GString, password: GString) -> Dictionary { - let empty_dict = Dictionary::new(); + fn login_with_steam(&self, ticket: GString) -> GString { if let Some(ref client) = self.client { let result = async { - match client.login_with_email(&email.to_string(), &password.to_string()).await { + match client.login_with_steam(&ticket.to_string()).await { Ok(api_key) => { - let mut dict = Dictionary::new(); - dict.insert("api_key", api_key); - dict - } - Err(err) => { - godot_print!("Error logging in with email: {:?}", err); - empty_dict - } - } - }; - - let rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on(result) - } else { - empty_dict - } - } - - #[func] - fn login_with_steam(&self, app_id: u64, ticket: GString) -> Dictionary { - let empty_dict = Dictionary::new(); - if let Some(ref client) = self.client { - let result = async { - match client.login_with_steam(&app_id.to_string(), &ticket.to_string()).await { - Ok(api_key) => { - let mut dict = Dictionary::new(); - dict.insert("api_key", api_key); - dict + api_key.to_godot() } Err(err) => { godot_print!("Error logging in with Steam: {:?}", err); - empty_dict + "".to_godot() } } }; @@ -438,7 +387,7 @@ impl ModIO { let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(result) } else { - empty_dict + "".to_godot() } } } \ No newline at end of file