Goby3 3.5.1
2026.06.04
Loading...
Searching...
No Matches
jwt.h File Reference
#include "picojson/picojson.h"
#include "base.h"
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iterator>
#include <locale>
#include <memory>
#include <set>
#include <system_error>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include "traits/kazuho-picojson/defaults.h"

Go to the source code of this file.

Classes

struct  jwt::error::signature_verification_exception
struct  jwt::error::signature_generation_exception
struct  jwt::error::rsa_exception
struct  jwt::error::ecdsa_exception
struct  jwt::error::token_verification_exception
struct  std::is_error_code_enum< jwt::error::rsa_error >
struct  std::is_error_code_enum< jwt::error::ecdsa_error >
struct  std::is_error_code_enum< jwt::error::signature_verification_error >
struct  std::is_error_code_enum< jwt::error::signature_generation_error >
struct  std::is_error_code_enum< jwt::error::token_verification_error >
class  jwt::helper::evp_pkey_handle
 Handle class for EVP_PKEY structures. More...
struct  jwt::algorithm::none
 "none" algorithm. More...
struct  jwt::algorithm::hmacsha
 Base class for HMAC family of algorithms. More...
struct  jwt::algorithm::rsa
 Base class for RSA family of algorithms. More...
struct  jwt::algorithm::ecdsa
 Base class for ECDSA family of algorithms. More...
struct  jwt::algorithm::eddsa
 Base class for EdDSA family of algorithms. More...
struct  jwt::algorithm::pss
 Base class for PSS-RSA family of algorithms. More...
struct  jwt::algorithm::hs256
struct  jwt::algorithm::hs384
struct  jwt::algorithm::hs512
struct  jwt::algorithm::rs256
struct  jwt::algorithm::rs384
struct  jwt::algorithm::rs512
struct  jwt::algorithm::es256
struct  jwt::algorithm::es384
struct  jwt::algorithm::es512
struct  jwt::algorithm::es256k
struct  jwt::algorithm::ed25519
struct  jwt::algorithm::ed448
struct  jwt::algorithm::ps256
struct  jwt::algorithm::ps384
struct  jwt::algorithm::ps512
struct  jwt::details::make_void< Ts >
struct  jwt::details::nonesuch
struct  jwt::details::detector< Default, AlwaysVoid, Op, Args >
struct  jwt::details::detector< Default, void_t< Op< Args... > >, Op, Args... >
struct  jwt::details::is_function_signature_detected< traits_type, Op, Signature >
struct  jwt::details::supports_get_type< traits_type, value_type >
struct  jwt::details::is_valid_traits< traits >
struct  jwt::details::is_valid_json_value< value_type >
struct  jwt::details::is_iterable< T, typename >
struct  jwt::details::is_iterable< T, void_t< decltype(std::begin(std::declval< T >())), decltype(std::end(std::declval< T >())), decltype(std::begin(std::declval< const T >())), decltype(std::end(std::declval< const T >())) > >
struct  jwt::details::is_subcription_operator_signature< object_type, string_type, typename >
struct  jwt::details::is_subcription_operator_signature< object_type, string_type, void_t< decltype(std::declval< object_type >().operator[](std::declval< string_type >()))> >
struct  jwt::details::is_valid_json_object< value_type, string_type, object_type >
struct  jwt::details::is_valid_json_array< value_type, array_type >
struct  jwt::details::is_valid_json_string< value_type, string_type, integer_type >
struct  jwt::details::is_valid_json_number< value_type, number_type >
struct  jwt::details::is_valid_json_integer< value_type, integer_type >
struct  jwt::details::is_valid_json_boolean< value_type, boolean_type >
struct  jwt::details::is_valid_json_types< value_type, object_type, array_type, string_type, number_type, integer_type, boolean_type >
class  jwt::basic_claim< json_traits >
 a class to store a generic JSON value as claim More...
