Skip to content

Commit 92cac1e

Browse files
committed
Updated tests and README for RFC Fixes
1 parent 9624da5 commit 92cac1e

12 files changed

+123
-123
lines changed

README.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This assertion can be used in some kind of bearer authentication mechanism that
4343

4444
Few good resources on this material which I found useful are:
4545
<a href="https://scotch.io/tutorials/the-anatomy-of-a-json-web-token">Anatomy of JWT</a>
46-
<a href="https://auth0.com/learn/json-web-tokens/">Learn JWT</a>
46+
<a href="https://auth0.com/learn/json-web-tokens/">Learn JWT</a>
4747
<a href="https://tools.ietf.org/html/rfc7519">RFC 7519</a>
4848

4949

@@ -78,7 +78,7 @@ Few good resources on this material which I found useful are:
7878
std::cout << enc_str << std::endl;
7979

8080
//Decode
81-
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret(key));
81+
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret(key));
8282
std::cout << dec_obj.header() << std::endl;
8383
std::cout << dec_obj.payload() << std::endl;
8484

@@ -96,7 +96,7 @@ Few good resources on this material which I found useful are:
9696
Almost the same API, except for some ugliness here and there. But close enough!
9797
9898
Lets take another example in which we will see to add payload claim having type other than string.
99-
The <code>payload</code> function used in the above example to create <code>jwt_object</code> object can only take strings. For anything else, it will throw a compilation error.
99+
The <code>payload</code> function used in the above example to create <code>jwt_object</code> object can only take strings. For anything else, it will throw a compilation error.
100100
101101
For adding claims having values other than string, <code>jwt_object</code> class provides <code>add_claim</code> API. We will also see few other APIs in the next example. Make sure to read the comments :).
102102
@@ -109,7 +109,7 @@ Few good resources on this material which I found useful are:
109109
int main() {
110110
using namespace jwt::params;
111111
112-
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"user", "admin"}})};
112+
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"user", "admin"}})};
113113
114114
//Use add_claim API to add claim values which are
115115
// _not_ strings.
@@ -313,17 +313,17 @@ All the parameters are basically a function which returns an instance of a type
313313
- Takes in any type which satifies the <strong>SequenceConcept</strong> (see <code>idetail::meta::is_sequence_concept</code>)
314314

315315
```cpp
316-
jwt::decode(algorithms({"none", "hs256", "rs256"}), ...);
317-
316+
jwt::decode(algorithms({"none", "HS256", "RS256"}), ...);
317+
318318
OR
319319

320-
std::vector<std::string> algs{"none", "hs256", "rs256"};
320+
std::vector<std::string> algs{"none", "HS256", "RS256"};
321321
jwt::decode(algorithms(algs), ...);
322322
```
323323
324324
- <strong>secret</strong>
325325
326-
Optional parameter. To be supplied only when the algorithm used is not "NONE". Else would throw/set <code>KeyNotPresentError</code> / <code>KeyNotPresent</code> exception/error.
326+
Optional parameter. To be supplied only when the algorithm used is not "none". Else would throw/set <code>KeyNotPresentError</code> / <code>KeyNotPresent</code> exception/error.
327327
328328
- <strong>leeway</strong>
329329
@@ -365,7 +365,7 @@ All the parameters are basically a function which returns an instance of a type
365365
366366
- <strong>validate_jti</strong>
367367
368-
Optional parameter.
368+
Optional parameter.
369369
Takes a boolean value.
370370
Validates the JTI claim. Only checks for the presence of the claim. If not throws or sets <code>InvalidJTIError</code> or <code>InvalidJTI</code>.
371371
@@ -394,7 +394,7 @@ For the registered claim types the library assumes specific data types for the c
394394
395395
396396
## Advanced Examples
397-
We will see few complete examples which makes use of error code checks and exception handling.
397+
We will see few complete examples which makes use of error code checks and exception handling.
398398
The examples are taken from the "tests" section. Users are requested to checkout the tests to find out more ways to use this library.
399399
400400
Expiration verification example (uses error_code):
@@ -406,7 +406,7 @@ Expiration verification example (uses error_code):
406406
int main() {
407407
using namespace jwt::params;
408408
409-
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
409+
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
410410
obj.add_claim("iss", "arun.muralidharan")
411411
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
412412
;
@@ -415,7 +415,7 @@ int main() {
415415
auto enc_str = obj.signature(ec);
416416
assert (!ec);
417417
418-
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true));
418+
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), verify(true));
419419
assert (ec);
420420
assert (ec.value() == static_cast<int>(jwt::VerificationErrc::TokenExpired));
421421
@@ -432,7 +432,7 @@ Expiration verification example (uses exception):
432432
int main() {
433433
using namespace jwt::params;
434434

435-
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
435+
jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
436436

437437
obj.add_claim("iss", "arun.muralidharan")
438438
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
@@ -441,7 +441,7 @@ int main() {
441441
auto enc_str = obj.signature();
442442

443443
try {
444-
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true));
444+
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true));
445445
} catch (const jwt::TokenExpiredError& e) {
446446
//Handle Token expired exception here
447447
//...
@@ -450,7 +450,7 @@ int main() {
450450
//...
451451
} catch (const jwt::DecodeError& e) {
452452
//Handle all kinds of other decode errors
453-
//...
453+
//...
454454
} catch (const jwt::VerificationError& e) {
455455
// Handle the base verification error.
456456
//NOTE: There are other derived types of verification errors
@@ -472,13 +472,13 @@ Invalid issuer test(uses error_code):
472472
int main() {
473473
using namespace jwt::params;
474474

475-
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})};
475+
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})};
476476

477477
std::error_code ec;
478478
auto enc_str = obj.signature(ec);
479479
assert (!ec);
480480

481-
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), issuer("arun.muralidharan"));
481+
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret("secret"), issuer("arun.muralidharan"));
482482
assert (ec);
483483

484484
assert (ec.value() == static_cast<int>(jwt::VerificationErrc::InvalidIssuer));
@@ -566,7 +566,7 @@ The error codes are divided into different categories:
566566
TypeConversionError,
567567
};
568568
```
569-
569+
570570
<strong>Exceptions:</strong>
571571
There are exception types created for almost all the error codes above.
572572
@@ -588,7 +588,7 @@ There are exception types created for almost all the error codes above.
588588
589589
- KeyNotPresentError
590590
591-
Thrown if key/secret is not passed in with the decode API if the algorithm used is something other than "NONE".
591+
Thrown if key/secret is not passed in with the decode API if the algorithm used is something other than "none".
592592
593593
- VerificationError
594594
@@ -644,7 +644,7 @@ int main() {
644644
645645
auto dec_obj = jwt::decode(enc_str, algorithms({"none"}), ec, verify(false));
646646
647-
// Should not be a hard error in general
647+
// Should not be a hard error in general
648648
assert (ec.value() == static_cast<int>(jwt::AlgorithmErrc::NoneAlgorithmUsed));
649649
}
650650
```

examples/simple_ex2.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
int main() {
77
using namespace jwt::params;
88

9-
jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"user", "admin"}})};
9+
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"user", "admin"}})};
1010

1111
//Use add_claim API to add claim values which are
1212
// _not_ strings.

examples/simple_ex3_rsa.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main() {
4444

4545
auto priv_key = read_from_file(priv_key_path);
4646

47-
jwt::jwt_object obj{algorithm("rs256"), secret(priv_key), payload({{"user", "admin"}})};
47+
jwt::jwt_object obj{algorithm("RS256"), secret(priv_key), payload({{"user", "admin"}})};
4848

4949
//Use add_claim API to add claim values which are
5050
// _not_ strings.
@@ -76,7 +76,7 @@ int main() {
7676
return 1;
7777
}
7878

79-
auto dec_obj = jwt::decode(sign, algorithms({"rs256"}), verify(false), secret(pub_key));
79+
auto dec_obj = jwt::decode(sign, algorithms({"RS256"}), verify(false), secret(pub_key));
8080

8181
return 0;
8282
}

include/jwt/test/test_jwt_decode.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void basic_decode_test()
2121
using namespace jwt::params;
2222

