SecureValue: simplify json encoding
GitOrigin-RevId: 5b32553969dc17457329337e1fcadd26002712ee
This commit is contained in:
parent
963b6c060a
commit
7149f65461
@ -588,40 +588,26 @@ EncryptedSecureValue encrypt_secure_value(FileManager *file_manager, const secur
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonScope &to_json(JsonValueScope &scope, const SecureDataCredentials &credentials) {
|
auto as_jsonable(const SecureDataCredentials &credentials) {
|
||||||
auto object = scope.enter_object();
|
return json_object([&credentials](auto &o) {
|
||||||
object << ctie("data_hash", base64_encode(credentials.hash));
|
o("data_hash", base64_encode(credentials.hash));
|
||||||
object << ctie("secret", base64_encode(credentials.secret));
|
o("secret", base64_encode(credentials.secret));
|
||||||
return scope;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonScope &to_json(JsonValueScope &scope, const SecureFileCredentials &credentials) {
|
auto as_jsonable(const SecureFileCredentials &credentials) {
|
||||||
auto object = scope.enter_object();
|
return json_object([&credentials](auto &o) {
|
||||||
object << ctie("file_hash", base64_encode(credentials.hash));
|
o("file_hash", base64_encode(credentials.hash));
|
||||||
object << ctie("secret", base64_encode(credentials.secret));
|
o("secret", base64_encode(credentials.secret));
|
||||||
return scope;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonScope &to_json(JsonValueScope &scope, const vector<SecureFileCredentials> &files) {
|
auto as_jsonable(const vector<SecureFileCredentials> &files) {
|
||||||
auto arr = scope.enter_array();
|
return json_array([&files](auto &arr) {
|
||||||
for (auto &file : files) {
|
for (auto &file : files) {
|
||||||
arr << ToJson(file);
|
arr(as_jsonable(file));
|
||||||
}
|
}
|
||||||
return scope;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
JsonScope &to_json(JsonValueScope &scope, const SecureValueCredentials &credentials) {
|
|
||||||
auto object = scope.enter_object();
|
|
||||||
if (credentials.data) {
|
|
||||||
object << ctie("data", ToJson(credentials.data.value()));
|
|
||||||
}
|
|
||||||
if (!credentials.files.empty()) {
|
|
||||||
object << ctie("files", ToJson(credentials.files));
|
|
||||||
}
|
|
||||||
if (credentials.selfie) {
|
|
||||||
object << ctie("selfie", ToJson(credentials.selfie.value()));
|
|
||||||
}
|
|
||||||
return scope;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Slice secure_value_type_as_slice(SecureValueType type) {
|
Slice secure_value_type_as_slice(SecureValueType type) {
|
||||||
@ -653,25 +639,30 @@ Slice secure_value_type_as_slice(SecureValueType type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonScope &to_json(JsonValueScope &scope, const std::vector<SecureValueCredentials> &credentials) {
|
auto credentials_as_jsonable(std::vector<SecureValueCredentials> &credentials, Slice payload) {
|
||||||
auto object = scope.enter_object();
|
return json_object([&credentials, &payload](auto &o) {
|
||||||
|
o("secure_data", json_object([&credentials](auto &o) {
|
||||||
for (auto &c : credentials) {
|
for (auto &c : credentials) {
|
||||||
object << ctie(secure_value_type_as_slice(c.type), ToJson(c));
|
o(secure_value_type_as_slice(c.type), json_object([&credentials = c](auto &o) {
|
||||||
|
if (credentials.data) {
|
||||||
|
o("data", as_jsonable(credentials.data.value()));
|
||||||
}
|
}
|
||||||
return scope;
|
if (!credentials.files.empty()) {
|
||||||
}
|
o("files", as_jsonable(credentials.files));
|
||||||
|
}
|
||||||
JsonScope &to_json(JsonValueScope &scope,
|
if (credentials.selfie) {
|
||||||
const std::tuple<const std::vector<SecureValueCredentials> &, const Slice &> &credentials) {
|
o("selfie", as_jsonable(credentials.selfie.value()));
|
||||||
auto object = scope.enter_object();
|
}
|
||||||
object << ctie("secure_data", ToJson(std::get<0>(credentials)));
|
}));
|
||||||
object << ctie("payload", std::get<1>(credentials));
|
}
|
||||||
return scope;
|
}));
|
||||||
|
o("payload", payload);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<EncryptedSecureCredentials> encrypted_credentials(std::vector<SecureValueCredentials> &credentials,
|
Result<EncryptedSecureCredentials> encrypted_credentials(std::vector<SecureValueCredentials> &credentials,
|
||||||
Slice payload, Slice public_key) {
|
Slice payload, Slice public_key) {
|
||||||
auto encoded_credentials = json_encode<std::string>(ToJson(ctie(credentials, payload)));
|
auto encoded_credentials = json_encode<std::string>(credentials_as_jsonable(credentials, payload));
|
||||||
|
|
||||||
auto secret = secure_storage::Secret::create_new();
|
auto secret = secure_storage::Secret::create_new();
|
||||||
auto encrypted_value = secure_storage::encrypt_value(secret, encoded_credentials).move_as_ok();
|
auto encrypted_value = secure_storage::encrypt_value(secret, encoded_credentials).move_as_ok();
|
||||||
|
Reference in New Issue
Block a user