Skip to content

Commit 99f3c1d

Browse files
committed
Make secret an optional argument for decode
1 parent 6c2bbe9 commit 99f3c1d

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

include/jwt/error_codes.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ enum class DecodeErrc
3434
TypHeaderMiss,
3535
TypMismatch,
3636
DuplClaims,
37+
KeyNotPresent,
38+
KeyNotRequiredForNoneAlg,
3739
};
3840

3941
/**

include/jwt/impl/error_codes.ipp

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ struct DecodeErrorCategory: std::error_category
5959
return "json parse failed";
6060
case DecodeErrc::DuplClaims:
6161
return "duplicate claims";
62+
case DecodeErrc::KeyNotPresent:
63+
return "key not present";
64+
case DecodeErrc::KeyNotRequiredForNoneAlg:
65+
return "key not required for NONE algorithm";
6266
};
6367

6468
assert (0 && "Code not reached");

include/jwt/impl/jwt.ipp

+29-7
Original file line numberDiff line numberDiff line change
@@ -464,21 +464,26 @@ jwt_object::three_parts(const string_view enc_str)
464464
return result;
465465
}
466466

467+
template <typename DecodeParams, typename... Rest>
468+
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::secret_param s, Rest&&... args)
469+
{
470+
dparams.secret.assign(s.get().data(), s.get().length());
471+
dparams.has_secret = true;
472+
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
473+
}
467474

468475
template <typename DecodeParams, typename... Rest>
469476
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args)
470477
{
471478
dparams.leeway = l.get();
472479
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
473-
return;
474480
}
475481

476482
template <typename DecodeParams, typename... Rest>
477483
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::verify_param v, Rest&&... args)
478484
{
479485
dparams.verify = v.get();
480486
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
481-
return;
482487
}
483488

484489
template <typename DecodeParams, typename... Rest>
@@ -487,7 +492,6 @@ void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::issuer
487492
dparams.issuer = std::move(i).get();
488493
dparams.has_issuer = true;
489494
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
490-
return;
491495
}
492496

493497
template <typename DecodeParams, typename... Rest>
@@ -508,7 +512,6 @@ void jwt_object::set_decode_params(DecodeParams& dparams)
508512

509513
template <typename SequenceT, typename... Args>
510514
jwt_object decode(const string_view enc_str,
511-
const string_view key,
512515
const params::detail::algorithms_param<SequenceT>& algos,
513516
std::error_code& ec,
514517
Args&&... args)
@@ -523,14 +526,21 @@ jwt_object decode(const string_view enc_str,
523526

524527
struct decode_params
525528
{
529+
/// key to decode the JWS
530+
bool has_secret = false;
531+
std::string secret;
532+
526533
/// Verify parameter. Defaulted to true.
527534
bool verify = true;
535+
528536
/// Leeway parameter. Defaulted to zero seconds.
529537
uint32_t leeway = 0;
538+
530539
///The issuer
531540
//TODO: optional type
532541
bool has_issuer = false;
533542
std::string issuer;
543+
534544
///The audience
535545
//TODO: optional type
536546
bool has_aud = false;
@@ -571,6 +581,15 @@ jwt_object decode(const string_view enc_str,
571581
ec = DecodeErrc::SignatureFormatError;
572582
return obj;
573583
}
584+
585+
if (!dparams.has_secret) {
586+
ec = DecodeErrc::KeyNotPresent;
587+
return obj;
588+
}
589+
} else {
590+
if (dparams.has_secret) {
591+
ec = DecodeErrc::KeyNotRequiredForNoneAlg;
592+
}
574593
}
575594

576595
//throws decode error
@@ -589,7 +608,7 @@ jwt_object decode(const string_view enc_str,
589608

590609
//Verify the signature only if some algorithm was used
591610
if (obj.header().algo() != algorithm::NONE) {
592-
jwt_signature jsign{key};
611+
jwt_signature jsign{dparams.secret};
593612

594613
// Length of the encoded header and payload only.
595614
// Addition of '1' to account for the '.' character.
@@ -617,13 +636,11 @@ jwt_object decode(const string_view enc_str,
617636

618637
template <typename SequenceT, typename... Args>
619638
jwt_object decode(const string_view enc_str,
620-
const string_view key,
621639
const params::detail::algorithms_param<SequenceT>& algos,
622640
Args&&... args)
623641
{
624642
std::error_code ec{};
625643
auto jwt_obj = decode(enc_str,
626-
key,
627644
algos,
628645
ec,
629646
std::forward<Args>(args)...);
@@ -681,6 +698,11 @@ void jwt_throw_exception(const std::error_code& ec)
681698
{
682699
throw SignatureFormatError(ec.message());
683700
}
701+
case DecodeErrc::KeyNotRequiredForNoneAlg:
702+
{
703+
// Not an error. Just to be ignored.
704+
break;
705+
}
684706
default:
685707
{
686708
throw DecodeError(ec.message());

include/jwt/jwt.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ class jwt_object
730730

731731
public: //TODO: Not good
732732
/// Decode parameters
733+
template <typename DecodeParams, typename... Rest>
734+
static void set_decode_params(DecodeParams& dparams, params::detail::secret_param s, Rest&&... args);
735+
733736
template <typename DecodeParams, typename... Rest>
734737
static void set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args);
735738

@@ -766,7 +769,6 @@ jwt_object jwt_decode(const string_view encoded_str, const string_view key, bool
766769
*/
767770
template <typename SequenceT, typename... Args>
768771
jwt_object decode(const string_view enc_str,
769-
const string_view key,
770772
const params::detail::algorithms_param<SequenceT>& algos,
771773
std::error_code& ec,
772774
Args&&... args);
@@ -775,7 +777,6 @@ jwt_object decode(const string_view enc_str,
775777
*/
776778
template <typename SequenceT, typename... Args>
777779
jwt_object decode(const string_view enc_str,
778-
const string_view key,
779780
const params::detail::algorithms_param<SequenceT>& algos,
780781
Args&&... args);
781782

tests/test_jwt_decode

26.2 KB
Binary file not shown.

tests/test_jwt_decode.cc

+10-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ TEST (DecodeTest, InvalidFinalDotForNoneAlg)
99
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ";
1010

1111
std::error_code ec;
12-
auto obj = jwt::decode(inv_enc_str, "", algorithms({"none", "hs256"}), ec);
12+
auto obj = jwt::decode(inv_enc_str, algorithms({"none", "hs256"}), ec);
1313

1414
ASSERT_TRUE (ec);
1515
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::SignatureFormatError));
@@ -22,7 +22,7 @@ TEST (DecodeTest, DecodeNoneAlgSign)
2222
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
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(false));
2626
EXPECT_TRUE (ec);
2727
EXPECT_EQ (ec.value(), static_cast<int>(jwt::AlgorithmErrc::NoneAlgorithmUsed));
2828

