diff --git a/README.md b/README.md index ae9d56d..ad5c8cf 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ # jwt-cpp +[![License Badge](https://img.shields.io/github/license/Thalhammer/jwt-cpp)](LICENSE) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/5f7055e294744901991fd0a1620b231d)](https://app.codacy.com/app/Thalhammer/jwt-cpp?utm_source=github.com&utm_medium=referral&utm_content=Thalhammer/jwt-cpp&utm_campaign=Badge_Grade_Settings) [![Build Status](https://travis-ci.com/Thalhammer/jwt-cpp.svg?branch=master)](https://travis-ci.com/Thalhammer/jwt-cpp) [![Coverage Status](https://coveralls.io/repos/github/Thalhammer/jwt-cpp/badge.svg?branch=master)](https://coveralls.io/github/Thalhammer/jwt-cpp?branch=master) +[![Documentation Badge](https://img.shields.io/badge/Documentation-master-blue)](https://thalhammer.github.io/jwt-cpp/) +[![Stars Badge](https://img.shields.io/github/stars/Thalhammer/jwt-cpp)](https://github.com/Thalhammer/jwt-cpp/stargazers) -A header only library for creating and validating json web tokens in c++. +A header only library for creating and validating [JSON Web Tokens](https://tools.ietf.org/html/rfc7519) in C++11. For a great introduction, [read this](https://jwt.io/introduction/) ## Signature algorithms As of version 0.2.0 jwt-cpp supports all algorithms defined by the spec. The modular design of jwt-cpp allows one to add additional algorithms without any problems. If you need any feel free to open a pull request. @@ -25,19 +28,23 @@ For the sake of completeness, here is a list of all supported algorithms: ## Overview Since 0.5.0-dev, there is no hard dependency on a JSON library; instead there's a generic `basic_claim` which is templated around the types required. It requires the "value type", object, array, string, number, integer, and boolean types and a traits type. -In order to maintain compatibility, [picojson](https://github.com/kazuho/picojson) is still used to provide a specialized `claim` along with all helpers. Defining `DISABLE_PICOJSON` will remove this optional dependency. +```cpp +jwt::basic_claim claim(json::object({{"json", true},{"example", 0}})); +``` -For using other JSON libraries, the picojson implementation is a good reference. +This allows for complete freedom when picking which libraries you want to use. For more information, [see below](#your-traits) + +In order to maintain compatibility, [picojson](https://github.com/kazuho/picojson) is still used to provide a specialized `claim` along with all helpers. Defining `DISABLE_PICOJSON` will remove this optional dependency. As for the base64 requirements of JWTs, this libary provides `base.h` with all the required implentation; However base64 implementations are very common, with varying degrees of performance. When providing your own base64 implementation, you can define `DISABLE_BASE64` to remove the jwt-cpp implementation. -## Examples +### Getting Started Simple example of decoding a token and printing all claims: ```c++ #include #include -int main(int argc, const char** argv) { +int main() { std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; auto decoded = jwt::decode(token); @@ -107,3 +114,59 @@ See [here](https://github.com/Thalhammer/jwt-cpp/issues/5) for more details. To * define NOMINMAX, which suppresses this behaviour * include this library before you include windows.h * place ```#undef max``` and ```#undef min``` before you include this library + +## Providing your own JSON Traits {#your-traits} + +There are several key items that need to provide to a `jwt::basic_claim` in order for it to be interoptable with you JSON library of choice. +- type specifications +- conversion from genric "value type" to specific +- serialization and parsing + +If ever you are not sure, the traits are heavily checked against static asserts to make sure you provide everything that's requires. + +> Note: there is currently limited support for narrow strings + +```cpp +struct my_favorite_json_library_traits { + // Type Specifications + using value_type = json; // The generic "value type" implementation, most libraries have one + using object_type = json::object_t; // The "map type" string to value + using array_type = json::array_t; // The "list type" array of values + using string_type = std::string; // The "list of chars" + using number_type = double; // The "percision type" + using integer_type = int64_t; // The "integral type" + using boolean_type = bool; // The "boolean type" + + // Translation between the implementation notion of type, to the jwt::json::type equivilant + static jwt::json::type get_type(const value_type &val) { + using jwt::json::type; + + if (val.type() == json::value_t::boolean) + return type::boolean; + else if (val.type() == json::value_t::number_integer) + return type::integer; + else if (val.type() == json::value_t::number_float) + return type::number; + else if (val.type() == json::value_t::string) + return type::string; + else if (val.type() == json::value_t::array) + return type::array; + else if (val.type() == json::value_t::object) + return type::object; + else + throw std::logic_error("invalid type"); + } + + // Conversion from generic value to specific type + static object_type as_object(const value_type &val); + static array_type as_array(const value_type &val); + static string_type as_string(const value_type &val); + static number_type as_number(const value_type &val); + static integer_type as_int(const value_type &val); + static boolean_type as_bool(const value_type &val); + + // serilization and parsing + static bool parse(value_type &val, string_type str); + static string_type serialize(const value_type &val); +}; +``` diff --git a/include/jwt-cpp/base.h b/include/jwt-cpp/base.h index 96353aa..4ea35ba 100644 --- a/include/jwt-cpp/base.h +++ b/include/jwt-cpp/base.h @@ -16,7 +16,13 @@ #endif namespace jwt { + /** + * \brief character maps when encoding and decoding + */ namespace alphabet { + /** + * \brief valid list of characted when working with [Base64](https://tools.ietf.org/html/rfc3548) + */ struct base64 { static const std::array& data() { static constexpr std::array data { @@ -32,6 +38,9 @@ namespace jwt { return fill; } }; + /** + * \brief valid list of characted when working with [Base64URL](https://tools.ietf.org/html/rfc4648) + */ struct base64url { static const std::array& data() { static constexpr std::array data { @@ -49,6 +58,9 @@ namespace jwt { }; } // namespace alphabet + /** + * \brief Alphabet generic methods for working with encoding/decoding the base64 family + */ class base { public: template diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index aa35976..9f1b05a 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -38,6 +38,13 @@ #define JWT_CLAIM_EXPLICIT explicit #endif +/** + * \brief JSON Web Token + * + * A namespace to contain everything related to handling JSON Web Tokens, JWT for short, + * as a part of [RFC7519](https://tools.ietf.org/html/rfc7519), or alternatively for + * JWS (JSON Web Signature)from [RFC7515](https://tools.ietf.org/html/rfc7515) + */ namespace jwt { using date = std::chrono::system_clock::time_point; @@ -88,6 +95,13 @@ namespace jwt { {} }; + /** + * \brief A collection for working with certificates + * + * These _helpers_ are usefully when working with certificates OpenSSL APIs. + * For example, when dealing with JWKS (JSON Web Key Set)[https://tools.ietf.org/html/rfc7517] + * you maybe need to extract the modulus and exponent of an RSA Public Key. + */ namespace helper { inline std::string extract_pubkey_from_cert(const std::string& certstr, const std::string& pw = "") { @@ -170,18 +184,33 @@ namespace jwt { } } // namespace helper + /** + * \brief Various cryptographic algorithms when working with JWT + * + * JWT (JSON Web Tokens) signatures are typically used as the payload for a JWS (JSON Web Signature) or + * JWE (JSON Web Encryption). Both of these use various cryptographic as specified by [RFC7518](https://tools.ietf.org/html/rfc7518) + * and are exposed through the a [JOSE Header](https://tools.ietf.org/html/rfc7515#section-4) which + * points to one of the JWA (JSON Web Algorithms)(https://tools.ietf.org/html/rfc7518#section-3.1) + */ namespace algorithm { /** - * "none" algorithm. + * \brief "none" algorithm. * * Returns and empty signature and checks if the given signature is empty. */ struct none { - /// Return an empty string + /** + * \brief Return an empty string + */ std::string sign(const std::string& /*unused*/) const { return ""; } - /// Check if the given signature is empty. JWT's with "none" algorithm should not contain a signature. + /** + * \brief Check if the given signature is empty. + * + * JWT's with "none" algorithm should not contain a signature. + * \throw signature_verification_exception + */ void verify(const std::string& /*unused*/, const std::string& signature) const { if (!signature.empty()) throw signature_verification_exception(); @@ -192,7 +221,7 @@ namespace jwt { } }; /** - * Base class for HMAC family of algorithms + * \brief Base class for HMAC family of algorithms */ struct hmacsha { /** @@ -243,7 +272,7 @@ namespace jwt { } /** * Returns the algorithm name provided to the constructor - * \return Algorithmname + * \return algorithm's name */ std::string name() const { return alg_name; @@ -253,11 +282,11 @@ namespace jwt { const std::string secret; /// HMAC hash generator const EVP_MD*(*md)(); - /// Algorithmname + /// algorithm's name const std::string alg_name; }; /** - * Base class for RSA family of algorithms + * \brief Base class for RSA family of algorithms */ struct rsa { /** @@ -332,7 +361,7 @@ namespace jwt { } /** * Returns the algorithm name provided to the constructor - * \return Algorithmname + * \return algorithm's name */ std::string name() const { return alg_name; @@ -342,11 +371,11 @@ namespace jwt { std::shared_ptr pkey; /// Hash generator const EVP_MD*(*md)(); - /// Algorithmname + /// algorithm's name const std::string alg_name; }; /** - * Base class for ECDSA family of algorithms + * \brief Base class for ECDSA family of algorithms */ struct ecdsa { /** @@ -460,7 +489,7 @@ namespace jwt { } /** * Returns the algorithm name provided to the constructor - * \return Algorithmname + * \return algorithm's name */ std::string name() const { return alg_name; @@ -494,14 +523,14 @@ namespace jwt { std::shared_ptr pkey; /// Hash generator function const EVP_MD*(*md)(); - /// Algorithmname + /// algorithm's name const std::string alg_name; /// Length of the resulting signature const size_t signature_length; }; /** - * Base class for PSS-RSA family of algorithms + * \brief Base class for PSS-RSA family of algorithms */ struct pss { /** @@ -565,7 +594,7 @@ namespace jwt { } /** * Returns the algorithm name provided to the constructor - * \return Algorithmname + * \return algorithm's name */ std::string name() const { return alg_name; @@ -599,7 +628,7 @@ namespace jwt { std::shared_ptr pkey; /// Hash generator function const EVP_MD*(*md)(); - /// Algorithmname + /// algorithm's name const std::string alg_name; }; @@ -777,9 +806,13 @@ namespace jwt { }; } // namespace algorithm + /** + * \brief JSON Abstractions for working with any library + */ namespace json { /** - * Generic JSON types used in JWTs + * \brief Generic JSON types used in JWTs + * * This enum is to abstract the third party underlying types */ enum class type { @@ -1868,8 +1901,8 @@ namespace jwt { * Default clock class using std::chrono::system_clock as a backend. */ struct default_clock { - std::chrono::system_clock::time_point now() const { - return std::chrono::system_clock::now(); + date now() const { + return date::clock::now(); } };