diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index 87140fe..447b7d1 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -782,19 +782,25 @@ namespace jwt { else throw std::logic_error("invalid type"); } - traits::string as_string(const traits::value& val) const { + static traits::object as_object(const traits::value& val) { + if (!val.is()) + throw std::bad_cast(); + return val.get(); + } + + static traits::string as_string(const traits::value& val) { if (!val.is()) throw std::bad_cast(); return val.get(); } - traits::array as_array(const traits::value& val) const { + static traits::array as_array(const traits::value& val) { if (!val.is()) throw std::bad_cast(); return val.get(); } - std::set as_set(const traits::value& val) const { + static std::set as_set(const traits::value& val) { std::set res; for(auto& e : as_array(val)) { if(!e.is()) @@ -804,23 +810,27 @@ namespace jwt { return res; } - traits::integer as_int(const traits::value& val) const { + static traits::integer as_int(const traits::value& val) { if (!val.is()) throw std::bad_cast(); return val.get(); } - traits::boolean as_bool(const traits::value& val) const { + static traits::boolean as_bool(const traits::value& val) { if (!val.is()) throw std::bad_cast(); return val.get(); } - traits::number as_number(const traits::value& val) const { + static traits::number as_number(const traits::value& val) { if (!val.is()) throw std::bad_cast(); return val.get(); } + + static bool parse(traits::value& val, traits::string str){ + return picojson::parse(val, str).empty(); + } }; /** @@ -833,7 +843,6 @@ namespace jwt { using set = std::set; claim() = default; - JWT_CLAIM_EXPLICIT claim(const typename traits::value& val) = default; JWT_CLAIM_EXPLICIT claim(typename traits::string s) : val(std::move(s)) @@ -846,10 +855,19 @@ namespace jwt { : val(std::move(a)) {} + JWT_CLAIM_EXPLICIT claim(const typename traits::value& v) + : val(v) + {} + claim(set s) : val(typename traits::array(s.begin(), s.end())) {} + template + claim(Iterator begin, Iterator end) + : val(typename traits::array(begin, end)) + {} + /** * Get wrapped json object * \return Wrapped json object @@ -872,7 +890,7 @@ namespace jwt { * \return Type * \throws std::logic_error An internal error occured */ - typename traits::type get_type() const { + json::type get_type() const { return traits::get_type(val); } @@ -890,7 +908,7 @@ namespace jwt { * \throws std::bad_cast Content was not a date */ date as_date() const { - return std::chrono::system_clock::from_time_t(as_int(val)); + return std::chrono::system_clock::from_time_t(as_int()); } /** @@ -1001,7 +1019,7 @@ namespace jwt { */ typename claim::set get_audience() const { auto aud = get_payload_claim("aud"); - if(aud.get_type() == jwt::traits::type::string) return { aud.as_string()}; + if(aud.get_type() == jwt::json::type::string) return { aud.as_string()}; else return aud.as_set(); } /** @@ -1042,7 +1060,7 @@ namespace jwt { * \return Requested claim * \throws std::runtime_error If claim was not present */ - const claim& get_payload_claim(const std::string& name) const { + claim get_payload_claim(const std::string& name) const { if (!has_payload_claim(name)) throw std::runtime_error("claim not found"); return payload_claims.at(name); @@ -1051,7 +1069,7 @@ namespace jwt { * Get all payload claims * \return map of claims */ - std::unordered_map get_payload_claims() const { return payload_claims; } + std::unordered_map> get_payload_claims() const { return payload_claims; } }; /** @@ -1184,18 +1202,20 @@ namespace jwt { signature = base::decode(signature); auto parse_claims = [](const std::string& str) { - std::unordered_map res; - picojson::value val; - if (!picojson::parse(val, str).empty()) + std::unordered_map> res; + typename traits::value val; + if (!traits::parse(val, str)) throw std::runtime_error("Invalid json"); - for (auto& e : val.get()) { res.insert({ e.first, claim(e.second) }); } + for (auto& e : traits::as_object(val)) { + res.insert({ e.first, claim(e.second) }); + } return res; }; - header_claims = parse_claims(header); - payload_claims = parse_claims(payload); + jwt::header::header_claims = parse_claims(header); + jwt::payload::payload_claims = parse_claims(payload); } /** @@ -1245,9 +1265,8 @@ namespace jwt { std::unordered_map> header_claims; std::unordered_map> payload_claims; - builder() {} - friend builder create(); public: + builder() {} /** * Set a header claim. * \param id Name of the claim @@ -1304,7 +1323,7 @@ namespace jwt { * \param l Audience set * \return *this to allow for method chaining */ - builder& set_audience(typename claim::set) { return set_payload_claim("aud", claim(l)); } + builder& set_audience(typename claim::set l) { return set_payload_claim("aud", claim(l)); } /** * Set audience claim * \param aud Single audience @@ -1343,12 +1362,12 @@ namespace jwt { */ template std::string sign(const T& algo) const { - picojson::object obj_header; - obj_header["alg"] = picojson::value(algo.name()); + typename traits::object obj_header; + obj_header["alg"] = typename traits::value(algo.name()); for (auto& e : header_claims) { obj_header[e.first] = e.second.to_json(); } - picojson::object obj_payload; + typename traits::object obj_payload; for (auto& e : payload_claims) { obj_payload.insert({ e.first, e.second.to_json() }); } @@ -1357,8 +1376,8 @@ namespace jwt { return base::trim(base::encode(data)); }; - std::string header = encode(picojson::value(obj_header).serialize()); - std::string payload = encode(picojson::value(obj_payload).serialize()); + std::string header = encode(typename traits::value(obj_header).serialize()); + std::string payload = encode(typename traits::value(obj_payload).serialize()); std::string token = header + "." + payload; @@ -1369,7 +1388,7 @@ namespace jwt { /** * Verifier class used to check if a decoded token contains all claims required by your application and has a valid signature. */ - template + template class verifier { struct algo_base { virtual ~algo_base() {} @@ -1385,7 +1404,7 @@ namespace jwt { }; /// Required claims - std::unordered_map claims; + std::unordered_map> claims; /// Leeway time for exp, nbf and iat size_t default_leeway = 0; /// Instance of clock type @@ -1411,63 +1430,63 @@ namespace jwt { * \param leeway Set leeway to use for expires at. * \return *this to allow chaining */ - verifier& expires_at_leeway(size_t leeway) { return with_claim("exp", claim(std::chrono::system_clock::from_time_t(leeway))); } + verifier& expires_at_leeway(size_t leeway) { return with_claim("exp", claim(std::chrono::system_clock::from_time_t(leeway))); } /** * Set leeway for not before. * If not specified the default leeway will be used. * \param leeway Set leeway to use for not before. * \return *this to allow chaining */ - verifier& not_before_leeway(size_t leeway) { return with_claim("nbf", claim(std::chrono::system_clock::from_time_t(leeway))); } + verifier& not_before_leeway(size_t leeway) { return with_claim("nbf", claim(std::chrono::system_clock::from_time_t(leeway))); } /** * Set leeway for issued at. * If not specified the default leeway will be used. * \param leeway Set leeway to use for issued at. * \return *this to allow chaining */ - verifier& issued_at_leeway(size_t leeway) { return with_claim("iat", claim(std::chrono::system_clock::from_time_t(leeway))); } + verifier& issued_at_leeway(size_t leeway) { return with_claim("iat", claim(std::chrono::system_clock::from_time_t(leeway))); } /** * Set an issuer to check for. * Check is casesensitive. * \param iss Issuer to check for. * \return *this to allow chaining */ - verifier& with_issuer(const std::string& iss) { return with_claim("iss", claim(iss)); } + verifier& with_issuer(const std::string& iss) { return with_claim("iss", claim(iss)); } /** * Set a subject to check for. * Check is casesensitive. * \param sub Subject to check for. * \return *this to allow chaining */ - verifier& with_subject(const std::string& sub) { return with_claim("sub", claim(sub)); } + verifier& with_subject(const std::string& sub) { return with_claim("sub", claim(sub)); } /** * Set an audience to check for. * If any of the specified audiences is not present in the token the check fails. * \param aud Audience to check for. * \return *this to allow chaining */ - verifier& with_audience(const std::set& aud) { return with_claim("aud", claim(aud)); } + verifier& with_audience(const std::set& aud) { return with_claim("aud", claim(aud)); } /** * Set an audience to check for. * If the specified audiences is not present in the token the check fails. * \param aud Audience to check for. * \return *this to allow chaining */ - verifier& with_audience(const std::string& aud) { return with_claim("aud", claim(aud)); } + verifier& with_audience(const std::string& aud) { return with_claim("aud", claim(aud)); } /** * Set an id to check for. * Check is casesensitive. * \param id ID to check for. * \return *this to allow chaining */ - verifier& with_id(const std::string& id) { return with_claim("jti", claim(id)); } + verifier& with_id(const std::string& id) { return with_claim("jti", claim(id)); } /** * Specify a claim to check for. * \param name Name of the claim to check for * \param c Claim to check for * \return *this to allow chaining */ - verifier& with_claim(const std::string& name, claim c) { claims[name] = c; return *this; } + verifier& with_claim(const std::string& name, claim c) { claims[name] = c; return *this; } /** * Add an algorithm available for checking. @@ -1485,7 +1504,7 @@ namespace jwt { * \param jwt Token to check * \throws token_verification_exception Verification failed */ - void verify(const decoded_jwt& jwt) const { + void verify(const decoded_jwt& jwt) const { const std::string data = jwt.get_header_base64() + "." + jwt.get_payload_base64(); const std::string sig = jwt.get_signature(); const std::string& algo = jwt.get_algorithm(); @@ -1493,17 +1512,17 @@ namespace jwt { throw token_verification_exception("wrong algorithm"); algs.at(algo)->verify(data, sig); - auto assert_claim_eq = [](const decoded_jwt& jwt, const std::string& key, const claim& c) { + auto assert_claim_eq = [](const decoded_jwt& jwt, const std::string& key, const claim& c) { if (!jwt.has_payload_claim(key)) throw token_verification_exception("decoded_jwt is missing " + key + " claim"); - auto& jc = jwt.get_payload_claim(key); + auto jc = jwt.get_payload_claim(key); if (jc.get_type() != c.get_type()) throw token_verification_exception("claim " + key + " type mismatch"); - if (c.get_type() == claim::type::int64) { + if (c.get_type() == jwt::json::type::integer) { if (c.as_date() != jc.as_date()) throw token_verification_exception("claim " + key + " does not match expected"); } - else if (c.get_type() == claim::type::array) { + else if (c.get_type() == jwt::json::type::array) { auto s1 = c.as_set(); auto s2 = jc.as_set(); if (s1.size() != s2.size()) @@ -1515,11 +1534,11 @@ namespace jwt { throw token_verification_exception("claim " + key + " does not match expected"); } } - else if (c.get_type() == claim::type::object) { + else if (c.get_type() == jwt::json::type::object) { if( c.to_json().serialize() != jc.to_json().serialize()) throw token_verification_exception("claim " + key + " does not match expected"); } - else if (c.get_type() == claim::type::string) { + else if (c.get_type() == jwt::json::type::string) { if (c.as_string() != jc.as_string()) throw token_verification_exception("claim " + key + " does not match expected"); } @@ -1572,7 +1591,7 @@ namespace jwt { * \param c Clock instance to use * \return verifier instance */ - template + template verifier verify(Clock c) { return verifier(c); } @@ -1598,9 +1617,9 @@ namespace jwt { /** * Return a builder instance to create a new token */ - inline - builder create() { - return builder(); + template + builder create() { + return builder(); } /** @@ -1610,18 +1629,20 @@ namespace jwt { * \throws std::invalid_argument Token is not in correct format * \throws std::runtime_error Base64 decoding failed or invalid json */ - inline - decoded_jwt decode(const std::string& token) { - return decoded_jwt(token); + template + decoded_jwt decode(const std::string& token) { + return decoded_jwt(token); } } -inline std::istream& operator>>(std::istream& is, jwt::claim& c) +template +std::istream& operator>>(std::istream& is, jwt::claim& c) { return c.operator>>(is); } -inline std::ostream& operator<<(std::ostream& os, const jwt::claim& c) +template +std::ostream& operator<<(std::ostream& os, const jwt::claim& c) { return os << c.to_json(); } diff --git a/tests/ClaimTest.cpp b/tests/ClaimTest.cpp index cb58564..c33f9e3 100644 --- a/tests/ClaimTest.cpp +++ b/tests/ClaimTest.cpp @@ -39,16 +39,16 @@ TEST(ClaimTest, SetArray) { 10 }; auto token = jwt::create() - .set_payload_claim("test", jwt::claim(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 object; + jwt::claim object; iss >> object; - ASSERT_EQ(object.get_type() , jwt::claim::type::object); + ASSERT_EQ(object.get_type() , jwt::json::type::object); auto token = jwt::create() .set_payload_claim("namespace", object) diff --git a/tests/TokenTest.cpp b/tests/TokenTest.cpp index 65915a1..f329727 100644 --- a/tests/TokenTest.cpp +++ b/tests/TokenTest.cpp @@ -346,14 +346,14 @@ TEST(TokenTest, VerifyFail) { auto verify = jwt::verify() .allow_algorithm(jwt::algorithm::none{}) .with_issuer("auth0") - .with_claim("myclaim", jwt::claim(std::string("test"))); + .with_claim("myclaim", jwt::claim(std::string("test"))); ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception); } { - jwt::claim object; + jwt::claim object; std::istringstream iss{R"({ "test": null })"}; iss >> object; - ASSERT_EQ(object.get_type() , jwt::claim::type::object); + ASSERT_EQ(object.get_type() , jwt::json::type::object); auto verify = jwt::verify() .allow_algorithm(jwt::algorithm::none{})