🪞 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:
Chris Mc
2026-03-09 22:19:31 -07:00
committed by GitHub
parent b12ab284d3
commit df914eba76
13 changed files with 508 additions and 20 deletions

View File

@@ -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

View File

@@ -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!"

View File

@@ -17,7 +17,7 @@ jobs:
- uses: ./.github/actions/install/boost-json - uses: ./.github/actions/install/boost-json
- uses: ./.github/actions/install/open-source-parsers-jsoncpp - uses: ./.github/actions/install/open-source-parsers-jsoncpp
- uses: ./.github/actions/install/glaze-json - uses: ./.github/actions/install/glaze-json
- uses: ./.github/actions/install/reflectcpp-json
- name: Configure LCOV to ignore mismatches - name: Configure LCOV to ignore mismatches
# https://github.com/linux-test-project/lcov/issues/316#issuecomment-2304694266 # https://github.com/linux-test-project/lcov/issues/316#issuecomment-2304694266

View File

@@ -72,6 +72,7 @@ jobs:
- { name: "nlohmann_json", library: "JSON for Modern C++", url: "https://github.com/nlohmann/json", disable_pico: true } - { name: "nlohmann_json", library: "JSON for Modern C++", url: "https://github.com/nlohmann/json", disable_pico: true }
- { name: "open_source_parsers_jsoncpp", library: "jsoncpp", url: "https://github.com/open-source-parsers/jsoncpp", disable_pico: true } - { name: "open_source_parsers_jsoncpp", library: "jsoncpp", url: "https://github.com/open-source-parsers/jsoncpp", disable_pico: true }
- { name: "glaze_json", library: "Glaze", url: "https://github.com/stephenberry/glaze", disable_pico: true } - { name: "glaze_json", library: "Glaze", url: "https://github.com/stephenberry/glaze", disable_pico: true }
- { name: "reflectcpp_json", library: "ReflectCpp", url: "https://github.com/getml/reflect-cpp", disable_pico: true }
name: render-defaults (${{ matrix.traits.name }}) name: render-defaults (${{ matrix.traits.name }})
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -102,6 +103,7 @@ jobs:
- { name: "nlohmann_json", suite: "NlohmannTest" } - { name: "nlohmann_json", suite: "NlohmannTest" }
- { name: "open_source_parsers_jsoncpp", suite: "OspJsoncppTest" } - { name: "open_source_parsers_jsoncpp", suite: "OspJsoncppTest" }
# - { name: "glaze_json", suite: "GlazeTest" } # No supported due to istream buffer being specialized # - { name: "glaze_json", suite: "GlazeTest" } # No supported due to istream buffer being specialized
# - { name: "reflectcpp_json", suite: "ReflectCppJsonTest" } # No supported due to istream buffer being specialized
name: render-tests (${{ matrix.traits.name }}) name: render-tests (${{ matrix.traits.name }})
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4

View File

@@ -22,6 +22,7 @@ jobs:
- { name: "kazuho-picojson", tag: "111c9be5188f7350c2eac9ddaedd8cca3d7bf394", version: "111c9be" } - { name: "kazuho-picojson", tag: "111c9be5188f7350c2eac9ddaedd8cca3d7bf394", version: "111c9be" }
- { name: "open-source-parsers-jsoncpp", tag: "1.9.6", version: "v1.9.6" } - { name: "open-source-parsers-jsoncpp", tag: "1.9.6", version: "v1.9.6" }
- { name: "glaze-json", tag: "7.0.2", version: "v7.0.2" } - { name: "glaze-json", tag: "7.0.2", version: "v7.0.2" }
- { name: "reflectcpp-json", tag: "0.24.0", version: "v0.24.0" }
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: lukka/get-cmake@latest - uses: lukka/get-cmake@latest
@@ -65,6 +66,11 @@ jobs:
with: with:
version: ${{matrix.target.tag}} version: ${{matrix.target.tag}}
- if: matrix.target.name == 'reflectcpp-json'
uses: ./.github/actions/install/reflectcpp-json
with:
version: ${{ matrix.target.tag }}
- name: test - name: test
working-directory: example/traits working-directory: example/traits
run: | run: |

View File