struct  jwt::error::invalid_json_exception
struct  jwt::error::claim_not_present_exception
struct  jwt::details::map_of_claims< json_traits >
class  jwt::payload< json_traits >
class  jwt::header< json_traits >
class  jwt::decoded_jwt< json_traits >
class  jwt::builder< Clock, json_traits >
struct  jwt::verify_ops::verify_context< json_traits >
struct  jwt::verify_ops::equals_claim< json_traits, in_header >
struct  jwt::verify_ops::date_before_claim< json_traits, in_header >
struct  jwt::verify_ops::date_after_claim< json_traits, in_header >
struct  jwt::verify_ops::is_subset_claim< json_traits, in_header >
struct  jwt::verify_ops::insensitive_string_claim< json_traits, in_header >
class  jwt::verifier< Clock, json_traits >
class  jwt::jwk< json_traits >
 JSON Web Key. More...
class  jwt::jwks< json_traits >
 JWK Set. More...
struct  jwt::default_clock

Namespaces

namespace  jwt
 JSON Web Token.
namespace  jwt::error
 Everything related to error codes issued by the library.
namespace  std
 STL namespace.
namespace  jwt::helper
 A collection for working with certificates.
namespace  jwt::algorithm
 Various cryptographic algorithms when working with JWT.
namespace  jwt::json
 JSON Abstractions for working with any library.
namespace  jwt::details
namespace  jwt::verify_ops

Macros

#define PICOJSON_USE_INT64
#define JWT_CLAIM_EXPLICIT   explicit
#define JWT_CPP_JSON_TYPE_TYPE(TYPE)
#define JWT_CPP_AS_TYPE_T(TYPE)
#define JWT_CPP_SUPPORTS_AS(TYPE)

Typedefs

using jwt::date = std::chrono::system_clock::time_point
template<typename... Ts>
using jwt::details::void_t = typename make_void<Ts...>::type
template<template< class... > class Op, class... Args>
using jwt::details::is_detected = typename detector<nonesuch, void, Op, Args...>::value
template<typename T, typename Signature>
using jwt::details::is_signature = typename std::is_same<T, Signature>
template<typename object_type, typename string_type>
using jwt::details::is_count_signature
template<typename object_type, typename value_type, typename string_type>
using jwt::details::is_at_const_signature
template<typename string_type, typename integer_type>
using jwt::details::is_substr_start_end_index_signature
template<typename string_type, typename integer_type>
using jwt::details::is_substr_start_index_signature
template<typename string_type>
using jwt::details::is_std_operate_plus_signature

Enumerations

enum class  jwt::error::rsa_error {
  jwt::error::ok = 0 , jwt::error::cert_load_failed = 10 , jwt::error::get_key_failed , jwt::error::write_key_failed ,
  jwt::error::write_cert_failed , jwt::error::convert_to_pem_failed , jwt::error::load_key_bio_write , jwt::error::load_key_bio_read ,
  jwt::error::create_mem_bio_failed , jwt::error::no_key_provided , jwt::error::set_rsa_failed , jwt::error::create_context_failed
}
 Errors related to processing of RSA signatures. More...
enum class  jwt::error::ecdsa_error {
  jwt::error::ok = 0 , jwt::error::load_key_bio_write = 10 , jwt::error::load_key_bio_read , jwt::error::create_mem_bio_failed ,
  jwt::error::no_key_provided , jwt::error::invalid_key_size , jwt::error::invalid_key , jwt::error::create_context_failed ,
  jwt::error::cert_load_failed , jwt::error::get_key_failed , jwt::error::write_key_failed , jwt::error::write_cert_failed ,
  jwt::error::convert_to_pem_failed , jwt::error::unknown_curve , jwt::error::set_ecdsa_failed
}
 Errors related to processing of RSA signatures. More...
