Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch OpenAPI spec instead of writing custom code #17

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ docs/FilesApi.md
docs/FinishFileDataUploadRequest.md
docs/FriendStatus.md
docs/FriendsApi.md
docs/GetCurrentUser200Response.md
docs/Group.md
docs/GroupAccessType.md
docs/GroupAnnouncement.md
Expand Down Expand Up @@ -132,6 +133,7 @@ docs/TransactionSteamInfo.md
docs/TransactionSteamWalletInfo.md
docs/TwoFactorAuthCode.md
docs/TwoFactorEmailCode.md
docs/TwoFactorRequired.md
docs/UnityPackage.md
docs/UpdateAvatarRequest.md
docs/UpdateFavoriteGroupRequest.md
Expand Down Expand Up @@ -215,6 +217,7 @@ src/models/file_version.rs
src/models/file_version_upload_status.rs
src/models/finish_file_data_upload_request.rs
src/models/friend_status.rs
src/models/get_current_user_200_response.rs
src/models/group.rs
src/models/group_access_type.rs
src/models/group_announcement.rs
Expand Down Expand Up @@ -294,6 +297,7 @@ src/models/transaction_steam_info.rs
src/models/transaction_steam_wallet_info.rs
src/models/two_factor_auth_code.rs
src/models/two_factor_email_code.rs
src/models/two_factor_required.rs
src/models/unity_package.rs
src/models/update_avatar_request.rs
src/models/update_favorite_group_request.rs
Expand Down
43 changes: 43 additions & 0 deletions bin/apply_json_patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env node

import { applyPatch } from 'rfc6902'
import { parse, stringify } from 'yaml'

import fs, { cp } from 'node:fs';

const helpText = 'Usage: apply_json_patch.js <patch_file> <source_file> [output_file]'

var args = process.argv.slice(2);

if (args.find(arg => arg === '--help')) {
console.log(helpText)
process.exit(0)
}

if (args.length < 2) {
console.error('Missing arguments')
console.log(helpText)
process.exit(1)
}

const patchFile = args[0]
const sourceFile = args[1]
const outputFile = args[2] || sourceFile

try {
const patch = JSON.parse(fs.readFileSync(patchFile, 'utf8'))
const source = fs.readFileSync(sourceFile, 'utf8')

const sourceJson = parse(source)
const results = applyPatch(sourceJson, patch)

results.forEach((result, i) => {
if (result !== null) {
throw new Error('Patch failed at operation ' + i)
}
})

fs.writeFileSync(outputFile, stringify(sourceJson, { singleQuote: false, lineWidth: 0, }))
} catch (err) {
console.error(err)
}
38 changes: 38 additions & 0 deletions bin/gen_patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node

import { createPatch } from 'rfc6902'
import { parse } from 'yaml'
import fs from 'node:fs';

const helpText = 'Usage: gen_patch.js <actual_file> <patched_file> <output_file>'

var args = process.argv.slice(2);

if (args.find(arg => arg === '--help')) {
console.log(helpText)
process.exit(0)
}

if (args.length < 3) {
console.error('Missing arguments')
console.log(helpText)
process.exit(1)
}

const actualFile = args[0]
const patchedFile = args[1]
const outputFile = args[2]

try {
const actual = fs.readFileSync(actualFile, 'utf8')
const patched = fs.readFileSync(patchedFile, 'utf8')

const actualJson = parse(actual)
const patchedJson = parse(patched)

const patch = createPatch(actualJson, patchedJson)

fs.writeFileSync(outputFile, JSON.stringify(patch, null, 2))
} catch (err) {
console.error(err)
}
4 changes: 2 additions & 2 deletions docs/AuthenticationApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Name | Type | Description | Required | Notes

## get_current_user

> models::CurrentUser get_current_user()
> models::GetCurrentUser200Response get_current_user()
Login and/or Get Current User Info

This endpoint does the following two operations: 1) Checks if you are already logged in by looking for a valid `auth` cookie. If you are have a valid auth cookie then no additional auth-related actions are taken. If you are **not** logged in then it will log you in with the `Authorization` header and set the `auth` cookie. The `auth` cookie will only be sent once. 2) If logged in, this function will also return the CurrentUser object containing detailed information about the currently logged in user. The auth string after `Authorization: Basic {string}` is a base64-encoded string of the username and password, both individually url-encoded, and then joined with a colon. > base64(urlencode(username):urlencode(password)) **WARNING: Session Limit:** Each authentication with login credentials counts as a separate session, out of which you have a limited amount. Make sure to save and reuse the `auth` cookie if you are often restarting the program. The provided API libraries automatically save cookies during runtime, but does not persist during restart. While it can be fine to use username/password during development, expect in production to very fast run into the rate-limit and be temporarily blocked from making new sessions until older ones expire. The exact number of simultaneous sessions is unknown/undisclosed.
Expand All @@ -91,7 +91,7 @@ This endpoint does not need any parameter.

### Return type

