Skip to content
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

jwt-cpp: fix building without picojson #12735

Merged
merged 7 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions recipes/jwt-cpp/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ sources:
url: "https://github.com/Thalhammer/jwt-cpp/archive/v0.5.1.tar.gz"
sha256: "d8f5ffb361824630b3b6f4aad26c730c915081071040c232ac57947d6177ef4f"
"0.5.0":
url: https://github.com/Thalhammer/jwt-cpp/archive/v0.5.0.tar.gz
sha256: 079a273f070dd11213e301712319a65881e51ab81535cc436d5313191df852a2
url: "https://github.com/Thalhammer/jwt-cpp/archive/v0.5.0.tar.gz"
sha256: "079a273f070dd11213e301712319a65881e51ab81535cc436d5313191df852a2"
"0.4.0":
url: https://github.com/Thalhammer/jwt-cpp/archive/v0.4.0.tar.gz
sha256: f0dcc7b0e8bef8f9c3f434e7121f9941145042c9fe3055a5bdd709085a4f2be4
url: "https://github.com/Thalhammer/jwt-cpp/archive/v0.4.0.tar.gz"
sha256: "f0dcc7b0e8bef8f9c3f434e7121f9941145042c9fe3055a5bdd709085a4f2be4"
"0.3.1":
url: https://github.com/Thalhammer/jwt-cpp/archive/v0.3.1.tar.gz
sha256: 399345e81883f2959df658cd945de39548ddfefdab2acf7b23ee07b9e9a02938
url: "https://github.com/Thalhammer/jwt-cpp/archive/v0.3.1.tar.gz"
sha256: "399345e81883f2959df658cd945de39548ddfefdab2acf7b23ee07b9e9a02938"
patches:
"0.6.0":
- patch_file: patches/0005-fix-picojson-header-location-for-conan.patch
base_path: source_subfolder
- patch_file: "patches/0005-fix-picojson-header-location-for-conan.patch"
patch_description: "Remove picojson namespace from its include"
patch_type: "conan"
"0.5.1":
- patch_file: patches/0004-fix-picojson-header-location-for-conan.patch
base_path: source_subfolder
- patch_file: "patches/0004-fix-picojson-header-location-for-conan.patch"
patch_description: "Remove picojson namespace from its include"
patch_type: "conan"
"0.5.0":
- patch_file: patches/0004-fix-picojson-header-location-for-conan.patch
base_path: source_subfolder
- patch_file: "patches/0004-fix-picojson-header-location-for-conan.patch"
patch_description: "Remove picojson namespace from its include"
patch_type: "conan"
"0.4.0":
- patch_file: patches/0003-fix-picojson-header-location-for-conan.patch
base_path: source_subfolder
- patch_file: "patches/0003-fix-picojson-header-location-for-conan.patch"
patch_description: "Remove picojson namespace from its include"
patch_type: "conan"
"0.3.1":
- patch_file: patches/0002-fix-openssl-change-version.patch
base_path: source_subfolder
- patch_file: "patches/0002-fix-openssl-change-version.patch"
patch_description: "Remove picojson namespace from its include"
patch_type: "conan"
40 changes: 26 additions & 14 deletions recipes/jwt-cpp/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from conans import ConanFile, tools
from conan import ConanFile
from conan.tools.scm import Version
from conan.tools.files import get, copy, apply_conandata_patches
from conan.tools.layout import basic_layout
import os

required_conan_version = ">=1.43.0"
Expand All @@ -10,15 +13,12 @@ class JwtCppConan(ConanFile):
homepage = "https://github.com/Thalhammer/jwt-cpp"
description = "A C++ JSON Web Token library for encoding/decoding"
topics = ("jwt-cpp", "json", "jwt", "jws", "jwe", "jwk", "jwks", "jose", "header-only")
settings = "arch", "build_type", "compiler", "os"
no_copy_source = True

@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _supports_generic_json(self):
return tools.Version(self.version) >= "0.5.0"
return Version(self.version) >= "0.5.0"

def export_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
Expand All @@ -30,18 +30,22 @@ def requirements(self):
self.requires("picojson/1.3.0")

def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def layout(self):
basic_layout(self, src_folder="src")

def build(self):
apply_conandata_patches(self)

def package(self):
header_dir = os.path.join(self._source_subfolder, "include")
self.copy(pattern="jwt-cpp/**.h", dst="include", src=header_dir, keep_path=True)
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
header_dir = os.path.join(self.source_folder, "include", "jwt-cpp")
copy(self, pattern="*.h", dst=os.path.join(self.package_folder, "include", "jwt-cpp"), src=header_dir, keep_path=True)
copy(self, "LICENSE", dst=os.path.join(self.package_folder,"licenses"), src=self.source_folder)

def package_id(self):
self.info.header_only()
self.info.clear()

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "jwt-cpp")
Expand All @@ -50,3 +54,11 @@ def package_info(self):
# TODO: to remove in conan v2 once cmake_find_package* generators removed
self.cpp_info.names["cmake_find_package"] = "jwt-cpp"
self.cpp_info.names["cmake_find_package_multi"] = "jwt-cpp"

if self._supports_generic_json:
self.cpp_info.defines.append("JWT_DISABLE_PICOJSON")

self.cpp_info.bindirs = []
self.cpp_info.frameworkdirs = []
self.cpp_info.libdirs = []
self.cpp_info.resdirs = []
21 changes: 13 additions & 8 deletions recipes/jwt-cpp/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
cmake_minimum_required(VERSION 3.1)
project(PackageTest CXX)
cmake_minimum_required(VERSION 3.8)
project(test_package CXX)

include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_basic_setup(TARGETS)
find_package(jwt-cpp REQUIRED CONFIG)
find_package(picojson REQUIRED CONFIG)

