Skip to content

Commit

Permalink
adding serialize trait
Browse files Browse the repository at this point in the history
  • Loading branch information
prince-chrismc committed May 24, 2020
1 parent f6d8c6b commit e1d2f58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
39 changes: 16 additions & 23 deletions include/jwt-cpp/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,22 +900,26 @@ namespace jwt {
static bool parse(picojson::value& val, std::string str){
return picojson::parse(val, str).empty();
}

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

template<typename value_type = picojson::value,
class object_type = picojson::object,
class array_type = picojson::array,
class string_type = std::string,
class boolean_type = bool,
class integer_type = int64_t,
class number_type = double,
typename traits = details::picojson_traits>
class object_type = picojson::object,
class array_type = picojson::array,
class string_type = std::string,
class boolean_type = bool,
class integer_type = int64_t,
class number_type = double,
typename traits = details::picojson_traits>
class basic_claim {
static_assert(details::is_json_traits<traits, value_type>::value, "traits must satisfy is_json_traits");
static_assert(details::supports_as_object<traits, value_type, object_type>::value, "traits must provide as_object");

value_type val;
value_type val;
public:
using set_t = std::set<string_type>;

Expand Down Expand Up @@ -1041,7 +1045,7 @@ namespace jwt {
};

/**
* Convenience wrapper for JSON value
* Convenience wrapper for JSON claim
*/
using claim = basic_claim<>;

Expand Down Expand Up @@ -1484,14 +1488,8 @@ namespace jwt {
return base::trim<alphabet::base64url>(base::encode<alphabet::base64url>(data));
};

std::ostringstream iss;
iss << value_type(obj_header);
std::string header = encode(iss.str());
iss.str(std::string());
iss.clear();
iss << value_type(obj_payload);
std::string payload = encode(iss.str());

std::string header = encode(traits::serialize(value_type(obj_header)));
std::string payload = encode(traits::serialize(value_type(obj_payload)));
std::string token = header + "." + payload;

return token + "." + encode(algo.sign(token));
Expand Down Expand Up @@ -1649,12 +1647,7 @@ namespace jwt {
}
}
else if (c.get_type() == json::type::object) {
auto serialize = [](const value_type& value) {
std::ostringstream oss;
oss << value;
return oss.str();
};
if( serialize(c.to_json()) != serialize(jc.to_json()))
if (traits::serialize(c.to_json()) != traits::serialize(jc.to_json()))
throw token_verification_exception("claim " + key + " does not match expected");
}
else if (c.get_type() == json::type::string) {
Expand Down
4 changes: 4 additions & 0 deletions tests/NlohmannTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ struct nlohmann_traits {
val = json::parse(str.begin(), str.end());
return true;
}

static std::string serialize(const json &val) {
return val.dump();
}
};

#define JWT_NHOLMANN_CLAIM_TPL \
Expand Down

0 comments on commit e1d2f58

Please sign in to comment.