diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index f217de458..01b779e09 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -8,6 +8,9 @@ endif() add_executable(print-claims print-claims.cpp) target_link_libraries(print-claims jwt-cpp::jwt-cpp) +add_executable(private-claims private-claims.cpp) +target_link_libraries(private-claims jwt-cpp::jwt-cpp) + add_executable(rsa-create rsa-create.cpp) target_link_libraries(rsa-create jwt-cpp::jwt-cpp) diff --git a/example/private-claims.cpp b/example/private-claims.cpp new file mode 100644 index 000000000..b1b3253b4 --- /dev/null +++ b/example/private-claims.cpp @@ -0,0 +1,152 @@ +#include +#include + +#include +#include + +using sec = std::chrono::seconds; +using min = std::chrono::minutes; + +std::string make_pico_token() { + jwt::claim from_raw_json; + std::istringstream iss{R"##({"api":{"array":[1,2,3],"null":null}})##"}; + iss >> from_raw_json; + + jwt::claim::set_t list{"once", "twice"}; + + std::vector big_numbers{727663072ull, 770979831ull, 427239169ull, + 525936436ull}; + + const auto time = jwt::date::clock::now(); + + return jwt::create() + .set_type("JWT") + .set_issuer("auth.mydomain.io") + .set_audience("mydomain.io") + .set_issued_at(time) + .set_not_before(time + sec{15}) + .set_expires_at(time + sec{15} + min{2}) + .set_payload_claim("boolean", picojson::value(true)) + .set_payload_claim("integer", picojson::value(int64_t{12345})) + .set_payload_claim("precision", picojson::value(12.345)) + .set_payload_claim("strings", jwt::claim(list)) + .set_payload_claim("array", + jwt::claim(big_numbers.begin(), big_numbers.end())) + .set_payload_claim("object", from_raw_json) + .sign(jwt::algorithm::none{}); +} + +std::string make_nlohmann_token() { + struct nlohmann_traits { + using json = nlohmann::json; + using value_type = json; + using object_type = json::object_t; + using array_type = json::array_t; + using string_type = std::string; + using number_type = json::number_float_t; + using integer_type = json::number_integer_t; + using boolean_type = json::boolean_t; + + static jwt::json::type get_type(const json &val) { + using jwt::json::type; + + if (val.type() == json::value_t::boolean) + return type::boolean; + else if (val.type() == json::value_t::number_integer) + return type::integer; + else if (val.type() == json::value_t::number_float) + return type::number; + else if (val.type() == json::value_t::string) + return type::string; + else if (val.type() == json::value_t::array) + return type::array; + else if (val.type() == json::value_t::object) + return type::object; + else + throw std::logic_error("invalid type"); + } + + static json::object_t as_object(const json &val) { + if (val.type() != json::value_t::object) + throw std::bad_cast(); + return val.get(); + } + + static std::string as_string(const json &val) { + if (val.type() != json::value_t::string) + throw std::bad_cast(); + return val.get(); + } + + static json::array_t as_array(const json &val) { + if (val.type() != json::value_t::array) + throw std::bad_cast(); + return val.get(); + } + + static int64_t as_int(const json &val) { + if (val.type() != json::value_t::number_integer) + throw std::bad_cast(); + return val.get(); + } + + static bool as_bool(const json &val) { + if (val.type() != json::value_t::boolean) + throw std::bad_cast(); + return val.get(); + } + + static double as_number(const json &val) { + if (val.type() != json::value_t::number_float) + throw std::bad_cast(); + return val.get(); + } + + static bool parse(json &val, std::string str) { + val = json::parse(str.begin(), str.end()); + return true; + } + + static std::string serialize(const json &val) { return val.dump(); } + }; + + using claim = jwt::basic_claim; + + claim from_raw_json; + std::istringstream iss{R"##({"api":{"array":[1,2,3],"null":null}})##"}; + iss >> from_raw_json; + + claim::set_t list{"once", "twice"}; + + std::vector big_numbers{727663072ull, 770979831ull, 427239169ull, + 525936436ull}; + + const auto time = jwt::date::clock::now(); + + return jwt::create() + .set_type("JWT") + .set_issuer("auth.mydomain.io") + .set_audience("mydomain.io") + .set_issued_at(time) + .set_not_before(time + sec{15}) + .set_expires_at(time + sec{15} + min{2}) + .set_payload_claim("boolean", true) + .set_payload_claim("integer", 12345) + .set_payload_claim("precision", 12.345) + .set_payload_claim("strings", list) + .set_payload_claim("array", {big_numbers.begin(), big_numbers.end()}) + .set_payload_claim("object", from_raw_json) + .sign(jwt::algorithm::none{}); +} + +int main(int argc, const char **argv) { + const auto token = make_pico_token(); + auto decoded = jwt::decode(token); + + for (auto &e : decoded.get_payload_claims()) + std::cout << e.first << " = " << e.second << std::endl; + + const auto api_array = + decoded.get_payload_claims()["object"].to_json().get("api").get("array"); + std::cout << "api array = " << api_array << std::endl; +} \ No newline at end of file