Readme changes for example code and fixed header decoding

This commit is contained in:
Arun M 2017-12-29 16:26:37 +05:30
parent 1236071a4f
commit 6302055db1
22 changed files with 1077 additions and 6 deletions

View file

@ -67,18 +67,17 @@ void jwt_header::decode(const jwt::string_view enc_str, std::error_code& ec)
{
ec.clear();
std::string json_str = base64_decode(enc_str);
json_t obj;
try {
obj = json_t::parse(std::move(json_str));
payload_ = json_t::parse(std::move(json_str));
} catch(const std::exception& e) {
ec = DecodeErrc::JsonParseError;
return;
}
//Look for the algorithm field
auto alg_itr = obj.find("alg");
if (alg_itr == obj.end()) {
auto alg_itr = payload_.find("alg");
if (alg_itr == payload_.end()) {
ec = DecodeErrc::AlgHeaderMiss;
return;
}
@ -87,13 +86,14 @@ void jwt_header::decode(const jwt::string_view enc_str, std::error_code& ec)
if (alg_ != algorithm::NONE)
{
auto itr = obj.find("typ");
if (itr == obj.end()) {
auto itr = payload_.find("typ");
if (itr == payload_.end()) {
ec = DecodeErrc::TypHeaderMiss;
return;
}
const auto& typ = itr.value().get<std::string>();
if (strcasecmp(typ.c_str(), "JWT")) {
ec = DecodeErrc::TypMismatch;
return;
@ -104,6 +104,15 @@ void jwt_header::decode(const jwt::string_view enc_str, std::error_code& ec)
//TODO:
}
// Populate header
for (auto it = payload_.begin(); it != payload_.end(); ++it) {
auto ret = headers_.insert(it.key());
if (!ret.second) {
ec = DecodeErrc::DuplClaims;
break;
}
}
return;
}