This commit is contained in:
Arun M 2020-06-13 12:40:18 +05:30
commit 701f4f26c1
24 changed files with 416 additions and 200 deletions

72
.github/workflows/main.yml vendored Normal file
View file

@ -0,0 +1,72 @@
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]
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 profile
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
shell: bash
- 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: ctest --verbose

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/build/

View file

@ -1,26 +1,96 @@
# 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 2.8.11)
cmake_minimum_required(VERSION 3.5.0)
project(cpp-jwt)
#SET (CMAKE_CXX_COMPILER /usr/local/bin/g++)
SET( CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra" )
option(CPP_JWT_BUILD_EXAMPLES "build examples" ON)
option(CPP_JWT_BUILD_TESTS "build tests" ON)
option(CPP_JWT_USE_VENDORED_NLOHMANN_JSON "use vendored json header" ON)
include_directories (include)
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
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
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()
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON)
find_package(nlohmann_json REQUIRED)
endif()
# ##############################################################################
# LIBRARY
# ##############################################################################
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(
${PROJECT_NAME}
INTERFACE $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
${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)
else()
add_definitions(-DCPP_JWT_USE_VENDORED_NLOHMANN_JSON)
endif()
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_14)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
# ##############################################################################
# TESTS
# ##############################################################################
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")

View file

@ -78,7 +78,7 @@ Few good resources on this material which I found useful are:
std::cout << enc_str << std::endl;
//Decode
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret(key));
std::cout << dec_obj.header() << std::endl;
std::cout << dec_obj.payload() << std::endl;
@ -109,7 +109,7 @@ Few good resources on this material which I found useful are:
int main() {
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"user", "admin"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"user", "admin"}})};
//Use add_claim API to add claim values which are
// _not_ strings.
@ -209,12 +209,27 @@ Tested with <strong>clang-5.0</strong> and <strong>g++-6.4</strong>.
With issue#12, <strong>VS2017</strong> 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
@ -313,17 +328,17 @@ All the parameters are basically a function which returns an instance of a type
- Takes in any type which satifies the <strong>SequenceConcept</strong> (see <code>idetail::meta::is_sequence_concept</code>)
```cpp
jwt::decode(algorithms({"none", "hs256", "rs256"}), ...);
jwt::decode(algorithms({"none", "HS256", "RS256"}), ...);
OR
std::vector<std::string> algs{"none", "hs256", "rs256"};
std::vector<std::string> algs{"none", "HS256", "RS256"};
jwt::decode(algorithms(algs), ...);
```
- <strong>secret</strong>
Optional parameter. To be supplied only when the algorithm used is not "NONE". Else would throw/set <code>KeyNotPresentError</code> / <code>KeyNotPresent</code> exception/error.
Optional parameter. To be supplied only when the algorithm used is not "none". Else would throw/set <code>KeyNotPresentError</code> / <code>KeyNotPresent</code> exception/error.
- <strong>leeway</strong>
@ -406,7 +421,7 @@ Expiration verification example (uses error_code):
int main() {
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
;
@ -415,7 +430,7 @@ int main() {
auto enc_str = obj.signature(ec);
assert (!ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), verify(true));
assert (ec);
assert (ec.value() == static_cast<int>(jwt::VerificationErrc::TokenExpired));
@ -432,7 +447,7 @@ Expiration verification example (uses exception):
int main() {
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
@ -441,7 +456,7 @@ int main() {
auto enc_str = obj.signature();
try {
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true));
} catch (const jwt::TokenExpiredError& e) {
//Handle Token expired exception here
//...
@ -472,13 +487,13 @@ Invalid issuer test(uses error_code):
int main() {
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
std::error_code ec;
auto enc_str = obj.signature(ec);
assert (!ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), issuer("arun.muralidharan"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), issuer("arun.muralidharan"));
assert (ec);
assert (ec.value() == static_cast<int>(jwt::VerificationErrc::InvalidIssuer));
@ -588,7 +603,7 @@ There are exception types created for almost all the error codes above.
- KeyNotPresentError
Thrown if key/secret is not passed in with the decode API if the algorithm used is something other than "NONE".
Thrown if key/secret is not passed in with the decode API if the algorithm used is something other than "none".
- VerificationError

10
conanfile.txt Normal file
View file

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

View file

@ -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 ${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 ${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 ${PROJECT_NAME})
add_test(
NAME simple_ex3_rsa
COMMAND ./simple_ex3_rsa
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

View file

@ -6,7 +6,7 @@
int main() {
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"user", "admin"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"user", "admin"}})};
//Use add_claim API to add claim values which are
// _not_ strings.

View file

@ -44,7 +44,7 @@ int main() {
auto priv_key = read_from_file(priv_key_path);
jwt::jwt_object obj{algorithm("rs256"), secret(priv_key), payload({{"user", "admin"}})};
jwt::jwt_object obj{algorithm("RS256"), secret(priv_key), payload({{"user", "admin"}})};
//Use add_claim API to add claim values which are
// _not_ strings.
@ -76,7 +76,7 @@ int main() {
return 1;
}
auto dec_obj = jwt::decode(sign, algorithms({"rs256"}), verify(false), secret(pub_key));
auto dec_obj = jwt::decode(sign, algorithms({"RS256"}), verify(false), secret(pub_key));
return 0;
}

View file

@ -235,7 +235,7 @@ inline jwt::string_view alg_to_str(SCOPED_ENUM algorithm alg) noexcept
case algorithm::ES384: return "ES384";
case algorithm::ES512: return "ES512";
case algorithm::TERM: return "TERM";
case algorithm::NONE: return "NONE";
case algorithm::NONE: return "none";
case algorithm::UNKN: return "UNKN";
default: assert (0 && "Unknown Algorithm");
};
@ -249,18 +249,18 @@ inline jwt::string_view alg_to_str(SCOPED_ENUM algorithm alg) noexcept
*/
inline SCOPED_ENUM algorithm str_to_alg(const jwt::string_view alg) noexcept
{
if (!alg.length()) return algorithm::NONE;
if (!alg.length()) return algorithm::UNKN;
if (!strcasecmp(alg.data(), "none")) return algorithm::NONE;
if (!strcasecmp(alg.data(), "hs256")) return algorithm::HS256;
if (!strcasecmp(alg.data(), "hs384")) return algorithm::HS384;
if (!strcasecmp(alg.data(), "hs512")) return algorithm::HS512;
if (!strcasecmp(alg.data(), "rs256")) return algorithm::RS256;
if (!strcasecmp(alg.data(), "rs384")) return algorithm::RS384;
if (!strcasecmp(alg.data(), "rs512")) return algorithm::RS512;
if (!strcasecmp(alg.data(), "es256")) return algorithm::ES256;
if (!strcasecmp(alg.data(), "es384")) return algorithm::ES384;
if (!strcasecmp(alg.data(), "es512")) return algorithm::ES512;
if (!strcasecmp(alg.data(), "HS256")) return algorithm::HS256;
if (!strcasecmp(alg.data(), "HS384")) return algorithm::HS384;
if (!strcasecmp(alg.data(), "HS512")) return algorithm::HS512;
if (!strcasecmp(alg.data(), "RS256")) return algorithm::RS256;
if (!strcasecmp(alg.data(), "RS384")) return algorithm::RS384;
if (!strcasecmp(alg.data(), "RS512")) return algorithm::RS512;
if (!strcasecmp(alg.data(), "ES256")) return algorithm::ES256;
if (!strcasecmp(alg.data(), "ES384")) return algorithm::ES384;
if (!strcasecmp(alg.data(), "ES512")) return algorithm::ES512;
return algorithm::UNKN;

View file

@ -92,7 +92,7 @@ inline std::string base64_encode(const char* in, size_t len)
const auto encoded_siz = encoding_size(len);
result.resize(encoded_siz);
constexpr static const EMap emap{};
constexpr static const EMap emap;
int i = 0;
int j = 0;

View file

@ -29,7 +29,7 @@
// To hack around Visual Studio error:
// error C3431: 'algorithm': a scoped enumeration cannot be redeclared as an unscoped enumeration
#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
#define SCOPED_ENUM enum class
#else
#define SCOPED_ENUM enum

View file

@ -1,7 +1,10 @@
#include <iostream>
#include <string>
#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()

View file

@ -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<std::chrono::system_clock>;

View file

@ -23,7 +23,7 @@ SOFTWARE.
#ifndef JWT_STRING_VIEW_HPP
#define JWT_STRING_VIEW_HPP
#if __cplusplus >= 201703L
#if defined(__cpp_lib_string_view)
#include <string_view>
@ -31,7 +31,7 @@ namespace jwt {
using string_view = std::string_view;
}
#else // __cplusplus >= 201703L
#else // defined(__cpp_lib_string_view)
#include <limits>
#include <string>
@ -375,6 +375,6 @@ using string_view = basic_string_view<char>;
#include "jwt/impl/string_view.ipp"
#endif // __cplusplus >= 201703L
#endif // defined(__cpp_lib_string_view)
#endif

View file

@ -21,7 +21,7 @@ void basic_decode_test()
using namespace jwt::params;
std::cout << "DECODE: \n";
jwt::decode(res, algorithms({"none", "hs256"}), ec, verify(false), secret("secret"));
jwt::decode(res, algorithms({"none", "HS256"}), ec, verify(false), secret("secret"));
}
int main() {

View file

@ -43,9 +43,9 @@ void basic_jwt_object_test()
std::cout << obj3.payload() << std::endl;
obj3.secret("secret");
obj3.header().algo("hs256");
obj3.header().algo("HS256");
auto dec_obj = jwt::decode(obj3.signature(), algorithms({"hs256"}), secret("secret"));
auto dec_obj = jwt::decode(obj3.signature(), algorithms({"HS256"}), secret("secret"));
}
void jwt_object_pem_test()
@ -82,7 +82,7 @@ MIGkAgEBBDBeLCgapjZmvTatMHaYX3A02+0Ys3Tr8kda+E9DFnmCSiCOEig519fT
std::cout << "Get claim value for exp: " <<
obj.payload().get_claim_value<uint64_t>("exp") << std::endl;
auto dec_obj = jwt::decode(obj.signature(), algorithms({"es256"}), secret(pub_key));
auto dec_obj = jwt::decode(obj.signature(), algorithms({"ES256"}), secret(pub_key));
std::cout << dec_obj.payload() << std::endl;
}

