mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 01:08:31 +00:00
Added tests for ES algorithm support
This commit is contained in:
parent
161e5d8753
commit
f13d6d7fb7
5 changed files with 132 additions and 9 deletions
|
@ -19,3 +19,6 @@ target_link_libraries(test_jwt_decode_verifiy_with_exception ssl crypto gtest)
|
|||
|
||||
add_executable(test_jwt_rsa test_jwt_rsa.cc)
|
||||
target_link_libraries(test_jwt_rsa ssl crypto gtest)
|
||||
|
||||
add_executable(test_jwt_es test_jwt_es.cc)
|
||||
target_link_libraries(test_jwt_es ssl crypto gtest)
|
||||
|
|
6
tests/certs/ec_certs/ec384_priv.pem
Normal file
6
tests/certs/ec_certs/ec384_priv.pem
Normal file
|
@ -0,0 +1,6 @@
|
|||
-----BEGIN EC PRIVATE KEY-----
|
||||
MIGkAgEBBDBeLCgapjZmvTatMHaYX3A02+0Ys3Tr8kda+E9DFnmCSiCOEig519fT
|
||||
13edeU8YdDugBwYFK4EEACKhZANiAASibEL3JxzwCRdLBZCm7WQ3kWaDL+wP8omo
|
||||
3e2VJmZQRnfDdzopgl8r3s8w5JlBpR17J0Gir8g6CVBA6PzMuq5urkilppSINDnR
|
||||
4mDv0+9e4uJVQf3xwEv+jywNUH+wbPM=
|
||||
-----END EC PRIVATE KEY-----
|
5
tests/certs/ec_certs/ec384_pub.pem
Normal file
5
tests/certs/ec_certs/ec384_pub.pem
Normal file
|
@ -0,0 +1,5 @@
|
|||
-----BEGIN PUBLIC KEY-----
|
||||
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEomxC9ycc8AkXSwWQpu1kN5Fmgy/sD/KJ
|
||||
qN3tlSZmUEZ3w3c6KYJfK97PMOSZQaUdeydBoq/IOglQQOj8zLqubq5IpaaUiDQ5
|
||||
0eJg79PvXuLiVUH98cBL/o8sDVB/sGzz
|
||||
-----END PUBLIC KEY-----
|
|
@ -1,9 +0,0 @@
|
|||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn/7RTQX1xlllgo+aqo5z
|
||||
d/+wqaRZwFWp3JJIUXryJ4d153J1gEYdz4RkjFO0X8kpv8qb5hdzWhhZHSItD+07
|
||||
LaQXP4lSUuiK1lJAT/YW51D37nCkWm839gAqEGdsWsYQCvqJKSJr4pWCZTEx2MEf
|
||||
ikmmnaXPR/VOVgZSj9kIoKo+kFwlw9LGVBHYAeR/W+l5DJQK6Yha8Igi36hnvZqq
|
||||
HcQ4gkGUqEc//Tq2nyYNwvAN3ieZvtvL71rFW26FnuA4OwDER+TYSkAr8Z4wH2Ed
|
||||
Gno1GZQAegIm+yWblleCaBVOnlk8VcDO9PecTiAFjjWAbQiaFKlkf/plD7KE4tAa
|
||||
mQIDAQAB
|
||||
-----END PUBLIC KEY-----
|
118
tests/test_jwt_es.cc
Normal file
118
tests/test_jwt_es.cc
Normal file
|
@ -0,0 +1,118 @@
|
|||
#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);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue