Skip to content

Commit 86af32e

Browse files
committed
make examples work with updated 2fa flow
1 parent 9eaf841 commit 86af32e

File tree

6 files changed

+63
-61
lines changed

6 files changed

+63
-61
lines changed

examples/cookies_load.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ use reqwest::cookie::CookieStore;
22
use reqwest::header::HeaderValue;
33
use std::str::FromStr;
44
use std::sync::Arc;
5-
pub use vrchatapi::apis;
6-
use vrchatapi::models::EitherUserOrTwoFactor;
75

86
#[tokio::main]
97
async fn main() {
10-
let mut config = apis::configuration::Configuration::default();
8+
let mut config = ::vrchatapi::apis::configuration::Configuration::default();
119
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
1210
config.user_agent = Some(String::from("ExampleProgram/0.0.1 [email protected]"));
1311

@@ -27,12 +25,16 @@ async fn main() {
2725
.build()
2826
.unwrap();
2927

30-
let user = apis::authentication_api::get_current_user(&config)
28+
let user = ::vrchatapi::apis::authentication_api::get_current_user(&config)
3129
.await
3230
.unwrap();
3331

3432
match user {
35-
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
36-
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
33+
::vrchatapi::models::RegisterUserAccount200Response::CurrentUser(user) => {
34+
println!("Current user: {}", user.display_name)
35+
}
36+
::vrchatapi::models::RegisterUserAccount200Response::RequiresTwoFactorAuth(_) => {
37+
println!("cookie invalid")
38+
}
3739
}
3840
}

examples/cookies_store.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
use reqwest::cookie::CookieStore;
2-
use std::io::{self, Write};
32
use std::str::FromStr;
4-
use url::Url;
5-
pub use vrchatapi::apis;
6-
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};
73

84
#[tokio::main]
95
async fn main() {
10-
let mut config = apis::configuration::Configuration::default();
6+
let mut config = ::vrchatapi::apis::configuration::Configuration::default();
117
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
128
config.user_agent = Some(String::from("ExampleProgram/0.0.1 [email protected]"));
139

@@ -17,64 +13,74 @@ async fn main() {
1713
.build()
1814
.unwrap();
1915

20-
match apis::authentication_api::get_current_user(&config)
16+
match ::vrchatapi::apis::authentication_api::get_current_user(&config)
2117
.await
2218
.unwrap()
2319
{
24-
vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
20+
::vrchatapi::models::RegisterUserAccount200Response::CurrentUser(me) => {
2521
println!("Username: {}", me.username.unwrap())
2622
}
27-
vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => {
23+
::vrchatapi::models::RegisterUserAccount200Response::RequiresTwoFactorAuth(
24+
requires_auth,
25+
) => {
2826
if requires_auth
2927
.requires_two_factor_auth
30-
.contains(&String::from("emailOtp"))
28+
.unwrap_or_default()
29+
.contains(&::vrchatapi::models::TwoFactorAuthType::EmailOtp)
3130
{
3231
let code = read_user_input("Please enter your Email 2fa code: ");
33-
if let Err(err) = apis::authentication_api::verify2_fa_email_code(
32+
if let Err(err) = ::vrchatapi::apis::authentication_api::verify2_fa_email_code(
3433
&config,
35-
TwoFactorEmailCode::new(code),
34+
::vrchatapi::models::TwoFactorEmailCode::new(code),
3635
)
3736
.await
3837
{
3938
eprintln!("Error verifying 2FA email code: {}", err);
4039
}
4140
} else {
4241
let code = read_user_input("Please enter your Authenticator 2fa code: ");
43-
if let Err(err) =
44-
apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code))
45-
.await
42+
if let Err(err) = ::vrchatapi::apis::authentication_api::verify2_fa(
43+
&config,
44+
::vrchatapi::models::TwoFactorAuthCode::new(code),
45+
)
46+
.await
4647
{
4748
eprintln!("Error verifying 2FA auth code: {}", err);
4849
}
4950
}
5051
}
5152
}
5253

53-
let user = apis::authentication_api::get_current_user(&config)
54+
let user = ::vrchatapi::apis::authentication_api::get_current_user(&config)
5455
.await
5556
.unwrap();
5657

5758
match user {
58-
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
59-
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
59+
::vrchatapi::models::RegisterUserAccount200Response::CurrentUser(user) => {
60+
println!("Current user: {}", user.display_name)
61+
}
62+
::vrchatapi::models::RegisterUserAccount200Response::RequiresTwoFactorAuth(_) => {
63+
println!("cookie invalid")
64+
}
6065
}
6166

6267
println!(
6368
"Cookie:{}",
6469
cookie_store
65-
.cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay"))
70+
.cookies(&url::Url::from_str("https://api.vrchat.cloud").expect("Url not okay"))
6671
.expect("Cookies not found")
6772
.to_str()
6873
.expect("Cookies not valid string")
6974
);
7075
}
7176

7277
fn read_user_input(prompt: &str) -> String {
78+
use ::std::io::Write;
7379
print!("{}", prompt);
74-
io::stdout().flush().expect("Failed to flush stdout");
80+
::std::io::stdout().flush().expect("Failed to flush stdout");
7581

7682
let mut input = String::new();
77-
io::stdin()
83+
::std::io::stdin()
7884
.read_line(&mut input)
7985
.expect("Failed to read line");
8086

examples/example.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,67 @@
1-
use std::io::{self, Write};
2-
pub use vrchatapi::apis;
3-
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};
4-
51
#[tokio::main]
62
async fn main() {
7-
let mut config = apis::configuration::Configuration::default();
3+
let mut config = ::vrchatapi::apis::configuration::Configuration::default();
84
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
95
config.user_agent = Some(String::from("ExampleProgram/0.0.1 [email protected]"));
10-
match apis::authentication_api::get_current_user(&config)
6+
match ::vrchatapi::apis::authentication_api::get_current_user(&config)
117
.await
128
.unwrap()
139
{
14-
vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
10+
::vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
1511
println!("Username: {}", me.username.unwrap())
1612
}
17-
vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => {
13+
::vrchatapi::models::RegisterUserAccount200Response::RequiresTwoFactorAuth(
14+
requires_auth,
15+
) => {
1816
if requires_auth
1917
.requires_two_factor_auth
20-
.contains(&String::from("emailOtp"))
18+
.unwrap_or_default()
19+
.contains(&::vrchatapi::models::TwoFactorAuthType::EmailOtp)
2120
{
2221
let code = read_user_input("Please enter your Email 2fa code: ");
23-
if let Err(err) = apis::authentication_api::verify2_fa_email_code(
22+
if let Err(err) = ::vrchatapi::apis::authentication_api::verify2_fa_email_code(
2423
&config,
25-
TwoFactorEmailCode::new(code),
24+
::vrchatapi::models::TwoFactorEmailCode::new(code),
2625
)
2726
.await
2827
{
2928
eprintln!("Error verifying 2FA email code: {}", err);
3029
}
3130
} else {
3231
let code = read_user_input("Please enter your Authenticator 2fa code: ");
33-
if let Err(err) =
34-
apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code))
35-
.await
32+
if let Err(err) = ::vrchatapi::apis::authentication_api::verify2_fa(
33+
&config,
34+
::vrchatapi::models::TwoFactorAuthCode::new(code),
35+
)
36+
.await
3637
{
3738
eprintln!("Error verifying 2FA auth code: {}", err);
3839
}
3940
}
4041
}
4142
}
4243

43-
let user = apis::authentication_api::get_current_user(&config)
44+
let user = ::vrchatapi::apis::authentication_api::get_current_user(&config)
4445
.await
4546
.unwrap();
4647

4748
match user {
48-
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
49-
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
49+
::vrchatapi::models::RegisterUserAccount200Response::CurrentUser(user) => {
50+
println!("Current user: {}", user.display_name)
51+
}
52+
::vrchatapi::models::RegisterUserAccount200Response::RequiresTwoFactorAuth(_) => {
53+
println!("cookie invalid")
54+
}
5055
}
5156
}
5257

