forked from arun11299/cpp-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameters.hpp
451 lines (367 loc) · 8.32 KB
/
parameters.hpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
Copyright (c) 2017 Arun Muralidharan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef CPP_JWT_PARAMETERS_HPP
#define CPP_JWT_PARAMETERS_HPP
#include <map>
#include <chrono>
#include <string>
#include <vector>
#include <utility>
#include <unordered_map>
#include "jwt/algorithm.hpp"
#include "jwt/detail/meta.hpp"
#include <string_view>
namespace jwt {
using system_time_t = std::chrono::time_point<std::chrono::system_clock>;
namespace params {
namespace detail {
/**
* Parameter for providing the payload.
* Takes a Mapping concept representing
* key-value pairs.
*
* NOTE: MappingConcept allows only strings
* for both keys and values. Use `add_header`
* API of `jwt_object` otherwise.
*
* Modeled as ParameterConcept.
*/
template <typename MappingConcept>
struct payload_param
{
payload_param(MappingConcept&& mc)
: payload_(std::forward<MappingConcept>(mc))
{}
MappingConcept get() && { return std::move(payload_); }
const MappingConcept& get() const& { return payload_; }
MappingConcept payload_;
};
/**
* Parameter for providing the secret key.
* Stores only the view of the provided string
* as string_view. Later the implementation may or
* may-not copy it.
*
* Modeled as ParameterConcept.
*/
struct secret_param
{
secret_param(std::string_view sv)
: secret_(sv)
{}
std::string_view get() { return secret_; }
std::string_view secret_;
};
template <typename T>
struct secret_function_param
{
T get() const { return fun_; }
template <typename U>
std::string get(U&& u) const { return fun_(u);}
T fun_;
};
/**
* Parameter for providing the algorithm to use.
* The parameter can accept either the string representation
* or the enum class.
*
* Modeled as ParameterConcept.
*/
struct algorithm_param
{
algorithm_param(std::string_view alg)
: alg_(str_to_alg(alg))
{}
algorithm_param(jwt::algorithm alg)
: alg_(alg)
{}
jwt::algorithm get() const noexcept
{
return alg_;
}
typename jwt::algorithm alg_;
};
/**
* Parameter for providing additional headers.
* Takes a mapping concept representing
* key-value pairs.
*
* Modeled as ParameterConcept.
*/
template <typename MappingConcept>
struct headers_param
{
headers_param(MappingConcept&& mc)
: headers_(std::forward<MappingConcept>(mc))
{}
MappingConcept get() && { return std::move(headers_); }
const MappingConcept& get() const& { return headers_; }
MappingConcept headers_;
};
/**
*/
struct verify_param
{
verify_param(bool v)
: verify_(v)
{}
bool get() const { return verify_; }
bool verify_;
};
/**
*/
template <typename Sequence>
struct algorithms_param
{
algorithms_param(Sequence&& seq)
: seq_(std::forward<Sequence>(seq))
{}
Sequence get() && { return std::move(seq_); }
const Sequence& get() const& { return seq_; }
Sequence seq_;
};
/**
*/
struct leeway_param
{
leeway_param(uint32_t v)
: leeway_(v)
{}
uint32_t get() const noexcept { return leeway_; }
uint32_t leeway_;
};
/**
*/
struct audience_param
{
audience_param(std::string aud)
: aud_(std::move(aud))
{}
const std::string& get() const& noexcept { return aud_; }
std::string get() && noexcept { return aud_; }
std::string aud_;
};
/**
*/
struct issuer_param
{
issuer_param(std::string iss)
: iss_(std::move(iss))
{}
const std::string& get() const& noexcept { return iss_; }
std::string get() && noexcept { return iss_; }
std::string iss_;
};
/**
*/
struct subject_param
{
subject_param(std::string sub)
: sub_(std::move(sub))
{}
const std::string& get() const& noexcept { return sub_; }
std::string get() && noexcept { return sub_; }
std::string sub_;
};
/**
*/
struct validate_iat_param
{
validate_iat_param(bool v)
: iat_(v)
{}
bool get() const noexcept { return iat_; }
bool iat_;
};
/**
*/
struct validate_jti_param
{
validate_jti_param(bool v)
: jti_(v)
{}
bool get() const noexcept { return jti_; }
bool jti_;
};
/**
*/
struct nbf_param
{
nbf_param(const jwt::system_time_t tp)
: duration_(std::chrono::duration_cast<
std::chrono::seconds>(tp.time_since_epoch()).count())
{}
nbf_param(const uint64_t epoch)
: duration_(epoch)
{}
uint64_t get() const noexcept { return duration_; }
uint64_t duration_;
};
} // END namespace detail
// Useful typedef
using param_init_list_t = std::initializer_list<std::pair<std::string_view, std::string_view>>;
using param_seq_list_t = std::initializer_list<std::string_view>;
/**
*/
inline detail::payload_param<std::unordered_map<std::string, std::string>>
payload(const param_init_list_t& kvs)
{
std::unordered_map<std::string, std::string> m;
for (const auto& elem : kvs) {
m.emplace(elem.first.data(), elem.second.data());
}
return { std::move(m) };
}
/**
*/
template <typename MappingConcept>
detail::payload_param<MappingConcept>
payload(MappingConcept&& mc)
{
static_assert (jwt::detail::meta::is_mapping_concept<MappingConcept>::value,
"Template parameter does not meet the requirements for MappingConcept.");
return { std::forward<MappingConcept>(mc) };
}
/**
*/
inline detail::secret_param secret(const std::string_view sv)
{
return detail::secret_param(sv);
}
template <typename T>
inline std::enable_if_t<!std::is_convertible<T, std::string_view>::value, detail::secret_function_param<T>>
secret(T&& fun)
{
return detail::secret_function_param<T>{ fun };
}
/**
*/
inline detail::algorithm_param algorithm(std::string_view sv)
{
return { sv };
}
/**
*/
inline detail::algorithm_param algorithm(jwt::algorithm alg)
{
return { alg };
}
/**
*/
inline detail::headers_param<std::map<std::string, std::string>>
headers(const param_init_list_t& kvs)
{
std::map<std::string, std::string> m;
for (const auto& elem : kvs) {
m.emplace(elem.first.data(), elem.second.data());
}
return { std::move(m) };
}
/**
*/
template <typename MappingConcept>
detail::headers_param<MappingConcept>
headers(MappingConcept&& mc)
{
static_assert (jwt::detail::meta::is_mapping_concept<MappingConcept>::value,
"Template parameter does not meet the requirements for MappingConcept.");
return { std::forward<MappingConcept>(mc) };
}
/**
*/
inline detail::verify_param
verify(bool v)
{
return { v };
}
/**
*/
inline detail::leeway_param
leeway(uint32_t l)
{
return { l };
}
/**
*/
inline detail::algorithms_param<std::vector<std::string>>
algorithms(const param_seq_list_t& seq)
{
std::vector<std::string> vec;
vec.reserve(seq.size());
for (const auto& e: seq) { vec.emplace_back(e.data(), e.length()); }
return { std::move(vec) };
}
template <typename SequenceConcept>
detail::algorithms_param<SequenceConcept>
algorithms(SequenceConcept&& sc)
{
return { std::forward<SequenceConcept>(sc) };
}
/**
*/
inline detail::audience_param
aud(const std::string_view aud)
{
return { aud.data() };
}
/**
*/
inline detail::issuer_param
issuer(const std::string_view iss)
{
return { iss.data() };
}
/**
*/
inline detail::subject_param
sub(const std::string_view subj)
{
return { subj.data() };
}
/**
*/
inline detail::validate_iat_param
validate_iat(bool v)
{
return { v };
}
/**
*/
inline detail::validate_jti_param
validate_jti(bool v)
{
return { v };
}
/**
*/
inline detail::nbf_param
nbf(const system_time_t tp)
{
return { tp };
}
/**
*/
inline detail::nbf_param
nbf(const uint64_t epoch)
{
return { epoch };
}
} // END namespace params
} // END namespace jwt
#endif