mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-14 16:58:34 +00:00
Add option to remove type fromt he jwt_header #26
This commit is contained in:
parent
87dcef903f
commit
129731da56
4 changed files with 84 additions and 14 deletions
|
@ -85,19 +85,16 @@ inline void jwt_header::decode(const jwt::string_view enc_str, std::error_code&
|
|||
if (alg_ != algorithm::NONE)
|
||||
{
|
||||
auto itr = payload_.find("typ");
|
||||
if (itr == payload_.end()) {
|
||||
ec = DecodeErrc::TypHeaderMiss;
|
||||
return;
|
||||
|
||||
if (itr != payload_.end()) {
|
||||
const auto& typ = itr.value().get<std::string>();
|
||||
if (strcasecmp(typ.c_str(), "JWT")) {
|
||||
ec = DecodeErrc::TypMismatch;
|
||||
return;
|
||||
}
|
||||
|
||||
typ_ = str_to_type(typ);
|
||||
}
|
||||
|
||||
const auto& typ = itr.value().get<std::string>();
|
||||
|
||||
if (strcasecmp(typ.c_str(), "JWT")) {
|
||||
ec = DecodeErrc::TypMismatch;
|
||||
return;
|
||||
}
|
||||
|
||||
typ_ = str_to_type(typ);
|
||||
} else {
|
||||
//TODO:
|
||||
}
|
||||
|
@ -107,7 +104,8 @@ inline void jwt_header::decode(const jwt::string_view enc_str, std::error_code&
|
|||
auto ret = headers_.insert(it.key());
|
||||
if (!ret.second) {
|
||||
ec = DecodeErrc::DuplClaims;
|
||||
break;
|
||||
//ATTN: Dont stop the decode here
|
||||
//Not a hard error.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -51,7 +51,8 @@ namespace jwt {
|
|||
*/
|
||||
enum class type
|
||||
{
|
||||
JWT = 0,
|
||||
NONE = 0,
|
||||
JWT = 1,
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -63,6 +64,7 @@ inline enum type str_to_type(const jwt::string_view typ) noexcept
|
|||
assert (typ.length() && "Empty type string");
|
||||
|
||||
if (!strcasecmp(typ.data(), "jwt")) return type::JWT;
|
||||
else if(!strcasecmp(typ.data(), "none")) return type::NONE;
|
||||
|
||||
throw std::runtime_error("Unknown token type");
|
||||
|
||||
|
@ -410,6 +412,40 @@ public: // Exposed APIs
|
|||
overwrite);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the header from JWT.
|
||||
* NOTE: Special handling for removing type field
|
||||
* from header. The typ_ is set to NONE when removed.
|
||||
*/
|
||||
bool remove_header(const jwt::string_view hname)
|
||||
{
|
||||
if (!strcasecmp(hname.data(), "typ")) {
|
||||
typ_ = type::NONE;
|
||||
payload_.erase(hname.data());
|
||||
return true;
|
||||
}
|
||||
|
||||
auto itr = headers_.find(hname);
|
||||
if (itr == std::end(headers_)) {
|
||||
return false;
|
||||
}
|
||||
payload_.erase(hname.data());
|
||||
headers_.erase(hname.data());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if header with the given name
|
||||
* is present or not.
|
||||
*/
|
||||
bool has_header(const jwt::string_view hname)
|
||||
{
|
||||
if (!strcasecmp(hname.data(), "typ")) return typ_ != type::NONE;
|
||||
return headers_.find(hname) != std::end(headers_);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the URL safe base64 encoded string
|
||||
* of the header.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue