Added new parameters for decode API

This commit is contained in:
Arun M 2017-12-04 19:01:45 +05:30
parent de38b3a82a
commit 09bc72c9f7
6 changed files with 107 additions and 6 deletions

View file

@ -291,13 +291,25 @@ void jwt_object::set_parameters()
return; return;
} }
template <typename T> template <typename T,
typename=typename std::enable_if_t<
!std::is_same<system_time_t, std::decay_t<T>>::value>
>
jwt_object& jwt_object::add_claim(const string_view name, T&& value) jwt_object& jwt_object::add_claim(const string_view name, T&& value)
{ {
payload_.add_claim(name, std::forward<T>(value)); payload_.add_claim(name, std::forward<T>(value));
return *this; 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) jwt_object& jwt_object::remove_claim(const string_view name)
{ {
payload_.remove_claim(name); payload_.remove_claim(name);

View file

@ -4,6 +4,7 @@
#include <set> #include <set>
#include <array> #include <array>
#include <string> #include <string>
#include <chrono>
#include <ostream> #include <ostream>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
@ -17,6 +18,7 @@
// For convenience // For convenience
using json_t = nlohmann::json; using json_t = nlohmann::json;
using system_time_t = std::chrono::time_point<std::chrono::system_clock>;
namespace jwt { namespace jwt {
@ -296,7 +298,10 @@ public: // 'tors
public: // Exposed APIs public: // Exposed APIs
/** /**
*/ */
template <typename T> template <typename T,
typename=typename std::enable_if_t<
!std::is_same<system_time_t, std::decay_t<T>>::value>
>
bool add_claim(const string_view cname, T&& cvalue, bool overwrite=false) bool add_claim(const string_view cname, T&& cvalue, bool overwrite=false)
{ {
// Duplicate claim names not allowed // Duplicate claim names not allowed
@ -315,6 +320,17 @@ public: // Exposed APIs
return true; 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) bool remove_claim(const string_view cname)
@ -548,9 +564,15 @@ public: // Exposed APIs
/** /**
*/ */
template <typename T> template <typename T, typename Cond>
jwt_object& add_claim(const string_view name, T&& value); 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); jwt_object& remove_claim(const string_view name);
@ -601,10 +623,15 @@ private: // Data Members
std::string secret_; std::string secret_;
}; };
/*! /**
*/ */
jwt_object jwt_decode(const string_view encoded_str, const string_view key, bool validate=true); jwt_object jwt_decode(const string_view encoded_str, const string_view key, bool validate=true);
/**
*/
template <typename... Args>
jwt_object decode(const string_view enc_str, const string_view key, Args&&... args);
} // END namespace jwt } // END namespace jwt

View file

@ -101,6 +101,47 @@ struct headers_param
MappingConcept headers_; MappingConcept headers_;
}; };
/**
*/
struct verify_param
{
verify_param(bool v)
: verify_(v)
{}
bool get() const { return verify_; }
bool verify_;
};
/**
*/
template <typename Sequence>
struct algorithms_param
{
algorithms_param(Sequence&& seq)
: seq_(std::forward<Sequence>(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 } // END namespace detail
// Useful typedef // Useful typedef
@ -181,6 +222,22 @@ headers(MappingConcept&& mc)
return { std::forward<MappingConcept>(mc) }; return { std::forward<MappingConcept>(mc) };
} }
/**
*/
detail::verify_param
verify(bool v)
{
return { v };
}
/**
*/
detail::leeway_param
leeway(uint32_t l)
{
return { l };
}
} // END namespace params } // END namespace params
} // END namespace jwt } // END namespace jwt

View file

@ -5,7 +5,9 @@
#include "jwt/stack_alloc.hpp" #include "jwt/stack_alloc.hpp"
namespace jwt { namespace jwt {
/*
* A basic_string implementation using stack allocation.
*/
template <size_t N> template <size_t N>
using short_string = std::basic_string<char, std::char_traits<char>, stack_alloc<char, N>>; using short_string = std::basic_string<char, std::char_traits<char>, stack_alloc<char, N>>;

Binary file not shown.

View file

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <string> #include <string>
#include <chrono>
#include <unordered_map> #include <unordered_map>
#include "jwt/jwt.hpp" #include "jwt/jwt.hpp"
@ -31,7 +32,9 @@ void basic_jwt_object_test()
jwt::jwt_object obj3{payload(um)}; jwt::jwt_object obj3{payload(um)};
obj3.add_claim("f", true) 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; std::cout << jwt::to_json_str(obj3.payload(), true) << std::endl;