mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
Implemented JsonCPP Traits (#317)
* Implemented JsonCPP Traits * quick pass to add CI * correct find_packge to use config and not be required * typo in default version * jsoncpp forces out of source builds * disable tests and shared library * add missing build step * try a different target + build less "they" do not maintain the CMake scripts so this will be fun https://github.com/open-source-parsers/jsoncpp/issues/455#issuecomment-478251641 * fix name for traits (missed replaces) * Added missing array_type constructor so that basic_claims can be formed from sets of values * Linters and more tests + fetchcontent gtest * linter * support library names with many separators * linters * run clang format after rendering defaults --------- Co-authored-by: Chris Mc <prince.chrismc@gmail.com>
This commit is contained in:
47
example/traits/open-source-parsers-jsoncpp.cpp
Normal file
47
example/traits/open-source-parsers-jsoncpp.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "jwt-cpp/traits/open-source-parsers-jsoncpp/traits.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
int main() {
|
||||
using sec = std::chrono::seconds;
|
||||
using min = std::chrono::minutes;
|
||||
using traits = jwt::traits::open_source_parsers_jsoncpp;
|
||||
using claim = jwt::basic_claim<traits>;
|
||||
|
||||
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<int64_t> big_numbers{727663072ULL, 770979831ULL, 427239169ULL, 525936436ULL};
|
||||
|
||||
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", {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 = traits::as_array(decoded.get_payload_claim("object").to_json()["api"]["array"]);
|
||||
std::cout << "payload /object/api/array = " << array << std::endl;
|
||||
|
||||
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