From de69fd2133400635cdc84231d065e806d18829be Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 09:21:52 +0100 Subject: [PATCH 01/15] build: improve cmake scripts --- .gitignore | 1 + CMakeLists.txt | 81 ++++++++++++++++++++++++++++++++--------- examples/CMakeLists.txt | 30 +++++++++------ tests/CMakeLists.txt | 16 ++++---- 4 files changed, 90 insertions(+), 38 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3d6549 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 07810da..9f1a58a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,26 +1,73 @@ -# CMakeLists files in this project can -# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and -# to the root binary directory of the project as ${HELLO_BINARY_DIR}. +cmake_minimum_required(VERSION 3.5.0) +project(cpp-jwt) -cmake_minimum_required (VERSION 2.8.11) -project (cpp-jwt) +option(CPP_JWT_BUILD_EXAMPLES "build examples" ON) +option(CPP_JWT_BUILD_TESTS "build examples" ON) -#SET (CMAKE_CXX_COMPILER /usr/local/bin/g++) -SET( CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra" ) +# only set compiler flags if we are the main project, otherwise let the main +# project decide on the flags +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra") +else() -include_directories (include) +endif() find_package(OpenSSL REQUIRED) -include_directories(${OPENSSL_INCLUDE_DIR}) -find_package(GTest REQUIRED) -include_directories(${GTEST_INCLUDE_DIRS}) +# ############################################################################## +# LIBRARY +# ############################################################################## -enable_testing() +add_library(${PROJECT_NAME} INTERFACE) +target_include_directories( + ${PROJECT_NAME} + INTERFACE $ + $) +target_link_libraries(${PROJECT_NAME} INTERFACE OpenSSL::SSL) +target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_14) -# Recurse into the "Hello" and "Demo" subdirectories. This does not actually -# cause another cmake executable to run. The same process will walk through -# the project's entire directory structure. -add_subdirectory (tests) +# ############################################################################## +# TESTS +# ############################################################################## -add_subdirectory (examples) +if(CPP_JWT_BUILD_TESTS) + find_package(GTest REQUIRED) + include_directories(${GTEST_INCLUDE_DIRS}) + enable_testing() + # Recurse into the "Hello" and "Demo" subdirectories. This does not actually + # cause another cmake executable to run. The same process will walk through + # the project's entire directory structure. + add_subdirectory(tests) +endif() + +# ############################################################################## +# EXAMPLES +# ############################################################################## + +if(CPP_JWT_BUILD_EXAMPLES) + add_subdirectory(examples) +endif() + +# ############################################################################## +# INSTALL +# ############################################################################## + +include(GNUInstallDirs) +install( + TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}_Targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/detail + DESTINATION include/jwt) +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/impl + DESTINATION include/jwt) +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/json + DESTINATION include/jwt) +install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/ + DESTINATION include/jwt + FILES_MATCHING + PATTERN "*.hpp") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 9180a01..a1e4094 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,18 +1,24 @@ - -include_directories(${OPENSSL_INCLUDE_DIR}) - -SET(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rsa_256") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"") +set(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rsa_256") +set(CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"") add_executable(simple_ex1 simple_ex1.cc) -target_link_libraries(simple_ex1 ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) -add_test(NAME simple_ex1 COMMAND ./simple_ex1 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(simple_ex1 OpenSSL::SSL ${PROJECT_NAME}) +add_test( + NAME simple_ex1 + COMMAND ./simple_ex1 + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(simple_ex2 simple_ex2.cc) -target_link_libraries(simple_ex2 ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) -add_test(NAME simple_ex2 COMMAND ./simple_ex2 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(simple_ex2 OpenSSL::SSL ${PROJECT_NAME}) +add_test( + NAME simple_ex2 + COMMAND ./simple_ex2 + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(simple_ex3_rsa simple_ex3_rsa.cc) -target_link_libraries(simple_ex3_rsa ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) -add_test(NAME simple_ex3_rsa COMMAND ./simple_ex3_rsa WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) - +target_link_libraries(simple_ex3_rsa OpenSSL::SSL ${PROJECT_NAME}) +add_test( + NAME simple_ex3_rsa + COMMAND ./simple_ex3_rsa + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1ee5201..48cf4b8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,33 +1,31 @@ -include_directories(${OPENSSL_INCLUDE_DIR}) - SET(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/certs") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"") add_executable(test_jwt_object test_jwt_object.cc) -target_link_libraries(test_jwt_object ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) +target_link_libraries(test_jwt_object OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) add_test(NAME test_jwt_object COMMAND ./test_jwt_object WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_encode test_jwt_encode.cc) -target_link_libraries(test_jwt_encode ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) +target_link_libraries(test_jwt_encode OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) add_test(NAME test_jwt_encode COMMAND ./test_jwt_encode WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_decode test_jwt_decode.cc) -target_link_libraries(test_jwt_decode ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) +target_link_libraries(test_jwt_decode OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) add_test(NAME test_jwt_decode COMMAND ./test_jwt_decode WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_decode_verifiy test_jwt_decode_verifiy.cc) -target_link_libraries(test_jwt_decode_verifiy ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) +target_link_libraries(test_jwt_decode_verifiy OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) add_test(NAME test_jwt_decode_verifiy COMMAND ./test_jwt_decode_verifiy WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_decode_verifiy_with_exception test_jwt_decode_verifiy_with_exception.cc) -target_link_libraries(test_jwt_decode_verifiy_with_exception ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) +target_link_libraries(test_jwt_decode_verifiy_with_exception OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) add_test(NAME test_jwt_decode_verifiy_with_exception COMMAND ./test_jwt_decode_verifiy_with_exception WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_rsa test_jwt_rsa.cc) -target_link_libraries(test_jwt_rsa ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES} ) +target_link_libraries(test_jwt_rsa OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) add_test(NAME test_jwt_rsa COMMAND ./test_jwt_rsa WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_es test_jwt_es.cc) -target_link_libraries(test_jwt_es ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES}) +target_link_libraries(test_jwt_es OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) add_test(NAME test_jwt_es COMMAND ./test_jwt_es WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) From 564e9f8d23f5bd9b422ca141a5edaab75574e293 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 10:29:57 +0100 Subject: [PATCH 02/15] build: add support for conan and make vendored json optional --- CMakeLists.txt | 14 +++++++++++++- README.md | 23 +++++++++++++++++++---- conanfile.txt | 10 ++++++++++ include/jwt/json/test_json.cc | 5 ++++- include/jwt/jwt.hpp | 5 ++++- 5 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 conanfile.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f1a58a..bffe31d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,11 @@ cmake_minimum_required(VERSION 3.5.0) project(cpp-jwt) option(CPP_JWT_BUILD_EXAMPLES "build examples" ON) -option(CPP_JWT_BUILD_TESTS "build examples" ON) +option(CPP_JWT_BUILD_TESTS "build tests" ON) +option(CPP_JWT_USE_VENDORED_NLOHMANN_JSON "use vendored json header" ON) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR}) # only set compiler flags if we are the main project, otherwise let the main # project decide on the flags @@ -14,6 +18,10 @@ endif() find_package(OpenSSL REQUIRED) +if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON) + find_package(nlohmann_json REQUIRED) +endif() + # ############################################################################## # LIBRARY # ############################################################################## @@ -24,6 +32,10 @@ target_include_directories( INTERFACE $ $) target_link_libraries(${PROJECT_NAME} INTERFACE OpenSSL::SSL) +if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON) + target_link_libraries(${PROJECT_NAME} INTERFACE nlohmann_json::nlohmann_json) + add_definitions(-DCPP_JWT_USE_VENDORED_NLOHMANN_JSON) +endif() target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_14) # ############################################################################## diff --git a/README.md b/README.md index 682789d..1cdff6e 100644 --- a/README.md +++ b/README.md @@ -209,12 +209,27 @@ Tested with clang-5.0 and g++-6.4. With issue#12, VS2017 is also supported. ## Installation -Use the C++ package manager..... just kidding :) -This is a header only library, so you can just add it to your include path and start using it. The only somewhat tricky part is to link it with openssl library. Check out the cmake file for building it properly. -For example one can run cmake like: +### using conan + +```shell +mkdir build +cd build +conan install .. --build missing +cmake .. +cmake --build . -j ``` -cmake -DOPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2j -DGTEST_ROOT=$HOME/googletest + +### using debian + +```shell +sudo apt install nlohmann-json3-dev +sudo apt install libgtest-dev +sudo apt install libssl-dev +mkdir build +cd build +cmake .. +cmake --build . -j ``` ## Parameters diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..b7af822 --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,10 @@ +[requires] +gtest/1.10.0 +nlohmann_json/3.7.0 +openssl/1.1.1d + +[generators] +cmake_find_package +cmake_paths + +[options] diff --git a/include/jwt/json/test_json.cc b/include/jwt/json/test_json.cc index 869d24f..d1ea502 100644 --- a/include/jwt/json/test_json.cc +++ b/include/jwt/json/test_json.cc @@ -1,7 +1,10 @@ #include #include +#if defined( CPP_JWT_USE_VENDORED_NLOHMANN_JSON) #include "./json.hpp" - +#else +#include "nlohmann/json.hpp" +#endif using json = nlohmann::json; void basic_json_test() diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 3c90549..3d00162 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -38,8 +38,11 @@ SOFTWARE. #include "jwt/string_view.hpp" #include "jwt/parameters.hpp" #include "jwt/exceptions.hpp" +#if defined(CPP_JWT_USE_VENDORED_NLOHMANN_JSON) #include "jwt/json/json.hpp" - +#else +#include "nlohmann/json.hpp" +#endif // For convenience using json_t = nlohmann::json; using system_time_t = std::chrono::time_point; From bc98be0883ac4c7da276a28334360c35dd80f570 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 11:21:17 +0100 Subject: [PATCH 03/15] test: add github pipelines --- .github/workflows/main.yml | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..3da0aa2 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,46 @@ +name: CMake + +on: [push] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: RelWithDebInfo + +jobs: + build: + + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macOS-latest] + + steps: + - uses: actions/checkout@v1 + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{runner.workspace}}/build + + - name: Configure CMake + # Use a bash shell so we can use the same syntax for environment variable + # access regardless of the host operating system + shell: bash + working-directory: ${{runner.workspace}}/build + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. + # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build + working-directory: ${{runner.workspace}}/build + shell: bash + # Execute the build. You can specify a specific target with "--target " + run: cmake --build . --config $BUILD_TYPE + + - name: Test + working-directory: ${{runner.workspace}}/build + shell: bash + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: make tests && ./tests \ No newline at end of file From 3b4ccc006d3c288e6d3ae2afc53e1d83bedc10b2 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 12:36:47 +0100 Subject: [PATCH 04/15] install conan before pipeline --- .github/workflows/main.yml | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3da0aa2..58ce079 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,30 +17,27 @@ jobs: steps: - uses: actions/checkout@v1 + - name: install install + run: pip install conan + - name: Create Build Environment - # Some projects don't allow in-source building, so create a separate build directory - # We'll use this as our working directory for all subsequent commands run: cmake -E make_directory ${{runner.workspace}}/build + - name: Install conan dependencies + working-directory: ${{runner.workspace}}/build + run: conan install .. --build missing + - name: Configure CMake - # Use a bash shell so we can use the same syntax for environment variable - # access regardless of the host operating system shell: bash working-directory: ${{runner.workspace}}/build - # Note the current convention is to use the -S and -B options here to specify source - # and build directories, but this is only available with CMake 3.13 and higher. - # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE - name: Build working-directory: ${{runner.workspace}}/build shell: bash - # Execute the build. You can specify a specific target with "--target " run: cmake --build . --config $BUILD_TYPE - name: Test working-directory: ${{runner.workspace}}/build shell: bash - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: make tests && ./tests \ No newline at end of file + run: make tests && ./tests From 6cc879005592d8dc79698dcd0a16af2d27dcae13 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 12:40:05 +0100 Subject: [PATCH 05/15] ci: use pip3 for python --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 58ce079..153b6db 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,8 +17,8 @@ jobs: steps: - uses: actions/checkout@v1 - - name: install install - run: pip install conan + - name: install conan + run: pip3 install conan - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From a4dc07112c46fa33a2b49ca47dd71643034ffb79 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 12:49:36 +0100 Subject: [PATCH 06/15] ci: install setup tools --- .github/workflows/main.yml | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 153b6db..c1dbc47 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,13 +12,32 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [windows-latest, ubuntu-latest, macOS-latest] + os: [windows-latest, ubuntu-latest] steps: - uses: actions/checkout@v1 - - name: install conan - run: pip3 install conan + - uses: actions/setup-python@v1 + with: + python-version: '3.x' + + - name: Install tools (Linux) + if: startsWith(runner.os, 'Linux') + run: | + sudo apt-get install python3-setuptools python3-wheel python3-pip + shell: bash + + - name: Install conan (Linux) + if: startsWith(runner.os, 'Linux') + run: | + sudo pip3 install conan --upgrade + shell: bash + + - name: Install conan (Windows) + if: startsWith(runner.os, 'Windows') + run: | + pip3 install conan --upgrade + shell: bash - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From 7df9127c00d568a59fb66af3435c329a622b3bea Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 12:55:12 +0100 Subject: [PATCH 07/15] chore: initialize conan profile --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c1dbc47..44f2c3b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -44,7 +44,7 @@ jobs: - name: Install conan dependencies working-directory: ${{runner.workspace}}/build - run: conan install .. --build missing + run: conan profile new default --detect && conan install .. --build missing - name: Configure CMake shell: bash From 92818726b5d15db2dd9bcaad48a468cc157f46c4 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 13:04:36 +0100 Subject: [PATCH 08/15] chore: use env variable for git workspace --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 44f2c3b..d6ade01 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -44,7 +44,7 @@ jobs: - name: Install conan dependencies working-directory: ${{runner.workspace}}/build - run: conan profile new default --detect && conan install .. --build missing + run: conan profile new default --detect && conan install $GITHUB_WORKSPACE --build missing - name: Configure CMake shell: bash From d4b5525eb9d10305e39bf90731a7e6bc5f203616 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 13:16:44 +0100 Subject: [PATCH 09/15] chore: use old lib variables for cmake --- CMakeLists.txt | 7 +++-- examples/CMakeLists.txt | 6 ++-- tests/CMakeLists.txt | 69 ++++++++++++++++++++++++++++++----------- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bffe31d..c3f2db6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ endif() find_package(OpenSSL REQUIRED) if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON) - find_package(nlohmann_json REQUIRED) + find_package(nlohmann_json REQUIRED) endif() # ############################################################################## @@ -30,8 +30,9 @@ add_library(${PROJECT_NAME} INTERFACE) target_include_directories( ${PROJECT_NAME} INTERFACE $ - $) -target_link_libraries(${PROJECT_NAME} INTERFACE OpenSSL::SSL) + $ + ${OpenSSL_INCLUDE_DIR}) +target_link_libraries(${PROJECT_NAME} INTERFACE ${OpenSSL_LIBRARIES}) if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON) target_link_libraries(${PROJECT_NAME} INTERFACE nlohmann_json::nlohmann_json) add_definitions(-DCPP_JWT_USE_VENDORED_NLOHMANN_JSON) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a1e4094..12245d9 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,21 +3,21 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"") add_executable(simple_ex1 simple_ex1.cc) -target_link_libraries(simple_ex1 OpenSSL::SSL ${PROJECT_NAME}) +target_link_libraries(simple_ex1 ${PROJECT_NAME}) add_test( NAME simple_ex1 COMMAND ./simple_ex1 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(simple_ex2 simple_ex2.cc) -target_link_libraries(simple_ex2 OpenSSL::SSL ${PROJECT_NAME}) +target_link_libraries(simple_ex2 ${PROJECT_NAME}) add_test( NAME simple_ex2 COMMAND ./simple_ex2 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(simple_ex3_rsa simple_ex3_rsa.cc) -target_link_libraries(simple_ex3_rsa OpenSSL::SSL ${PROJECT_NAME}) +target_link_libraries(simple_ex3_rsa ${PROJECT_NAME}) add_test( NAME simple_ex3_rsa COMMAND ./simple_ex3_rsa diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 48cf4b8..e423283 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,31 +1,64 @@ - -SET(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/certs") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"") +set(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/certs") +set(CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"") add_executable(test_jwt_object test_jwt_object.cc) -target_link_libraries(test_jwt_object OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) -add_test(NAME test_jwt_object COMMAND ./test_jwt_object WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_jwt_object ${GTest_LIBRARIES} ${PROJECT_NAME}) +target_include_directories(test_jwt_object PRIVATE ${GTEST_INCLUDE_DIRS}) +add_test( + NAME test_jwt_object + COMMAND ./test_jwt_object + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_encode test_jwt_encode.cc) -target_link_libraries(test_jwt_encode OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) -add_test(NAME test_jwt_encode COMMAND ./test_jwt_encode WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_jwt_encode ${GTest_LIBRARIES} ${PROJECT_NAME}) +target_include_directories(test_jwt_encode PRIVATE ${GTEST_INCLUDE_DIRS}) +add_test( + NAME test_jwt_encode + COMMAND ./test_jwt_encode + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_decode test_jwt_decode.cc) -target_link_libraries(test_jwt_decode OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) -add_test(NAME test_jwt_decode COMMAND ./test_jwt_decode WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_jwt_decode ${GTest_LIBRARIES} ${PROJECT_NAME}) +target_include_directories(test_jwt_decode PRIVATE ${GTEST_INCLUDE_DIRS}) +add_test( + NAME test_jwt_decode + COMMAND ./test_jwt_decode + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_decode_verifiy test_jwt_decode_verifiy.cc) -target_link_libraries(test_jwt_decode_verifiy OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) -add_test(NAME test_jwt_decode_verifiy COMMAND ./test_jwt_decode_verifiy WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_jwt_decode_verifiy ${GTest_LIBRARIES} + ${PROJECT_NAME}) +target_include_directories(test_jwt_decode_verifiy + PRIVATE ${GTEST_INCLUDE_DIRS}) +add_test( + NAME test_jwt_decode_verifiy + COMMAND ./test_jwt_decode_verifiy + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -add_executable(test_jwt_decode_verifiy_with_exception test_jwt_decode_verifiy_with_exception.cc) -target_link_libraries(test_jwt_decode_verifiy_with_exception OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) -add_test(NAME test_jwt_decode_verifiy_with_exception COMMAND ./test_jwt_decode_verifiy_with_exception WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +add_executable(test_jwt_decode_verifiy_with_exception + test_jwt_decode_verifiy_with_exception.cc) +target_link_libraries(test_jwt_decode_verifiy_with_exception ${GTest_LIBRARIES} + ${PROJECT_NAME}) +target_include_directories(test_jwt_decode_verifiy_with_exception + PRIVATE ${GTEST_INCLUDE_DIRS}) +add_test( + NAME test_jwt_decode_verifiy_with_exception + COMMAND ./test_jwt_decode_verifiy_with_exception + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_rsa test_jwt_rsa.cc) -target_link_libraries(test_jwt_rsa OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) -add_test(NAME test_jwt_rsa COMMAND ./test_jwt_rsa WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_jwt_rsa ${GTest_LIBRARIES} ${PROJECT_NAME}) +target_include_directories(test_jwt_rsa PRIVATE ${GTEST_INCLUDE_DIRS}) +add_test( + NAME test_jwt_rsa + COMMAND ./test_jwt_rsa + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_executable(test_jwt_es test_jwt_es.cc) -target_link_libraries(test_jwt_es OpenSSL::SSL GTest::GTest GTest::Main ${PROJECT_NAME}) -add_test(NAME test_jwt_es COMMAND ./test_jwt_es WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(test_jwt_es ${GTest_LIBRARIES} ${PROJECT_NAME}) +target_include_directories(test_jwt_es PRIVATE ${GTEST_INCLUDE_DIRS}) +add_test( + NAME test_jwt_es + COMMAND ./test_jwt_es + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) From 72c26dda67004b2f96ac4e41d4ad05cced6c041f Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 13:18:06 +0100 Subject: [PATCH 10/15] ci: fix windows build --- .github/workflows/main.yml | 95 ++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d6ade01..d8a4f73 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,55 +8,58 @@ env: jobs: build: - runs-on: ${{ matrix.os }} strategy: matrix: os: [windows-latest, ubuntu-latest] steps: - - uses: actions/checkout@v1 - - - uses: actions/setup-python@v1 - with: - python-version: '3.x' - - - name: Install tools (Linux) - if: startsWith(runner.os, 'Linux') - run: | - sudo apt-get install python3-setuptools python3-wheel python3-pip - shell: bash - - - name: Install conan (Linux) - if: startsWith(runner.os, 'Linux') - run: | - sudo pip3 install conan --upgrade - shell: bash - - - name: Install conan (Windows) - if: startsWith(runner.os, 'Windows') - run: | - pip3 install conan --upgrade - shell: bash - - - name: Create Build Environment - run: cmake -E make_directory ${{runner.workspace}}/build - - - name: Install conan dependencies - working-directory: ${{runner.workspace}}/build - run: conan profile new default --detect && conan install $GITHUB_WORKSPACE --build missing - - - name: Configure CMake - shell: bash - working-directory: ${{runner.workspace}}/build - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE - - - name: Build - working-directory: ${{runner.workspace}}/build - shell: bash - run: cmake --build . --config $BUILD_TYPE - - - name: Test - working-directory: ${{runner.workspace}}/build - shell: bash - run: make tests && ./tests + - uses: actions/checkout@v1 + + - uses: actions/setup-python@v1 + with: + python-version: "3.x" + + - name: Install tools (Linux) + if: startsWith(runner.os, 'Linux') + run: | + sudo apt-get install python3-setuptools python3-wheel python3-pip + shell: bash + + - name: Install conan (Linux) + if: startsWith(runner.os, 'Linux') + run: | + sudo pip3 install conan --upgrade + shell: bash + + - name: Install conan (Windows) + if: startsWith(runner.os, 'Windows') + run: | + pip3 install conan --upgrade + shell: bash + + - name: Create Build Environment + run: cmake -E make_directory ${{runner.workspace}}/build + + - name: Install conan profile + working-directory: ${{runner.workspace}}/build + run: conan profile new default --detect + + - name: Install conan dependencies + working-directory: ${{runner.workspace}}/build + run: conan install $GITHUB_WORKSPACE --build missing + + - name: Configure CMake + shell: bash + working-directory: ${{runner.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build + working-directory: ${{runner.workspace}}/build + shell: bash + run: cmake --build . --config $BUILD_TYPE + + - name: Test + working-directory: ${{runner.workspace}}/build + shell: bash + run: make tests && ./tests From f43b6e93fafc5342224eadd14a6c0fc449a304d6 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 13:26:13 +0100 Subject: [PATCH 11/15] chore: use both options for gtest include dir variable --- .github/workflows/main.yml | 1 + tests/CMakeLists.txt | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d8a4f73..30fbc69 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -48,6 +48,7 @@ jobs: - name: Install conan dependencies working-directory: ${{runner.workspace}}/build run: conan install $GITHUB_WORKSPACE --build missing + shell: bash - name: Configure CMake shell: bash diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e423283..763dcb3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,7 +4,8 @@ set(CMAKE_CXX_FLAGS add_executable(test_jwt_object test_jwt_object.cc) target_link_libraries(test_jwt_object ${GTest_LIBRARIES} ${PROJECT_NAME}) -target_include_directories(test_jwt_object PRIVATE ${GTEST_INCLUDE_DIRS}) +target_include_directories(test_jwt_object PRIVATE ${GTEST_INCLUDE_DIRS} + ${GTest_INCLUDE_DIRS}) add_test( NAME test_jwt_object COMMAND ./test_jwt_object @@ -12,7 +13,8 @@ add_test( add_executable(test_jwt_encode test_jwt_encode.cc) target_link_libraries(test_jwt_encode ${GTest_LIBRARIES} ${PROJECT_NAME}) -target_include_directories(test_jwt_encode PRIVATE ${GTEST_INCLUDE_DIRS}) +target_include_directories(test_jwt_encode PRIVATE ${GTEST_INCLUDE_DIRS} + ${GTest_INCLUDE_DIRS}) add_test( NAME test_jwt_encode COMMAND ./test_jwt_encode @@ -20,7 +22,8 @@ add_test( add_executable(test_jwt_decode test_jwt_decode.cc) target_link_libraries(test_jwt_decode ${GTest_LIBRARIES} ${PROJECT_NAME}) -target_include_directories(test_jwt_decode PRIVATE ${GTEST_INCLUDE_DIRS}) +target_include_directories(test_jwt_decode PRIVATE ${GTEST_INCLUDE_DIRS} + ${GTest_INCLUDE_DIRS}) add_test( NAME test_jwt_decode COMMAND ./test_jwt_decode @@ -30,7 +33,7 @@ add_executable(test_jwt_decode_verifiy test_jwt_decode_verifiy.cc) target_link_libraries(test_jwt_decode_verifiy ${GTest_LIBRARIES} ${PROJECT_NAME}) target_include_directories(test_jwt_decode_verifiy - PRIVATE ${GTEST_INCLUDE_DIRS}) + PRIVATE ${GTEST_INCLUDE_DIRS} ${GTest_INCLUDE_DIRS}) add_test( NAME test_jwt_decode_verifiy COMMAND ./test_jwt_decode_verifiy @@ -41,7 +44,7 @@ add_executable(test_jwt_decode_verifiy_with_exception target_link_libraries(test_jwt_decode_verifiy_with_exception ${GTest_LIBRARIES} ${PROJECT_NAME}) target_include_directories(test_jwt_decode_verifiy_with_exception - PRIVATE ${GTEST_INCLUDE_DIRS}) + PRIVATE ${GTEST_INCLUDE_DIRS} ${GTest_INCLUDE_DIRS}) add_test( NAME test_jwt_decode_verifiy_with_exception COMMAND ./test_jwt_decode_verifiy_with_exception @@ -49,7 +52,8 @@ add_test( add_executable(test_jwt_rsa test_jwt_rsa.cc) target_link_libraries(test_jwt_rsa ${GTest_LIBRARIES} ${PROJECT_NAME}) -target_include_directories(test_jwt_rsa PRIVATE ${GTEST_INCLUDE_DIRS}) +target_include_directories(test_jwt_rsa PRIVATE ${GTEST_INCLUDE_DIRS} + ${GTest_INCLUDE_DIRS}) add_test( NAME test_jwt_rsa COMMAND ./test_jwt_rsa @@ -57,7 +61,8 @@ add_test( add_executable(test_jwt_es test_jwt_es.cc) target_link_libraries(test_jwt_es ${GTest_LIBRARIES} ${PROJECT_NAME}) -target_include_directories(test_jwt_es PRIVATE ${GTEST_INCLUDE_DIRS}) +target_include_directories(test_jwt_es PRIVATE ${GTEST_INCLUDE_DIRS} + ${GTest_INCLUDE_DIRS}) add_test( NAME test_jwt_es COMMAND ./test_jwt_es From 9920f88379aae6de279d5558b34164cffada9096 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 13:33:43 +0100 Subject: [PATCH 12/15] chore: define vendor macro if needed --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c3f2db6..569cbc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ target_include_directories( target_link_libraries(${PROJECT_NAME} INTERFACE ${OpenSSL_LIBRARIES}) if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON) target_link_libraries(${PROJECT_NAME} INTERFACE nlohmann_json::nlohmann_json) +else() add_definitions(-DCPP_JWT_USE_VENDORED_NLOHMANN_JSON) endif() target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_14) From ac31850cdcb04a6ab399b8c9c0c3f7e963fb6972 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 13:38:11 +0100 Subject: [PATCH 13/15] chore: use cpp 11 by default --- .github/workflows/main.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 30fbc69..bac67c0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -45,6 +45,12 @@ jobs: working-directory: ${{runner.workspace}}/build run: conan profile new default --detect + - name: Use cpp 11 (Linux) + if: startsWith(runner.os, 'Linux') + run: | + conan profile update settings.compiler.libcxx=libstdc++11 default + shell: bash + - name: Install conan dependencies working-directory: ${{runner.workspace}}/build run: conan install $GITHUB_WORKSPACE --build missing From f7ffc00a29e5fd61cfa963fbe4370a45e1de28b0 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 14:58:03 +0100 Subject: [PATCH 14/15] chore: use w4 for msvc --- CMakeLists.txt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 569cbc7..0200bd6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,16 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR}) # only set compiler flags if we are the main project, otherwise let the main # project decide on the flags if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra") -else() + set(CMAKE_CXX_STANDARD 14) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" + MATCHES "Clang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") + endif() + + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() endif() From 4dd70e5935961de86a84d7992a08923ea0efbce3 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 24 Feb 2020 15:07:29 +0100 Subject: [PATCH 15/15] test: add ctest --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bac67c0..87ea7b0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -69,4 +69,4 @@ jobs: - name: Test working-directory: ${{runner.workspace}}/build shell: bash - run: make tests && ./tests + run: ctest --verbose