2323
std::cout << "DECODE: \n";
24-
jwt::decode(res, algorithms({"none", "hs256"}), ec, verify(false), secret("secret"));
24+
jwt::decode(res, algorithms({"none", "HS256"}), ec, verify(false), secret("secret"));
2525
}
2626

2727
int main() {

include/jwt/test/test_jwt_object.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void basic_jwt_object_test()
1111
using namespace jwt::params;
1212
jwt::jwt_object obj(payload({
1313
{"a", "b"},
14-
{"c", "d"}
14+
{"c", "d"}
1515
}));
1616

1717
//check with std::map
@@ -20,7 +20,7 @@ void basic_jwt_object_test()
2020
m["c"] = "d";
2121

2222
jwt::jwt_object obj1{payload(m)};
23-
23+
2424
auto obj2 = std::move(obj1);
2525

2626
std::cout << obj2.payload() << std::endl;
@@ -43,23 +43,23 @@ void basic_jwt_object_test()
4343
std::cout << obj3.payload() << std::endl;
4444

4545
obj3.secret("secret");
46-
obj3.header().algo("hs256");
46+
obj3.header().algo("HS256");
4747

48-
auto dec_obj = jwt::decode(obj3.signature(), algorithms({"hs256"}), secret("secret"));
48+
auto dec_obj = jwt::decode(obj3.signature(), algorithms({"HS256"}), secret("secret"));
4949
}
5050

5151
void jwt_object_pem_test()
5252
{
5353
using namespace jwt::params;
5454

55-
std::string pub_key =
55+
std::string pub_key =
5656
R"(-----BEGIN PUBLIC KEY-----
5757
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEomxC9ycc8AkXSwWQpu1kN5Fmgy/sD/KJ
5858
qN3tlSZmUEZ3w3c6KYJfK97PMOSZQaUdeydBoq/IOglQQOj8zLqubq5IpaaUiDQ5
5959
0eJg79PvXuLiVUH98cBL/o8sDVB/sGzz
6060
-----END PUBLIC KEY-----)";
6161

62-
std::string priv_key =
62+
std::string priv_key =
6363
R"(-----BEGIN EC PRIVATE KEY-----
6464
MIGkAgEBBDBeLCgapjZmvTatMHaYX3A02+0Ys3Tr8kda+E9DFnmCSiCOEig519fT
6565
13edeU8YdDugBwYFK4EEACKhZANiAASibEL3JxzwCRdLBZCm7WQ3kWaDL+wP8omo
@@ -79,10 +79,10 @@ MIGkAgEBBDBeLCgapjZmvTatMHaYX3A02+0Ys3Tr8kda+E9DFnmCSiCOEig519fT
7979
;
8080

8181
std::cout << "pem sign " << obj.signature() << std::endl;
82-
std::cout << "Get claim value for exp: " <<
82+
std::cout << "Get claim value for exp: " <<
8383
obj.payload().get_claim_value<uint64_t>("exp") << std::endl;
8484

85-
auto dec_obj = jwt::decode(obj.signature(), algorithms({"es256"}), secret(pub_key));
85+
auto dec_obj = jwt::decode(obj.signature(), algorithms({"ES256"}), secret(pub_key));
8686
std::cout << dec_obj.payload() << std::endl;
8787
}
8888

tests/test_jwt_decode.cc

+16-16
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
TEST (DecodeTest, InvalidFinalDotForNoneAlg)
66
{
77
using namespace jwt::params;
8-
const char* inv_enc_str =
8+
const char* inv_enc_str =
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));
@@ -18,7 +18,7 @@ TEST (DecodeTest, InvalidFinalDotForNoneAlg)
1818
TEST (DecodeTest, DecodeNoneAlgSign)
1919
{
2020
using namespace jwt::params;
21-
const char* enc_str =
21+
const char* enc_str =
2222
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjo0NTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
2323

2424
std::error_code ec;
@@ -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, secret(""), 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, secret(""), 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, DecodeEmptyHeader)
7272
".eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
7373

7474
std::error_code ec;
75-
auto obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret(""), verify(true));
75+
auto obj = jwt::decode(enc_str, algorithms({"HS256"}), ec, secret(""), verify(true));
7676
ASSERT_TRUE (ec);
7777
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::JsonParseError));
7878

