Chris Mc 3ddac0c776 Documentation Refresh (#323)
* Create getting-started.md

* Update getting-started.md

* Create header-only.yml

* Create package-managers.yml

* Update package-managers.yml

* Update getting-started.md

* Update getting-started.md

* Update header-only.yml

* Update header-only.yml

* Update header-only.yml

* Update header-only.yml

* use `jwt::algorithm::hs256`

 to link against openssl

* add crypto

* switch to ssl

* Update header-only.yml

* tidy

* lets install conan

* tidying

* Adding a CMake target to make the docs

* cleaning up repo root folder

* fix typo

* set docs flag as on

* update awesome doxy theme to latest

* Updated overrides for new version

* output folder and target name are changed

* add a few more docs

* white space

* cmake format

* playing with settings

* move algorithms - magically fixed missing structs

* playing with the idea of more refactoring

* more testing

* moved evp_handle def to special namespace for doxygen

* split evp class by it's self

* fix doc links

* playing with evp class to fix error -- no luck

* revert testing changes

* clang format

* cache should be container specific

* clean up includes

* combined class definition

* split algos but running in ci with older version

* trying slightly newer version of doxygen

* doxygen 196

* doxygen 197

* doxygen 198

* avoid regression in 198

* more docs

* playing with adding examples

* playing around with example support to see if I like

* docs include all the example files to be referenced in the code

* bump version to 0.7.0

* missing header

* devcontainer for testing openssl 3

* add test explorer

* add clang helpers

* there's a lot of package managers in 2023

* fixup colors with doxygen 1.10

* bump doxygen version

since my bug has been fixed

* move everything to one file (too many changes)

* update doxyfile to 1.10

* minimize diff

* fix extra line

* fix whitespace

* clean up dev container

* min changes

* cleanup

* better checking + warnings for doxygen

* whitespace

* adding in more docs

* fix spelling

* make sure traits appear

* trying to add snippets from examples

cant figure it out
https://github.com/doxygen/doxygen/issues/10517

* Update faqs.md

* Delete .github/workflows/header-only.yml

* Delete .github/workflows/package-managers.yml

* Delete example/conan/CMakeLists.txt

* Delete example/conan/conanfile.txt

* Delete example/conan/main.cpp

* Delete example/conan/README.md

* Update nlohmann-json.cpp

* add updated getting started docs

first pass towards https://github.com/Thalhammer/jwt-cpp/issues/319

* touch ups

* linking deprecated messages

* improve cmake with find_package examples

* fix code link color

* be more clear about turning off examples

good suggestion

https://github.com/Thalhammer/jwt-cpp/pull/321#issuecomment-1870488941

* clarify openssl install and fixup fetch tag

* apply workaround from

https://github.com/doxygen/doxygen/issues/10517#issuecomment-1870509240

* fix renamed types

* linting

* cross reference new docs

* trying new alert syntax

https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts

* move sections out of readme

trying to make it shorter and more approachable

* improve "getting started" readme section with a more through example

* sort out doubled "getting started"

second one in docs/ is just installation

* linter

* fixup english

* formatting

* whitespace

* tie in more examples with snippets

* minor touch ups

* Update signing.md

* JWT_BUILD_DOCS

* better cache string for new option

* word crafting

* word crafting

* add missing open-source-parsers/jsoncpp to traits list

* better github alert

* fixup new shields io start style

* improve docs cmake target name

* linter

---------

Co-authored-by: Christopher McArthur <christopherm@jfrog.com>
2024-01-10 09:29:46 -05:00
2024-01-10 09:29:46 -05:00
2023-07-25 18:45:15 +02:00
2024-01-10 09:29:46 -05:00
2024-01-10 09:29:46 -05:00
2024-01-10 09:29:46 -05:00
2023-12-10 17:50:22 -05:00
2021-01-11 17:37:14 -05:00
2021-01-11 17:37:14 -05:00
2024-01-10 09:29:46 -05:00
2024-01-10 09:29:46 -05:00
2018-02-28 16:41:30 +01:00
2024-01-10 09:29:46 -05:00

logo

License Badge Codacy Badge Linux Badge MacOS Badge Windows Badge Coverage Status

Documentation Badge

Stars Badge GitHub release (latest SemVer including pre-releases) ConanCenter package Vcpkg package

Overview

A header only library for creating and validating JSON Web Tokens in C++11. For a great introduction, read this.

The objective is to deliver a versatile and universally applicable collection of algorithms, classes, and data structures, fostering adaptability and seamless integration with other libraries that you may already be employing.

Signature algorithms

jwt-cpp comprehensively supports all algorithms specified in the standard. Its modular design facilitates the seamless inclusion of additional algorithms without encountering any complications. Should you wish to contribute new algorithms, feel free to initiate a pull request or open an issue.

For completeness, here is a list of all supported algorithms:

HMSC RSA ECDSA PSS EdDSA
HS256 RS256 ES256 PS256 Ed25519
HS384 RS384 ES384 PS384 Ed448
HS512 RS512 ES512 PS512
ES256K

Getting Started

Installation instructions can be found here.

A simple example is decoding a token and printing all of its claims let's (try it out):

#include <jwt-cpp/jwt.h>
#include <iostream>

int main() {
    std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCIsInNhbXBsZSI6InRlc3QifQ.lQm3N2bVlqt2-1L-FsOjtR6uE-L4E9zJutMWKIe1v1M";
    auto decoded = jwt::decode(token);

    for(auto& e : decoded.get_payload_json())
        std::cout << e.first << " = " << e.second << std::endl;
}

You can build and run this example locally after cloning the repository. Running some commands, we can see the contents of the JWT payload

cmake .
cmake --build . --target print-claims
./print-claims
# iss = "auth0"
# sample = "test"

You'll very quickly notice JWT are not encrypted but rather cryptographically signed to provide non-repudiation.

In order to verify a token you first build a verifier and use it to verify a decoded token.

auto verifier = jwt::verify()
    .with_issuer("auth0")
    .with_claim("sample", jwt::claim(std::string("test")));
    .allow_algorithm(jwt::algorithm::hs256{"secret"})

verifier.verify(decoded_token);

The verifier is stateless so you can reuse it for different tokens.

Creating the token above (and signing it) is equally as easy.

auto token = jwt::create()
    .set_type("JWS")
    .set_issuer("auth0")
    .set_payload_claim("sample", jwt::claim(std::string("test")))
    .sign(jwt::algorithm::hs256{"secret"});

If you are looking to issue or verify more unique tokens, checkout out the examples working with RSA public and private keys, elliptic curve tokens, and much more!

Configuration Options

Building on the goal of providing flexibility.

SSL Compatibility

jwt-cpp supports OpenSSL, LibreSSL, and wolfSSL. For a listed of tested versions, check this page for more details.

JSON Implementation

There is no strict reliance on a specific JSON library in this context. Instead, the jwt-cpp utilizes a generic jwt::basic_claim that is templated based on type trait. This trait provides the semantic JSON types for values, objects, arrays, strings, numbers, integers, and booleans, along with methods to seamlessly translate between them.

This design offers flexibility in choosing the JSON library that best suits your needs. To leverage one of the provided JSON traits, refer to docs/traits.md for detailed guidance.

Providing your own JSON Traits
jwt::basic_claim<my_favorite_json_library_traits> claim(json::object({{"json", true},{"example", 0}}));

To learn how to writes a trait's implementation, checkout the these instructions

Base64 Options

With regard to the base64 specifications for JWTs, this library includes base.h encompassing all necessary variants. While the library itself offers a proficient base64 implementation, it's worth noting that base64 implementations are widely available, exhibiting diverse performance levels. If you prefer to use your own base64 implementation, you have the option to define JWT_DISABLE_BASE64 to exclude the jwt-cpp implementation.

Contributing

If you have suggestions for improvement or if you've identified a bug, please don't hesitate to open an issue or contribute by creating a pull request. When reporting a bug, provide comprehensive details about your environment, including compiler version and other relevant information, to facilitate issue reproduction. Additionally, if you're introducing a new feature, ensure that you include corresponding test cases to validate its functionality.

Dependencies

In order to use jwt-cpp you need the following tools.

  • libcrypto (openssl or compatible)
  • libssl-dev (for the header files)
  • a compiler supporting at least c++11
  • basic stl support

In order to build the test cases you also need

  • gtest
  • pthread

Troubleshooting

See the FAQs for tips.

Conference Coverage

CppCon

Description
A header only library for creating and validating json web tokens in c++
Readme MIT 1.4 MiB
Languages
C++ 92.5%
CMake 7.5%