enum class  jwt::error::signature_verification_error {
  jwt::error::ok = 0 , jwt::error::invalid_signature = 10 , jwt::error::create_context_failed , jwt::error::verifyinit_failed ,
  jwt::error::verifyupdate_failed , jwt::error::verifyfinal_failed , jwt::error::get_key_failed , jwt::error::set_rsa_pss_saltlen_failed ,
  jwt::error::signature_encoding_failed
}
 Errors related to verification of signatures. More...
enum class  jwt::error::signature_generation_error {
  jwt::error::ok = 0 , jwt::error::hmac_failed = 10 , jwt::error::create_context_failed , jwt::error::signinit_failed ,
  jwt::error::signupdate_failed , jwt::error::signfinal_failed , jwt::error::ecdsa_do_sign_failed , jwt::error::digestinit_failed ,
  jwt::error::digestupdate_failed , jwt::error::digestfinal_failed , jwt::error::rsa_padding_failed , jwt::error::rsa_private_encrypt_failed ,
  jwt::error::get_key_failed , jwt::error::set_rsa_pss_saltlen_failed , jwt::error::signature_decoding_failed
}
 Errors related to signature generation errors. More...
enum class  jwt::error::token_verification_error {
  jwt::error::ok = 0 , jwt::error::wrong_algorithm = 10 , jwt::error::missing_claim , jwt::error::claim_type_missmatch ,
  jwt::error::claim_value_missmatch , jwt::error::token_expired , jwt::error::audience_missmatch
}
 Errors related to token verification errors. More...
enum class  jwt::json::type {
  jwt::json::boolean , jwt::json::integer , jwt::json::number , jwt::json::string ,
  jwt::json::array , jwt::json::object
}
 Categories for the various JSON types used in JWTs. More...

Functions

std::error_category & jwt::error::rsa_error_category ()
 Error category for RSA errors.
std::error_code jwt::error::make_error_code (rsa_error e)
 Converts JWT-CPP errors into generic STL error_codes.
std::error_category & jwt::error::ecdsa_error_category ()
 Error category for ECDSA errors.
std::error_code jwt::error::make_error_code (ecdsa_error e)
 Converts JWT-CPP errors into generic STL error_codes.
std::error_category & jwt::error::signature_verification_error_category ()
 Error category for verification errors.
std::error_code jwt::error::make_error_code (signature_verification_error e)
 Converts JWT-CPP errors into generic STL error_codes.
std::error_category & jwt::error::signature_generation_error_category ()
 Error category for signature generation errors.
std::error_code jwt::error::make_error_code (signature_generation_error e)
 Converts JWT-CPP errors into generic STL error_codes.
std::error_category & jwt::error::token_verification_error_category ()
 Error category for token verification errors.
std::error_code jwt::error::make_error_code (token_verification_error e)
 Converts JWT-CPP errors into generic STL error_codes.
void jwt::error::throw_if_error (std::error_code ec)
 Raises an exception if any JWT-CPP error codes are active.
std::unique_ptr< BIO, decltype(&BIO_free_all)> jwt::helper::make_mem_buf_bio ()
std::unique_ptr< BIO, decltype(&BIO_free_all)> jwt::helper::make_mem_buf_bio (const std::string &data)
template<typename error_category = error::rsa_error>
std::string jwt::helper::write_bio_to_string (std::unique_ptr< BIO, decltype(&BIO_free_all)> &bio_out, std::error_code &ec)
std::unique_ptr< EVP_MD_CTX, void(*)(EVP_MD_CTX *)> jwt::helper::make_evp_md_ctx ()
template<typename error_category = error::rsa_error>
std::string jwt::helper::extract_pubkey_from_cert (const std::string &certstr, const std::string &pw, std::error_code &ec)
 Extract the public key of a pem certificate.
template<typename error_category = error::rsa_error>
std::string jwt::helper::extract_pubkey_from_cert (const std::string &certstr, const std::string &pw="")
 Extract the public key of a pem certificate.
std::string jwt::helper::convert_der_to_pem (const std::string &cert_der_str, std::error_code &ec)
 Convert the certificate provided as DER to PEM.