[**models::CurrentUser**](CurrentUser.md)
[**models::GetCurrentUser200Response**](getCurrentUser_200_response.md)

### Authorization

Expand Down
12 changes: 12 additions & 0 deletions docs/GetCurrentUser200Response.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# GetCurrentUser200Response

## Enum Variants

| Name | Description |
|---- | -----|
| CurrentUser | |
| TwoFactorRequired | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


4 changes: 2 additions & 2 deletions docs/Group.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Name | Type | Description | Notes
**member_count_synced_at** | Option<**String**> | | [optional]
**is_verified** | Option<**bool**> | | [optional][default to false]
**join_state** | Option<[**models::GroupJoinState**](GroupJoinState.md)> | | [optional]
**tags** | Option<**Vec<String>**> | | [optional]
**tags** | Option<**Vec<String>**> | | [optional]
**transfer_target_id** | Option<**String**> | A users unique ID, usually in the form of `usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469`. Legacy players can have old IDs in the form of `8JoV9XEdpo`. The ID can never be changed. | [optional]
**galleries** | Option<[**Vec<models::GroupGallery>**](GroupGallery.md)> | | [optional]
**galleries** | Option<[**Vec<models::GroupGallery>**](GroupGallery.md)> | | [optional]
**created_at** | Option<**String**> | | [optional]
**updated_at** | Option<**String**> | | [optional]
**last_post_created_at** | Option<**String**> | | [optional]
Expand Down
11 changes: 11 additions & 0 deletions docs/TwoFactorRequired.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TwoFactorRequired

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**requires_two_factor_auth** | **Vec<String>** | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


79 changes: 71 additions & 8 deletions examples/online.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,78 @@
pub use vrchatapi::apis;
use vrchatapi::models::TwoFactorEmailCode;

#[tokio::main]
async fn main() {
let mut config = apis::configuration::Configuration::default();
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
let config = apis::configuration::Configuration {
// credentials do not belong in source code, use arguments or environment variables
basic_auth: Some((String::from("username"), Some(String::from("password")))),
..Default::default()
};

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) => println!("The Username requires Auth: {:?}", requires_auth.requires_two_factor_auth)
}
match apis::authentication_api::get_current_user(&config)
.await
.expect("Failed to get current user")
{
vrchatapi::models::GetCurrentUser200Response::CurrentUser(_user) => {
println!("Already logged in");
}
vrchatapi::models::GetCurrentUser200Response::TwoFactorRequired(two_factor_resp) => {
// The API returns you a list of methods to verify your 2FA, this determines what function to call next
// There are only 2 ways the array of available methods looks, either ['emailOpt'] or ['otp', 'totp']
// One deals with email verification, the other two with either TOTP or a recovery code generated from the VRChat website

let online = apis::system_api::get_current_online_users(&config).await.unwrap();
let code = "123456".to_owned(); // grab this via stdin / generate it / read it from email server

let verified: bool;
if two_factor_resp
.requires_two_factor_auth
.contains(&vrchatapi::models::two_factor_required::RequiresTwoFactorAuth::EmailOtp)
{
let resp = apis::authentication_api::verify2_fa_email_code(
&config,
TwoFactorEmailCode::new(code.clone()),
)
.await
.expect("Failed toemail 2FA response");

verified = resp.verified;
} else {
let resp = apis::authentication_api::verify2_fa(
&config,
vrchatapi::models::TwoFactorAuthCode { code },
)
.await
.expect("Failed to get totp 2FA response");

// If you have a recovery code, use this method instead
// let resp = apis::authentication_api::verify_recovery_code(
// &config,
// vrchatapi::models::two_factor_auth_code::TwoFactorAuthCode { code },
// )
// .await
// .expect("Failed to get recovery code 2FA response");

verified = resp.verified;
}

if !verified {
panic!("Failed to verify 2FA");
}
0xkubectl marked this conversation as resolved.
Show resolved Hide resolved
}
};

let current_user = match apis::authentication_api::get_current_user(&config)
.await
.expect("Failed to get current user")
{
vrchatapi::models::GetCurrentUser200Response::CurrentUser(user) => user,
_ => panic!("Got 2FA response, even after verifying 2FA"),
};

println!("Current User: {:?}", current_user.display_name);

let online = apis::system_api::get_current_online_users(&config)
.await
.expect("Failed to get online users");
println!("Current Online Users: {}", online);
}
}
28 changes: 19 additions & 9 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#!/usr/bin/env bash
# This script assumes that all npm dependencies are installed (e.g. by a previous step in CI)

# Generate Client
set -eux -o pipefail

# clean up old files
rm src/apis src/models docs -rf

# Download OpenAPI spec
wget https://raw.githubusercontent.com/vrchatapi/specification/gh-pages/openapi.yaml -O openapi.yaml

# patch openapi.yaml
for p in patches/*.json; do
node bin/apply_json_patch.js $p openapi.yaml
done

# Generate client
./node_modules/\@openapitools/openapi-generator-cli/main.js generate \
-g rust \
--additional-properties=packageName=vrchatapi,supportAsync=true \
--git-user-id=vrchatapi \
--git-repo-id=vrchatapi-rust \
-o . \
-i https://raw.githubusercontent.com/vrchatapi/specification/gh-pages/openapi.yaml \
-i openapi.yaml \
--http-user-agent="vrchatapi-rust"
#--global-property debugOperations=true

# Remove openapi.yaml
rm openapi.yaml

# Update entire description (replace entire line, match the random data there) line in Cargo.toml
sed -i 's/^description = ".*"/description = "VRChat API Client for Rust"/' Cargo.toml
Expand All @@ -27,13 +41,9 @@ find src -type f -exec sed -i '/^\s*\/\/\/\s*$/d' {} \;
sed -i 's/Client::new()/Client::builder().cookie_store(true).build().unwrap()/g' src/apis/configuration.rs
sed -i 's/features = \["json", "multipart"\]/features = \["json", "cookies", "multipart"\]/g' Cargo.toml

#Fix example
# Fix example
printf "\n[dev-dependencies]\ntokio = { version = '1', features = ['macros', 'rt-multi-thread'] }" >> Cargo.toml

# https://github.com/vrchatapi/specification/issues/241
cat patches/2FA_Current_User.rs >> src/models/current_user.rs
sed -i 's/pub use self::current_user::CurrentUser;/pub use self::current_user::{EitherUserOrTwoFactor, CurrentUser};/g' src/models/mod.rs
sed -i 's/Result<models::CurrentUser, Error<GetCurrentUserError>>/Result<models::EitherUserOrTwoFactor, Error<GetCurrentUserError>>/g' src/apis/authentication_api.rs

# Format and test
cargo build
cargo test
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"type": "module",
"dependencies": {
"@openapitools/openapi-generator-cli": "^2.13.4"
"@openapitools/openapi-generator-cli": "^2.13.4",
"rfc6902": "^5.1.1",
"yaml": "^2.5.0"
}
}
36 changes: 36 additions & 0 deletions patches/0001_fix_2fa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"op": "add",
"path": "/components/schemas/TwoFactorRequired",
"value": {
"title": "TwoFactorRequired",
"type": "object",
"properties": {
"requiresTwoFactorAuth": {
"type": "array",
"items": {
"type": "string",
"enum": ["totp", "otp", "emailOtp"]
}
}
},
"required": ["requiresTwoFactorAuth"]
}
},
{
"op": "remove",
"path": "/components/responses/CurrentUserLoginResponse/content/application~1json/schema/$ref"
},
{
"op": "add",
"path": "/components/responses/CurrentUserLoginResponse/content/application~1json/schema/oneOf",
"value": [
{
"$ref": "#/components/schemas/CurrentUser"
},
{
"$ref": "#/components/schemas/TwoFactorRequired"
}
]
}
]
12 changes: 0 additions & 12 deletions patches/2FA_Current_User.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/apis/authentication_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub async fn delete_user(configuration: &configuration::Configuration, user_id:
}

/// This endpoint does the following two operations: 1) Checks if you are already logged in by looking for a valid `auth` cookie. If you are have a valid auth cookie then no additional auth-related actions are taken. If you are **not** logged in then it will log you in with the `Authorization` header and set the `auth` cookie. The `auth` cookie will only be sent once. 2) If logged in, this function will also return the CurrentUser object containing detailed information about the currently logged in user. The auth string after `Authorization: Basic {string}` is a base64-encoded string of the username and password, both individually url-encoded, and then joined with a colon. > base64(urlencode(username):urlencode(password)) **WARNING: Session Limit:** Each authentication with login credentials counts as a separate session, out of which you have a limited amount. Make sure to save and reuse the `auth` cookie if you are often restarting the program. The provided API libraries automatically save cookies during runtime, but does not persist during restart. While it can be fine to use username/password during development, expect in production to very fast run into the rate-limit and be temporarily blocked from making new sessions until older ones expire. The exact number of simultaneous sessions is unknown/undisclosed.
pub async fn get_current_user(configuration: &configuration::Configuration, ) -> Result<models::EitherUserOrTwoFactor, Error<GetCurrentUserError>> {
pub async fn get_current_user(configuration: &configuration::Configuration, ) -> Result<models::GetCurrentUser200Response, Error<GetCurrentUserError>> {
let local_var_configuration = configuration;

let local_var_client = &local_var_configuration.client;
Expand Down
12 changes: 0 additions & 12 deletions src/models/current_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,3 @@ impl CurrentUser {
}
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum EitherUserOrTwoFactor{
CurrentUser(CurrentUser),
RequiresTwoFactorAuth(RequiresTwoFactorAuth),
}

#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct RequiresTwoFactorAuth{
#[serde(rename = "requiresTwoFactorAuth")]
pub requires_two_factor_auth: Vec<String>
}
Loading