|
| 1 | +use reqwest::cookie::CookieStore; |
| 2 | +use std::io::{self, Write}; |
| 3 | +use std::str::FromStr; |
| 4 | +use url::Url; |
| 5 | +pub use vrchatapi::apis; |
| 6 | +use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode}; |
| 7 | + |
| 8 | +#[tokio::main] |
| 9 | +async fn main() { |
| 10 | + let mut config = apis::configuration::Configuration::default(); |
| 11 | + config.basic_auth = Some((String::from("username"), Some(String::from("password")))); |
| 12 | + config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com")); |
| 13 | + |
| 14 | + let cookie_store = std::sync::Arc::new(reqwest::cookie::Jar::default()); |
| 15 | + config.client = reqwest::Client::builder() |
| 16 | + .cookie_store(true) |
| 17 | + .cookie_provider(cookie_store.clone()) |
| 18 | + .build() |
| 19 | + .unwrap(); |
| 20 | + |
| 21 | + match apis::authentication_api::get_current_user(&config) |
| 22 | + .await |
| 23 | + .unwrap() |
| 24 | + { |
| 25 | + vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => { |
| 26 | + println!("Username: {}", me.username.unwrap()) |
| 27 | + } |
| 28 | + vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => { |
| 29 | + if requires_auth |
| 30 | + .requires_two_factor_auth |
| 31 | + .contains(&String::from("emailOtp")) |
| 32 | + { |
| 33 | + let code = read_user_input("Please enter your Email 2fa code: "); |
| 34 | + if let Err(err) = apis::authentication_api::verify2_fa_email_code( |
| 35 | + &config, |
| 36 | + TwoFactorEmailCode::new(code), |
| 37 | + ) |
| 38 | + .await |
| 39 | + { |
| 40 | + eprintln!("Error verifying 2FA email code: {}", err); |
| 41 | + } |
| 42 | + } else { |
| 43 | + let code = read_user_input("Please enter your Authenticator 2fa code: "); |
| 44 | + if let Err(err) = |
| 45 | + apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code)) |
| 46 | + .await |
| 47 | + { |
| 48 | + eprintln!("Error verifying 2FA auth code: {}", err); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + let user = apis::authentication_api::get_current_user(&config) |
| 55 | + .await |
| 56 | + .unwrap(); |
| 57 | + |
| 58 | + match user { |
| 59 | + EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name), |
| 60 | + EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"), |
| 61 | + } |
| 62 | + |
| 63 | + println!( |
| 64 | + "Cookie:{}", |
| 65 | + cookie_store |
| 66 | + .cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay")) |
| 67 | + .expect("Cookies not found") |
| 68 | + .to_str() |
| 69 | + .expect("Cookies not valid string") |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +fn read_user_input(prompt: &str) -> String { |
| 74 | + print!("{}", prompt); |
| 75 | + io::stdout().flush().expect("Failed to flush stdout"); |
| 76 | + |
| 77 | + let mut input = String::new(); |
| 78 | + io::stdin() |
| 79 | + .read_line(&mut input) |
| 80 | + .expect("Failed to read line"); |
| 81 | + |
| 82 | + input.trim().to_string() |
| 83 | +} |
0 commit comments