template<typename Decode>
std::string jwt::helper::convert_base64_der_to_pem (const std::string &cert_base64_der_str, Decode decode, std::error_code &ec)
 Convert the certificate provided as base64 DER to PEM.
template<typename Decode>
std::string jwt::helper::convert_base64_der_to_pem (const std::string &cert_base64_der_str, Decode decode)
 Convert the certificate provided as base64 DER to PEM.
std::string jwt::helper::convert_der_to_pem (const std::string &cert_der_str)
 Convert the certificate provided as DER to PEM.
std::string jwt::helper::convert_base64_der_to_pem (const std::string &cert_base64_der_str, std::error_code &ec)
 Convert the certificate provided as base64 DER to PEM.
std::string jwt::helper::convert_base64_der_to_pem (const std::string &cert_base64_der_str)
 Convert the certificate provided as base64 DER to PEM.
template<typename error_category = error::rsa_error>
evp_pkey_handle jwt::helper::load_public_key_from_string (const std::string &key, const std::string &password, std::error_code &ec)
 Load a public key from a string.
template<typename error_category = error::rsa_error>
evp_pkey_handle jwt::helper::load_public_key_from_string (const std::string &key, const std::string &password="")
 Load a public key from a string.
template<typename error_category = error::rsa_error>
evp_pkey_handle jwt::helper::load_private_key_from_string (const std::string &key, const std::string &password, std::error_code &ec)
 Load a private key from a string.
template<typename error_category = error::rsa_error>
evp_pkey_handle jwt::helper::load_private_key_from_string (const std::string &key, const std::string &password="")
 Load a private key from a string.
evp_pkey_handle jwt::helper::load_public_ec_key_from_string (const std::string &key, const std::string &password, std::error_code &ec)
 Load a public key from a string.
std::string jwt::helper::bn2raw (const BIGNUM *bn)
std::unique_ptr< BIGNUM, decltype(&BN_free)> jwt::helper::raw2bn (const std::string &raw, std::error_code &ec)
std::unique_ptr< BIGNUM, decltype(&BN_free)> jwt::helper::raw2bn (const std::string &raw)
evp_pkey_handle jwt::helper::load_public_ec_key_from_string (const std::string &key, const std::string &password="")
 Load a public key from a string.
evp_pkey_handle jwt::helper::load_private_ec_key_from_string (const std::string &key, const std::string &password, std::error_code &ec)
 Load a private key from a string.
template<typename Decode>
std::string jwt::helper::create_public_key_from_rsa_components (const std::string &modulus, const std::string &exponent, Decode decode, std::error_code &ec)
 create public key from modulus and exponent. This is defined in RFC 7518 Section 6.3 Using the required "n" (Modulus) Parameter and "e" (Exponent) Parameter.
template<typename Decode>
std::string jwt::helper::create_public_key_from_rsa_components (const std::string &modulus, const std::string &exponent, Decode decode)
std::string jwt::helper::create_public_key_from_rsa_components (const std::string &modulus, const std::string &exponent, std::error_code &ec)
std::string jwt::helper::create_public_key_from_rsa_components (const std::string &modulus, const std::string &exponent)
evp_pkey_handle jwt::helper::load_private_ec_key_from_string (const std::string &key, const std::string &password="")
 Load a private key from a string.
int jwt::helper::curve2nid (const std::string curve, std::error_code &ec)
 Convert a curve name to an ID.
