-
-
Notifications
You must be signed in to change notification settings - Fork 306
Add support for reflect-cpp's JSON #421
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
Merged
prince-chrismc
merged 25 commits into
Thalhammer:master
from
prince-chrismc:reflect-traits-2
Mar 10, 2026
Merged
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d2fde01
add reflect json traits
aligirayhanozbay-abex 5dac26c
jwt-cpp defaults
aligirayhanozbay-abex fca6491
fix clang tidy warnings
aligirayhanozbay-abex 11ad8e1
fix clang tidy warnings
aligirayhanozbay-abex 039bded
rename directory
aligirayhanozbay-abex f94251b
add tests
aligirayhanozbay 5a03297
add tests
aligirayhanozbay 4d447a5
add tests
aligirayhanozbay 0537b2f
replace unnecessary copy with move
aligirayhanozbay bcbbc1e
gate reflect cpp tests behind flag
aligirayhanozbay f4c37ad
reflect cpp CI work
aligirayhanozbay 256ffea
reflect-cpp example
aligirayhanozbay afdc69e
reflect-cpp CI
aligirayhanozbay 14d3bbb
reflect-cpp CI
aligirayhanozbay 152f210
match naming + update ci
prince-chrismc 8b1f1e4
first pass code review
prince-chrismc 905aa1e
linter
prince-chrismc 34edfc4
linter
prince-chrismc ba7ff88
align naming
prince-chrismc c4dc0bd
make file +x
prince-chrismc b106294
align target name
prince-chrismc 8b1a1e7
fix typo
prince-chrismc 279da1d
Fix target_link_libraries syntax for jwt-cpp
prince-chrismc 4ab4ce8
cleanup example code
prince-chrismc 1291d79
add reflect-json to coverage tests
prince-chrismc 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,12 @@ | ||
| name: Install reflect-cpp | ||
| description: Install reflect-cpp for building test application | ||
| inputs: | ||
| version: | ||
| description: The desired reflect-cpp version to install | ||
| required: false | ||
| default: "0.24.0" | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - run: ${{ github.action_path }}/install-reflect-cpp.sh ${{ inputs.version }} | ||
| shell: bash |
48 changes: 48 additions & 0 deletions
48
.github/actions/install/reflectcpp-json/install-reflect-cpp.sh
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,48 @@ | ||
| #!/usr/bin/env bash | ||
| # Install reflect-cpp library with specified version | ||
|
|
||
| set -e # Exit on error | ||
|
|
||
| # Check if version is provided | ||
| if [[ -z "$1" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then | ||
| echo "Usage: $0 VERSION" | ||
| echo "" | ||
| echo "Install reflect-cpp library" | ||
| echo "" | ||
| echo "Arguments:" | ||
| echo " VERSION reflect-cpp version to install (required)" | ||
| echo "" | ||
| echo "Examples:" | ||
| echo " $0 0.24.0" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Configuration | ||
| reflectcpp_VERSION="$1" | ||
| INSTALL_DIR="/tmp" | ||
| BUILD_DIR="${INSTALL_DIR}/reflect-cpp-${reflectcpp_VERSION}" | ||
|
|
||
| echo "Installing reflectcpp v${reflectcpp_VERSION}..." | ||
|
|
||
| # Download | ||
| echo "Downloading reflectcpp v${reflectcpp_VERSION}..." | ||
| cd "${INSTALL_DIR}" | ||
| wget -q "https://github.com/getml/reflect-cpp/archive/v${reflectcpp_VERSION}.tar.gz" | ||
|
|
||
| # Extract | ||
| echo "Extracting archive..." | ||
| tar -zxf "v${reflectcpp_VERSION}.tar.gz" | ||
|
|
||
| # Build and install | ||
| echo "Building and installing..." | ||
| cd "${BUILD_DIR}" | ||
| cmake . -DCMAKE_BUILD_TYPE=Release -DREFLECTCPP_INSTALL=ON | ||
| cmake --build . | ||
| sudo cmake --install . | ||
|
|
||
| # Cleanup | ||
| echo "Cleaning up..." | ||
| rm -f "${INSTALL_DIR}/v${reflectcpp_VERSION}.tar.gz" | ||
| rm -rf "${BUILD_DIR}" | ||
|
|
||
| echo "✓ reflect-cpp v${reflectcpp_VERSION} installed successfully!" |
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/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>; | ||
|
|
||
| // 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,91 @@ | ||
| #ifndef JWT_CPP_REFLECTCPP_JSON_DEFAULTS_H | ||
| #define JWT_CPP_REFLECTCPP_JSON_DEFAULTS_H | ||
|
|
||
| #ifndef JWT_DISABLE_PICOJSON | ||
| #define JWT_DISABLE_PICOJSON | ||
| #endif | ||
|
|
||
| #include "traits.h" | ||
|
|
||
| namespace jwt { | ||
| /** | ||
| * \brief a class to store a generic [ReflectCpp](https://github.com/getml/reflect-cpp) value as claim | ||
| * | ||
| * This type is the specialization of the \ref basic_claim class which | ||
| * uses the standard template types. | ||
| */ | ||
| using claim = basic_claim<traits::reflectcpp_json>; | ||
|
|
||
| /** | ||
| * Create a verifier using the default clock | ||
| * \return verifier instance | ||
| */ | ||
| inline verifier<default_clock, traits::reflectcpp_json> verify() { | ||
| return verify<default_clock, traits::reflectcpp_json>(default_clock{}); | ||
| } | ||
|
|
||
| /** | ||
| * Create a builder using the default clock | ||
| * \return builder instance to create a new token | ||
| */ | ||
| inline builder<default_clock, traits::reflectcpp_json> create() { | ||
| return builder<default_clock, traits::reflectcpp_json>(default_clock{}); | ||
| } | ||
|
|
||
| #ifndef JWT_DISABLE_BASE64 | ||
| /** | ||
| * Decode a token | ||
| * \param token Token to decode | ||
| * \return Decoded token | ||
| * \throw std::invalid_argument Token is not in correct format | ||
| * \throw std::runtime_error Base64 decoding failed or invalid json | ||
| */ | ||
| inline decoded_jwt<traits::reflectcpp_json> decode(const std::string& token) { | ||
| return decoded_jwt<traits::reflectcpp_json>(token); | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| * Decode a token | ||
| * \tparam Decode is callable, taking a string_type and returns a string_type. | ||
| * It should ensure the padding of the input and then base64url decode and | ||
| * return the results. | ||
| * \param token Token to decode | ||
| * \param decode The token to parse | ||
| * \return Decoded token | ||
| * \throw std::invalid_argument Token is not in correct format | ||
| * \throw std::runtime_error Base64 decoding failed or invalid json | ||
| */ | ||
| template<typename Decode> | ||
| decoded_jwt<traits::reflectcpp_json> decode(const std::string& token, Decode decode) { | ||
| return decoded_jwt<traits::reflectcpp_json>(token, decode); | ||
| } | ||
|
|
||
| /** | ||
| * Parse a jwk | ||
| * \param token JWK Token to parse | ||
| * \return Parsed JWK | ||
| * \throw std::runtime_error Token is not in correct format | ||
| */ | ||
| inline jwk<traits::reflectcpp_json> parse_jwk(const traits::reflectcpp_json::string_type& token) { | ||
| return jwk<traits::reflectcpp_json>(token); | ||
| } | ||
|
|
||
| /** | ||
| * Parse a jwks | ||
| * \param token JWKs Token to parse | ||
| * \return Parsed JWKs | ||
| * \throw std::runtime_error Token is not in correct format | ||
| */ | ||
| inline jwks<traits::reflectcpp_json> parse_jwks(const traits::reflectcpp_json::string_type& token) { | ||
| return jwks<traits::reflectcpp_json>(token); | ||
| } | ||
|
|
||
| /** | ||
| * This type is the specialization of the \ref verify_ops::verify_context class which | ||
| * uses the standard template types. | ||
| */ | ||
| using verify_context = verify_ops::verify_context<traits::reflectcpp_json>; | ||
| } // namespace jwt | ||
|
|
||
| #endif // JWT_CPP_REFLECTCPP_JSON_DEFAULTS_H |
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.