From 01fc6b276ff16b81f76ac55533116d3f40fd455b Mon Sep 17 00:00:00 2001 From: jellejurre Date: Mon, 25 Nov 2024 10:57:36 +0100 Subject: [PATCH 1/7] Update online example --- examples/online.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/examples/online.rs b/examples/online.rs index 0a1ad52..6af6c4c 100644 --- a/examples/online.rs +++ b/examples/online.rs @@ -1,10 +1,12 @@ +use std::io::{self, Write}; pub use vrchatapi::apis; +use vrchatapi::models::{TwoFactorAuthCode, TwoFactorEmailCode}; #[tokio::main] async fn main() { let mut config = apis::configuration::Configuration::default(); config.basic_auth = Some((String::from("username"), Some(String::from("password")))); - + config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com")); match apis::authentication_api::get_current_user(&config) .await .unwrap() @@ -12,10 +14,30 @@ async fn main() { vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => { println!("Username: {}", me.username.unwrap()) } - vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => println!( - "The Username requires Auth: {:?}", - requires_auth.requires_two_factor_auth - ), + vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => { + if (requires_auth.requires_two_factor_auth.contains(&String::from("email"))){ + let code = read_user_input("Please enter your Email 2fa code: "); + if let Err(err) = apis::authentication_api::verify2_fa_email_code( + &config, + TwoFactorEmailCode::new(code), + ) + .await + { + eprintln!("Error verifying 2FA email code: {}", err); + } + } else { + let code = read_user_input("Please enter your Authenticator 2fa code: "); + if let Err(err) = apis::authentication_api::verify2_fa( + &config, + TwoFactorAuthCode::new(code), + ) + .await + { + eprintln!("Error verifying 2FA auth code: {}", err); + } + } + + } } let online = apis::system_api::get_current_online_users(&config) @@ -23,3 +45,15 @@ async fn main() { .unwrap(); println!("Current Online Users: {}", online); } + +fn read_user_input(prompt: &str) -> String { + print!("{}", prompt); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + + input.trim().to_string() +} From 894ecd143476a7b2cdb03b73c69502de0f476c8f Mon Sep 17 00:00:00 2001 From: jellejurre Date: Mon, 25 Nov 2024 10:59:39 +0100 Subject: [PATCH 2/7] Fix string name --- examples/online.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/online.rs b/examples/online.rs index 6af6c4c..25b6fd4 100644 --- a/examples/online.rs +++ b/examples/online.rs @@ -15,7 +15,7 @@ async fn main() { println!("Username: {}", me.username.unwrap()) } vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => { - if (requires_auth.requires_two_factor_auth.contains(&String::from("email"))){ + if (requires_auth.requires_two_factor_auth.contains(&String::from("emailOtp"))){ let code = read_user_input("Please enter your Email 2fa code: "); if let Err(err) = apis::authentication_api::verify2_fa_email_code( &config, From 59c59c7cdac74fe39a1b24a46f61d40f5f84ecfd Mon Sep 17 00:00:00 2001 From: jellejurre Date: Mon, 25 Nov 2024 12:18:39 +0100 Subject: [PATCH 3/7] Make linter happy --- examples/online.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/online.rs b/examples/online.rs index 25b6fd4..258f2cd 100644 --- a/examples/online.rs +++ b/examples/online.rs @@ -15,7 +15,7 @@ async fn main() { println!("Username: {}", me.username.unwrap()) } vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => { - if (requires_auth.requires_two_factor_auth.contains(&String::from("emailOtp"))){ + if requires_auth.requires_two_factor_auth.contains(&String::from("emailOtp")){ let code = read_user_input("Please enter your Email 2fa code: "); if let Err(err) = apis::authentication_api::verify2_fa_email_code( &config, From c2d3c81a1b27bbb020347a65959987cec60513d8 Mon Sep 17 00:00:00 2001 From: jellejurre Date: Mon, 25 Nov 2024 12:29:14 +0100 Subject: [PATCH 4/7] Update examples and add cookie examples --- examples/cookies_load.rs | 31 ++++++++++++ examples/cookies_store.rs | 76 ++++++++++++++++++++++++++++++ examples/{online.rs => example.rs} | 10 ++-- 3 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 examples/cookies_load.rs create mode 100644 examples/cookies_store.rs rename examples/{online.rs => example.rs} (84%) diff --git a/examples/cookies_load.rs b/examples/cookies_load.rs new file mode 100644 index 0000000..43a1587 --- /dev/null +++ b/examples/cookies_load.rs @@ -0,0 +1,31 @@ +use std::io::{self, Write}; +use reqwest::header::{HeaderMap, HeaderValue}; +pub use vrchatapi::apis; +use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode}; + +#[tokio::main] +async fn main() { + let mut config = apis::configuration::Configuration::default(); + config.basic_auth = Some((String::from("username"), Some(String::from("password")))); + config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com")); + + let mut header_map = HeaderMap::new(); + header_map.append("Cookie", HeaderValue::from_str( + &"auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]" + ).unwrap()); + + config.client = reqwest::Client::builder() + .cookie_store(true) + .default_headers(header_map) + .build() + .unwrap(); + + let user = apis::authentication_api::get_current_user(&config) + .await + .unwrap(); + + match user { + EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name), + EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid") + } +} diff --git a/examples/cookies_store.rs b/examples/cookies_store.rs new file mode 100644 index 0000000..9616eba --- /dev/null +++ b/examples/cookies_store.rs @@ -0,0 +1,76 @@ +use std::io::{self, Write}; +use std::str::FromStr; +use reqwest::cookie::CookieStore; +use url::Url; +pub use vrchatapi::apis; +use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode}; + +#[tokio::main] +async fn main() { + let mut config = apis::configuration::Configuration::default(); + config.basic_auth = Some((String::from("username"), Some(String::from("password")))); + config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com")); + + let cookie_store = std::sync::Arc::new(reqwest::cookie::Jar::default()); + config.client = reqwest::Client::builder() + .cookie_store(true) + .cookie_provider(cookie_store.clone()) + .build() + .unwrap(); + + match apis::authentication_api::get_current_user(&config) + .await + .unwrap() + { + vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => { + println!("Username: {}", me.username.unwrap()) + } + vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => { + if requires_auth.requires_two_factor_auth.contains(&String::from("emailOtp")){ + let code = read_user_input("Please enter your Email 2fa code: "); + if let Err(err) = apis::authentication_api::verify2_fa_email_code( + &config, + TwoFactorEmailCode::new(code), + ) + .await + { + eprintln!("Error verifying 2FA email code: {}", err); + } + } else { + let code = read_user_input("Please enter your Authenticator 2fa code: "); + if let Err(err) = apis::authentication_api::verify2_fa( + &config, + TwoFactorAuthCode::new(code), + ) + .await + { + eprintln!("Error verifying 2FA auth code: {}", err); + } + } + + } + } + + let user = apis::authentication_api::get_current_user(&config) + .await + .unwrap(); + + match user { + EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name), + EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid") + } + + println!("Cookie:{}", cookie_store.cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay")).expect("Cookies not found").to_str().expect("Cookies not valid string")); +} + +fn read_user_input(prompt: &str) -> String { + print!("{}", prompt); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + + input.trim().to_string() +} diff --git a/examples/online.rs b/examples/example.rs similarity index 84% rename from examples/online.rs rename to examples/example.rs index 258f2cd..2d99a95 100644 --- a/examples/online.rs +++ b/examples/example.rs @@ -1,6 +1,6 @@ use std::io::{self, Write}; pub use vrchatapi::apis; -use vrchatapi::models::{TwoFactorAuthCode, TwoFactorEmailCode}; +use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode}; #[tokio::main] async fn main() { @@ -40,10 +40,14 @@ async fn main() { } } - let online = apis::system_api::get_current_online_users(&config) + let user = apis::authentication_api::get_current_user(&config) .await .unwrap(); - println!("Current Online Users: {}", online); + + match user { + EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name), + EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid") + } } fn read_user_input(prompt: &str) -> String { From 334142376e99cdfcdd7a56ecea6f08b4fd8bb7bf Mon Sep 17 00:00:00 2001 From: jellejurre Date: Mon, 25 Nov 2024 14:08:16 +0100 Subject: [PATCH 5/7] Run cargo fmt --- examples/cookies_load.rs | 22 +++++++++++++--------- examples/cookies_store.rs | 37 ++++++++++++++++++++++--------------- examples/example.rs | 18 +++++++++--------- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/examples/cookies_load.rs b/examples/cookies_load.rs index 43a1587..4597b8e 100644 --- a/examples/cookies_load.rs +++ b/examples/cookies_load.rs @@ -1,5 +1,5 @@ -use std::io::{self, Write}; use reqwest::header::{HeaderMap, HeaderValue}; +use std::io::{self, Write}; pub use vrchatapi::apis; use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode}; @@ -10,15 +10,19 @@ async fn main() { config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com")); let mut header_map = HeaderMap::new(); - header_map.append("Cookie", HeaderValue::from_str( - &"auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]" - ).unwrap()); + header_map.append( + "Cookie", + HeaderValue::from_str( + &"auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]", + ) + .unwrap(), + ); config.client = reqwest::Client::builder() - .cookie_store(true) - .default_headers(header_map) - .build() - .unwrap(); + .cookie_store(true) + .default_headers(header_map) + .build() + .unwrap(); let user = apis::authentication_api::get_current_user(&config) .await @@ -26,6 +30,6 @@ async fn main() { match user { EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name), - EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid") + EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"), } } diff --git a/examples/cookies_store.rs b/examples/cookies_store.rs index 9616eba..52e58a0 100644 --- a/examples/cookies_store.rs +++ b/examples/cookies_store.rs @@ -1,6 +1,6 @@ +use reqwest::cookie::CookieStore; use std::io::{self, Write}; use std::str::FromStr; -use reqwest::cookie::CookieStore; use url::Url; pub use vrchatapi::apis; use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode}; @@ -13,10 +13,10 @@ async fn main() { let cookie_store = std::sync::Arc::new(reqwest::cookie::Jar::default()); config.client = reqwest::Client::builder() - .cookie_store(true) - .cookie_provider(cookie_store.clone()) - .build() - .unwrap(); + .cookie_store(true) + .cookie_provider(cookie_store.clone()) + .build() + .unwrap(); match apis::authentication_api::get_current_user(&config) .await @@ -26,28 +26,28 @@ async fn main() { println!("Username: {}", me.username.unwrap()) } vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => { - if requires_auth.requires_two_factor_auth.contains(&String::from("emailOtp")){ + if requires_auth + .requires_two_factor_auth + .contains(&String::from("emailOtp")) + { let code = read_user_input("Please enter your Email 2fa code: "); if let Err(err) = apis::authentication_api::verify2_fa_email_code( &config, TwoFactorEmailCode::new(code), ) - .await + .await { eprintln!("Error verifying 2FA email code: {}", err); } } else { let code = read_user_input("Please enter your Authenticator 2fa code: "); - if let Err(err) = apis::authentication_api::verify2_fa( - &config, - TwoFactorAuthCode::new(code), - ) - .await + if let Err(err) = + apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code)) + .await { eprintln!("Error verifying 2FA auth code: {}", err); } } - } } @@ -57,10 +57,17 @@ async fn main() { match user { EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name), - EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid") + EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"), } - println!("Cookie:{}", cookie_store.cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay")).expect("Cookies not found").to_str().expect("Cookies not valid string")); + println!( + "Cookie:{}", + cookie_store + .cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay")) + .expect("Cookies not found") + .to_str() + .expect("Cookies not valid string") + ); } fn read_user_input(prompt: &str) -> String { diff --git a/examples/example.rs b/examples/example.rs index 2d99a95..39fb5da 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -15,28 +15,28 @@ async fn main() { println!("Username: {}", me.username.unwrap()) } vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => { - if requires_auth.requires_two_factor_auth.contains(&String::from("emailOtp")){ + if requires_auth + .requires_two_factor_auth + .contains(&String::from("emailOtp")) + { let code = read_user_input("Please enter your Email 2fa code: "); if let Err(err) = apis::authentication_api::verify2_fa_email_code( &config, TwoFactorEmailCode::new(code), ) - .await + .await { eprintln!("Error verifying 2FA email code: {}", err); } } else { let code = read_user_input("Please enter your Authenticator 2fa code: "); - if let Err(err) = apis::authentication_api::verify2_fa( - &config, - TwoFactorAuthCode::new(code), - ) - .await + if let Err(err) = + apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code)) + .await { eprintln!("Error verifying 2FA auth code: {}", err); } } - } } @@ -46,7 +46,7 @@ async fn main() { match user { EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name), - EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid") + EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"), } } From dffafeed8f6c5dfee8a6e3361c1776bc64f1d3cb Mon Sep 17 00:00:00 2001 From: jellejurre Date: Mon, 25 Nov 2024 15:23:31 +0100 Subject: [PATCH 6/7] Implement feedback --- examples/cookies_load.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/examples/cookies_load.rs b/examples/cookies_load.rs index 4597b8e..356297b 100644 --- a/examples/cookies_load.rs +++ b/examples/cookies_load.rs @@ -1,7 +1,9 @@ -use reqwest::header::{HeaderMap, HeaderValue}; -use std::io::{self, Write}; +use reqwest::header::{HeaderValue}; +use std::str::FromStr; +use std::sync::Arc; +use reqwest::cookie::CookieStore; pub use vrchatapi::apis; -use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode}; +use vrchatapi::models::{EitherUserOrTwoFactor}; #[tokio::main] async fn main() { @@ -9,18 +11,15 @@ async fn main() { config.basic_auth = Some((String::from("username"), Some(String::from("password")))); config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com")); - let mut header_map = HeaderMap::new(); - header_map.append( - "Cookie", - HeaderValue::from_str( - &"auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]", - ) - .unwrap(), - ); + let mut jar = reqwest::cookie::Jar::default(); + jar.set_cookies(&mut [HeaderValue::from_str( + &"auth=[AUTH_COOKIE_HERE], twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]", + ).expect("Cookie not okay")].iter(), &url::Url::from_str("https://api.vrchat.cloud").expect("Url not okay")); + let jar = Arc::new(jar); config.client = reqwest::Client::builder() .cookie_store(true) - .default_headers(header_map) + .cookie_provider(jar) .build() .unwrap(); From 0e6e8a06f65f9e048409ee9217bfbf718cdca56c Mon Sep 17 00:00:00 2001 From: jellejurre Date: Mon, 25 Nov 2024 15:24:44 +0100 Subject: [PATCH 7/7] Run cargo fmt --- examples/cookies_load.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/examples/cookies_load.rs b/examples/cookies_load.rs index 356297b..6f954ba 100644 --- a/examples/cookies_load.rs +++ b/examples/cookies_load.rs @@ -1,9 +1,9 @@ -use reqwest::header::{HeaderValue}; +use reqwest::cookie::CookieStore; +use reqwest::header::HeaderValue; use std::str::FromStr; use std::sync::Arc; -use reqwest::cookie::CookieStore; pub use vrchatapi::apis; -use vrchatapi::models::{EitherUserOrTwoFactor}; +use vrchatapi::models::EitherUserOrTwoFactor; #[tokio::main] async fn main() { @@ -12,9 +12,14 @@ async fn main() { config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com")); let mut jar = reqwest::cookie::Jar::default(); - jar.set_cookies(&mut [HeaderValue::from_str( - &"auth=[AUTH_COOKIE_HERE], twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]", - ).expect("Cookie not okay")].iter(), &url::Url::from_str("https://api.vrchat.cloud").expect("Url not okay")); + jar.set_cookies( + &mut [HeaderValue::from_str( + &"auth=[AUTH_COOKIE_HERE], twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]", + ) + .expect("Cookie not okay")] + .iter(), + &url::Url::from_str("https://api.vrchat.cloud").expect("Url not okay"), + ); let jar = Arc::new(jar); config.client = reqwest::Client::builder()