-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathtest_jwt_es.cc
149 lines (107 loc) · 3.73 KB
/
test_jwt_es.cc
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
#include <iostream>
#include <fstream>
#include <map>
#include <chrono>
#include "gtest/gtest.h"
#include "jwt/jwt.hpp"
#define EC384_PUB_KEY CERT_ROOT_DIR "/ec_certs/ec384_pub.pem"
#define EC384_PRIV_KEY CERT_ROOT_DIR "/ec_certs/ec384_priv.pem"
std::string read_from_file(const std::string& path)
{
std::string contents;
std::ifstream is{path, std::ifstream::binary};
if (is) {
// get length of file:
is.seekg (0, is.end);
auto length = is.tellg();
is.seekg (0, is.beg);
contents.resize(length);
is.read(&contents[0], length);
if (!is) {
is.close();
return {};
}
}
is.close();
return contents;
}
TEST (ESAlgo, ES256EncodingDecodingTest)
{
using namespace jwt::params;
std::string key = read_from_file(EC384_PRIV_KEY);
ASSERT_TRUE (key.length());
jwt::jwt_object obj{algorithm("ES256"), secret(key)};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("aud", "all")
.add_claim("exp", 1513862371)
;
std::error_code ec;
auto enc_str = obj.signature(ec);
EXPECT_FALSE (ec);
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"ES256"}), ec, verify(false), secret(key));
EXPECT_FALSE (ec);
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES256);
EXPECT_TRUE (dec_obj.has_claim("iss"));
EXPECT_TRUE (dec_obj.has_claim("aud"));
EXPECT_TRUE (dec_obj.has_claim("exp"));
EXPECT_FALSE (dec_obj.has_claim("sub"));
}
TEST (ESAlgo, ES384EncodingDecodingTest)
{
using namespace jwt::params;
std::string key = read_from_file(EC384_PRIV_KEY);
ASSERT_TRUE (key.length());
jwt::jwt_object obj{algorithm("ES384"), secret(key)};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("aud", "all")
.add_claim("exp", 1513862371)
;
auto enc_str = obj.signature();
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"ES384"}), verify(false), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES384);
}
TEST (ESAlgo, ES512EncodingDecodingTest)
{
using namespace jwt::params;
std::string key = read_from_file(EC384_PRIV_KEY);
ASSERT_TRUE (key.length());
jwt::jwt_object obj{algorithm("ES512"), secret(key)};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("aud", "all")
.add_claim("exp", 1513862371)
;
auto enc_str = obj.signature();
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"ES512"}), verify(false), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES512);
}
TEST (ESAlgo, ES384EncodingDecodingValidTest)
{
using namespace jwt::params;
std::string key = read_from_file(EC384_PRIV_KEY);
ASSERT_TRUE (key.length());
jwt::jwt_object obj{algorithm("ES384"), secret(key)};
obj.add_claim("iss", "arun.muralidharan")
.add_claim("aud", "all")
.add_claim("exp", 4682665886) // Expires on Sunday, May 22, 2118 12:31:26 PM GMT
;
auto enc_str = obj.signature();
key = read_from_file(EC384_PUB_KEY);
ASSERT_TRUE (key.length());
auto dec_obj = jwt::decode(enc_str, algorithms({"ES384"}), verify(true), secret(key));
EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES384);
EXPECT_TRUE (dec_obj.has_claim("exp"));
EXPECT_TRUE (obj.payload().has_claim_with_value("exp", 4682665886));
std::map<std::string, std::string> keystore{{"arun.muralidharan", key}};
auto l = [&keystore](const jwt::jwt_payload& payload){
auto iss = payload.get_claim_value<std::string>("iss");
return keystore[iss];
};
auto dec_obj2 = jwt::decode(enc_str, algorithms({"ES384"}), verify(true), secret(l));
EXPECT_EQ (dec_obj2.header().algo(), jwt::algorithm::ES384);
}