Skip to content

Commit

Permalink
[skip ci] more progress
Browse files Browse the repository at this point in the history
  • Loading branch information
prince-chrismc committed May 20, 2020
1 parent f5e3716 commit 767c3fc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
46 changes: 31 additions & 15 deletions include/jwt-cpp/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ namespace jwt {

namespace details{
struct picojson_traits{
json::type get_type(const picojson::value& val) {
static json::type get_type(const picojson::value& val) {
using json::type;

if (val.is<picojson::null>()) return type::null;
Expand Down Expand Up @@ -880,7 +880,7 @@ namespace jwt {

template<typename Iterator>
basic_claim(Iterator begin, Iterator end)
: val(typename traits::array(begin, end))
: val(array_type(begin, end))
{}

/**
Expand Down Expand Up @@ -968,7 +968,7 @@ namespace jwt {
* \return content as double
* \throws std::bad_cast Content was not a number
*/
typename traits::number as_number() const {
number_type as_number() const {
return traits::as_number(val);
}
};
Expand All @@ -979,19 +979,17 @@ namespace jwt {
using claim = basic_claim<>;

#define JWT_BASIC_CLAIM_TPL_DECLARATION_TYPES \
typename value_type, \
class object_type, \
class array_type, \
class string_type, class boolean_type, class number_type, \
typename value_type, class object_type, class array_type, \
class string_type, class boolean_type, \
class integer_type, class number_type, \
typename traits


#define JWT_BASIC_CLAIM_TPL_DECLARATION \
template<JWT_BASIC_CLAIM_TPL_DECLARATION_TYPES>

#define JWT_BASIC_CLAIM_TPL \
value_type, object_type, array_type, string_type, \
boolean_type, number_type, traits
boolean_type, integer_type, number_type, traits

/**
* Base class that represents a token payload.
Expand Down Expand Up @@ -1250,8 +1248,8 @@ namespace jwt {
if (!traits::parse(val, str))
throw std::runtime_error("Invalid json");

for (auto& e : traits::as_object(val)) {
res.insert({ e.first, basic_claim_t(e.second) });
for (auto e : traits::as_object(val)) {
res.emplace(e.first, basic_claim_t(e.second));
}

return res;
Expand Down Expand Up @@ -1654,9 +1652,9 @@ namespace jwt {
* Create a verifier using the default clock
* \return verifier instance
*/
inline
verifier<default_clock, picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits> verify() {
return verify<default_clock, picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits>({});
inline
verifier<default_clock, picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits> verify() {
return verify<default_clock, picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits>(default_clock{});
}

/**
Expand All @@ -1667,6 +1665,13 @@ namespace jwt {
return builder<JWT_BASIC_CLAIM_TPL>();
}

/**
* Return a picojson builder instance to create a new token
*/
builder<picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits> create() {
return builder<picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits>();
}

/**
* Decode a token
* \param token Token to decode
Expand All @@ -1675,9 +1680,20 @@ namespace jwt {
* \throws std::runtime_error Base64 decoding failed or invalid json
*/
JWT_BASIC_CLAIM_TPL_DECLARATION
decoded_jwt<JWT_BASIC_CLAIM_TPL> decode(const std::string& token) {
decoded_jwt<JWT_BASIC_CLAIM_TPL> decode(const string_type& token) {
return decoded_jwt<JWT_BASIC_CLAIM_TPL>(token);
}

/**
* Decode a token
* \param token Token to decode
* \return Decoded token
* \throws std::invalid_argument Token is not in correct format
* \throws std::runtime_error Base64 decoding failed or invalid json
*/
decoded_jwt<picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits> decode(const std::string& token) {
return decoded_jwt<picojson::value, picojson::object, picojson::array, std::string, bool, int64_t, double, details::picojson_traits>(token);
}
}

JWT_BASIC_CLAIM_TPL_DECLARATION
Expand Down
4 changes: 2 additions & 2 deletions tests/ClaimTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ TEST(ClaimTest, SetArray) {
10
};
auto token = jwt::create()
.set_payload_claim("test", jwt::claim<jwt::picojson_traits>(vect.begin(), vect.end()))
.set_payload_claim("test", jwt::claim(vect.begin(), vect.end()))
.sign(jwt::algorithm::none{});
ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0.");
}

TEST(ClaimTest, SetObject) {
std::istringstream iss{"{\"api-x\": [1]}"};
jwt::claim<jwt::picojson_traits> object;
jwt::claim object;
iss >> object;
ASSERT_EQ(object.get_type() , jwt::json::type::object);

Expand Down
45 changes: 24 additions & 21 deletions tests/NlohmannTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
#include "nlohmann/json.hpp"
#include <gtest/gtest.h>

struct nlohmann_traits
: jwt::json::traits<nlohmann::json, nlohmann::json, nlohmann::json,
std::string, double, int64_t, bool> {
struct nlohmann_traits {
using json = nlohmann::json;

static jwt::json::type get_type(const traits::value &val) {
static jwt::json::type get_type(const json &val) {
using jwt::json::type;

if (val.type() == json::value_t::null)
Expand All @@ -28,63 +26,68 @@ struct nlohmann_traits
throw std::logic_error("invalid type");
}

static traits::object as_object(const traits::value &val) {
static json::value_t as_object(const json &val) {
if (val.type() != json::value_t::object)
throw std::bad_cast();
return val.get<traits::object>();
return val.get<json::value_t>();
}

static traits::string as_string(const traits::value &val) {
static std::string as_string(const json &val) {
if (val.type() != json::value_t::string)
throw std::bad_cast();
return val.get<traits::string>();
return val.get<std::string>();
}

static traits::array as_array(const traits::value &val) {
static json::array_t as_array(const json &val) {
if (val.type() != json::value_t::array)
throw std::bad_cast();
return val.get<traits::array>();
return val.get<json::array_t>();
}

static std::set<traits::string> as_set(const traits::value &val) {
std::set<traits::string> res;
static std::set<std::string> as_set(const json &val) {
std::set<std::string> res;
for (auto &e : as_array(val)) {
if (val.type() != json::value_t::string)
throw std::bad_cast();
res.insert(e.get<traits::string>());
res.insert(e.get<std::string>());
}
return res;
}

static traits::integer as_int(const traits::value &val) {
static int64_t as_int(const json &val) {
if (val.type() != json::value_t::number_integer)
throw std::bad_cast();
return val.get<traits::integer>();
return val.get<int64_t>();
}

static traits::boolean as_bool(const traits::value &val) {
static bool as_bool(const json &val) {
if (val.type() != json::value_t::boolean)
throw std::bad_cast();
return val.get<traits::boolean>();
return val.get<bool>();
}

static traits::number as_number(const traits::value &val) {
static double as_number(const json &val) {
if (val.type() != json::value_t::number_float)
throw std::bad_cast();
return val.get<traits::number>();
return val.get<double>();
}

static bool parse(traits::value &val, traits::string str) {
static bool parse(json &val, std::string str) {
val = json::parse(str.begin(), str.end());
return true;
}
};


TEST(ClaimTest, NholmannTest) {
const auto claim = jwt::basic_claim<nlohmann::json::value_type, nlohmann::json::object_t, nlohmann::json::array_t,
std::string, double, int64_t, bool>(std::string("string"));

std::string token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0."
"WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4";
auto decoded = jwt::decode(token);
auto decoded = jwt::decode<nlohmann::json::value_type, nlohmann::json::object_t, nlohmann::json::array_t,
std::string, double, int64_t, bool, nlohmann_traits>(token);

ASSERT_TRUE(decoded.has_algorithm());
ASSERT_TRUE(decoded.has_type());
Expand Down
4 changes: 2 additions & 2 deletions tests/TokenTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,11 @@ TEST(TokenTest, VerifyFail) {
auto verify = jwt::verify()
.allow_algorithm(jwt::algorithm::none{})
.with_issuer("auth0")
.with_claim("myclaim", jwt::claim<jwt::picojson_traits>(std::string("test")));
.with_claim("myclaim", jwt::claim(std::string("test")));
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
}
{
jwt::claim<jwt::picojson_traits> object;
jwt::claim object;
std::istringstream iss{R"({ "test": null })"};
iss >> object;
ASSERT_EQ(object.get_type() , jwt::json::type::object);
Expand Down

0 comments on commit 767c3fc

Please sign in to comment.