5358
fn read_user_input(prompt: &str) -> String {
59+
use ::std::io::Write;
5460
print!("{}", prompt);
55-
io::stdout().flush().expect("Failed to flush stdout");
61+
::std::io::stdout().flush().expect("Failed to flush stdout");
5662

5763
let mut input = String::new();
58-
io::stdin()
64+
::std::io::stdin()
5965
.read_line(&mut input)
6066
.expect("Failed to read line");
6167

generate.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ sed -i 's/, features = \["json", "multipart"\]/, features = \["json", "cookies",
3939
#Fix example
4040
printf "\n[dev-dependencies]\ntokio = { version = '1', features = ['macros', 'rt-multi-thread'] }" >> Cargo.toml
4141

42-
# https://github.com/vrchatapi/specification/issues/241
43-
cat patches/2FA_Current_User.rs >> src/models/current_user.rs
44-
sed -i 's/pub use self::current_user::CurrentUser;/pub use self::current_user::{EitherUserOrTwoFactor, CurrentUser};/g' src/models/mod.rs
45-
sed -i 's/Result<models::CurrentUser, Error<GetCurrentUserError>>/Result<models::EitherUserOrTwoFactor, Error<GetCurrentUserError>>/g' src/apis/authentication_api.rs
42+
#Transition help from old EitherUserOrTwoFactor
43+
cat patches/2fa-reexport.rs >> src/models/mod.rs
4644

4745
# https://github.com/vrchatapi/vrchatapi-rust/pull/29
4846
sed -i "s/local_var_req_builder = local_var_req_builder.json(&\(.*\));/if let Some(\1) = \1 { \0 }/g" src/apis/files_api.rs

patches/2FA_Current_User.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

patches/2fa-reexport.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[deprecated(since= "1.20.7-nightly.28", note= "Use RegisterUserAccount200Response directly. This re-export is only provided as a transition-help!")]
2+
pub use self::RegisterUserAccount200Response as EitherUserOrTwoFactor;

0 commit comments

Comments
 (0)