Skip to content

Commit 87dcef9

Browse files
committed
Why does the existence of a signature algorithm entail a required verification? arun11299#24
1 parent b114070 commit 87dcef9

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

include/jwt/impl/jwt.ipp

+24-24
Original file line numberDiff line numberDiff line change
@@ -703,34 +703,34 @@ jwt_object decode(const jwt::string_view enc_str,
703703
}
704704

705705
if (ec) return obj;
706-
}
707706

708-
//Verify the signature only if some algorithm was used
709-
if (obj.header().algo() != algorithm::NONE)
710-
{
711-
if (!dparams.has_secret) {
712-
ec = DecodeErrc::KeyNotPresent;
713-
return obj;
714-
}
715-
jwt_signature jsign{dparams.secret};
707+
//Verify the signature only if some algorithm was used
708+
if (obj.header().algo() != algorithm::NONE)
709+
{
710+
if (!dparams.has_secret) {
711+
ec = DecodeErrc::KeyNotPresent;
712+
return obj;
713+
}
714+
jwt_signature jsign{dparams.secret};
716715

717-
// Length of the encoded header and payload only.
718-
// Addition of '1' to account for the '.' character.
719-
auto l = parts[0].length() + 1 + parts[1].length();
720-
721-
//MemoryAllocationError is not caught
722-
verify_result_t res = jsign.verify(obj.header(), enc_str.substr(0, l), parts[2]);
723-
if (res.second) {
724-
ec = res.second;
725-
return obj;
726-
}
716+
// Length of the encoded header and payload only.
717+
// Addition of '1' to account for the '.' character.
718+
auto l = parts[0].length() + 1 + parts[1].length();
719+
720+
//MemoryAllocationError is not caught
721+
verify_result_t res = jsign.verify(obj.header(), enc_str.substr(0, l), parts[2]);
722+
if (res.second) {
723+
ec = res.second;
724+
return obj;
725+
}
727726

728-
if (!res.first) {
729-
ec = VerificationErrc::InvalidSignature;
730-
return obj;
727+
if (!res.first) {
728+
ec = VerificationErrc::InvalidSignature;
729+
return obj;
730+
}
731+
} else {
732+
ec = AlgorithmErrc::NoneAlgorithmUsed;
731733
}
732-
} else {
733-
ec = AlgorithmErrc::NoneAlgorithmUsed;
734734
}
735735

736736
return obj;

tests/test_jwt_decode.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ TEST (DecodeTest, DecodeNoneAlgSign)
1919
{
2020
using namespace jwt::params;
2121
const char* enc_str =
22-
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
22+
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjo0NTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
2323

2424
std::error_code ec;
25-
auto obj = jwt::decode(enc_str, algorithms({"none"}), ec, verify(false));
25+
auto obj = jwt::decode(enc_str, algorithms({"none"}), ec, verify(true));
2626
EXPECT_TRUE (ec);
2727
EXPECT_EQ (ec.value(), static_cast<int>(jwt::AlgorithmErrc::NoneAlgorithmUsed));
2828

@@ -34,7 +34,7 @@ TEST (DecodeTest, DecodeNoneAlgSign)
3434
EXPECT_TRUE (obj.has_claim("aud"));
3535
EXPECT_TRUE (obj.has_claim("exp"));
3636

37-
EXPECT_EQ (obj.payload().get_claim_value<uint64_t>("exp"), static_cast<uint64_t>(1513863371));
37+
EXPECT_EQ (obj.payload().get_claim_value<uint64_t>("exp"), static_cast<uint64_t>(4513863371));
3838
}
3939

4040
TEST (DecodeTest, DecodeWrongAlgo)
@@ -111,7 +111,7 @@ TEST (DecodeTest, SecretKeyNotPassed)
111111
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
112112

113113
std::error_code ec;
114-
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(false));
114+
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(true));
115115

116116
ASSERT_TRUE (ec);
117117
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::KeyNotPresent));

tests/test_jwt_decode_verifiy_with_exception.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ TEST (DecodeVerifyExp, KeyNotPresentTest)
160160
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
161161
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
162162

163-
EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "hs256"}), verify(false)),
163+
EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "hs256"}), verify(true)),
164164
jwt::KeyNotPresentError);
165165
}
166166

tests/test_jwt_encode.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ TEST (EncodeTest, HeaderParamTest)
286286
std::error_code ec;
287287
auto enc_str = obj.signature();
288288

289-
auto dec_obj = jwt::decode(enc_str, algorithms({"none"}), ec, verify(false));
289+
auto dec_obj = jwt::decode(enc_str, algorithms({"none"}), ec, verify(true));
290290
EXPECT_EQ (ec.value(), static_cast<int>(jwt::AlgorithmErrc::NoneAlgorithmUsed));
291291

292292
std::cout << dec_obj.header() << std::endl;

0 commit comments

Comments
 (0)