json schema generation

This commit is contained in:
John Smith
2023-06-04 21:22:55 -04:00
parent 6a86f2265a
commit 06081df22a
23 changed files with 552 additions and 154 deletions

View File

@@ -18,49 +18,210 @@ pub struct CryptoSystemResponse {
#[serde(tag = "cs_op")]
pub enum CryptoSystemRequestOp {
Release,
CachedDh,
ComputeDh,
RandomBytes,
CachedDh {
#[schemars(with = "String")]
key: PublicKey,
#[schemars(with = "String")]
secret: SecretKey,
},
ComputeDh {
#[schemars(with = "String")]
key: PublicKey,
#[schemars(with = "String")]
secret: SecretKey,
},
RandomBytes {
len: u32,
},
DefaultSaltLength,
HashPassword,
VerifyPassword,
DeriveSharedSecret,
HashPassword {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
password: Vec<u8>,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
salt: Vec<u8>,
},
VerifyPassword {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
password: Vec<u8>,
password_hash: String,
},
DeriveSharedSecret {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
password: Vec<u8>,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
salt: Vec<u8>,
},
RandomNonce,
RandomSharedSecret,
GenerateKeyPair,
GenerateHash,
ValidateKeyPair,
ValidateHash,
Distance,
Sign,
Verify,
GenerateHash {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
data: Vec<u8>,
},
ValidateKeyPair {
#[schemars(with = "String")]
key: PublicKey,
#[schemars(with = "String")]
secret: SecretKey,
},
ValidateHash {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
data: Vec<u8>,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
hash_digest: Vec<u8>,
},
Distance {
#[schemars(with = "String")]
key1: CryptoKey,
#[schemars(with = "String")]
key2: CryptoKey,
},
Sign {
#[schemars(with = "String")]
key: PublicKey,
#[schemars(with = "String")]
secret: SecretKey,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
data: Vec<u8>,
},
Verify {
#[schemars(with = "String")]
key: PublicKey,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
data: Vec<u8>,
#[schemars(with = "String")]
secret: Signature,
},
AeadOverhead,
DecryptAead,
EncryptAead,
CryptNoAuth,
DecryptAead {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
body: Vec<u8>,
#[schemars(with = "String")]
nonce: Nonce,
#[schemars(with = "String")]
shared_secret: SharedSecret,
#[serde(with = "opt_json_as_base64")]
#[schemars(with = "Option<String>")]
associated_data: Option<Vec<u8>>,
},
EncryptAead {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
body: Vec<u8>,
#[schemars(with = "String")]
nonce: Nonce,
#[schemars(with = "String")]
shared_secret: SharedSecret,
#[serde(with = "opt_json_as_base64")]
#[schemars(with = "Option<String>")]
associated_data: Option<Vec<u8>>,
},
CryptNoAuth {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
body: Vec<u8>,
#[schemars(with = "String")]
nonce: Nonce,
#[schemars(with = "String")]
shared_secret: SharedSecret,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "cs_op")]
pub enum CryptoSystemResponseOp {
Release,
CachedDh,
ComputeDh,
RandomBytes,
DefaultSaltLength,
HashPassword,
VerifyPassword,
DeriveSharedSecret,
RandomNonce,
RandomSharedSecret,
GenerateKeyPair,
GenerateHash,
ValidateKeyPair,
ValidateHash,
Distance,
Sign,
Verify,
AeadOverhead,
DecryptAead,
EncryptAead,
CryptNoAuth,
CachedDh {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithString<SharedSecret>,
},
ComputeDh {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithString<SharedSecret>,
},
RandomBytes {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithVecU8,
},
DefaultSaltLength {
value: u32,
},
HashPassword {
#[serde(flatten)]
result: ApiResult<String>,
},
VerifyPassword {
#[serde(flatten)]
result: ApiResult<bool>,
},
DeriveSharedSecret {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithString<SharedSecret>,
},
RandomNonce {
#[schemars(with = "String")]
value: Nonce,
},
RandomSharedSecret {
#[schemars(with = "String")]
value: SharedSecret,
},
GenerateKeyPair {
#[schemars(with = "String")]
value: KeyPair,
},
GenerateHash {
#[schemars(with = "String")]
value: HashDigest,
},
ValidateKeyPair {
value: bool,
},
ValidateHash {
value: bool,
},
Distance {
#[schemars(with = "String")]
value: CryptoKeyDistance,
},
Sign {
#[schemars(with = "String")]
value: Signature,
},
Verify {
#[serde(flatten)]
result: ApiResult<()>,
},
AeadOverhead {
value: u32,
},
DecryptAead {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithVecU8,
},
EncryptAead {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithVecU8,
},
CryptNoAuth {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithVecU8,
},
}

View File

@@ -11,7 +11,8 @@ pub use crypto_system::*;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Request {
/// Operation Id (pairs with Response)
/// Operation Id (pairs with Response, or empty if unidirectional)
#[serde(default)]
id: String,
/// The request operation variant
#[serde(flatten)]
@@ -20,7 +21,8 @@ pub struct Request {
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Response {
/// Operation Id (pairs with Request)
/// Operation Id (pairs with Request, or empty if unidirectional)
#[serde(default)]
id: String,
/// The response operation variant
#[serde(flatten)]
@@ -36,17 +38,14 @@ pub enum RequestOp {
NewPrivateRoute,
NewCustomPrivateRoute {
#[schemars(with = "Vec<String>")]
crypto_kinds: Vec<CryptoKind>,
kinds: Vec<CryptoKind>,
#[serde(default)]
stability: Stability,
#[serde(default)]
sequencing: Sequencing,
},
ImportRemotePrivateRoute {
#[serde(
serialize_with = "json_as_base64::serialize",
deserialize_with = "json_as_base64::deserialize"
)]
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
blob: Vec<u8>,
},
@@ -57,10 +56,7 @@ pub enum RequestOp {
AppCallReply {
#[schemars(with = "String")]
call_id: OperationId,
#[serde(
serialize_with = "json_as_base64::serialize",
deserialize_with = "json_as_base64::deserialize"
)]
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
message: Vec<u8>,
},
@@ -79,27 +75,21 @@ pub enum RequestOp {
// Crypto
GetCryptoSystem {
#[schemars(with = "String")]
crypto_kind: CryptoKind,
kind: CryptoKind,
},
BestCryptoSystem,
CryptoSystem(CryptoSystemRequest),
VerifySignatures {
#[schemars(with = "Vec<String>")]
node_ids: Vec<TypedKey>,
#[serde(
serialize_with = "json_as_base64::serialize",
deserialize_with = "json_as_base64::deserialize"
)]
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
data: Vec<u8>,
#[schemars(with = "Vec<String>")]
signatures: Vec<TypedSignature>,
},
GenerateSignatures {
#[serde(
serialize_with = "json_as_base64::serialize",
deserialize_with = "json_as_base64::deserialize"
)]
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
data: Vec<u8>,
#[schemars(with = "Vec<String>")]
@@ -107,7 +97,7 @@ pub enum RequestOp {
},
GenerateKeyPair {
#[schemars(with = "String")]
crypto_kind: CryptoKind,
kind: CryptoKind,
},
// Misc
Now,
@@ -122,10 +112,7 @@ pub enum RequestOp {
pub struct NewPrivateRouteResult {
#[schemars(with = "String")]
route_id: RouteId,
#[serde(
serialize_with = "json_as_base64::serialize",
deserialize_with = "json_as_base64::deserialize"
)]
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
blob: Vec<u8>,
}
@@ -133,6 +120,9 @@ pub struct NewPrivateRouteResult {
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "op")]
pub enum ResponseOp {
Update {
value: VeilidUpdate,
},
GetState {
#[serde(flatten)]
result: ApiResult<VeilidState>,
@@ -153,28 +143,74 @@ pub enum ResponseOp {
#[serde(flatten)]
result: ApiResult<NewPrivateRouteResult>,
},
ImportRemotePrivateRoute,
ReleasePrivateRoute,
AppCallReply,
ImportRemotePrivateRoute {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithString<RouteId>,
},
ReleasePrivateRoute {
#[serde(flatten)]
result: ApiResult<()>,
},
AppCallReply {
#[serde(flatten)]
result: ApiResult<()>,
},
// Routing Context
NewRoutingContext,
NewRoutingContext {
value: String,
},
RoutingContext(RoutingContextResponse),
// TableDb
OpenTableDb,
DeleteTableDb,
OpenTableDb {
#[serde(flatten)]
result: ApiResult<String>,
},
DeleteTableDb {
#[serde(flatten)]
result: ApiResult<bool>,
},
TableDb(TableDbResponse),
// Crypto
GetCryptoSystem,
BestCryptoSystem,
GetCryptoSystem {
#[serde(flatten)]
result: ApiResult<String>,
},
BestCryptoSystem {
value: String,
},
CryptoSystem(CryptoSystemResponse),
VerifySignatures,
GenerateSignatures,
GenerateKeyPair,
VerifySignatures {
#[serde(flatten)]
#[schemars(with = "ApiResult<Vec<String>>")]
result: ApiResultWithVecString<TypedKeySet>,
},
GenerateSignatures {
#[serde(flatten)]
#[schemars(with = "ApiResult<Vec<String>>")]
result: ApiResultWithVecString<TypedSignatureSet>,
},
GenerateKeyPair {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithString<TypedKeyPair>,
},
// Misc
Now,
Debug,
VeilidVersionString,
VeilidVersion,
Now {
#[schemars(with = "String")]
value: Timestamp,
},
Debug {
value: String,
},
VeilidVersionString {
value: String,
},
VeilidVersion {
major: u32,
minor: u32,
patch: u32,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
@@ -187,6 +223,49 @@ where
Err { error: VeilidAPIError },
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum ApiResultWithString<T>
where
T: Clone + fmt::Debug,
{
Ok {
#[schemars(with = "String")]
value: T,
},
Err {
error: VeilidAPIError,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum ApiResultWithVecU8 {
Ok {
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
value: Vec<u8>,
},
Err {
error: VeilidAPIError,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum ApiResultWithVecString<T>
where
T: Clone + fmt::Debug,
{
Ok {
#[schemars(with = "Vec<String>")]
value: T,
},
Err {
error: VeilidAPIError,
},
}
pub fn emit_schemas(out: &mut HashMap<String, String>) {
let schema_request = schema_for!(Request);
let schema_response = schema_for!(Response);

View File

@@ -19,34 +19,122 @@ pub struct RoutingContextResponse {
pub enum RoutingContextRequestOp {
Release,
WithPrivacy,
WithCustomPrivacy,
WithSequencing,
AppCall,
AppMessage,
CreateDhtRecord,
OpenDhtRecord,
CloseDhtRecord,
DeleteDhtRecord,
GetDhtValue,
SetDhtValue,
WatchDhtValues,
CancelDhtWatch,
WithCustomPrivacy {
stability: Stability,
},
WithSequencing {
sequencing: Sequencing,
},
AppCall {
target: String,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
request: Vec<u8>,
},
AppMessage {
target: String,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
message: Vec<u8>,
},
CreateDhtRecord {
#[schemars(with = "String")]
kind: CryptoKind,
schema: DHTSchema,
},
OpenDhtRecord {
#[schemars(with = "String")]
key: TypedKey,
#[schemars(with = "Option<String>")]
writer: Option<KeyPair>,
},
CloseDhtRecord {
#[schemars(with = "String")]
key: TypedKey,
},
DeleteDhtRecord {
#[schemars(with = "String")]
key: TypedKey,
},
GetDhtValue {
#[schemars(with = "String")]
key: TypedKey,
subkey: ValueSubkey,
force_refresh: bool,
},
SetDhtValue {
#[schemars(with = "String")]
key: TypedKey,
subkey: ValueSubkey,
#[serde(with = "json_as_base64")]
#[schemars(with = "String")]
data: Vec<u8>,
},
WatchDhtValues {
#[schemars(with = "String")]
key: TypedKey,
subkeys: ValueSubkeyRangeSet,
expiration: Timestamp,
count: u32,
},
CancelDhtWatch {
#[schemars(with = "String")]
key: TypedKey,
subkeys: ValueSubkeyRangeSet,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "rc_op")]
pub enum RoutingContextResponseOp {
Release,
WithPrivacy,
WithCustomPrivacy,
WithSequencing,
AppCall,
AppMessage,
CreateDhtRecord,
OpenDhtRecord,
CloseDhtRecord,
DeleteDhtRecord,
GetDhtValue,
SetDhtValue,
WatchDhtValues,
CancelDhtWatch,
WithPrivacy {
value: String,
},
WithCustomPrivacy {
value: String,
},
WithSequencing {
value: String,
},
AppCall {
#[serde(flatten)]
#[schemars(with = "ApiResult<String>")]
result: ApiResultWithVecU8,
},
AppMessage {
#[serde(flatten)]
result: ApiResult<()>,
},
CreateDhtRecord {
#[serde(flatten)]
result: ApiResult<DHTRecordDescriptor>,
},
OpenDhtRecord {
#[serde(flatten)]
result: ApiResult<DHTRecordDescriptor>,
},
CloseDhtRecord {
#[serde(flatten)]
result: ApiResult<()>,
},
DeleteDhtRecord {
#[serde(flatten)]
result: ApiResult<()>,
},
GetDhtValue {
#[serde(flatten)]
result: ApiResult<Option<ValueData>>,
},
SetDhtValue {
#[serde(flatten)]
result: ApiResult<Option<ValueData>>,
},
WatchDhtValues {
#[serde(flatten)]
result: ApiResult<Timestamp>,
},
CancelDhtWatch {
#[serde(flatten)]
result: ApiResult<bool>,
},
}