-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.cpp
203 lines (169 loc) · 4.36 KB
/
jwt.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
/**
* @file jwt.cpp
* @author Lochemem Bruno Michael ([email protected])
* @brief
* @version 0.1.0
*/
#include <jwt-cpp/jwt.h>
#include "jwt.h"
using namespace std;
using namespace jwt;
using timepoint = chrono::time_point<chrono::system_clock>;
/**
* @brief
*
* @tparam B
* @tparam L
* @tparam S
* @param obj jwt::builder object
* @param algo jwt algorithm
* @param S jwt secret key
* @return string
*/
template <typename B, typename L, typename S>
auto signJwt(B obj, L algo, const S &secret) -> S
{
string token("");
switch (algo)
{
case JWT_ALGO_HS256:
default:
token += obj.sign(algorithm::hs256{secret});
break;
case JWT_ALGO_HS384:
token += obj.sign(algorithm::hs384{secret});
break;
case JWT_ALGO_HS512:
token += obj.sign(algorithm::hs512{secret});
break;
case JWT_ALGO_PS256:
token += obj.sign(algorithm::ps256("", secret, "", ""));
break;
case JWT_ALGO_PS384:
token += obj.sign(algorithm::ps384("", secret, "", ""));
break;
case JWT_ALGO_PS512:
token += obj.sign(algorithm::ps512("", secret, "", ""));
break;
case JWT_ALGO_RS256:
token += obj.sign(algorithm::rs256("", secret, "", ""));
break;
case JWT_ALGO_RS384:
token += obj.sign(algorithm::rs384("", secret, "", ""));
break;
case JWT_ALGO_RS512:
token += obj.sign(algorithm::rs512("", secret, "", ""));
break;
}
return token;
}
/**
* @brief
*
* @tparam B
* @tparam S
* @param obj
* @param claims
* @return B
*/
template <typename B, typename S>
auto addClaims(B obj, S claims) -> B
{
// convert string to C++ time
auto strToTime = [](const string &timeval) {
const chrono::system_clock::duration timeT = chrono::seconds{stoi(timeval)};
const timepoint duration(timeT);
return duration; };
for (auto &claim : claims)
{
switch (JWT_CLAIM_REPRESENT[claim.first])
{
case 1:
obj.set_issued_at(strToTime(claim.second));
break;
case 2:
obj.set_not_before(strToTime(claim.second));
break;
case 3:
obj.set_expires_at(strToTime(claim.second));
break;
default:
obj.set_payload_claim(claim.first, claim.second);
break;
}
}
return obj;
}
/**
* @brief
*
* @tparam S
* @tparam L
* @tparam I
* @param secret
* @param algo
* @param claims
* @return S
*/
template <typename S, typename L, typename I>
auto jwtEncode(const S &secret, L algo, I claims) -> S
{
auto tokenObj = addClaims<builder, I>(create().set_type("JWS"),
claims);
return signJwt<builder, long, string>(tokenObj,
algo,
secret);
}
/**
* @brief
*
* @tparam S
* @tparam L
* @param token
* @param algo
* @param secret
* @return strmap
*/
template <typename S, typename L>
auto jwtDecode(const S &token, L algo, const S &secret) -> strmap
{
auto decoded = decode(token);
auto verifier = verify();
strmap claims;
switch (algo)
{
case JWT_ALGO_HS256:
verifier.allow_algorithm(algorithm::hs256{secret});
break;
case JWT_ALGO_HS384:
verifier.allow_algorithm(algorithm::hs384{secret});
break;
case JWT_ALGO_HS512:
verifier.allow_algorithm(algorithm::hs512{secret});
break;
case JWT_ALGO_RS256:
verifier.allow_algorithm(algorithm::rs256(secret, "", "", ""));
break;
case JWT_ALGO_RS384:
verifier.allow_algorithm(algorithm::rs384(secret, "", "", ""));
break;
case JWT_ALGO_RS512:
verifier.allow_algorithm(algorithm::rs512(secret, "", "", ""));
break;
case JWT_ALGO_PS256:
verifier.allow_algorithm(algorithm::ps256(secret, "", "", ""));
break;
case JWT_ALGO_PS384:
verifier.allow_algorithm(algorithm::ps384(secret, "", "", ""));
break;
case JWT_ALGO_PS512:
verifier.allow_algorithm(algorithm::ps512(secret, "", "", ""));
break;
}
verifier.verify(decoded);
for (auto &iter : decoded.get_payload_claims())
{
claims[iter.first] = iter.second.to_json().to_str();
}
return claims;
}