@@ -36,7 +36,7 @@ set(JWT_SSL_LIBRARY OpenSSL CACHE STRING "Determines which SSL library to build
set_property(CACHE JWT_SSL_LIBRARY PROPERTY STRINGS ${JWT_SSL_LIBRARY_OPTIONS}) set_property(CACHE JWT_SSL_LIBRARY PROPERTY STRINGS ${JWT_SSL_LIBRARY_OPTIONS})
set(JWT_JSON_TRAITS_OPTIONS boost-json danielaparker-jsoncons kazuho-picojson nlohmann-json open-source-parsers-jsoncpp set(JWT_JSON_TRAITS_OPTIONS boost-json danielaparker-jsoncons kazuho-picojson nlohmann-json open-source-parsers-jsoncpp
glaze-json) glaze-json reflectcpp-json)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

View File

@@ -11,7 +11,8 @@ For your convenience there are serval traits implementation which provide some p
[![jsoncons][jsoncons]](https://github.com/danielaparker/jsoncons) [![jsoncons][jsoncons]](https://github.com/danielaparker/jsoncons)
[![boostjson][boostjson]](https://github.com/boostorg/json) [![boostjson][boostjson]](https://github.com/boostorg/json)
[![jsoncpp][jsoncpp]](https://github.com/open-source-parsers/jsoncpp) [![jsoncpp][jsoncpp]](https://github.com/open-source-parsers/jsoncpp)
[![glaze][glaze]]([https://github.com/open-source-parsers/jsoncpp](https://github.com/stephenberry/glaze)) [![glaze][glaze]](https://github.com/stephenberry/glaze)
[![reflectcpp][reflectcpp]](https://github.com/getml/reflect-cpp)
[picojson]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/kazuho-picojson/shields.json [picojson]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/kazuho-picojson/shields.json
[nlohmann]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/nlohmann-json/shields.json [nlohmann]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/nlohmann-json/shields.json
@@ -19,6 +20,7 @@ For your convenience there are serval traits implementation which provide some p
[boostjson]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/boost-json/shields.json [boostjson]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/boost-json/shields.json
[jsoncpp]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/open-source-parsers-jsoncpp/shields.json [jsoncpp]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/open-source-parsers-jsoncpp/shields.json
[glaze]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/glaze-json/shields.json [glaze]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/glaze-json/shields.json
[reflectcpp]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Thalhammer/jwt-cpp/badges/traits/reflectcpp-json/shields.json
In order to maintain compatibility, [picojson](https://github.com/kazuho/picojson) is still used to provide a specialized `jwt::claim` along with all helpers. Defining `JWT_DISABLE_PICOJSON` will remove this optional dependency. It's possible to directly include the traits defaults for the other JSON libraries. See the [traits examples](https://github.com/Thalhammer/jwt-cpp/tree/master/example/traits) for details. In order to maintain compatibility, [picojson](https://github.com/kazuho/picojson) is still used to provide a specialized `jwt::claim` along with all helpers. Defining `JWT_DISABLE_PICOJSON` will remove this optional dependency. It's possible to directly include the traits defaults for the other JSON libraries. See the [traits examples](https://github.com/Thalhammer/jwt-cpp/tree/master/example/traits) for details.

View File

@@ -38,5 +38,13 @@ endif()
find_package(glaze CONFIG) find_package(glaze CONFIG)
if(TARGET glaze::glaze) if(TARGET glaze::glaze)
add_executable(glaze-json glaze-json.cpp) add_executable(glaze-json glaze-json.cpp)
target_compile_features(glaze-json PRIVATE cxx_std_23)
target_link_libraries(glaze-json glaze::glaze jwt-cpp::jwt-cpp) target_link_libraries(glaze-json glaze::glaze jwt-cpp::jwt-cpp)
endif() endif()
find_package(reflectcpp CONFIG)
if(TARGET reflectcpp::reflectcpp)
add_executable(reflectcpp-json reflectcpp-json.cpp)
target_compile_features(reflectcpp-json PRIVATE cxx_std_20)
target_link_libraries(reflectcpp-json jwt-cpp::jwt-cpp reflectcpp::reflectcpp)
endif()

View 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;
}

View File

@@ -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

View File

@@ -0,0 +1,95 @@
#ifndef JWT_CPP_REFLECT_CPP_TRAITS_H
#define JWT_CPP_REFLECT_CPP_TRAITS_H
#ifndef JWT_DISABLE_PICOJSON
#define JWT_DISABLE_PICOJSON
#endif
#include "jwt-cpp/jwt.h"
#include <rfl/Generic.hpp>
#include <rfl/json.hpp>
#include <variant>
namespace jwt::traits {
struct reflectcpp_json {
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
#endif

View File

@@ -51,16 +51,22 @@ if(TARGET boost_json)
list(APPEND AVAILABLE_TRAIT_TYPES "::jwt::traits::boost_json") list(APPEND AVAILABLE_TRAIT_TYPES "::jwt::traits::boost_json")
endif() endif()
find_package(jsoncpp CONFIG)
if(TARGET jsoncpp_static)
list(APPEND AVAILABLE_TRAIT_NAMES "jsoncpp")
list(APPEND AVAILABLE_TRAIT_TYPES "::jwt::traits::open_source_parsers_jsoncpp")
endif()
find_package(glaze CONFIG) find_package(glaze CONFIG)
if(TARGET glaze::glaze) if(TARGET glaze::glaze)
list(APPEND AVAILABLE_TRAIT_NAMES "glaze_json") list(APPEND AVAILABLE_TRAIT_NAMES "glaze_json")
list(APPEND AVAILABLE_TRAIT_TYPES "::jwt::traits::glaze_json") list(APPEND AVAILABLE_TRAIT_TYPES "::jwt::traits::glaze_json")
endif() endif()
find_package(jsoncpp CONFIG) find_package(reflectcpp CONFIG)
if(TARGET jsoncpp_static) if(TARGET reflectcpp::reflectcpp)
list(APPEND AVAILABLE_TRAIT_NAMES "jsoncpp") list(APPEND AVAILABLE_TRAIT_NAMES "reflectcpp")
list(APPEND AVAILABLE_TRAIT_TYPES "::jwt::traits::open_source_parsers_jsoncpp") list(APPEND AVAILABLE_TRAIT_TYPES "::jwt::traits::reflectcpp_json")
endif() endif()
message(STATUS "jwt-cpp: Generating traits_typelist.h with ${AVAILABLE_TRAIT_NAMES}") message(STATUS "jwt-cpp: Generating traits_typelist.h with ${AVAILABLE_TRAIT_NAMES}")
@@ -72,7 +78,7 @@ string(APPEND TRAITS_TYPELIST_CONTENT "// Auto-generated list of available JWT t
string(APPEND TRAITS_TYPELIST_CONTENT "// Includes all necessary trait headers\n\n") string(APPEND TRAITS_TYPELIST_CONTENT "// Includes all necessary trait headers\n\n")
# Include headers for all available traits # Include headers for all available traits
string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/kazuho-picojson/traits.h>\n\n") string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/kazuho-picojson/traits.h>\n")
string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/nlohmann-json/traits.h>\n") string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/nlohmann-json/traits.h>\n")
if(TARGET jsoncons) if(TARGET jsoncons)
@@ -83,16 +89,20 @@ if(TARGET boost_json)
string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/boost-json/traits.h>\n") string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/boost-json/traits.h>\n")
endif() endif()
if(TARGET glaze::glaze)
string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/glaze-json/traits.h>\n")
endif()
if(TARGET jsoncpp_static) if(TARGET jsoncpp_static)
string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/open-source-parsers-jsoncpp/traits.h>\n") string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/open-source-parsers-jsoncpp/traits.h>\n")
endif() endif()
if(TARGET glaze::glaze)
string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/glaze-json/traits.h>\n")
endif()
if(TARGET reflectcpp::reflectcpp)
string(APPEND TRAITS_TYPELIST_CONTENT "#include <jwt-cpp/traits/reflectcpp-json/traits.h>\n")
endif()
# Add the type list using the available traits # Add the type list using the available traits
string(APPEND TRAITS_TYPELIST_CONTENT "using AllTraitTypes = ::testing::Types<\n") string(APPEND TRAITS_TYPELIST_CONTENT "\nusing AllTraitTypes = ::testing::Types<\n")
list(LENGTH AVAILABLE_TRAIT_TYPES count) list(LENGTH AVAILABLE_TRAIT_TYPES count)
math(EXPR last_index "${count} - 1") math(EXPR last_index "${count} - 1")
@@ -127,12 +137,16 @@ if(TARGET boost_json)
list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/BoostJsonTest.cpp) list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/BoostJsonTest.cpp)
endif() endif()
if(TARGET jsoncpp_static)
list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/OspJsoncppTest.cpp)
endif()
if(TARGET glaze::glaze) if(TARGET glaze::glaze)
list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/GlazeTest.cpp) list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/GlazeTest.cpp)
endif() endif()
if(TARGET jsoncpp_static) if(TARGET reflectcpp::reflectcpp)
list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/OspJsoncppTest.cpp) list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/ReflectCppJsonTest.cpp)
endif() endif()
add_executable(jwt-cpp-test ${TEST_SOURCES}) add_executable(jwt-cpp-test ${TEST_SOURCES})
@@ -147,6 +161,7 @@ set(JWT_TESTER_CLANG_FLAGS -Weverything -Wno-c++98-compat -Wno-global-constructo
target_compile_options( target_compile_options(
jwt-cpp-test PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/W4> $<$<CXX_COMPILER_ID:GNU>:${JWT_TESTER_GCC_FLAGS}> jwt-cpp-test PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/W4> $<$<CXX_COMPILER_ID:GNU>:${JWT_TESTER_GCC_FLAGS}>
$<$<CXX_COMPILER_ID:Clang>:${JWT_TESTER_CLANG_FLAGS}>) $<$<CXX_COMPILER_ID:Clang>:${JWT_TESTER_CLANG_FLAGS}>)
if(HUNTER_ENABLED) if(HUNTER_ENABLED)
target_link_libraries(jwt-cpp-test PRIVATE GTest::gtest GTest::gtest_main) target_link_libraries(jwt-cpp-test PRIVATE GTest::gtest GTest::gtest_main)
# Define a compile define to bypass openssl error tests # Define a compile define to bypass openssl error tests
@@ -160,15 +175,19 @@ else()
if(TARGET boost_json) if(TARGET boost_json)
target_link_libraries(jwt-cpp-test PRIVATE boost_json) target_link_libraries(jwt-cpp-test PRIVATE boost_json)
endif() endif()
if(TARGET glaze::glaze)
target_link_libraries(jwt-cpp-test PRIVATE glaze::glaze)
target_compile_features(jwt-cpp-test PUBLIC cxx_std_23)
set_target_properties(jwt-cpp-test PROPERTIES CXX_STANDARD 23 CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON)
endif()
if(TARGET jsoncpp_static) if(TARGET jsoncpp_static)
target_link_libraries(jwt-cpp-test PRIVATE jsoncpp_static) target_link_libraries(jwt-cpp-test PRIVATE jsoncpp_static)
endif() endif()
if(TARGET glaze::glaze)
target_compile_features(jwt-cpp-test PRIVATE cxx_std_23)
target_link_libraries(jwt-cpp-test PRIVATE glaze::glaze)
endif()
if(TARGET reflectcpp::reflectcpp)
target_compile_features(jwt-cpp-test PRIVATE cxx_std_20)
target_link_libraries(jwt-cpp-test PRIVATE reflectcpp::reflectcpp)
endif()
endif() endif()
target_link_libraries(jwt-cpp-test PRIVATE jwt-cpp nlohmann_json::nlohmann_json target_link_libraries(jwt-cpp-test PRIVATE jwt-cpp nlohmann_json::nlohmann_json
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${CMAKE_DL_LIBS}>) $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${CMAKE_DL_LIBS}>)

View File

@@ -0,0 +1,149 @@
#include "jwt-cpp/traits/reflectcpp-json/traits.h"
#include <gtest/gtest.h>
TEST(ReflectCppTest, BasicClaims) {
const auto string =
jwt::basic_claim<jwt::traits::reflectcpp_json>(jwt::traits::reflectcpp_json::string_type("string"));
ASSERT_EQ(string.get_type(), jwt::json::type::string);
const auto array = jwt::basic_claim<jwt::traits::reflectcpp_json>(
std::set<jwt::traits::reflectcpp_json::string_type>{"string", "string"});
ASSERT_EQ(array.get_type(), jwt::json::type::array);
const auto integer = jwt::basic_claim<jwt::traits::reflectcpp_json>(159816816);
ASSERT_EQ(integer.get_type(), jwt::json::type::integer);
}
TEST(ReflectCppTest, AudienceAsString) {
jwt::traits::reflectcpp_json::string_type token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0.WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4";
auto decoded = jwt::decode<jwt::traits::reflectcpp_json>(token);
ASSERT_TRUE(decoded.has_algorithm());
ASSERT_TRUE(decoded.has_type());
ASSERT_FALSE(decoded.has_content_type());
ASSERT_FALSE(decoded.has_key_id());
ASSERT_FALSE(decoded.has_issuer());
ASSERT_FALSE(decoded.has_subject());
ASSERT_TRUE(decoded.has_audience());
ASSERT_FALSE(decoded.has_expires_at());
ASSERT_FALSE(decoded.has_not_before());
ASSERT_FALSE(decoded.has_issued_at());
ASSERT_FALSE(decoded.has_id());
ASSERT_EQ("HS256", decoded.get_algorithm());
ASSERT_EQ("JWT", decoded.get_type());
auto aud = decoded.get_audience();
ASSERT_EQ(1, aud.size());
ASSERT_EQ("test", *aud.begin());
}
TEST(ReflectCppTest, SetArray) {
jwt::traits::reflectcpp_json::array_type arr{100, 20, 10};
jwt::traits::reflectcpp_json::value_type value(arr);
jwt::basic_claim<jwt::traits::reflectcpp_json> array_claim(value);
auto token =
jwt::create<jwt::traits::reflectcpp_json>().set_payload_claim("test", array_claim).sign(jwt::algorithm::none{});
ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0.");
}
TEST(ReflectCppTest, SetObject) {
jwt::traits::reflectcpp_json::value_type value;
ASSERT_TRUE(jwt::traits::reflectcpp_json::parse(value, "{\"api-x\": [1]}"));
// Wrap into a claim and verify type
jwt::basic_claim<jwt::traits::reflectcpp_json> object(value);
ASSERT_EQ(object.get_type(), jwt::json::type::object);
auto token = jwt::create<jwt::traits::reflectcpp_json>()
.set_payload_claim("namespace", object)
.sign(jwt::algorithm::hs256("test"));
ASSERT_EQ(token,
"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA");
}
TEST(ReflectCppTest, VerifyTokenHS256) {
jwt::traits::reflectcpp_json::string_type token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
const auto decoded_token = jwt::decode<jwt::traits::reflectcpp_json>(token);
const auto verify = jwt::verify<jwt::traits::reflectcpp_json>()
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_issuer("auth0");
verify.verify(decoded_token);
}
TEST(ReflectCppTest, VerifyTokenExpirationValid) {
const auto token = jwt::create<jwt::traits::reflectcpp_json>()
.set_issuer("auth0")
.set_issued_at(std::chrono::system_clock::now())
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{3600})
.sign(jwt::algorithm::hs256{"secret"});
const auto decoded_token = jwt::decode<jwt::traits::reflectcpp_json>(token);
const auto verify = jwt::verify<jwt::traits::reflectcpp_json>()
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_issuer("auth0");
verify.verify(decoded_token);
}
TEST(ReflectCppTest, VerifyTokenExpirationInValid) {
const auto token = jwt::create<jwt::traits::reflectcpp_json>()
.set_issuer("auth0")
.set_issued_now()
.set_expires_in(std::chrono::hours{1})
.sign(jwt::algorithm::hs256{"secret"});
const auto decoded_token = jwt::decode<jwt::traits::reflectcpp_json>(token);
const auto verify = jwt::verify<jwt::traits::reflectcpp_json>()
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_issuer("auth0");
verify.verify(decoded_token);
}
TEST(ReflectCppTest, VerifyTokenExpired) {
const auto token = jwt::create<jwt::traits::reflectcpp_json>()
.set_issuer("auth0")
.set_issued_at(std::chrono::system_clock::now() - std::chrono::seconds{3601})
.set_expires_at(std::chrono::system_clock::now() - std::chrono::seconds{1})
.sign(jwt::algorithm::hs256{"secret"});
const auto decoded_token = jwt::decode<jwt::traits::reflectcpp_json>(token);
const auto verify = jwt::verify<jwt::traits::reflectcpp_json>()
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_issuer("auth0");
ASSERT_THROW(verify.verify(decoded_token), jwt::error::token_verification_exception);
std::error_code ec;
ASSERT_NO_THROW(verify.verify(decoded_token, ec));
ASSERT_TRUE(!(!ec));
ASSERT_EQ(ec.category(), jwt::error::token_verification_error_category());
ASSERT_EQ(ec.value(), static_cast<int>(jwt::error::token_verification_error::token_expired));
}
TEST(ReflectCppTest, VerifyArray) {
jwt::traits::reflectcpp_json::string_type token = "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0.";
const auto decoded_token = jwt::decode<jwt::traits::reflectcpp_json>(token);
jwt::traits::reflectcpp_json::array_type arr{100, 20, 10};
jwt::basic_claim<jwt::traits::reflectcpp_json> array_claim{jwt::traits::reflectcpp_json::value_type(arr)};
const auto verify = jwt::verify<jwt::traits::reflectcpp_json>()
.allow_algorithm(jwt::algorithm::none{})
.with_claim("test", array_claim);
ASSERT_NO_THROW(verify.verify(decoded_token));
}
TEST(ReflectCppTest, VerifyObject) {
jwt::traits::reflectcpp_json::string_type token =
"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA";
const auto decoded_token = jwt::decode<jwt::traits::reflectcpp_json>(token);
jwt::traits::reflectcpp_json::value_type value;
ASSERT_TRUE(jwt::traits::reflectcpp_json::parse(value, "{\"api-x\": [1]}"));
jwt::basic_claim<jwt::traits::reflectcpp_json> object_claim(value);
const auto verify = jwt::verify<jwt::traits::reflectcpp_json>()
.allow_algorithm(jwt::algorithm::hs256("test"))
.with_claim("namespace", object_claim);
ASSERT_NO_THROW(verify.verify(decoded_token));
}