Add api to supply secret based on decoded payload

This commit is contained in:
Huang-Ming Huang 2019-02-16 15:36:12 -06:00
parent 0b68fd46fb
commit 74b7b344c6
4 changed files with 41 additions and 3 deletions

View file

@ -544,6 +544,14 @@ void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::secret
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
}
template <typename DecodeParams, typename T, typename... Rest>
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::secret_function_param<T>&& s, Rest&&... args)
{
dparams.secret = s.get(*dparams.payload_ptr);
dparams.has_secret = true;
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
}
template <typename DecodeParams, typename... Rest>
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args)
{
@ -650,10 +658,11 @@ jwt_object decode(const jwt::string_view enc_str,
//Validate JTI
bool validate_jti = false;
const jwt_payload* payload_ptr = 0;
};
decode_params dparams{};
jwt_object::set_decode_params(dparams, std::forward<Args>(args)...);
//Signature must have atleast 2 dots
auto dot_cnt = std::count_if(std::begin(enc_str), std::end(enc_str),
@ -695,7 +704,8 @@ jwt_object decode(const jwt::string_view enc_str,
return obj;
}
obj.payload(std::move(payload));
dparams.payload_ptr = & obj.payload();
jwt_object::set_decode_params(dparams, std::forward<Args>(args)...);
if (dparams.verify) {
try {
ec = obj.verify(dparams, algos);

View file

@ -82,7 +82,7 @@ inline jwt::string_view type_to_str(SCOPED_ENUM type typ)
case type::JWT: return "JWT";
default: assert (0 && "Unknown type");
};
__builtin_unreachable();
assert (0 && "Code not reached");
}
@ -1121,6 +1121,9 @@ public: //TODO: Not good
template <typename DecodeParams, typename... Rest>
static void set_decode_params(DecodeParams& dparams, params::detail::secret_param s, Rest&&... args);
template <typename DecodeParams, typename T, typename... Rest>
static void set_decode_params(DecodeParams& dparams, params::detail::secret_function_param<T>&& s, Rest&&... args);
template <typename DecodeParams, typename... Rest>
static void set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args);

View file

@ -84,6 +84,15 @@ struct secret_param
string_view secret_;
};
template <typename T>
struct secret_function_param
{
T get() const { return fun_; }
template <typename U>
std::string get(U&& u) const { return fun_(u);}
T fun_;
};
/**
* Parameter for providing the algorithm to use.
* The parameter can accept either the string representation
@ -297,6 +306,13 @@ inline detail::secret_param secret(const string_view sv)
return { sv };
}
template <typename T>
inline std::enable_if_t<!std::is_convertible<T, string_view>::value, detail::secret_function_param<T>>
secret(T&& fun)
{
return detail::secret_function_param<T>{ fun };
}
/**
*/
inline detail::algorithm_param algorithm(const string_view sv)