View file

@ -1,33 +1,69 @@
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}\\\"\"")
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})
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}
${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_LIBRARIES} ${GTEST_LIBRARIES})
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}
${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_LIBRARIES} ${GTEST_LIBRARIES})
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}
${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_LIBRARIES} ${GTEST_LIBRARIES})
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} ${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_LIBRARIES} ${GTEST_LIBRARIES})
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} ${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_LIBRARIES} ${GTEST_LIBRARIES} )
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}
${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_LIBRARIES} ${GTEST_LIBRARIES})
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}
${GTest_INCLUDE_DIRS})
add_test(
NAME test_jwt_es
COMMAND ./test_jwt_es
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

View file

@ -9,7 +9,7 @@ TEST (DecodeTest, InvalidFinalDotForNoneAlg)
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ";
std::error_code ec;
auto obj = jwt::decode(inv_enc_str, algorithms({"none", "hs256"}), ec);
auto obj = jwt::decode(inv_enc_str, algorithms({"none", "HS256"}), ec);
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::SignatureFormatError));
@ -45,7 +45,7 @@ TEST (DecodeTest, DecodeWrongAlgo)
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret(""), verify(true));
auto obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret(""), verify(true));
EXPECT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::InvalidAlgorithm));
}
@ -58,7 +58,7 @@ TEST (DecodeTest, DecodeInvalidHeader)
"ehbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret(""), verify(true));
auto obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret(""), verify(true));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::JsonParseError));
@ -72,7 +72,7 @@ TEST (DecodeTest, DecodeEmptyHeader)
".eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret(""), verify(true));
auto obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret(""), verify(true));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::JsonParseError));
@ -102,7 +102,7 @@ TEST (DecodeTest, DecodeHS256)
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(false), secret("secret"));
auto obj = jwt::decode(enc_str, algorithms({"none", "HS256"}), ec, verify(false), secret("secret"));
ASSERT_FALSE (ec);
EXPECT_TRUE (obj.has_claim("iss"));
@ -125,7 +125,7 @@ TEST (DecodeTest, SecretKeyNotPassed)
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(true));
auto obj = jwt::decode(enc_str, algorithms({"none", "HS256"}), ec, verify(true));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::KeyNotPresent));
@ -143,7 +143,7 @@ TEST (DecodeTest, DecodeHS384)
const jwt::string_view key = "0123456789abcdefghijklmnopqrstuvwxyz";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"none", "hs384"}), ec, verify(false), secret(key));
auto obj = jwt::decode(enc_str, algorithms({"none", "HS384"}), ec, verify(false), secret(key));
ASSERT_FALSE (ec);
EXPECT_TRUE (obj.has_claim("sub"));
@ -162,7 +162,7 @@ TEST (DecodeTest, DecodeHS512)
const jwt::string_view key = "00112233445566778899";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"none", "hs384", "hs512"}), ec, verify(false), secret(key));
auto obj = jwt::decode(enc_str, algorithms({"none", "HS384", "HS512"}), ec, verify(false), secret(key));
ASSERT_FALSE (ec);
@ -180,7 +180,7 @@ TEST (DecodeTest, TypHeaderMiss)
"pMWBLSWl1p4V958lfe_6ZhvgFMOQv9Eq5mlndVKFKkA";
std::error_code ec;
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(false));
auto obj = jwt::decode(enc_str, algorithms({"none", "HS256"}), ec, verify(false));
std::cout << "Decode header: " << obj.header() << std::endl;
EXPECT_FALSE (ec);

