diff --git a/include/jwt/impl/jwt.ipp b/include/jwt/impl/jwt.ipp index 9555dac..73ad035 100644 --- a/include/jwt/impl/jwt.ipp +++ b/include/jwt/impl/jwt.ipp @@ -291,13 +291,25 @@ void jwt_object::set_parameters() return; } -template +template >::value> + > jwt_object& jwt_object::add_claim(const string_view name, T&& value) { payload_.add_claim(name, std::forward(value)); return *this; } +jwt_object& jwt_object::add_claim(const string_view name, system_time_t tp) +{ + return add_claim( + name, + std::chrono::duration_cast< + std::chrono::seconds>(tp.time_since_epoch()).count() + ); +} + jwt_object& jwt_object::remove_claim(const string_view name) { payload_.remove_claim(name); diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 329f89e..0b4c32e 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,7 @@ // For convenience using json_t = nlohmann::json; +using system_time_t = std::chrono::time_point; namespace jwt { @@ -296,7 +298,10 @@ public: // 'tors public: // Exposed APIs /** */ - template + template >::value> + > bool add_claim(const string_view cname, T&& cvalue, bool overwrite=false) { // Duplicate claim names not allowed @@ -315,6 +320,17 @@ public: // Exposed APIs return true; } + /** + */ + bool add_claim(const string_view cname, system_time_t tp, bool overwrite=false) + { + return add_claim( + cname, + std::chrono::duration_cast< + std::chrono::seconds>(tp.time_since_epoch()).count() + ); + } + /** */ bool remove_claim(const string_view cname) @@ -548,9 +564,15 @@ public: // Exposed APIs /** */ - template + template jwt_object& add_claim(const string_view name, T&& value); + /** + * Specialization for time points. + * Eg: Users can set `exp` claim to `chrono::system_clock::now()`. + */ + jwt_object& add_claim(const string_view name, system_time_t time_point); + /** */ jwt_object& remove_claim(const string_view name); @@ -601,10 +623,15 @@ private: // Data Members std::string secret_; }; -/*! +/** */ jwt_object jwt_decode(const string_view encoded_str, const string_view key, bool validate=true); +/** + */ +template +jwt_object decode(const string_view enc_str, const string_view key, Args&&... args); + } // END namespace jwt diff --git a/include/jwt/parameters.hpp b/include/jwt/parameters.hpp index af16ed7..471effd 100644 --- a/include/jwt/parameters.hpp +++ b/include/jwt/parameters.hpp @@ -101,6 +101,47 @@ struct headers_param MappingConcept headers_; }; +/** + */ +struct verify_param +{ + verify_param(bool v) + : verify_(v) + {} + + bool get() const { return verify_; } + + bool verify_; +}; + +/** + */ +template +struct algorithms_param +{ + algorithms_param(Sequence&& seq) + : seq_(std::forward(seq)) + {} + + Sequence get() && { return std::move(seq_); } + const Sequence& get() const& { return seq_; } + + Sequence seq_; +}; + +/** + */ +struct leeway_param +{ + leeway_param(uint32_t v) + : leeway_(v) + {} + + uint32_t get() const noexcept { return leeway_; } + + uint32_t leeway_; +}; + } // END namespace detail // Useful typedef @@ -181,6 +222,22 @@ headers(MappingConcept&& mc) return { std::forward(mc) }; } +/** + */ +detail::verify_param +verify(bool v) +{ + return { v }; +} + +/** + */ +detail::leeway_param +leeway(uint32_t l) +{ + return { l }; +} + } // END namespace params } // END namespace jwt diff --git a/include/jwt/short_string.hpp b/include/jwt/short_string.hpp index da92d67..f552350 100644 --- a/include/jwt/short_string.hpp +++ b/include/jwt/short_string.hpp @@ -5,7 +5,9 @@ #include "jwt/stack_alloc.hpp" namespace jwt { - +/* + * A basic_string implementation using stack allocation. + */ template using short_string = std::basic_string, stack_alloc>; diff --git a/include/jwt/test/test_jwt_object b/include/jwt/test/test_jwt_object index 51ee854..5933024 100755 Binary files a/include/jwt/test/test_jwt_object and b/include/jwt/test/test_jwt_object differ diff --git a/include/jwt/test/test_jwt_object.cc b/include/jwt/test/test_jwt_object.cc index e1e14fb..42bbe0b 100644 --- a/include/jwt/test/test_jwt_object.cc +++ b/include/jwt/test/test_jwt_object.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "jwt/jwt.hpp" @@ -31,7 +32,9 @@ void basic_jwt_object_test() jwt::jwt_object obj3{payload(um)}; obj3.add_claim("f", true) - .add_claim("time", 176353563); + .add_claim("time", 176353563) + .add_claim("exp", std::chrono::system_clock::now()) + ; std::cout << jwt::to_json_str(obj3.payload(), true) << std::endl;