JWT-CPP v0.7.0
A header only library for creating and validating JSON Web Tokens (JWT) in C++
Loading...
Searching...
No Matches
traits.h
1#define JWT_DISABLE_PICOJSON
2#define JSONCONS_NO_DEPRECATED
3
4#include "jwt-cpp/jwt.h"
5
6#include "jsoncons/json.hpp"
7
8#include <sstream>
9
10namespace jwt {
14 namespace traits {
17 // Needs at least https://github.com/danielaparker/jsoncons/commit/28c56b90ec7337f98a5b8942574590111a5e5831
18 static_assert(jsoncons::version().minor >= 167, "A higher version of jsoncons is required!");
19
20 using json = jsoncons::json;
21 using value_type = json;
22 struct object_type : json::object {
23 // Add missing C++11 member types
24 // https://github.com/danielaparker/jsoncons/commit/1b1ceeb572f9a2db6d37cff47ac78a4f14e072e2#commitcomment-45391411
25 using value_type = key_value_type; // Enable optional jwt-cpp methods
26 using mapped_type = key_value_type::value_type;
27 using size_type = size_t; // for implementing count
28
29 object_type() = default;
30 object_type(const object_type&) = default;
31 explicit object_type(const json::object& o) : json::object(o) {}
32 object_type(object_type&&) = default;
33 explicit object_type(json::object&& o) : json::object(o) {}
34 ~object_type() = default;
35 object_type& operator=(const object_type& o) = default;
36 object_type& operator=(object_type&& o) noexcept = default;
37
38 // Add missing C++11 subscription operator
39 mapped_type& operator[](const key_type& key) {
40 // https://github.com/microsoft/STL/blob/2914b4301c59dc7ffc09d16ac6f7979fde2b7f2c/stl/inc/map#L325
41 return try_emplace(key).first->value();
42 }
43
44 // Add missing C++11 element access
45 const mapped_type& at(const key_type& key) const {
46 auto target = find(key);
47 if (target != end()) return target->value();
48
49 throw std::out_of_range("invalid key");
50 }
51
52 // Add missing C++11 lookup method
53 size_type count(const key_type& key) const {
54 struct compare {
55 bool operator()(const value_type& val, const key_type& key) const { return val.key() < key; }
56 bool operator()(const key_type& key, const value_type& val) const { return key < val.key(); }
57 };
58
59 // https://en.cppreference.com/w/cpp/algorithm/binary_search#Complexity
60 if (std::binary_search(this->begin(), this->end(), key, compare{})) return 1;
61 return 0;
62 }
63 };
64 class array_type : public json::array {
65 public:
66 using json::array::array;
67 explicit array_type(const json::array& a) : json::array(a) {}
68 explicit array_type(json::array&& a) : json::array(a) {}
69 value_type const& front() const { return this->operator[](0U); }
70 };
71 using string_type = std::string; // current limitation of traits implementation
72 using number_type = double;
73 using integer_type = int64_t;
74 using boolean_type = bool;
75
76 static jwt::json::type get_type(const json& val) {
77 using jwt::json::type;
78
79 if (val.type() == jsoncons::json_type::bool_value) return type::boolean;
80 if (val.type() == jsoncons::json_type::int64_value) return type::integer;
81 if (val.type() == jsoncons::json_type::uint64_value) return type::integer;
82 if (val.type() == jsoncons::json_type::half_value) return type::number;
83 if (val.type() == jsoncons::json_type::double_value) return type::number;
84 if (val.type() == jsoncons::json_type::string_value) return type::string;
85 if (val.type() == jsoncons::json_type::array_value) return type::array;
86 if (val.type() == jsoncons::json_type::object_value) return type::object;
87
88 throw std::logic_error("invalid type");
89 }
90
91 static object_type as_object(const json& val) {
92 if (val.type() != jsoncons::json_type::object_value) throw std::bad_cast();
93 return object_type(val.object_value());
94 }
95
96 static array_type as_array(const json& val) {
97 if (val.type() != jsoncons::json_type::array_value) throw std::bad_cast();
98 return array_type(val.array_value());
99 }
100
101 static string_type as_string(const json& val) {
102 if (val.type() != jsoncons::json_type::string_value) throw std::bad_cast();
103 return val.as_string();
104 }
105
106 static number_type as_number(const json& val) {
107 if (get_type(val) != jwt::json::type::number) throw std::bad_cast();
108 return val.as_double();
109 }
110
111 static integer_type as_integer(const json& val) {
112 if (get_type(val) != jwt::json::type::integer) throw std::bad_cast();
113 return val.as<integer_type>();
114 }
115
116 static boolean_type as_boolean(const json& val) {
117 if (val.type() != jsoncons::json_type::bool_value) throw std::bad_cast();
118 return val.as_bool();
119 }
120
121 static bool parse(json& val, const std::string& str) {
122 val = json::parse(str);
123 return true;
124 }
125
126 static std::string serialize(const json& val) {
127 std::ostringstream os;
128 os << jsoncons::print(val);
129 return os.str();
130 }
131 };
132 } // namespace traits
133} // namespace jwt
type
Categories for the various JSON types used in JWTs.
Definition jwt.h:2274
JSON Web Token.
Definition base.h:21
basic_claim's JSON trait implementation for jsoncons.
Definition traits.h:16