View file

@ -9,7 +9,7 @@ TEST (DecodeVerify, BeforeExpiryTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() + std::chrono::seconds{10})
;
@ -19,7 +19,7 @@ TEST (DecodeVerify, BeforeExpiryTest)
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), verify(true));
ASSERT_FALSE (ec);
}
@ -27,7 +27,7 @@ TEST (DecodeVerify, AfterExpiryTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
;
@ -36,7 +36,7 @@ TEST (DecodeVerify, AfterExpiryTest)
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), verify(true));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::TokenExpired));
}
@ -45,7 +45,7 @@ TEST (DecodeVerify, AfterExpiryWithLeeway)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
;
@ -54,7 +54,7 @@ TEST (DecodeVerify, AfterExpiryWithLeeway)
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true), leeway(2));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), verify(true), leeway(2));
ASSERT_FALSE (ec);
}
@ -62,7 +62,7 @@ TEST (DecodeVerify, ValidIssuerTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("sub", "test")
;
@ -71,7 +71,7 @@ TEST (DecodeVerify, ValidIssuerTest)
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), issuer("arun.muralidharan"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), issuer("arun.muralidharan"));
ASSERT_FALSE (ec);
}
@ -79,12 +79,12 @@ TEST (DecodeVerify, InvalidIssuerTest_1)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
std::error_code ec;
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), issuer("arun.muralidharan"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), issuer("arun.muralidharan"));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::InvalidIssuer));
@ -94,14 +94,14 @@ TEST (DecodeVerify, InvalidIssuerTest_2)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim("iss", "arun.muralidharan");
std::error_code ec;
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), issuer("arun.murali"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), issuer("arun.murali"));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::InvalidIssuer));
}
@ -110,14 +110,14 @@ TEST (DecodeVerify, NotImmatureSignatureTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() - std::chrono::seconds{10});
std::error_code ec;
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"));
ASSERT_FALSE (ec);
}
@ -125,14 +125,14 @@ TEST (DecodeVerify, ImmatureSignatureTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10});
std::error_code ec;
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::ImmatureSignature));
}
@ -141,14 +141,14 @@ TEST (DecodeVerify, ImmatureSignatureTestWithLeeway)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10});
std::error_code ec;
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), leeway(10));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), leeway(10));
ASSERT_FALSE (ec);
}
@ -156,13 +156,13 @@ TEST (DecodeVerify, InvalidAudienceTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
std::error_code ec;
auto enc_str = obj.signature(ec);
ASSERT_FALSE (ec);
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), aud("ww"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), aud("ww"));
ASSERT_TRUE (ec);
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::InvalidAudience));
}
@ -171,13 +171,13 @@ TEST (DecodeVerify, InvalidIATTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
obj.add_claim("iat", "what?");
auto enc_str = obj.signature();
std::error_code ec;
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), validate_iat(true));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), validate_iat(true));
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::TypeConversionError));
}
@ -186,11 +186,11 @@ TEST (DecodeVerify, InvalidSignatureTest)
using namespace jwt::params;
std::error_code ec;
auto dec_obj = jwt::decode("", algorithms({"hs256"}), ec, secret("secret"), validate_iat(true));
auto dec_obj = jwt::decode("", algorithms({"HS256"}), ec, secret("secret"), validate_iat(true));
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::SignatureFormatError));
ec.clear();
dec_obj = jwt::decode("abcdsdfhbsdhjfbsdj.", algorithms({"hs256"}), ec, secret("secret"), validate_iat(true));
dec_obj = jwt::decode("abcdsdfhbsdhjfbsdj.", algorithms({"HS256"}), ec, secret("secret"), validate_iat(true));
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::SignatureFormatError));
}