@@ -45,7 +45,7 @@ TEST (DecodeTest, DecodeWrongAlgo)
4545
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
4646

4747
std::error_code ec;
48-
auto obj = jwt::decode(enc_str, "", algorithms({"hs256"}), ec, verify(true));
48+
auto obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret(""), verify(true));
4949
EXPECT_TRUE (ec);
5050
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::InvalidAlgorithm));
5151
}
@@ -58,7 +58,7 @@ TEST (DecodeTest, DecodeInvalidHeader)
5858
"ehbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
5959

6060
std::error_code ec;
61-
auto obj = jwt::decode(enc_str, "", algorithms({"hs256"}), ec, verify(true));
61+
auto obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret(""), verify(true));
6262
ASSERT_TRUE (ec);
6363
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::JsonParseError));
6464

@@ -72,7 +72,7 @@ TEST (DecodeTest, DecodeInvalidPayload)
7272
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyfhuWcikiJyaWZ0LmlvIiwiZXhwIsexNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
7373

7474
std::error_code ec;
75-
auto obj = jwt::decode(enc_str, "", algorithms({"none"}), ec, verify(true));
75+
auto obj = jwt::decode(enc_str, algorithms({"none"}), ec, verify(true));
7676
ASSERT_TRUE (ec);
7777

7878
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::JsonParseError));
@@ -88,7 +88,7 @@ TEST (DecodeTest, DecodeHS256)
8888
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
8989

9090
std::error_code ec;
91-
auto obj = jwt::decode(enc_str, "secret", algorithms({"none", "hs256"}), ec, verify(false));
91+
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(false), secret("secret"));
9292
ASSERT_FALSE (ec);
9393

9494
EXPECT_TRUE (obj.has_claim("iss"));
@@ -108,10 +108,10 @@ TEST (DecodeTest, DecodeHS384)
108108
"eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
109109
"cGN4FZCe9Y2c1dA-jP71IXGnYbJRc4OaUTa5m7N7ybF5h6wBwxWQ-pdcxYchjDBL";
110110

111-
const jwt::string_view secret = "0123456789abcdefghijklmnopqrstuvwxyz";
111+
const jwt::string_view key = "0123456789abcdefghijklmnopqrstuvwxyz";
112112

113113
std::error_code ec;
114-
auto obj = jwt::decode(enc_str, secret, algorithms({"none", "hs384"}), ec, verify(false));
114+
auto obj = jwt::decode(enc_str, algorithms({"none", "hs384"}), ec, verify(false), secret(key));
115115
ASSERT_FALSE (ec);
116116

117117
EXPECT_TRUE (obj.has_claim("sub"));
@@ -127,18 +127,17 @@ TEST (DecodeTest, DecodeHS512)
127127
"eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
128128
"vQ-1JSFN1kPjUI3URP6AFK5z8V7xLhyhw-76QWhQg9Xcy-IgrJ-bCTYLBjgaprrcEWwpSnBQnP3QnIxYK0HEaQ";
129129

130-
const jwt::string_view secret = "00112233445566778899";
130+
const jwt::string_view key = "00112233445566778899";
131131

132132
std::error_code ec;
133-
auto obj = jwt::decode(enc_str, secret, algorithms({"none", "hs384", "hs512"}), ec, verify(false));
133+
auto obj = jwt::decode(enc_str, algorithms({"none", "hs384", "hs512"}), ec, verify(false), secret(key));
134134

135135
ASSERT_FALSE (ec);
136136

137137
EXPECT_TRUE (obj.has_claim("sub"));
138138
EXPECT_TRUE (obj.payload().has_claim_with_value("sub", "nothing much"));
139139
}
140140

141-
142141
int main(int argc, char* argv[]) {
143142
::testing::InitGoogleTest(&argc, argv);
144143
return RUN_ALL_TESTS();

0 commit comments

Comments
 (0)