Debian Codegraph @

tests/test_jwt_encode.cc

raw
1 #include <iostream>
2 #include <map>
3 #include <chrono>
4 #include "gtest/gtest.h"
5 #include "jwt/jwt.hpp"
6
7 TEST (EncodeTest, TestRemoveClaim)
8 {
9 using namespace jwt::params;
10
11 jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
12
13 obj.add_claim("iss", "arun.muralidharan")
14 .add_claim("sub", "admin")
15 .add_claim("id", "a-b-c-d-e-f-1-2-3")
16 .add_claim("iat", 1513862371)
17 .add_claim("exp", std::chrono::system_clock::now());
18
19 EXPECT_TRUE (obj.has_claim(jwt::registered_claims::expiration));
20
21 obj.remove_claim("exp");
22 EXPECT_FALSE (obj.has_claim(jwt::registered_claims::expiration));
23
24 obj.remove_claim(jwt::registered_claims::subject);
25 EXPECT_FALSE (obj.has_claim("sub"));
26 }
27
28 TEST (EncodeTest, TestRemoveTypHeader)
29 {
30 using namespace jwt::params;
31
32 jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
33
34 obj.add_claim("iss", "arun.muralidharan")
35 .add_claim("sub", "admin")
36 .add_claim("id", "a-b-c-d-e-f-1-2-3")
37 .add_claim("iat", 1513862371)
38 .add_claim("exp", std::chrono::system_clock::now());
39
40 EXPECT_TRUE (obj.header().has_header("typ"));
41 obj.header().remove_header("typ");
42 EXPECT_FALSE (obj.header().has_header("typ"));
43
44 std::cout << "Header: " << obj.header() << '\n';
45 std::cout << "Signature: " << obj.signature() << '\n';
46 }
47
48 TEST (EncodeTest, StrEncodeHS256_1)
49 {
50 using namespace jwt::params;
51
52 const char* expected_sign =
53 "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
54 "eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
55 "jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
56
57 jwt::jwt_object obj{algorithm("HS256"), secret("secret")};
58
59 obj.add_claim("iss", "arun.muralidharan")
60 .add_claim("sub", "admin")
61 .add_claim("id", "a-b-c-d-e-f-1-2-3")
62 .add_claim("iat", 1513862371)
63 ;
64
65 std::cout << "Header: " << obj.header() << std::endl;
66 std::cout << "Payload: "<< obj.payload() << std::endl;
67
68 std::string enc_str = obj.signature();
69
70 std::cout << "Signature: " << enc_str << std::endl;
71
72 EXPECT_EQ (enc_str, expected_sign);
73 }
74
75 TEST (EncodeTest, StrEncodeHS256_2)
76 {
77 using namespace jwt::params;
78
79 const char* expected_sign =
80 "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
81 "eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
82 "jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
83
84 jwt::jwt_object obj{algorithm("HS256"),
85 secret("secret"),
86 payload(
87 {
88 {"iss", "arun.muralidharan"},
89 {"sub", "admin"},
90 {"id", "a-b-c-d-e-f-1-2-3"}
91 })
92 };
93
94 obj.add_claim("iat", 1513862371);
95
96 std::string enc_str = obj.signature();
97 EXPECT_EQ (enc_str, expected_sign);
98 }
99
100
101 TEST (EncodeTest, StrEncodeNONE)
102 {
103 using namespace jwt::params;
104
105 const char* expected_sign =
106 "eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
107
108 jwt::jwt_object obj{algorithm("none")};
109
110 obj.add_claim("aud", "rift.io")
111 .add_claim("exp", 1513863371)
112 .add_claim("sub", "nothing much")
113 ;
114
115 std::cout << "Header: " << obj.header() << std::endl;
116 std::cout << "Payload: " << obj.payload() << std::endl;
117
118 std::string enc_str = obj.signature();
119
120 EXPECT_EQ (enc_str, expected_sign);
121 }
122
123 TEST (EncodeTest, StrEncodeHS256WithKey)
124 {
125 using namespace jwt::params;
126
127 const char* expected_sign =
128 "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
129 "eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
130 "W6t7mUX6ZJwOVTsVhHSKyBSwi0wnibobdsk456wSmJg";
131
132 jwt::jwt_object obj{algorithm(jwt::algorithm::HS256),
133 secret("0123456789abcdefghijklmnopqrstuvwxyz"),
134 payload(
135 {
136 {"aud", "rift.io"},
137 {"sub", "nothing much"}
138 })
139 };
140 obj.add_claim("exp", 1513863371);
141
142 std::string enc_str = obj.signature();
143
144 EXPECT_EQ (expected_sign, enc_str);
145 }
146
147 TEST (EncodeTest, StrEncodeHS384WithKey)
148 {
149
150 using namespace jwt::params;
151
152 const char* expected_sign =
153 "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9."
154 "eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
155 "cGN4FZCe9Y2c1dA-jP71IXGnYbJRc4OaUTa5m7N7ybF5h6wBwxWQ-pdcxYchjDBL";
156
157 jwt::jwt_object obj{algorithm(jwt::algorithm::HS384),
158 secret("0123456789abcdefghijklmnopqrstuvwxyz"),
159 payload(
160 {
161 {"aud", "rift.io"},
162 {"sub", "nothing much"}
163 })
164 };
165 obj.add_claim("exp", 1513863371);
166
167 std::string enc_str = obj.signature();
168
169 EXPECT_EQ (expected_sign, enc_str);
170 }
171
172 TEST (EncodeTest, StrEncodeHS512WithKey)
173 {
174 using namespace jwt::params;
175
176 const char* expected_sign =
177 "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9."
178 "eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
179 "vQ-1JSFN1kPjUI3URP6AFK5z8V7xLhyhw-76QWhQg9Xcy-IgrJ-bCTYLBjgaprrcEWwpSnBQnP3QnIxYK0HEaQ";
180
181 jwt::string_view key = "00112233445566778899";
182
183 std::map<jwt::string_view, jwt::string_view> p;
184 p["aud"] = "rift.io";
185 p["sub"] = "nothing much";
186
187 jwt::jwt_object obj{algorithm(jwt::algorithm::HS512),
188 secret(key),
189 payload(std::move(p))
190 };
191 obj.add_claim("exp", 1513863371);
192
193 std::string enc_str = obj.signature();
194
195 EXPECT_EQ (enc_str, expected_sign);
196 }
197
198 TEST (EncodeTest, StrEncodeChangeAlg)
199 {
200 using namespace jwt::params;
201
202 const char* expected_none_sign =
203 "eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
204
205 jwt::string_view key = "00112233445566778899";
206
207 std::map<std::string, jwt::string_view> p;
208 p["aud"] = "rift.io";
209 p["sub"] = "nothing much";
210
211 jwt::jwt_object obj{algorithm(jwt::algorithm::HS512),
212 secret(key),
213 payload(std::move(p))
214 };
215 obj.add_claim("exp", 1513863371);
216
217 obj.header().algo("none");
218 std::string enc_str = obj.signature();
219
220 EXPECT_EQ (expected_none_sign, enc_str);
221 }
222
223 TEST (EncodeTest, StrEncodeNoKey)
224 {
225 using namespace jwt::params;
226
227 jwt::jwt_object obj{algorithm(jwt::algorithm::HS512),
228 payload({{"iss", "arn-ml"}})
229 };
230
231 std::error_code ec;
232 std::string enc_str = obj.signature(ec);
233
234 ASSERT_TRUE (ec);
235 EXPECT_EQ (ec.value(), static_cast<int>(jwt::AlgorithmErrc::KeyNotFoundErr));
236 }
237
238 TEST (EncodeTest, StrEncodeNoneAlgWithKey)
239 {
240 using namespace jwt::params;
241
242 const jwt::string_view secret1 = "abcdefghijklmnopqrstuvwxyz";
243 const jwt::string_view secret2 = "0123456789qwertybabe";
244
245 jwt::jwt_object obj{algorithm("none"),
246 payload({{"iss", "arn-ml"}}),
247 secret(secret1)};
248
249 std::error_code ec;
250 std::string enc_str1 = obj.signature(ec);
251 ASSERT_FALSE (ec);
252
253 obj.secret(secret2);
254 std::string enc_str2 = obj.signature(ec);
255 ASSERT_FALSE (ec);
256
257 EXPECT_EQ (enc_str1, enc_str2);
258 }
259
260 TEST (EncodeTest, OverwriteClaimsTest)
261 {
262 using namespace jwt::params;
263
264 jwt::jwt_object obj{algorithm("none"),
265 payload({
266 {"iss", "arn-ml"},
267 {"x-pld1", "data1"},
268 {"x-pld2", "data2"},
269 {"x-pld3", "123"}
270 })
271 };
272
273 bool ret = obj.payload().add_claim("x-pld1", "1data");
274 EXPECT_FALSE (ret);
275
276 ret = obj.payload().add_claim("x-pld1", "1data", true/*overwrite*/);
277 EXPECT_TRUE (ret);
278
279 EXPECT_TRUE (obj.payload().has_claim_with_value("x-pld1", "1data"));
280 }
281
282 TEST (EncodeTest, HeaderParamTest)
283 {
284 using namespace jwt::params;
285
286 jwt::jwt_object obj{
287 headers({
288 {"alg", "none"},
289 {"typ", "jwt"},
290 }),
291 payload({
292 {"iss", "arun.muralidharan"},
293 {"sub", "nsfw"},
294 {"x-pld", "not my ex"}
295 })
296 };
297
298 bool ret = obj.header().add_header("kid", 1234567);
299 EXPECT_TRUE (ret);
300
301 ret = obj.header().add_header("crit", std::array<std::string, 1>{ {"exp"} });
302 EXPECT_TRUE (ret);
303
304 std::cout << obj.header() << std::endl;
305
306 std::error_code ec;
307 auto enc_str = obj.signature();
308
309 auto dec_obj = jwt::decode(enc_str, algorithms({"none"}), ec, verify(true));
310 EXPECT_EQ (ec.value(), static_cast<int>(jwt::AlgorithmErrc::NoneAlgorithmUsed));
311
312 std::cout << dec_obj.header() << std::endl;
313 }
314