Debian Codegraph @

include/jwt/parameters.hpp

raw
1 /*
2 Copyright (c) 2017 Arun Muralidharan
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #ifndef CPP_JWT_PARAMETERS_HPP
24 #define CPP_JWT_PARAMETERS_HPP
25
26 #include <map>
27 #include <chrono>
28 #include <string>
29 #include <vector>
30 #include <utility>
31 #include <unordered_map>
32
33 #include "jwt/algorithm.hpp"
34 #include "jwt/detail/meta.hpp"
35 #include "jwt/string_view.hpp"
36
37 namespace jwt {
38
39 using system_time_t = std::chrono::time_point<std::chrono::system_clock>;
40
41 namespace params {
42
43
44 namespace detail {
45 /**
46 * Parameter for providing the payload.
47 * Takes a Mapping concept representing
48 * key-value pairs.
49 *
50 * NOTE: MappingConcept allows only strings
51 * for both keys and values. Use `add_header`
52 * API of `jwt_object` otherwise.
53 *
54 * Modeled as ParameterConcept.
55 */
56 template <typename MappingConcept>
57 struct payload_param
58 {
59 payload_param(MappingConcept&& mc)
60 : payload_(std::forward<MappingConcept>(mc))
61 {}
62
63 MappingConcept get() && { return std::move(payload_); }
64 const MappingConcept& get() const& { return payload_; }
65
66 MappingConcept payload_;
67 };
68
69 /**
70 * Parameter for providing the secret key.
71 * Stores only the view of the provided string
72 * as string_view. Later the implementation may or
73 * may-not copy it.
74 *
75 * Modeled as ParameterConcept.
76 */
77 struct secret_param
78 {
79 secret_param(string_view sv)
80 : secret_(sv)
81 {}
82
83 string_view get() { return secret_; }
84 string_view secret_;
85 };
86
87 template <typename T>
88 struct secret_function_param
89 {
90 T get() const { return fun_; }
91 template <typename U>
92 std::string get(U&& u) const { return fun_(u);}
93 T fun_;
94 };
95
96 /**
97 * Parameter for providing the algorithm to use.
98 * The parameter can accept either the string representation
99 * or the enum class.
100 *
101 * Modeled as ParameterConcept.
102 */
103 struct algorithm_param
104 {
105 algorithm_param(const string_view alg)
106 : alg_(str_to_alg(alg))
107 {}
108
109 algorithm_param(jwt::algorithm alg)
110 : alg_(alg)
111 {}
112
113 jwt::algorithm get() const noexcept
114 {
115 return alg_;
116 }
117
118 typename jwt::algorithm alg_;
119 };
120
121 /**
122 * Parameter for providing additional headers.
123 * Takes a mapping concept representing
124 * key-value pairs.
125 *
126 * Modeled as ParameterConcept.
127 */
128 template <typename MappingConcept>
129 struct headers_param
130 {
131 headers_param(MappingConcept&& mc)
132 : headers_(std::forward<MappingConcept>(mc))
133 {}
134
135 MappingConcept get() && { return std::move(headers_); }
136 const MappingConcept& get() const& { return headers_; }
137
138 MappingConcept headers_;
139 };
140
141 /**
142 */
143 struct verify_param
144 {
145 verify_param(bool v)
146 : verify_(v)
147 {}
148
149 bool get() const { return verify_; }
150
151 bool verify_;
152 };
153
154 /**
155 */
156 template <typename Sequence>
157 struct algorithms_param
158 {
159 algorithms_param(Sequence&& seq)
160 : seq_(std::forward<Sequence>(seq))
161 {}
162
163 Sequence get() && { return std::move(seq_); }
164 const Sequence& get() const& { return seq_; }
165
166 Sequence seq_;
167 };
168
169 /**
170 */
171 struct leeway_param
172 {
173 leeway_param(uint32_t v)
174 : leeway_(v)
175 {}
176
177 uint32_t get() const noexcept { return leeway_; }
178
179 uint32_t leeway_;
180 };
181
182 /**
183 */
184 struct audience_param
185 {
186 audience_param(std::string aud)
187 : aud_(std::move(aud))
188 {}
189
190 const std::string& get() const& noexcept { return aud_; }
191 std::string get() && noexcept { return aud_; }
192
193 std::string aud_;
194 };
195
196 /**
197 */
198 struct issuer_param
199 {
200 issuer_param(std::string iss)
201 : iss_(std::move(iss))
202 {}
203
204 const std::string& get() const& noexcept { return iss_; }
205 std::string get() && noexcept { return iss_; }
206
207 std::string iss_;
208 };
209
210 /**
211 */
212 struct subject_param
213 {
214 subject_param(std::string sub)
215 : sub_(std::move(sub))
216 {}
217
218 const std::string& get() const& noexcept { return sub_; }
219 std::string get() && noexcept { return sub_; }
220
221 std::string sub_;
222 };
223
224 /**
225 */
226 struct validate_iat_param
227 {
228 validate_iat_param(bool v)
229 : iat_(v)
230 {}
231
232 bool get() const noexcept { return iat_; }
233
234 bool iat_;
235 };
236
237 /**
238 */
239 struct validate_jti_param
240 {
241 validate_jti_param(bool v)
242 : jti_(v)
243 {}
244
245 bool get() const noexcept { return jti_; }
246
247 bool jti_;
248 };
249
250 /**
251 */
252 struct nbf_param
253 {
254 nbf_param(const jwt::system_time_t tp)
255 : duration_(std::chrono::duration_cast<
256 std::chrono::seconds>(tp.time_since_epoch()).count())
257 {}
258
259 nbf_param(const uint64_t epoch)
260 : duration_(epoch)
261 {}
262
263 uint64_t get() const noexcept { return duration_; }
264
265 uint64_t duration_;
266 };
267
268 } // END namespace detail
269
270 // Useful typedef
271 using param_init_list_t = std::initializer_list<std::pair<jwt::string_view, jwt::string_view>>;
272 using param_seq_list_t = std::initializer_list<jwt::string_view>;
273
274
275 /**
276 */
277 inline detail::payload_param<std::unordered_map<std::string, std::string>>
278 payload(const param_init_list_t& kvs)
279 {
280 std::unordered_map<std::string, std::string> m;
281
282 for (const auto& elem : kvs) {
283 m.emplace(elem.first.data(), elem.second.data());
284 }
285
286 return { std::move(m) };
287 }
288
289 /**
290 */
291 template <typename MappingConcept>
292 detail::payload_param<MappingConcept>
293 payload(MappingConcept&& mc)
294 {
295 static_assert (jwt::detail::meta::is_mapping_concept<MappingConcept>::value,
296 "Template parameter does not meet the requirements for MappingConcept.");
297
298 return { std::forward<MappingConcept>(mc) };
299 }
300
301
302 /**
303 */
304 inline detail::secret_param secret(const string_view sv)
305 {
306 return { sv };
307 }
308
309 template <typename T>
310 inline std::enable_if_t<!std::is_convertible<T, string_view>::value, detail::secret_function_param<T>>
311 secret(T&& fun)
312 {
313 return detail::secret_function_param<T>{ fun };
314 }
315
316 /**
317 */
318 inline detail::algorithm_param algorithm(const string_view sv)
319 {
320 return { sv };
321 }
322
323 /**
324 */
325 inline detail::algorithm_param algorithm(jwt::algorithm alg)
326 {
327 return { alg };
328 }
329
330 /**
331 */
332 inline detail::headers_param<std::map<std::string, std::string>>
333 headers(const param_init_list_t& kvs)
334 {
335 std::map<std::string, std::string> m;
336
337 for (const auto& elem : kvs) {
338 m.emplace(elem.first.data(), elem.second.data());
339 }
340
341 return { std::move(m) };
342 }
343
344 /**
345 */
346 template <typename MappingConcept>
347 detail::headers_param<MappingConcept>
348 headers(MappingConcept&& mc)
349 {
350 static_assert (jwt::detail::meta::is_mapping_concept<MappingConcept>::value,
351 "Template parameter does not meet the requirements for MappingConcept.");
352
353 return { std::forward<MappingConcept>(mc) };
354 }
355
356 /**
357 */
358 inline detail::verify_param
359 verify(bool v)
360 {
361 return { v };
362 }
363
364 /**
365 */
366 inline detail::leeway_param
367 leeway(uint32_t l)
368 {
369 return { l };
370 }
371
372 /**
373 */
374 inline detail::algorithms_param<std::vector<std::string>>
375 algorithms(const param_seq_list_t& seq)
376 {
377 std::vector<std::string> vec;
378 vec.reserve(seq.size());
379
380 for (const auto& e: seq) { vec.emplace_back(e.data(), e.length()); }
381
382 return { std::move(vec) };
383 }
384
385 template <typename SequenceConcept>
386 detail::algorithms_param<SequenceConcept>
387 algorithms(SequenceConcept&& sc)
388 {
389 return { std::forward<SequenceConcept>(sc) };
390 }
391
392 /**
393 */
394 inline detail::audience_param
395 aud(const jwt::string_view aud)
396 {
397 return { aud.data() };
398 }
399
400 /**
401 */
402 inline detail::issuer_param
403 issuer(const jwt::string_view iss)
404 {
405 return { iss.data() };
406 }
407
408 /**
409 */
410 inline detail::subject_param
411 sub(const jwt::string_view subj)
412 {
413 return { subj.data() };
414 }
415
416 /**
417 */
418 inline detail::validate_iat_param
419 validate_iat(bool v)
420 {
421 return { v };
422 }
423
424 /**
425 */
426 inline detail::validate_jti_param
427 validate_jti(bool v)
428 {
429 return { v };
430 }
431
432 /**
433 */
434 inline detail::nbf_param
435 nbf(const system_time_t tp)
436 {
437 return { tp };
438 }
439
440 /**
441 */
442 inline detail::nbf_param
443 nbf(const uint64_t epoch)
444 {
445 return { epoch };
446 }
447
448 } // END namespace params
449 } // END namespace jwt
450
451 #endif