mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
🪞 Add support for reflect-cpp's JSON (#421)
* add reflect json traits * jwt-cpp defaults * fix clang tidy warnings * fix clang tidy warnings * rename directory * add tests * add tests * add tests * replace unnecessary copy with move * gate reflect cpp tests behind flag * reflect cpp CI work * reflect-cpp example * reflect-cpp CI * reflect-cpp CI * match naming + update ci * first pass code review * linter * linter * align naming * make file +x * align target name fix order or targets in tests * fix typo * Fix target_link_libraries syntax for jwt-cpp * cleanup example code * add reflect-json to coverage tests --------- Co-authored-by: Ali Girayhan Ozbay <aligirayhan.ozbay@abex.capital> Co-authored-by: Ali Girayhan Özbay <aligirayhan@ozbay.org>
This commit is contained in:
56
example/traits/reflectcpp-json.cpp
Normal file
56
example/traits/reflectcpp-json.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "jwt-cpp/jwt.h"
|
||||
#include "jwt-cpp/traits/reflectcpp-json/traits.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
using sec = std::chrono::seconds;
|
||||
using min = std::chrono::minutes;
|
||||
using traits = jwt::traits::reflectcpp_json;
|
||||
using claim = jwt::basic_claim<traits>;
|
||||
|
||||
traits::value_type raw_value;
|
||||
traits::parse(raw_value, R"##({"api":{"array":[1,2,3],"null":null}})##");
|
||||
claim from_raw_json(raw_value);
|
||||
|
||||
claim::set_t list{"once", "twice"};
|
||||
std::vector<int64_t> big_numbers{727663072LL, 770979831LL, 427239169LL, 525936436LL};
|
||||
|
||||
const auto time = jwt::date::clock::now();
|
||||
const auto token = jwt::create<traits>()
|
||||
.set_type("JWT")
|
||||
.set_issuer("auth.mydomain.io")
|
||||
.set_audience("mydomain.io")
|
||||
.set_issued_at(time)
|
||||
.set_not_before(time)
|
||||
.set_expires_at(time + min{2} + sec{15})
|
||||
.set_payload_claim("boolean", true)
|
||||
.set_payload_claim("integer", 12345)
|
||||
.set_payload_claim("precision", 12.3456789)
|
||||
.set_payload_claim("strings", claim(list))
|
||||
.set_payload_claim("array", claim{big_numbers.begin(), big_numbers.end()})
|
||||
.set_payload_claim("object", from_raw_json)
|
||||
.sign(jwt::algorithm::none{});
|
||||
|
||||
const auto decoded = jwt::decode<traits>(token);
|
||||
|
||||
const auto array = decoded.get_payload_claim("object")
|
||||
.to_json()
|
||||
.to_object()
|
||||
.value()["api"]
|
||||
.to_object()
|
||||
.value()["array"]
|
||||
.to_array()
|
||||
.value();
|
||||
std::cout << "payload /object/api/array = " << rfl::json::write(array) << '\n';
|
||||
|
||||
jwt::verify<traits>()
|
||||
.allow_algorithm(jwt::algorithm::none{})
|
||||
.with_issuer("auth.mydomain.io")
|
||||
.with_audience("mydomain.io")
|
||||
.with_claim("object", from_raw_json)
|
||||
.verify(decoded);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user