Debian Codegraph @

tests/test_jwt_es.cc

raw
1 #include <iostream>
2 #include <fstream>
3 #include <map>
4 #include <chrono>
5
6 #include "gtest/gtest.h"
7 #include "jwt/jwt.hpp"
8
9 #define EC384_PUB_KEY CERT_ROOT_DIR "/ec_certs/ec384_pub.pem"
10 #define EC384_PRIV_KEY CERT_ROOT_DIR "/ec_certs/ec384_priv.pem"
11
12 std::string read_from_file(const std::string& path)
13 {
14 std::string contents;
15 std::ifstream is{path, std::ifstream::binary};
16
17 if (is) {
18 // get length of file:
19 is.seekg (0, is.end);
20 auto length = is.tellg();
21 is.seekg (0, is.beg);
22 contents.resize(length);
23
24 is.read(&contents[0], length);
25 if (!is) {
26 is.close();
27 return {};
28 }
29 }
30
31 is.close();
32 return contents;
33 }
34
35 TEST (ESAlgo, ES256EncodingDecodingTest)
36 {
37 using namespace jwt::params;
38
39 std::string key = read_from_file(EC384_PRIV_KEY);
40 ASSERT_TRUE (key.length());
41
42 jwt::jwt_object obj{algorithm("ES256"), secret(key)};
43
44 obj.add_claim("iss", "arun.muralidharan")
45 .add_claim("aud", "all")
46 .add_claim("exp", 1513862371)
47 ;
48
49 std::error_code ec;
50 auto enc_str = obj.signature(ec);
51 EXPECT_FALSE (ec);
52
53 key = read_from_file(EC384_PUB_KEY);
54 ASSERT_TRUE (key.length());
55
56 auto dec_obj = jwt::decode(enc_str, algorithms({"ES256"}), ec, verify(false), secret(key));
57 EXPECT_FALSE (ec);
58
59 EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES256);
60 EXPECT_TRUE (dec_obj.has_claim("iss"));
61 EXPECT_TRUE (dec_obj.has_claim("aud"));
62 EXPECT_TRUE (dec_obj.has_claim("exp"));
63
64 EXPECT_FALSE (dec_obj.has_claim("sub"));
65 }
66
67 TEST (ESAlgo, ES384EncodingDecodingTest)
68 {
69 using namespace jwt::params;
70
71 std::string key = read_from_file(EC384_PRIV_KEY);
72 ASSERT_TRUE (key.length());
73
74 jwt::jwt_object obj{algorithm("ES384"), secret(key)};
75
76 obj.add_claim("iss", "arun.muralidharan")
77 .add_claim("aud", "all")
78 .add_claim("exp", 1513862371)
79 ;
80
81 auto enc_str = obj.signature();
82
83 key = read_from_file(EC384_PUB_KEY);
84 ASSERT_TRUE (key.length());
85
86 auto dec_obj = jwt::decode(enc_str, algorithms({"ES384"}), verify(false), secret(key));
87
88 EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES384);
89 }
90
91 TEST (ESAlgo, ES512EncodingDecodingTest)
92 {
93 using namespace jwt::params;
94
95 std::string key = read_from_file(EC384_PRIV_KEY);
96 ASSERT_TRUE (key.length());
97
98 jwt::jwt_object obj{algorithm("ES512"), secret(key)};
99
100 obj.add_claim("iss", "arun.muralidharan")
101 .add_claim("aud", "all")
102 .add_claim("exp", 1513862371)
103 ;
104
105 auto enc_str = obj.signature();
106
107 key = read_from_file(EC384_PUB_KEY);
108 ASSERT_TRUE (key.length());
109
110 auto dec_obj = jwt::decode(enc_str, algorithms({"ES512"}), verify(false), secret(key));
111
112 EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES512);
113 }
114
115 TEST (ESAlgo, ES384EncodingDecodingValidTest)
116 {
117 using namespace jwt::params;
118
119 std::string key = read_from_file(EC384_PRIV_KEY);
120 ASSERT_TRUE (key.length());
121
122 jwt::jwt_object obj{algorithm("ES384"), secret(key)};
123
124 obj.add_claim("iss", "arun.muralidharan")
125 .add_claim("aud", "all")
126 .add_claim("exp", 4682665886) // Expires on Sunday, May 22, 2118 12:31:26 PM GMT
127 ;
128
129 auto enc_str = obj.signature();
130
131 key = read_from_file(EC384_PUB_KEY);
132 ASSERT_TRUE (key.length());
133
134 auto dec_obj = jwt::decode(enc_str, algorithms({"ES384"}), verify(true), secret(key));
135
136 EXPECT_EQ (dec_obj.header().algo(), jwt::algorithm::ES384);
137 EXPECT_TRUE (dec_obj.has_claim("exp"));
138 EXPECT_TRUE (obj.payload().has_claim_with_value("exp", 4682665886));
139
140 std::map<std::string, std::string> keystore{{"arun.muralidharan", key}};
141
142 auto l = [&keystore](const jwt::jwt_payload& payload){
143 auto iss = payload.get_claim_value<std::string>("iss");
144 return keystore[iss];
145 };
146 auto dec_obj2 = jwt::decode(enc_str, algorithms({"ES384"}), verify(true), secret(l));
147 EXPECT_EQ (dec_obj2.header().algo(), jwt::algorithm::ES384);
148 }
149