| 1 | #include "mtx/common.hpp" |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | using json = nlohmann::json; |
| 6 | |
| 7 | namespace mtx { |
| 8 | namespace crypto { |
| 9 | void |
| 10 | from_json(const json &obj, UnsignedDeviceInfo &res) |
| 11 | { |
| 12 | if (obj.find(key: "device_display_name" ) != obj.end()) |
| 13 | res.device_display_name = obj.at(key: "device_display_name" ).get<std::string>(); |
| 14 | } |
| 15 | |
| 16 | void |
| 17 | to_json(json &obj, const UnsignedDeviceInfo &res) |
| 18 | { |
| 19 | if (!res.device_display_name.empty()) |
| 20 | obj["device_display_name" ] = res.device_display_name; |
| 21 | } |
| 22 | |
| 23 | void |
| 24 | from_json(const json &obj, DeviceKeys &res) |
| 25 | { |
| 26 | res.user_id = obj.at(key: "user_id" ).get<std::string>(); |
| 27 | res.device_id = obj.at(key: "device_id" ).get<std::string>(); |
| 28 | res.algorithms = obj.at(key: "algorithms" ).get<std::vector<std::string>>(); |
| 29 | |
| 30 | res.keys = obj.at(key: "keys" ).get<std::map<AlgorithmDevice, std::string>>(); |
| 31 | |
| 32 | if (obj.contains(key: "signatures" )) |
| 33 | res.signatures = |
| 34 | obj.at(key: "signatures" ).get<std::map<std::string, std::map<AlgorithmDevice, std::string>>>(); |
| 35 | |
| 36 | if (obj.find(key: "unsigned" ) != obj.end()) |
| 37 | res.unsigned_info = obj.at(key: "unsigned" ).get<UnsignedDeviceInfo>(); |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | to_json(json &obj, const DeviceKeys &res) |
| 42 | { |
| 43 | obj["user_id" ] = res.user_id; |
| 44 | obj["device_id" ] = res.device_id; |
| 45 | obj["algorithms" ] = res.algorithms; |
| 46 | obj["keys" ] = res.keys; |
| 47 | obj["signatures" ] = res.signatures; |
| 48 | |
| 49 | if (!res.unsigned_info.device_display_name.empty()) |
| 50 | obj["unsigned" ] = res.unsigned_info; |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | from_json(const json &obj, CrossSigningKeys &res) |
| 55 | { |
| 56 | res.user_id = obj.at(key: "user_id" ).get<std::string>(); |
| 57 | res.usage = obj.at(key: "usage" ).get<std::vector<std::string>>(); |
| 58 | res.keys = obj.at(key: "keys" ).get<std::map<std::string, std::string>>(); |
| 59 | |
| 60 | if (obj.contains(key: "signatures" )) |
| 61 | res.signatures = |
| 62 | obj.at(key: "signatures" ).get<std::map<std::string, std::map<std::string, std::string>>>(); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | to_json(json &obj, const CrossSigningKeys &res) |
| 67 | { |
| 68 | obj["user_id" ] = res.user_id; |
| 69 | obj["usage" ] = res.usage; |
| 70 | obj["keys" ] = res.keys; |
| 71 | obj["signatures" ] = res.signatures; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | from_json(const json &obj, JWK &res) |
| 76 | { |
| 77 | res.kty = obj.at(key: "kty" ).get<std::string>(); |
| 78 | res.key_ops = obj.at(key: "key_ops" ).get<std::vector<std::string>>(); |
| 79 | res.alg = obj.at(key: "alg" ).get<std::string>(); |
| 80 | res.k = obj.at(key: "k" ).get<std::string>(); |
| 81 | res.ext = obj.at(key: "ext" ).get<bool>(); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | to_json(json &obj, const JWK &res) |
| 86 | { |
| 87 | obj["kty" ] = res.kty; |
| 88 | obj["key_ops" ] = res.key_ops; |
| 89 | obj["alg" ] = res.alg; |
| 90 | obj["k" ] = res.k; |
| 91 | obj["ext" ] = res.ext; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | from_json(const json &obj, EncryptedFile &res) |
| 96 | { |
| 97 | res.url = obj.at(key: "url" ).get<std::string>(); |
| 98 | res.key = obj.at(key: "key" ).get<JWK>(); |
| 99 | res.iv = obj.at(key: "iv" ).get<std::string>(); |
| 100 | res.hashes = obj.at(key: "hashes" ).get<std::map<std::string, std::string>>(); |
| 101 | res.v = obj.at(key: "v" ).get<std::string>(); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | to_json(json &obj, const EncryptedFile &res) |
| 106 | { |
| 107 | obj["url" ] = res.url; |
| 108 | obj["key" ] = res.key; |
| 109 | obj["iv" ] = res.iv; |
| 110 | obj["hashes" ] = res.hashes; |
| 111 | obj["v" ] = res.v; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |