Skip to content

Commit

Permalink
Extract login into seperate method
Browse files Browse the repository at this point in the history
  • Loading branch information
C0D3-M4513R committed Sep 19, 2024
1 parent 02e156b commit f21e958
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@ async fn test() -> Result<(), anyhow::Error>{
dotenv::dotenv().map_err(|e|Error::DotenvInitError(e))?;
let username = dotenv::var("USERNAME").map_err(|e|Error::NoUsername(e))?;
let password = dotenv::var("PASSWORD").map_err(|e|Error::NoPassword(e))?;

let mut client = vrchatapi::apis::configuration::Configuration::default();
client.user_agent = Some(format!("vrchatapi-rust@{VERSION} https://github.com/vrchatapi/vrchatapi-rust/issues/new"));
client.basic_auth = Some((username.clone(), Some(password)));
let u = login(&client, &username).await?;

logout(&client).await?;
Ok(())
}

async fn login(client: &vrchatapi::apis::configuration::Configuration, username: &String) -> Result<vrchatapi::models::CurrentUser, anyhow::Error> {
let totp_secret = dotenv::var("TOTP_SECRET").map_err(|e|Error::NoTotpSecret(e))?;
let totp_secret = base32::decode(Alphabet::Rfc4648Lower {padding: false}, totp_secret.as_str()).ok_or(Error::TOTPBase32)?;
let generator = totp_rfc6238::TotpGenerator::new()
.build();

let mut client = vrchatapi::apis::configuration::Configuration::default();
client.user_agent = Some(format!("vrchatapi-rust@{VERSION} https://github.com/vrchatapi/vrchatapi-rust/issues/new"));
client.basic_auth = Some((username.clone(), Some(password)));
let u = match vrchatapi::apis::authentication_api::get_current_user(&client).await.map_err(|e|Error::GetCurrentUser(e))? {
EitherUserOrTwoFactor::CurrentUser(u) => u,
EitherUserOrTwoFactor::RequiresTwoFactorAuth(r2fa) => {
Expand All @@ -68,11 +76,8 @@ async fn test() -> Result<(), anyhow::Error>{
}
};
println!("Logged in as: {} (Login Name was {username}", u.username.as_ref().map(String::as_str).unwrap_or("Unknown User"));

logout(&client).await?;
Ok(())
Ok(u)
}

async fn logout(client: &vrchatapi::apis::configuration::Configuration) -> Result<(), anyhow::Error> {
vrchatapi::apis::authentication_api::logout(&client).await.map_err(|e|Error::Logout(e))?;
Ok(())
Expand Down

0 comments on commit f21e958

Please sign in to comment.