mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
🗺️ Add support for Boost.JSON traits (#165)
* wip boost test * wip hardcoded just to test * wip more * working POC * trying boost json with c++17 * working boost json standalone with cxx17 * Delete conanfile.txt * revert some changes to see if they are required * adding a workflow * cleanup * naming convention * testing for substr on string_type * trying to test for operator+ * fix check for operator+ * losen std::string requirement (experimental) * Update .gitignore Co-authored-by: gerardoprada <gerardoprada@users.noreply.github.com> Co-authored-by: gerardoprada <gerardoprada@users.noreply.github.com>
This commit is contained in:
21
.github/workflows/traits.yml
vendored
21
.github/workflows/traits.yml
vendored
@@ -34,3 +34,24 @@ jobs:
|
||||
cmake . -DCMAKE_FIND_DEBUG_MODE=1
|
||||
cmake --build .
|
||||
./jsoncons-traits-narrow
|
||||
|
||||
boost-json:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: lukka/get-cmake@latest
|
||||
- uses: ./.github/actions/install/gtest
|
||||
|
||||
- name: setup
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DJWT_BUILD_EXAMPLES=OFF
|
||||
sudo cmake --install .
|
||||
|
||||
- name: test
|
||||
run: |
|
||||
cd tests/boost
|
||||
cmake .
|
||||
cmake --build .
|
||||
./boostjson-traits
|
||||
|
||||
@@ -1779,7 +1779,7 @@ namespace jwt {
|
||||
std::is_constructible<value_type, const value_type&>::value && // a more generic is_copy_constructible
|
||||
std::is_move_constructible<value_type>::value && std::is_assignable<value_type, value_type>::value &&
|
||||
std::is_copy_assignable<value_type>::value && std::is_move_assignable<value_type>::value;
|
||||
// TODO(cmcarthur): Stream operators
|
||||
// TODO(prince-chrismc): Stream operators
|
||||
};
|
||||
|
||||
template<typename traits_type>
|
||||
@@ -1847,7 +1847,8 @@ namespace jwt {
|
||||
is_detected<has_mapped_type, object_type>::value &&
|
||||
std::is_same<typename object_type::mapped_type, value_type>::value &&
|
||||
is_detected<has_key_type, object_type>::value &&
|
||||
std::is_same<typename object_type::key_type, string_type>::value &&
|
||||
(std::is_same<typename object_type::key_type, string_type>::value ||
|
||||
std::is_constructible<typename object_type::key_type, string_type>::value) &&
|
||||
supports_begin<object_type>::value && supports_end<object_type>::value &&
|
||||
is_count_signature<object_type, string_type>::value &&
|
||||
is_subcription_operator_signature<object_type, value_type, string_type>::value &&
|
||||
@@ -1863,11 +1864,57 @@ namespace jwt {
|
||||
static constexpr auto value = std::is_same<typename array_type::value_type, value_type>::value;
|
||||
};
|
||||
|
||||
template<typename value_type, typename string_type, typename object_type, typename array_type>
|
||||
template<typename string_type, typename integer_type>
|
||||
using is_substr_start_end_index_signature =
|
||||
typename std::is_same<decltype(std::declval<string_type>().substr(std::declval<integer_type>(),
|
||||
std::declval<integer_type>())),
|
||||
string_type>;
|
||||
|
||||
template<typename string_type, typename integer_type>
|
||||
using is_substr_start_index_signature =
|
||||
typename std::is_same<decltype(std::declval<string_type>().substr(std::declval<integer_type>())),
|
||||
string_type>;
|
||||
|
||||
template<typename string_type>
|
||||
struct has_operate_plus_method { // https://stackoverflow.com/a/9154394/8480874
|
||||
template<class>
|
||||
struct sfinae_true : std::true_type {};
|
||||
|
||||
template<class T, class A0>
|
||||
static auto test_operator_plus(int)
|
||||
-> sfinae_true<decltype(std::declval<T>().operator+(std::declval<A0>()))>;
|
||||
template<class, class A0>
|
||||
static auto test_operator_plus(long) -> std::false_type;
|
||||
|
||||
static constexpr auto value = decltype(test_operator_plus<string_type, string_type>(0)){};
|
||||
};
|
||||
|
||||
template<typename string_type>
|
||||
using is_std_operate_plus_signature =
|
||||
typename std::is_same<decltype(std::operator+(std::declval<string_type>(), std::declval<string_type>())),
|
||||
string_type>;
|
||||
|
||||
template<typename string_type, typename integer_type>
|
||||
struct is_valid_json_string {
|
||||
static constexpr auto substr = is_substr_start_end_index_signature<string_type, integer_type>::value &&
|
||||
is_substr_start_index_signature<string_type, integer_type>::value;
|
||||
static_assert(substr, "string_type must have a substr method taking only a start index and an overload "
|
||||
"taking a start and end index, both must return a string_type");
|
||||
|
||||
static constexpr auto operator_plus =
|
||||
has_operate_plus_method<string_type>::value || is_std_operate_plus_signature<string_type>::value;
|
||||
static_assert(operator_plus,
|
||||
"string_type must have a '+' operator implemented which returns the concatenated string");
|
||||
|
||||
static constexpr auto value = substr && operator_plus;
|
||||
};
|
||||
|
||||
template<typename value_type, typename string_type, typename integer_type, typename object_type,
|
||||
typename array_type>
|
||||
struct is_valid_json_types {
|
||||
// Internal assertions for better feedback
|
||||
static_assert(is_valid_json_value<value_type>::value,
|
||||
"value type must meet basic requirements, default constructor, copyable, moveable");
|
||||
"value_type must meet basic requirements, default constructor, copyable, moveable");
|
||||
static_assert(is_valid_json_object<value_type, string_type, object_type>::value,
|
||||
"object_type must be a string_type to value_type container");
|
||||
static_assert(is_valid_json_array<value_type, array_type>::value,
|
||||
@@ -1875,7 +1922,8 @@ namespace jwt {
|
||||
|
||||
static constexpr auto value = is_valid_json_object<value_type, string_type, object_type>::value &&
|
||||
is_valid_json_value<value_type>::value &&
|
||||
is_valid_json_array<value_type, array_type>::value;
|
||||
is_valid_json_array<value_type, array_type>::value &&
|
||||
is_valid_json_string<string_type, integer_type>::value;
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
@@ -1896,12 +1944,15 @@ namespace jwt {
|
||||
* https://github.com/nlohmann/json/issues/774. It maybe be expanded to
|
||||
* support custom string types.
|
||||
*/
|
||||
static_assert(std::is_same<typename json_traits::string_type, std::string>::value,
|
||||
"string_type must be a std::string.");
|
||||
static_assert(std::is_same<typename json_traits::string_type, std::string>::value ||
|
||||
std::is_convertible<typename json_traits::string_type, std::string>::value ||
|
||||
std::is_constructible<typename json_traits::string_type, std::string>::value,
|
||||
"string_type must be a std::string, convertible to a std::string, or construct a std::string.");
|
||||
|
||||
static_assert(
|
||||
details::is_valid_json_types<typename json_traits::value_type, typename json_traits::string_type,
|
||||
typename json_traits::object_type, typename json_traits::array_type>::value,
|
||||
typename json_traits::integer_type, typename json_traits::object_type,
|
||||
typename json_traits::array_type>::value,
|
||||
"must staisfy json container requirements");
|
||||
static_assert(details::is_valid_traits<json_traits>::value, "traits must satisfy requirements");
|
||||
|
||||
|
||||
1
tests/boost/.gitignore
vendored
Normal file
1
tests/boost/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
build/
|
||||
173
tests/boost/BoostJsonTest.cpp
Normal file
173
tests/boost/BoostJsonTest.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
#define JWT_DISABLE_PICOJSON
|
||||
#include "jwt-cpp/jwt.h"
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <boost/json/src.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
namespace json = boost::json;
|
||||
|
||||
struct boostjson_traits {
|
||||
using value_type = json::value;
|
||||
using object_type = json::object;
|
||||
using array_type = json::array;
|
||||
using string_type = std::string;
|
||||
using number_type = double;
|
||||
using integer_type = std::int64_t;
|
||||
using boolean_type = bool;
|
||||
|
||||
static jwt::json::type get_type(const value_type& val) {
|
||||
using jwt::json::type;
|
||||
|
||||
if (val.kind() == json::kind::bool_) return type::boolean;
|
||||
if (val.kind() == json::kind::int64) return type::integer;
|
||||
if (val.kind() == json::kind::uint64) // boost internally tracks two types of integers
|
||||
return type::integer;
|
||||
if (val.kind() == json::kind::double_) return type::number;
|
||||
if (val.kind() == json::kind::string) return type::string;
|
||||
if (val.kind() == json::kind::array) return type::array;
|
||||
if (val.kind() == json::kind::object) return type::object;
|
||||
|
||||
throw std::logic_error("invalid type");
|
||||
}
|
||||
|
||||
static object_type as_object(const value_type& val) {
|
||||
if (val.kind() != json::kind::object) throw std::bad_cast();
|
||||
return val.get_object();
|
||||
}
|
||||
|
||||
static array_type as_array(const value_type& val) {
|
||||
if (val.kind() != json::kind::array) throw std::bad_cast();
|
||||
return val.get_array();
|
||||
}
|
||||
|
||||
static string_type as_string(const value_type& val) {
|
||||
if (val.kind() != json::kind::string) throw std::bad_cast();
|
||||
return string_type{val.get_string()};
|
||||
}
|
||||
|
||||
static integer_type as_int(const value_type& val) {
|
||||
switch (val.kind()) {
|
||||
case json::kind::int64: return val.get_int64();
|
||||
case json::kind::uint64: return static_cast<int64_t>(val.get_uint64());
|
||||
default: throw std::bad_cast();
|
||||
}
|
||||
}
|
||||
|
||||
static boolean_type as_bool(const value_type& val) {
|
||||
if (val.kind() != json::kind::bool_) throw std::bad_cast();
|
||||
return val.get_bool();
|
||||
}
|
||||
|
||||
static number_type as_number(const value_type& val) {
|
||||
if (val.kind() != json::kind::double_) throw std::bad_cast();
|
||||
return val.get_double();
|
||||
}
|
||||
|
||||
static bool parse(value_type& val, string_type str) {
|
||||
val = json::parse(str);
|
||||
return true;
|
||||
}
|
||||
|
||||
static std::string serialize(const value_type& val) { return json::serialize(val); }
|
||||
};
|
||||
|
||||
TEST(BoostJSONTest, BasicClaims) {
|
||||
using boostjson_claim = jwt::basic_claim<boostjson_traits>;
|
||||
|
||||
const auto string = boostjson_claim(boostjson_traits::string_type("string"));
|
||||
const auto array = boostjson_claim(std::set<boostjson_traits::string_type>{"string", "string"});
|
||||
const auto integer = boostjson_claim(boostjson_traits::value_type{159816816});
|
||||
}
|
||||
|
||||
TEST(BoostJSONTest, AudienceAsString) {
|
||||
boostjson_traits::string_type token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0."
|
||||
"WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4";
|
||||
auto decoded = jwt::decode<boostjson_traits>(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(BoostJSONTest, SetArray) {
|
||||
std::vector<int64_t> vect = {100, 20, 10};
|
||||
auto token = jwt::create<boostjson_traits>()
|
||||
.set_payload_claim("test", jwt::basic_claim<boostjson_traits>(vect.begin(), vect.end()))
|
||||
.sign(jwt::algorithm::none{});
|
||||
ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0.");
|
||||
}
|
||||
|
||||
TEST(BoostJSONTest, SetObject) {
|
||||
jwt::basic_claim<boostjson_traits> object{json::parse("{\"api-x\": [1]}")};
|
||||
ASSERT_EQ(object.get_type(), jwt::json::type::object);
|
||||
|
||||
auto token =
|
||||
jwt::create<boostjson_traits>().set_payload_claim("namespace", object).sign(jwt::algorithm::hs256("test"));
|
||||
ASSERT_EQ(token,
|
||||
"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA");
|
||||
}
|
||||
|
||||
TEST(BoostJSONTest, VerifyTokenHS256) {
|
||||
boostjson_traits::string_type token =
|
||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
|
||||
|
||||
auto verify = jwt::verify<jwt::default_clock, boostjson_traits>({})
|
||||
.allow_algorithm(jwt::algorithm::hs256{"secret"})
|
||||
.with_issuer("auth0");
|
||||
|
||||
auto decoded_token = jwt::decode<boostjson_traits>(token);
|
||||
verify.verify(decoded_token);
|
||||
}
|
||||
|
||||
TEST(BoostJSONTest, VerifyTokenExpirationValid) {
|
||||
const auto token = jwt::create<boostjson_traits>()
|
||||
.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"});
|
||||
|
||||
auto verify = jwt::verify<jwt::default_clock, boostjson_traits>({})
|
||||
.allow_algorithm(jwt::algorithm::hs256{"secret"})
|
||||
.with_issuer("auth0");
|
||||
|
||||
auto decoded_token = jwt::decode<boostjson_traits>(token);
|
||||
verify.verify(decoded_token);
|
||||
}
|
||||
|
||||
TEST(BoostJSONTest, VerifyTokenExpired) {
|
||||
const auto token = jwt::create<boostjson_traits>()
|
||||
.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"});
|
||||
|
||||
auto verify = jwt::verify<jwt::default_clock, boostjson_traits>({})
|
||||
.allow_algorithm(jwt::algorithm::hs256{"secret"})
|
||||
.with_issuer("auth0");
|
||||
|
||||
auto decoded_token = jwt::decode<boostjson_traits>(token);
|
||||
ASSERT_THROW(verify.verify(decoded_token), jwt::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));
|
||||
}
|
||||
18
tests/boost/CMakeLists.txt
Normal file
18
tests/boost/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
cmake_minimum_required(VERSION 3.18)
|
||||
project(jwt-cpp-boostjson-tests)
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
find_package(GTest REQUIRED)
|
||||
if(NOT TARGET jwt-cpp::jwt-cpp)
|
||||
find_package(jwt-cpp CONFIG REQUIRED)
|
||||
endif()
|
||||
|
||||
file(DOWNLOAD https://github.com/boostorg/json/archive/refs/tags/boost-1.77.0.tar.gz
|
||||
${CMAKE_CURRENT_BINARY_DIR}/boost-1.77.0.tar.gz)
|
||||
file(ARCHIVE_EXTRACT INPUT ${CMAKE_CURRENT_BINARY_DIR}/boost-1.77.0.tar.gz DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
add_executable(boostjson-traits BoostJsonTest.cpp)
|
||||
target_link_libraries(boostjson-traits jwt-cpp::jwt-cpp GTest::GTest GTest::Main)
|
||||
target_compile_definitions(boostjson-traits PRIVATE BOOST_JSON_STANDALONE)
|
||||
target_compile_features(boostjson-traits PRIVATE cxx_std_17)
|
||||
target_include_directories(boostjson-traits PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/json-boost-1.77.0/include")
|
||||
Reference in New Issue
Block a user