Skip to content

Commit

Permalink
Fixed auth and added summary/description to ModIOMod definition
Browse files Browse the repository at this point in the history
  • Loading branch information
aNaOH committed Jun 11, 2024
1 parent 35edd4c commit 962cad4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 84 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
image = "0.23"
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
93 changes: 21 additions & 72 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use tokio::fs::read;

use image::GenericImageView;

use serde::Deserialize;

struct ModIOAddon;

#[gdextension]
Expand All @@ -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,
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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(),
Expand All @@ -138,12 +148,6 @@ impl ModIOMod {
}
}

#[derive(Deserialize)]
struct AuthResponse {
access_token: String,
expires_in: u64,
}

struct ModIOClient {
client: Modio,
id: u64
Expand Down Expand Up @@ -221,39 +225,12 @@ impl ModIOClient {
Ok(mod_info)
}

pub async fn login_with_email(&self, email: &str, password: &str) -> Result<String, Box<dyn std::error::Error>> {
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<String, Box<dyn std::error::Error>> {

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<String, Box<dyn std::error::Error>> {
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())
}
Expand Down Expand Up @@ -393,52 +370,24 @@ 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()
}
}
};

let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(result)
} else {
empty_dict
"".to_godot()
}
}
}

0 comments on commit 962cad4

Please sign in to comment.