find_package(jwt-cpp REQUIRED)
add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE jwt-cpp::jwt-cpp picojson::picojson)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)

add_executable(example example.cpp)
set_property(TARGET example PROPERTY CXX_STANDARD 11)
if (jwt-cpp_VERSION VERSION_GREATER 0.4.0)
target_compile_definitions(${PROJECT_NAME} PRIVATE JSON_TRAITS_NEEDED)
endif()

target_link_libraries(example jwt-cpp::jwt-cpp CONAN_PKG::picojson)
if (jwt-cpp_VERSION VERSION_GREATER_EQUAL 0.6.0)
target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_DEFAULT_TRAITS)
endif()
17 changes: 12 additions & 5 deletions recipes/jwt-cpp/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class JsonCppTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)
self.requires("picojson/1.3.0")

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "example")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#include <jwt-cpp/jwt.h>

#ifdef JSON_TRAITS_NEEDED
#ifndef HAS_DEFAULT_TRAITS
#include "traits/defaults.h"
#else
#include <jwt-cpp/traits/kazuho-picojson/defaults.h>
#endif
#endif

#include <iostream>

int main() {
Expand Down
58 changes: 58 additions & 0 deletions recipes/jwt-cpp/all/test_package/traits/defaults.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef JWT_CPP_KAZUHO_PICOJSON_DEFAULTS_H
#define JWT_CPP_KAZUHO_PICOJSON_DEFAULTS_H

#include "traits.h"

namespace jwt {
/**
* \brief a class to store a generic [picojson](https://github.com/kazuho/picojson) 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::kazuho_picojson>;

/**
* Create a verifier using the default clock
* \return verifier instance
*/
inline verifier<default_clock, traits::kazuho_picojson> verify() {
return verify<default_clock, traits::kazuho_picojson>(default_clock{});
}

/**
* Return a builder instance to create a new token
*/
inline builder<traits::kazuho_picojson> create() { return builder<traits::kazuho_picojson>(); }

#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::kazuho_picojson> decode(const std::string& token) {
return decoded_jwt<traits::kazuho_picojson>(token);
}
#endif

/**
* Decode a token
* \tparam Decode is callabled, 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::kazuho_picojson> decode(const std::string& token, Decode decode) {
return decoded_jwt<traits::kazuho_picojson>(token, decode);
}
} // namespace jwt

#endif // JWT_CPP_KAZUHO_PICOJSON_DEFAULTS_H
76 changes: 76 additions & 0 deletions recipes/jwt-cpp/all/test_package/traits/traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef JWT_CPP_PICOJSON_TRAITS_H
#define JWT_CPP_PICOJSON_TRAITS_H

#ifndef PICOJSON_USE_INT64
#define PICOJSON_USE_INT64
#endif
#include <picojson.h>

#ifndef JWT_DISABLE_PICOJSON
#define JWT_DISABLE_PICOJSON
#endif
#include "jwt-cpp/jwt.h"

namespace jwt {
namespace traits {
struct kazuho_picojson {
using value_type = picojson::value;
using object_type = picojson::object;
using array_type = picojson::array;
using string_type = std::string;
using number_type = double;
using integer_type = int64_t;
using boolean_type = bool;

static json::type get_type(const picojson::value& val) {
using json::type;
if (val.is<bool>()) return type::boolean;
if (val.is<int64_t>()) return type::integer;
if (val.is<double>()) return type::number;
if (val.is<std::string>()) return type::string;
if (val.is<picojson::array>()) return type::array;
if (val.is<picojson::object>()) return type::object;

throw std::logic_error("invalid type");
}

static picojson::object as_object(const picojson::value& val) {
if (!val.is<picojson::object>()) throw std::bad_cast();
return val.get<picojson::object>();
}

static std::string as_string(const picojson::value& val) {
if (!val.is<std::string>()) throw std::bad_cast();
return val.get<std::string>();
}

static picojson::array as_array(const picojson::value& val) {
if (!val.is<picojson::array>()) throw std::bad_cast();
return val.get<picojson::array>();
}

static int64_t as_int(const picojson::value& val) {
if (!val.is<int64_t>()) throw std::bad_cast();
return val.get<int64_t>();
}

static bool as_bool(const picojson::value& val) {
if (!val.is<bool>()) throw std::bad_cast();
return val.get<bool>();
}

static double as_number(const picojson::value& val) {
if (!val.is<double>()) throw std::bad_cast();
return val.get<double>();
}

static bool parse(picojson::value& val, const std::string& str) {
return picojson::parse(val, str).empty();
}

static std::string serialize(const picojson::value& val) { return val.serialize(); }
};
} // namespace traits
} // namespace jwt

#endif
20 changes: 20 additions & 0 deletions recipes/jwt-cpp/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)

include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_basic_setup(TARGETS)

find_package(jwt-cpp REQUIRED CONFIG)
find_package(picojson REQUIRED CONFIG)

add_executable(${PROJECT_NAME} ../test_package/test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE jwt-cpp::jwt-cpp picojson::picojson)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)

if (jwt-cpp_VERSION VERSION_GREATER 0.4.0)
target_compile_definitions(${PROJECT_NAME} PRIVATE JSON_TRAITS_NEEDED)
endif()

if (jwt-cpp_VERSION VERSION_GREATER_EQUAL 0.6.0)
target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_DEFAULT_TRAITS)
endif()
21 changes: 21 additions & 0 deletions recipes/jwt-cpp/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from conans import ConanFile, CMake
from conan.tools.build import cross_building
import os


class TestPackageV1Conan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def requirements(self):
self.requires("picojson/1.3.0")

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)