mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 01:08:31 +00:00
Added new parameters for decode API
This commit is contained in:
parent
de38b3a82a
commit
09bc72c9f7
6 changed files with 107 additions and 6 deletions
|
@ -291,13 +291,25 @@ void jwt_object::set_parameters()
|
|||
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)
|
||||
{
|
||||
payload_.add_claim(name, std::forward<T>(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);
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <set>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <ostream>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
@ -17,6 +18,7 @@
|
|||
|
||||
// For convenience
|
||||
using json_t = nlohmann::json;
|
||||
using system_time_t = std::chrono::time_point<std::chrono::system_clock>;
|
||||
|
||||
namespace jwt {
|
||||
|
||||
|
@ -296,7 +298,10 @@ public: // 'tors
|
|||
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)
|
||||
{
|
||||
// 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 <typename T>
|
||||
template <typename T, typename Cond>
|
||||
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 <typename... Args>
|
||||
jwt_object decode(const string_view enc_str, const string_view key, Args&&... args);
|
||||
|
||||
|
||||
} // END namespace jwt
|
||||
|
||||
|
|
|
@ -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 <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
|
||||
|
||||
// Useful typedef
|
||||
|
@ -181,6 +222,22 @@ headers(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 jwt
|
||||
|
||||
|
|
|
@ -5,7 +5,9 @@
|
|||
#include "jwt/stack_alloc.hpp"
|
||||
|
||||
namespace jwt {
|
||||
|
||||
/*
|
||||
* A basic_string implementation using stack allocation.
|
||||
*/
|
||||
template <size_t N>
|
||||
using short_string = std::basic_string<char, std::char_traits<char>, stack_alloc<char, N>>;
|
||||
|
||||
|
|
Binary file not shown.
|
@ -1,6 +1,7 @@
|
|||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <unordered_map>
|
||||
#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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue