-
-
Notifications
You must be signed in to change notification settings - Fork 291
Reflect-cpp traits #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aligirayhanozbay
wants to merge
17
commits into
Thalhammer:master
Choose a base branch
from
aligirayhanozbay:feature/reflect-traits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Reflect-cpp traits #392
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9666878
add reflect json traits
aligirayhanozbay-abex f864cb5
jwt-cpp defaults
aligirayhanozbay-abex fa6d47d
fix clang tidy warnings
aligirayhanozbay-abex 76099fd
fix clang tidy warnings
aligirayhanozbay-abex 051e6bf
rename directory
aligirayhanozbay-abex ca4c97e
add tests
aligirayhanozbay 4ee3643
add tests
aligirayhanozbay abbdf9d
add tests
aligirayhanozbay 3a13494
replace unnecessary copy with move
aligirayhanozbay f546ffe
gate reflect cpp tests behind flag
aligirayhanozbay 808c249
reflect cpp CI work
aligirayhanozbay 45375a8
reflect-cpp example
aligirayhanozbay d8f7c8f
reflect-cpp CI
aligirayhanozbay 0803017
reflect-cpp CI
aligirayhanozbay e4d7ce0
reflect-cpp CI
aligirayhanozbay 79ad997
add reflect-cpp to traits.md
aligirayhanozbay 25372b6
add reflect-cpp to traits.md
aligirayhanozbay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: Install reflect-cpp | ||
| description: Build & install reflect-cpp at a specific commit | ||
| inputs: | ||
| version: | ||
| description: Commit/tag to checkout | ||
| required: true | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - shell: bash | ||
| run: | | ||
| set -eux | ||
| git clone https://github.com/getml/reflect-cpp.git _thirdparty/reflect-cpp | ||
| cd _thirdparty/reflect-cpp | ||
| git checkout "${{ inputs.version }}" | ||
| cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=20 | ||
| sudo cmake --build build --target install -j |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #include "jwt-cpp/jwt.h" | ||
| #include "jwt-cpp/traits/reflect-cpp/traits.h" | ||
|
|
||
| #include <chrono> | ||
| #include <iostream> | ||
|
|
||
| int main() { | ||
| using sec = std::chrono::seconds; | ||
| using min = std::chrono::minutes; | ||
| using traits = jwt::traits::reflect_cpp; | ||
| using claim = jwt::basic_claim<traits>; | ||
|
|
||
| // Load a raw JSON object into a claim (reflect-cpp: parse -> wrap) | ||
| claim from_raw_json; | ||
| { | ||
| traits::value_type value; | ||
| // Mirrors the nlohmann example’s JSON | ||
| const auto* const json_text = R"##({"api":{"array":[1,2,3],"null":null}})##"; | ||
| if (!traits::parse(value, json_text)) { | ||
| std::cerr << "failed to parse raw json\n"; | ||
| return 1; | ||
| } | ||
| from_raw_json = claim{std::move(value)}; | ||
| } | ||
|
|
||
| claim::set_t list{"once", "twice"}; | ||
| std::vector<int64_t> big_numbers{727663072LL, 770979831LL, 427239169LL, 525936436LL}; | ||
|
|
||
| // Build an array claim from the big_numbers vector | ||
| traits::array_type arr; | ||
| arr.reserve(big_numbers.size()); | ||
| for (auto val : big_numbers) { | ||
| arr.emplace_back(val); | ||
| } | ||
| claim array_claim{traits::value_type{arr}}; | ||
| claim strings_claim{list.begin(), list.end()}; | ||
|
|
||
| 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", strings_claim) // <— fixed | ||
| .set_payload_claim("array", array_claim) | ||
| .set_payload_claim("object", from_raw_json) | ||
| .sign(jwt::algorithm::none{}); | ||
|
|
||
| const auto decoded = jwt::decode<traits>(token); | ||
|
|
||
| // Access payload /object/api/array using reflect-cpp's Result-returning get() | ||
| { | ||
| const auto obj_v = decoded.get_payload_claim("object").to_json(); // R::value_type | ||
| const auto obj = traits::as_object(obj_v); // rfl::Object<rfl::Generic> | ||
|
|
||
| if (auto api_res = obj.get("api"); api_res) { // rfl::Result<rfl::Generic> | ||
| const auto api_obj = traits::as_object(api_res.value()); // nested object | ||
|
|
||
| if (auto arr_res = api_obj.get("array"); arr_res) { | ||
| const auto& nested = traits::as_array(arr_res.value()); // vector-like | ||
| std::cout << "payload /object/api/array = " << rfl::json::write(nested) << '\n'; | ||
| } else { | ||
| std::cout << "payload /object/api/array missing\n"; | ||
| } | ||
| } else { | ||
| std::cout << "payload /object/api missing\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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #pragma once | ||
|
|
||
| #ifndef JWT_DISABLE_PICOJSON | ||
| #define JWT_DISABLE_PICOJSON | ||
| #endif | ||
|
|
||
| #include "traits.h" | ||
|
|
||
| namespace jwt { | ||
|
|
||
| /** | ||
| * \brief a class to store a generic reflect-cpp (rfl::Generic) value as claim | ||
| * | ||
| * This type is the specialization of the \ref basic_claim class which | ||
| * uses the reflect-cpp JSON traits. | ||
| */ | ||
| using claim = basic_claim<traits::reflect_cpp>; | ||
|
|
||
| /** Create a verifier using the default clock */ | ||
| inline verifier<default_clock, traits::reflect_cpp> verify() { | ||
| return verify<default_clock, traits::reflect_cpp>(default_clock{}); | ||
| } | ||
|
|
||
| /** Create a builder using the default clock */ | ||
| inline builder<default_clock, traits::reflect_cpp> create() { | ||
| return builder<default_clock, traits::reflect_cpp>(default_clock{}); | ||
| } | ||
|
|
||
| #ifndef JWT_DISABLE_BASE64 | ||
| /** Decode a token (uses jwt-cpp’s built-in base64 if not disabled) */ | ||
| inline decoded_jwt<traits::reflect_cpp> decode(const std::string& token) { | ||
| return decoded_jwt<traits::reflect_cpp>(token); | ||
| } | ||
| #endif | ||
|
|
||
| template<typename Decode> | ||
| decoded_jwt<traits::reflect_cpp> decode(const std::string& token, Decode decode) { | ||
| return decoded_jwt<traits::reflect_cpp>(token, decode); | ||
| } | ||
|
|
||
| /** Parse a JWK */ | ||
| inline jwk<traits::reflect_cpp> parse_jwk(const traits::reflect_cpp::string_type& token) { | ||
| return jwk<traits::reflect_cpp>(token); | ||
| } | ||
|
|
||
| /** Parse a JWKS */ | ||
| inline jwks<traits::reflect_cpp> parse_jwks(const traits::reflect_cpp::string_type& token) { | ||
| return jwks<traits::reflect_cpp>(token); | ||
| } | ||
|
|
||
| /** Verify context type alias (for advanced verification ops) */ | ||
| using verify_context = verify_ops::verify_context<traits::reflect_cpp>; | ||
|
|
||
| } // namespace jwt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #pragma once | ||
|
|
||
| #ifndef JWT_DISABLE_PICOJSON | ||
| #define JWT_DISABLE_PICOJSON | ||
| #endif | ||
|
|
||
| #include "jwt-cpp/jwt.h" | ||
|
|
||
| #include <rfl/Generic.hpp> | ||
| #include <rfl/json.hpp> | ||
| #include <stdexcept> | ||
| #include <variant> | ||
|
|
||
| namespace jwt::traits { | ||
|
|
||
| struct reflect_cpp { | ||
|
|
||
| using value_type = rfl::Generic; | ||
| using object_type = rfl::Generic::Object; | ||
| using array_type = rfl::Generic::Array; | ||
| using string_type = std::string; | ||
| using number_type = double; | ||
| using integer_type = int64_t; | ||
| using boolean_type = bool; | ||
|
|
||
| template<class... Ts> | ||
| struct variant_overloaded : Ts... { | ||
| using Ts::operator()...; | ||
| }; | ||
|
|
||
| static jwt::json::type get_type(const value_type& val) { | ||
| return std::visit( | ||
| variant_overloaded{ | ||
| [](boolean_type const&) -> jwt::json::type { return jwt::json::type::boolean; }, | ||
| [](integer_type const&) -> jwt::json::type { return jwt::json::type::integer; }, | ||
| [](number_type const&) -> jwt::json::type { return jwt::json::type::number; }, | ||
| [](string_type const&) -> jwt::json::type { return jwt::json::type::string; }, | ||
| [](array_type const&) -> jwt::json::type { return jwt::json::type::array; }, | ||
| [](object_type const&) -> jwt::json::type { return jwt::json::type::object; }, | ||
| [](std::nullopt_t const&) -> jwt::json::type { throw std::logic_error("invalid type"); }, | ||
| }, | ||
| val.get()); | ||
| } | ||
|
|
||
| static object_type as_object(const value_type& val) { | ||
| const auto& variant = val.get(); | ||
| if (!std::holds_alternative<object_type>(variant)) throw std::bad_cast(); | ||
| return std::get<object_type>(variant); | ||
| } | ||
|
|
||
| static array_type as_array(const value_type& val) { | ||
| const auto& variant = val.get(); | ||
| if (!std::holds_alternative<array_type>(variant)) throw std::bad_cast(); | ||
| return std::get<array_type>(variant); | ||
| } | ||
|
|
||
| static string_type as_string(const value_type& val) { | ||
| const auto& variant = val.get(); | ||
| if (!std::holds_alternative<string_type>(variant)) throw std::bad_cast(); | ||
| return std::get<string_type>(variant); | ||
| } | ||
|
|
||
| static integer_type as_integer(const value_type& val) { | ||
| const auto& variant = val.get(); | ||
| if (!std::holds_alternative<integer_type>(variant)) throw std::bad_cast(); | ||
| return std::get<integer_type>(variant); | ||
| } | ||
|
|
||
| static boolean_type as_boolean(const value_type& val) { | ||
| const auto& variant = val.get(); | ||
| if (!std::holds_alternative<boolean_type>(variant)) throw std::bad_cast(); | ||
| return std::get<boolean_type>(variant); | ||
| } | ||
|
|
||
| static number_type as_number(const value_type& val) { | ||
| const auto& variant = val.get(); | ||
| if (!std::holds_alternative<number_type>(variant)) throw std::bad_cast(); | ||
| return std::get<number_type>(variant); | ||
| } | ||
|
|
||
| static bool parse(value_type& out, string_type const& json) { | ||
| auto res = rfl::json::read<rfl::Generic>(json); | ||
| if (res) { | ||
| out = *std::move(res); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static std::string serialize(const value_type& val) { return rfl::json::write(val); } | ||
| }; | ||
|
|
||
| } // namespace jwt::traits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.