forked from pokowaka/jwt-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.cpp
137 lines (116 loc) · 4.47 KB
/
sample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <sstream>
#include <string>
#include "gtest/gtest.h"
#include "jwt/jwt_all.h"
TEST(Sample, sign) {
// Setup a signer
HS256Validator signer("secret!");
// Create the json payload that expires 01/01/2017 @ 12:00am (UTC)
::json json = {{"sub", "subject"}, {"exp", 1483228800}};
// Let's encode the token to a char*
auto token = JWT::Encode(signer, json);
EXPECT_NE(nullptr, token.c_str());
}
TEST(Sample, invalid_tokens) {
// Improper formatted tokens will result in an exception;
try {
JWT::Decode("ceci n'est pas une jwt");
FAIL();
} catch (TokenFormatError &tfe) {
}
}
TEST(Sample, payload_deserialize) {
// Let's use the HS256 signer & validator.
HS256Validator signer("secret");
// Setup the json payload we want to use
::json json = {{"sub", "subject"}, {"exp", time(NULL) + 360000}};
// Encode the jwt token.
auto str_token = JWT::Encode(signer, json);
// Use the expiration validator
ExpValidator exp;
// Decode and validate the token
::json header, payload;
try {
// Note in C++ 17 you can use: auto [ header, payload ] instead of tie.
std::tie(header, payload) = JWT::Decode(str_token, &signer, &exp);
} catch (TokenFormatError &tfe) {
// Badly encoded token
FAIL();
}
// We can also decode with using a validator, you can use this to
// inspect tokens that are not properly signed.
std::tie(header, payload) = JWT::Decode(str_token);
EXPECT_STREQ(json.dump().c_str(), payload.dump().c_str());
}
TEST(Sample, from_json) {
// Let's create a signed token, issued by foo that expires 01/01/2040 @
// 12:00am (UTC)
HS256Validator signer("safe");
::json json = {{"iss", "foo"}, {"exp", 2208988800}};
auto str_token = JWT::Encode(signer, json);
// Let's setup a claim validator where we will accept tokens that
// are have been issues by either foo or bar
// and have an optional expiration claim with a leeway of 32s.
std::string json_claim =
"{ \"all\" : "
" [ "
" { \"optional\" : { \"exp\" : { \"leeway\" : 32} } },"
" { \"iss\" : [\"foo\", \"bar\"] }"
" ]"
"}";
// Lets build the claim validator
claim_ptr claim_validator(ClaimValidatorFactory::Build(json_claim));
// Next we are going to setup the message validators. We will accept
// the HS256 & HS512 validators with the given secrets.
std::string json_validators =
"{ \"set\" : [ "
" { \"HS256\" : { \"secret\" : \"safe\" } }, "
" { \"HS512\" : { \"secret\" : \"supersafe\" } }"
" ]"
"}";
validator_ptr message_validator(
MessageValidatorFactory::Build(json_validators));
// Now let's use these validators to parse and verify the token we
// created above
try {
::json header, payload;
// Note in C++ 17 you can use: auto [ header, payload ] instead of tie.
std::tie(header, payload) = JWT::Decode(
str_token, message_validator.get(), claim_validator.get());
std::cout << "Header: " << header << std::endl;
std::cout << "Payload: " << payload << std::endl;
} catch (InvalidTokenError &tfe) {
std::cout << tfe.what() << std::endl;
// Bad token
FAIL();
}
}
TEST(Sample, kid) {
// Let's create a signed token, issued by foo that expires 01/01/2040 @
// 12:00am (UTC)
HS256Validator signer("safe");
::json json = {{"iss", "foo"}, {"exp", 2208988800}};
// Lets add a header with a specific key id field set
::json keyid = {{"kid", "key_id_1"}};
auto token = JWT::Encode(signer, json, keyid);
// Next we are going to setup the message validators.
// We will accept the various key ids that are mapped to the
// their corresponding validator.
std::string json_validators =
"{ \"kid\" : { "
" \"key_id_1\" : { \"HS256\" : { \"secret\" : \"safe\" } }, "
" \"key_id_2\" : { \"HS256\" : { \"secret\" : \"supersafe\" } }"
" }"
"}";
validator_ptr message_validator(
MessageValidatorFactory::Build(json_validators));
// Now let's use these validators to parse and verify the token we
// created above
try {
::json header, payload;
// Note in C++ 17 you can use: auto [ header, payload ] instead of tie.
std::tie(header, payload) = JWT::Decode(token, message_validator.get());
} catch (InvalidTokenError &ite) {
FAIL();
}
}