-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathJsonconsTest.cpp
267 lines (229 loc) · 7.55 KB
/
JsonconsTest.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#define JWT_DISABLE_PICOJSON
#define JSONCONS_NO_DEPRECATED
#include "jwt-cpp/jwt.h"
#include <gtest/gtest.h>
#include <jsoncons/json.hpp>
#include <sstream>
struct jsoncons_traits
{
using json = jsoncons::json;
using value_type = json;
struct object_type : json::object
{
using value_type = key_value_type;
using mapped_type = key_value_type::value_type; // https://github.com/danielaparker/jsoncons/commit/1b1ceeb572f9a2db6d37cff47ac78a4f14e072e2#commitcomment-45391411
object_type() = default;
object_type(const object_type &) = default;
object_type(const json::object &o) : json::object(o) {}
object_type(object_type &&) = default;
object_type(json::object &&o) : json::object(o) {}
object_type &operator=(const object_type &o)
{
// avoid private deleted copy operator= https://github.com/danielaparker/jsoncons/pull/298
object_type t(static_cast<const json::object &>(o));
(*this) = std::move(t);
return *this;
}
object_type &operator=(object_type &&o) noexcept
{
swap(o);
return *this;
}
mapped_type &operator[](const key_type &key)
{
auto ret = try_emplace(key); // https://github.com/microsoft/STL/blob/2914b4301c59dc7ffc09d16ac6f7979fde2b7f2c/stl/inc/map#L325
return ret.first->value();
}
mapped_type &operator[](key_type &&key)
{
auto ret = try_emplace(key);
return ret.first->value();
}
const mapped_type &at(const key_type &key) const
{
auto target = find(key);
if (target != end())
{
return target->value();
}
std::out_of_range("invalid key");
}
size_t count(const key_type &key) const
{
size_t ret = 0;
for (const_iterator first = begin(); first != end(); ++first)
{
if (first->key() == key)
{
++ret;
}
}
return ret;
}
};
using array_type = json::array;
using string_type = std::string; // current limitation of traits implementation
using number_type = double;
using integer_type = int64_t;
using boolean_type = bool;
static jwt::json::type get_type(const json &val)
{
using jwt::json::type;
if (val.type() == jsoncons::json_type::bool_value)
return type::boolean;
if (val.type() == jsoncons::json_type::int64_value)
return type::integer;
if (val.type() == jsoncons::json_type::uint64_value)
return type::integer;
if (val.type() == jsoncons::json_type::half_value)
return type::number;
if (val.type() == jsoncons::json_type::double_value)
return type::number;
if (val.type() == jsoncons::json_type::string_value)
return type::string;
if (val.type() == jsoncons::json_type::array_value)
return type::array;
if (val.type() == jsoncons::json_type::object_value)
return type::object;
throw std::logic_error("invalid type");
}
static object_type as_object(const json &val)
{
if (val.type() != jsoncons::json_type::object_value)
throw std::bad_cast();
return object_type(val.object_value());
}
static array_type as_array(const json &val)
{
if (val.type() != jsoncons::json_type::array_value)
throw std::bad_cast();
return val.array_value();
}
static string_type as_string(const json &val)
{
if (val.type() != jsoncons::json_type::string_value)
throw std::bad_cast();
return val.as_string();
}
static number_type as_number(const json &val)
{
if (get_type(val) != jwt::json::type::number)
throw std::bad_cast();
return val.as_double();
}
static integer_type as_int(const json &val)
{
if (get_type(val) != jwt::json::type::integer)
throw std::bad_cast();
return val.as<integer_type>();
}
static boolean_type as_bool(const json &val)
{
if (val.type() != jsoncons::json_type::bool_value)
throw std::bad_cast();
return val.as_bool();
}
static bool parse(json &val, std::string str)
{
val = json::parse(str);
return true;
}
static std::string serialize(const json &val)
{
std::ostringstream os;
os << jsoncons::print(val);
return os.str();
}
};
TEST(JsonconsTest, BasicClaims)
{
using jsoncons_claim = jwt::basic_claim<jsoncons_traits>;
const auto string = jsoncons_claim(std::string("string"));
const auto array = jsoncons_claim(std::set<std::string>{"string", "string"});
const auto integer = jsoncons_claim(159816816);
}
TEST(JsonconsTest, AudienceAsString)
{
std::string token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0."
"WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4";
auto decoded = jwt::decode<jsoncons_traits>(token);
ASSERT_TRUE(decoded.has_algorithm());
ASSERT_TRUE(decoded.has_type());
ASSERT_FALSE(decoded.has_content_type());
ASSERT_FALSE(decoded.has_key_id());
ASSERT_FALSE(decoded.has_issuer());
ASSERT_FALSE(decoded.has_subject());
ASSERT_TRUE(decoded.has_audience());
ASSERT_FALSE(decoded.has_expires_at());
ASSERT_FALSE(decoded.has_not_before());
ASSERT_FALSE(decoded.has_issued_at());
ASSERT_FALSE(decoded.has_id());
ASSERT_EQ("HS256", decoded.get_algorithm());
ASSERT_EQ("JWT", decoded.get_type());
auto aud = decoded.get_audience();
ASSERT_EQ(1, aud.size());
ASSERT_EQ("test", *aud.begin());
}
TEST(JsonconsTest, SetArray)
{
std::vector<int64_t> vect = {
100,
20,
10};
auto token = jwt::create<jsoncons_traits>()
.set_payload_claim("test", jwt::basic_claim<jsoncons_traits>(vect.begin(), vect.end()))
.sign(jwt::algorithm::none{});
ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0.");
}
TEST(JsonconsTest, SetObject)
{
std::istringstream iss{"{\"api-x\": [1]}"};
jwt::basic_claim<jsoncons_traits> object;
iss >> object;
ASSERT_EQ(object.get_type(), jwt::json::type::object);
auto token = jwt::create<jsoncons_traits>()
.set_payload_claim("namespace", object)
.sign(jwt::algorithm::hs256("test"));
ASSERT_EQ(token, "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA");
}
TEST(JsonconsTest, VerifyTokenHS256)
{
std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
auto verify = jwt::verify<jwt::default_clock, jsoncons_traits>({})
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_issuer("auth0");
auto decoded_token = jwt::decode<jsoncons_traits>(token);
verify.verify(decoded_token);
}
TEST(JsonconsTest, VerifyTokenExpirationValid)
{
const auto token = jwt::create<jsoncons_traits>()
.set_issuer("auth0")
.set_issued_at(std::chrono::system_clock::now())
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{3600})
.sign(jwt::algorithm::hs256{"secret"});
auto verify = jwt::verify<jwt::default_clock, jsoncons_traits>({})
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_issuer("auth0");
auto decoded_token = jwt::decode<jsoncons_traits>(token);
verify.verify(decoded_token);
}
TEST(JsonconsTest, VerifyTokenExpired)
{
const auto token = jwt::create<jsoncons_traits>()
.set_issuer("auth0")
.set_issued_at(std::chrono::system_clock::now() - std::chrono::seconds{3601})
.set_expires_at(std::chrono::system_clock::now() - std::chrono::seconds{1})
.sign(jwt::algorithm::hs256{"secret"});
auto verify = jwt::verify<jwt::default_clock, jsoncons_traits>({})
.allow_algorithm(jwt::algorithm::hs256{"secret"})
.with_issuer("auth0");
auto decoded_token = jwt::decode<jsoncons_traits>(token);
ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception);
std::error_code ec;
ASSERT_NO_THROW(verify.verify(decoded_token, ec));
ASSERT_TRUE(!(!ec));
ASSERT_EQ(ec.category(), jwt::error::token_verification_error_category());
ASSERT_EQ(ec.value(), static_cast<int>(jwt::error::token_verification_error::token_expired));
}