template<typename Decode>
std::string jwt::helper::create_public_key_from_ec_components (const std::string &curve, const std::string &x, const std::string &y, Decode decode, std::error_code &ec)
template<typename Decode>
std::string jwt::helper::create_public_key_from_ec_components (const std::string &curve, const std::string &x, const std::string &y, Decode decode)
std::string jwt::helper::create_public_key_from_ec_components (const std::string &curve, const std::string &x, const std::string &y, std::error_code &ec)
std::string jwt::helper::create_public_key_from_ec_components (const std::string &curve, const std::string &x, const std::string &y)
 jwt::details::JWT_CPP_SUPPORTS_AS (object)
 jwt::details::JWT_CPP_SUPPORTS_AS (array)
 jwt::details::JWT_CPP_SUPPORTS_AS (string)
 jwt::details::JWT_CPP_SUPPORTS_AS (number)
 jwt::details::JWT_CPP_SUPPORTS_AS (integer)
 jwt::details::JWT_CPP_SUPPORTS_AS (boolean)
template<typename Clock, typename json_traits>
verifier< Clock, json_traits > jwt::verify (Clock c)
template<typename Clock, typename json_traits>
builder< Clock, json_traits > jwt::create (Clock c)
template<typename json_traits>
verifier< default_clock, json_traits > jwt::verify (default_clock c={})
template<typename json_traits>
builder< default_clock, json_traits > jwt::create (default_clock c={})
template<typename json_traits, typename Decode>
decoded_jwt< json_traits > jwt::decode (const typename json_traits::string_type &token, Decode decode)
 Decode a token. This can be used to to help access important feild like 'x5c' for verifying tokens. See associated example rsa-verify.cpp for more details.
template<typename json_traits>
decoded_jwt< json_traits > jwt::decode (const typename json_traits::string_type &token)
template<typename json_traits>
jwk< json_traits > jwt::parse_jwk (const typename json_traits::string_type &jwk_)
template<typename json_traits>
jwks< json_traits > jwt::parse_jwks (const typename json_traits::string_type &jwks_)
template<typename json_traits>
std::istream & operator>> (std::istream &is, jwt::basic_claim< json_traits > &c)
template<typename json_traits>
std::ostream & operator<< (std::ostream &os, const jwt::basic_claim< json_traits > &c)

Macro Definition Documentation

◆ JWT_CLAIM_EXPLICIT

#define JWT_CLAIM_EXPLICIT   explicit

Definition at line 72 of file jwt.h.

◆ JWT_CPP_AS_TYPE_T

#define JWT_CPP_AS_TYPE_T ( TYPE)
Value:
as_##TYPE_t

Definition at line 2382 of file jwt.h.

◆ JWT_CPP_JSON_TYPE_TYPE

#define JWT_CPP_JSON_TYPE_TYPE ( TYPE)
Value:
json_##TYPE_type

Definition at line 2381 of file jwt.h.

◆ JWT_CPP_SUPPORTS_AS

#define JWT_CPP_SUPPORTS_AS ( TYPE)
Value:
template<typename traits_type, typename value_type, typename JWT_CPP_JSON_TYPE_TYPE(TYPE)> \
struct supports_as_##TYPE { \
template<typename T> \
using JWT_CPP_AS_TYPE_T(TYPE) = decltype(T::as_##TYPE); \
\
static constexpr auto value = \
is_function_signature_detected<traits_type, JWT_CPP_AS_TYPE_T(TYPE), \
JWT_CPP_JSON_TYPE_TYPE(TYPE)(const value_type&)>::value; \
\
static_assert(value, "traits implementation must provide `" #TYPE "_type as_" #TYPE "(const value_type&)`"); \
}
#define JWT_CPP_AS_TYPE_T(TYPE)
Definition jwt.h:2382

Definition at line 2383 of file jwt.h.

◆ PICOJSON_USE_INT64

#define PICOJSON_USE_INT64

Definition at line 6 of file jwt.h.

Function Documentation

◆ operator<<()

template<typename json_traits>
std::ostream & operator<< ( std::ostream & os,
const jwt::basic_claim< json_traits > & c )

Definition at line 4258 of file jwt.h.

◆ operator>>()

template<typename json_traits>
std::istream & operator>> ( std::istream & is,
jwt::basic_claim< json_traits > & c )

Definition at line 4253 of file jwt.h.