View file

@ -9,28 +9,28 @@ TEST (DecodeVerifyExp, BeforeExpiryTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() + std::chrono::seconds{10})
;
auto enc_str = obj.signature();
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true));
}
TEST (DecodeVerifyExp, AfterExpiryTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
;
auto enc_str = obj.signature();
EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true)),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true)),
jwt::TokenExpiredError);
}
@ -39,13 +39,13 @@ TEST (DecodeVerifyExp, AfterExpiryWithLeeway)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
;
auto enc_str = obj.signature();
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true), leeway(2));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true), leeway(2));
(void)dec_obj;
}
@ -53,14 +53,14 @@ TEST (DecodeVerifyExp, ValidIssuerTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("sub", "test")
;
auto enc_str = obj.signature();
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), issuer("arun.muralidharan"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), issuer("arun.muralidharan"));
(void)dec_obj;
}
@ -68,10 +68,10 @@ TEST (DecodeVerifyExp, InvalidIssuerTest_1)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
auto enc_str = obj.signature();
EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), issuer("arun.muralidharan")),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), issuer("arun.muralidharan")),
jwt::InvalidIssuerError);
}
@ -80,12 +80,12 @@ TEST (DecodeVerifyExp, InvalidIssuerTest_2)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim("iss", "arun.muralidharan");
auto enc_str = obj.signature();
EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), issuer("arun.murali")),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), issuer("arun.murali")),
jwt::InvalidIssuerError);
}
@ -93,12 +93,12 @@ TEST (DecodeVerifyExp, NotImmatureSignatureTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() - std::chrono::seconds{10});
auto enc_str = obj.signature();
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"));
(void)dec_obj;
}
@ -106,12 +106,12 @@ TEST (DecodeVerifyExp, ImmatureSignatureTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10});
auto enc_str = obj.signature();
EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret")),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret")),
jwt::ImmatureSignatureError);
}
@ -119,12 +119,12 @@ TEST (DecodeVerifyExp, ImmatureSignatureTestWithLeeway)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10});
auto enc_str = obj.signature();
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), leeway(10));
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), leeway(10));
(void)dec_obj;
}
@ -132,11 +132,11 @@ TEST (DecodeVerifyExp, InvalidAudienceTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
auto enc_str = obj.signature();
EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), aud("ww")),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), aud("ww")),
jwt::InvalidAudienceError);
}
@ -147,7 +147,7 @@ TEST (DecodeVerifyExp, InvalidSignatureTest)
const char* inv_enc_str =
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ";
EXPECT_THROW (jwt::decode(inv_enc_str, algorithms({"none", "hs256"})),
EXPECT_THROW (jwt::decode(inv_enc_str, algorithms({"none", "HS256"})),
jwt::SignatureFormatError);
}
@ -160,7 +160,7 @@ TEST (DecodeVerifyExp, KeyNotPresentTest)
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "hs256"}), verify(true)),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "HS256"}), verify(true)),
jwt::KeyNotPresentError);
}
@ -168,11 +168,11 @@ TEST (DecodeVerifyExp, InvalidSubjectTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})};
auto enc_str = obj.signature();
EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), sub("TEST")),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), sub("TEST")),
jwt::InvalidSubjectError);
}

View file

@ -8,7 +8,7 @@ TEST (EncodeTest, TestRemoveClaim)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("sub", "admin")
@ -29,7 +29,7 @@ TEST (EncodeTest, TestRemoveTypHeader)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("sub", "admin")
@ -54,7 +54,7 @@ TEST (EncodeTest, StrEncodeHS256_1)
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("sub", "admin")
@ -242,7 +242,7 @@ TEST (EncodeTest, StrEncodeNoneAlgWithKey)
const jwt::string_view secret1 = "abcdefghijklmnopqrstuvwxyz";
const jwt::string_view secret2 = "0123456789qwertybabe";
jwt::jwt_object obj{algorithm("NONE"),
jwt::jwt_object obj{algorithm("none"),
payload({{"iss", "arn-ml"}}),
secret(secret1)};
@ -261,7 +261,7 @@ TEST (EncodeTest, OverwriteClaimsTest)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("NONE"),
jwt::jwt_object obj{algorithm("none"),
payload({
{"iss", "arn-ml"},
{"x-pld1", "data1"},

View file

@ -53,7 +53,7 @@ TEST (ESAlgo, ES256EncodingDecodingTest)
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"es256"}), ec, verify(false), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"ES256"}), ec, verify(false), secret(key));
EXPECT_FALSE (ec);
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES256);
@ -83,7 +83,7 @@ TEST (ESAlgo, ES384EncodingDecodingTest)
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"es384"}), verify(false), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"ES384"}), verify(false), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES384);
}
@ -107,7 +107,7 @@ TEST (ESAlgo, ES512EncodingDecodingTest)
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"es512"}), verify(false), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"ES512"}), verify(false), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES512);
}
@ -131,7 +131,7 @@ TEST (ESAlgo, ES384EncodingDecodingValidTest)
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"es384"}), verify(true), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"ES384"}), verify(true), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES384);
EXPECT_TRUE (dec_obj.has_claim("exp"));
@ -143,7 +143,7 @@ TEST (ESAlgo, ES384EncodingDecodingValidTest)
auto iss = payload.get_claim_value<std::string>("iss");
return keystore[iss];
};
auto dec_obj2 = jwt::decode(enc_str, algorithms({"es384"}), verify(true), secret(l));
auto dec_obj2 = jwt::decode(enc_str, algorithms({"ES384"}), verify(true), secret(l));
EXPECT_EQ (dec_obj2.header().algo(), jwt::algorithm::ES384);
}

View file

@ -17,7 +17,7 @@ TEST (ObjectTest, MoveConstructor)
{
using namespace jwt::params;
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
obj.add_claim("iss", "arun.muralidharan");

View file

@ -63,7 +63,7 @@ TEST (RSAAlgo, RSA256EncodingDecodingTest)
key = read_from_file(RSA256_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"rs256"}), ec, verify(false), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"RS256"}), ec, verify(false), secret(key));
EXPECT_FALSE (ec);
}
@ -91,7 +91,7 @@ TEST (RSAAlgo, RSA384EncodingDecodingTest)
key = read_from_file(RSA384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"none", "hs384", "rs384"}), verify(false), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"none", "HS384", "RS384"}), verify(false), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::RS384);
}
@ -118,7 +118,7 @@ TEST (RSAAlgo, RSA512EncodingDecodingTest)
key = read_from_file(RSA512_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"none", "hs384", "rs512"}), verify(false), secret(key));
auto dec_obj = jwt::decode(enc_str, algorithms({"none", "HS384", "RS512"}), verify(false), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::RS512);
}
@ -139,7 +139,7 @@ TEST (RSAAlgo, NoSpecificAlgo)
key = read_from_file(RSA512_PUB_KEY);
ASSERT_TRUE (key.length());
EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "hs384", "rs384"}), verify(true), secret(key)),
EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "HS384", "RS384"}), verify(true), secret(key)),
jwt::InvalidAlgorithmError);
}