This commit is contained in:
Arun M 2020-06-13 12:40:18 +05:30
commit 701f4f26c1
24 changed files with 416 additions and 200 deletions

View file

@ -57,7 +57,7 @@ using sign_result_t = std::pair<std::string, std::error_code>;
/// The result type of verification function
using verify_result_t = std::pair<bool, std::error_code>;
/// The function pointer type for the signing function
using sign_func_t = sign_result_t (*) (const jwt::string_view key,
using sign_func_t = sign_result_t (*) (const jwt::string_view key,
const jwt::string_view data);
/// The function pointer type for the verifying function
using verify_func_t = verify_result_t (*) (const jwt::string_view key,
@ -235,7 +235,7 @@ inline jwt::string_view alg_to_str(SCOPED_ENUM algorithm alg) noexcept
case algorithm::ES384: return "ES384";
case algorithm::ES512: return "ES512";
case algorithm::TERM: return "TERM";
case algorithm::NONE: return "NONE";
case algorithm::NONE: return "none";
case algorithm::UNKN: return "UNKN";
default: assert (0 && "Unknown Algorithm");
};
@ -249,18 +249,18 @@ inline jwt::string_view alg_to_str(SCOPED_ENUM algorithm alg) noexcept
*/
inline SCOPED_ENUM algorithm str_to_alg(const jwt::string_view alg) noexcept
{
if (!alg.length()) return algorithm::NONE;
if (!alg.length()) return algorithm::UNKN;
if (!strcasecmp(alg.data(), "none")) return algorithm::NONE;
if (!strcasecmp(alg.data(), "hs256")) return algorithm::HS256;
if (!strcasecmp(alg.data(), "hs384")) return algorithm::HS384;
if (!strcasecmp(alg.data(), "hs512")) return algorithm::HS512;
if (!strcasecmp(alg.data(), "rs256")) return algorithm::RS256;
if (!strcasecmp(alg.data(), "rs384")) return algorithm::RS384;
if (!strcasecmp(alg.data(), "rs512")) return algorithm::RS512;
if (!strcasecmp(alg.data(), "es256")) return algorithm::ES256;
if (!strcasecmp(alg.data(), "es384")) return algorithm::ES384;
if (!strcasecmp(alg.data(), "es512")) return algorithm::ES512;
if (!strcasecmp(alg.data(), "HS256")) return algorithm::HS256;
if (!strcasecmp(alg.data(), "HS384")) return algorithm::HS384;
if (!strcasecmp(alg.data(), "HS512")) return algorithm::HS512;
if (!strcasecmp(alg.data(), "RS256")) return algorithm::RS256;
if (!strcasecmp(alg.data(), "RS384")) return algorithm::RS384;
if (!strcasecmp(alg.data(), "RS512")) return algorithm::RS512;
if (!strcasecmp(alg.data(), "ES256")) return algorithm::ES256;
if (!strcasecmp(alg.data(), "ES384")) return algorithm::ES384;
if (!strcasecmp(alg.data(), "ES512")) return algorithm::ES512;
return algorithm::UNKN;
@ -385,14 +385,14 @@ struct HMACSign
* Returns:
* verify_result_t
* verify_result_t::first set to true if verification succeeds.
* false otherwise.
* false otherwise.
* verify_result_t::second set to relevant error if verification fails.
*
* Exceptions:
* Any allocation failure will result in jwt::MemoryAllocationException
* being thrown.
*/
static verify_result_t
static verify_result_t
verify(const jwt::string_view key, const jwt::string_view head, const jwt::string_view sign);
};
@ -405,10 +405,10 @@ struct HMACSign
* PEM based algorithms.
*
* The signing and verification APIs are
* basically no-op except that they would
* basically no-op except that they would
* set the relevant error code.
*
* NOTE: error_code would be set in the case
* NOTE: error_code would be set in the case
* of usage of NONE algorithm.
* Users of this API are expected to check for
* the case explicitly.
@ -534,7 +534,7 @@ private:
/**
*/
static int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s)
{
{
if (r == nullptr || s == nullptr) return 0;
BN_clear_free(sig->r);

View file

@ -92,7 +92,7 @@ inline std::string base64_encode(const char* in, size_t len)
const auto encoded_siz = encoding_size(len);
result.resize(encoded_siz);
constexpr static const EMap emap{};
constexpr static const EMap emap;
int i = 0;
int j = 0;

View file

@ -29,7 +29,7 @@
// To hack around Visual Studio error:
// error C3431: 'algorithm': a scoped enumeration cannot be redeclared as an unscoped enumeration
#ifdef _MSC_VER
#if defined(_MSC_VER) && !defined(__clang__)
#define SCOPED_ENUM enum class
#else
#define SCOPED_ENUM enum

View file

@ -1,7 +1,10 @@
#include <iostream>
#include <string>
#if defined( CPP_JWT_USE_VENDORED_NLOHMANN_JSON)
#include "./json.hpp"
#else
#include "nlohmann/json.hpp"
#endif
using json = nlohmann::json;
void basic_json_test()

View file

@ -38,8 +38,11 @@ SOFTWARE.
#include "jwt/string_view.hpp"
#include "jwt/parameters.hpp"
#include "jwt/exceptions.hpp"
#if defined(CPP_JWT_USE_VENDORED_NLOHMANN_JSON)
#include "jwt/json/json.hpp"
#else
#include "nlohmann/json.hpp"
#endif
// For convenience
using json_t = nlohmann::json;
using system_time_t = std::chrono::time_point<std::chrono::system_clock>;

View file

@ -23,7 +23,7 @@ SOFTWARE.
#ifndef JWT_STRING_VIEW_HPP
#define JWT_STRING_VIEW_HPP
#if __cplusplus >= 201703L
#if defined(__cpp_lib_string_view)
#include <string_view>
@ -31,7 +31,7 @@ namespace jwt {
using string_view = std::string_view;
}
#else // __cplusplus >= 201703L
#else // defined(__cpp_lib_string_view)
#include <limits>
#include <string>
@ -375,6 +375,6 @@ using string_view = basic_string_view<char>;
#include "jwt/impl/string_view.ipp"
#endif // __cplusplus >= 201703L
#endif // defined(__cpp_lib_string_view)
#endif

View file

@ -21,7 +21,7 @@ void basic_decode_test()
using namespace jwt::params;
std::cout << "DECODE: \n";
jwt::decode(res, algorithms({"none", "hs256"}), ec, verify(false), secret("secret"));
jwt::decode(res, algorithms({"none", "HS256"}), ec, verify(false), secret("secret"));
}
int main() {

View file

@ -11,7 +11,7 @@ void basic_jwt_object_test()
using namespace jwt::params;
jwt::jwt_object obj(payload({
{"a", "b"},
{"c", "d"}
{"c", "d"}
}));
//check with std::map
@ -20,7 +20,7 @@ void basic_jwt_object_test()
m["c"] = "d";
jwt::jwt_object obj1{payload(m)};
auto obj2 = std::move(obj1);
std::cout << obj2.payload() << std::endl;
@ -43,23 +43,23 @@ void basic_jwt_object_test()
std::cout << obj3.payload() << std::endl;
obj3.secret("secret");
obj3.header().algo("hs256");
obj3.header().algo("HS256");
auto dec_obj = jwt::decode(obj3.signature(), algorithms({"hs256"}), secret("secret"));
auto dec_obj = jwt::decode(obj3.signature(), algorithms({"HS256"}), secret("secret"));
}
void jwt_object_pem_test()
{
using namespace jwt::params;
std::string pub_key =
std::string pub_key =
R"(-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEomxC9ycc8AkXSwWQpu1kN5Fmgy/sD/KJ
qN3tlSZmUEZ3w3c6KYJfK97PMOSZQaUdeydBoq/IOglQQOj8zLqubq5IpaaUiDQ5
0eJg79PvXuLiVUH98cBL/o8sDVB/sGzz
-----END PUBLIC KEY-----)";
std::string priv_key =
std::string priv_key =
R"(-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDBeLCgapjZmvTatMHaYX3A02+0Ys3Tr8kda+E9DFnmCSiCOEig519fT
13edeU8YdDugBwYFK4EEACKhZANiAASibEL3JxzwCRdLBZCm7WQ3kWaDL+wP8omo
@ -79,10 +79,10 @@ MIGkAgEBBDBeLCgapjZmvTatMHaYX3A02+0Ys3Tr8kda+E9DFnmCSiCOEig519fT
;
std::cout << "pem sign " << obj.signature() << std::endl;
std::cout << "Get claim value for exp: " <<
std::cout << "Get claim value for exp: " <<
obj.payload().get_claim_value<uint64_t>("exp") << std::endl;
auto dec_obj = jwt::decode(obj.signature(), algorithms({"es256"}), secret(pub_key));
auto dec_obj = jwt::decode(obj.signature(), algorithms({"ES256"}), secret(pub_key));
std::cout << dec_obj.payload() << std::endl;
}