@@ -82,7 +82,7 @@ TEST (DecodeTest, DecodeInvalidPayload)
8282
{
8383
using namespace jwt::params;
8484

85-
const char* enc_str =
85+
const char* enc_str =
8686
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyfhuWcikiJyaWZ0LmlvIiwiZXhwIsexNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
8787

8888
std::error_code ec;
@@ -95,14 +95,14 @@ TEST (DecodeTest, DecodeInvalidPayload)
9595
TEST (DecodeTest, DecodeHS256)
9696
{
9797
using namespace jwt::params;
98-
98+
9999
const char* enc_str =
100100
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
101101
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
102102
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
103103

104104
std::error_code ec;
105-
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(false), secret("secret"));
105+
auto obj = jwt::decode(enc_str, algorithms({"none", "HS256"}), ec, verify(false), secret("secret"));
106106
ASSERT_FALSE (ec);
107107

108108
EXPECT_TRUE (obj.has_claim("iss"));
@@ -119,13 +119,13 @@ TEST (DecodeTest, SecretKeyNotPassed)
119119
{
120120
using namespace jwt::params;
121121

122-
const char* enc_str =
122+
const char* enc_str =
123123
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
124124
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
125125
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
126126

127127
std::error_code ec;
128-
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(true));
128+
auto obj = jwt::decode(enc_str, algorithms({"none", "HS256"}), ec, verify(true));
129129

130130
ASSERT_TRUE (ec);
131131
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::KeyNotPresent));
@@ -143,7 +143,7 @@ TEST (DecodeTest, DecodeHS384)
143143
const jwt::string_view key = "0123456789abcdefghijklmnopqrstuvwxyz";
144144

145145
std::error_code ec;
146-
auto obj = jwt::decode(enc_str, algorithms({"none", "hs384"}), ec, verify(false), secret(key));
146+
auto obj = jwt::decode(enc_str, algorithms({"none", "HS384"}), ec, verify(false), secret(key));
147147
ASSERT_FALSE (ec);
148148

149149
EXPECT_TRUE (obj.has_claim("sub"));
@@ -154,15 +154,15 @@ TEST (DecodeTest, DecodeHS512)
154154
{
155155
using namespace jwt::params;
156156

157-
const char* enc_str =
157+
const char* enc_str =
158158
"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9."
159159
"eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
160160
"vQ-1JSFN1kPjUI3URP6AFK5z8V7xLhyhw-76QWhQg9Xcy-IgrJ-bCTYLBjgaprrcEWwpSnBQnP3QnIxYK0HEaQ";
161161

162162
const jwt::string_view key = "00112233445566778899";
163163

164164
std::error_code ec;
165-
auto obj = jwt::decode(enc_str, algorithms({"none", "hs384", "hs512"}), ec, verify(false), secret(key));
165+
auto obj = jwt::decode(enc_str, algorithms({"none", "HS384", "HS512"}), ec, verify(false), secret(key));
166166

167167
ASSERT_FALSE (ec);
168168

@@ -174,13 +174,13 @@ TEST (DecodeTest, TypHeaderMiss)
174174
{
175175
using namespace jwt::params;
176176

177-
const char* enc_str =
177+
const char* enc_str =
178178
"eyJhbGciOiJIUzI1NiJ9."
179179
"eyJleHAiOjE1MzM0NjE1NTMsImlhdCI6MTUxMzg2MjM3MSwiaWQiOiJhLWItYy1kLWUtZi0xLTItMyIsImlzcyI6ImFydW4ubXVyYWxpZGhhcmFuIiwic3ViIjoiYWRtaW4ifQ."
180180
"pMWBLSWl1p4V958lfe_6ZhvgFMOQv9Eq5mlndVKFKkA";
181181

182182
std::error_code ec;
183-
auto obj = jwt::decode(enc_str, algorithms({"none", "hs256"}), ec, verify(false));
183+
auto obj = jwt::decode(enc_str, algorithms({"none", "HS256"}), ec, verify(false));
184184
std::cout << "Decode header: " << obj.header() << std::endl;
185185

186186
EXPECT_FALSE (ec);

0 commit comments

Comments
 (0)