Files
jwt-cpp/example/private-claims.cpp
Chris Mc a6927cb814 Add support for CMake Presets (#348)
* dev container + vscode extensions

* exploring cmake presets for CI

* ignore user presets

* add configure to build preset

windows ran in debug, which means the multi config needs more help

* coverage preset

* creating a fuzzing preset

* fuzzing fix cwd + more configs to preset

* move cmake set vars to right section

https://learn.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-170

"You can set C and C++ compilers by using cacheVariables.CMAKE_C_COMPILER and cacheVariables.CMAKE_CXX_COMPILER in a Configure Preset. It's equivalent to passing -D CMAKE_C_COMPILER=<value> and -D CMAKE_CXX_COMPILER=<value> to CMake from the command line."

* fix workdir for new fuzz run targets

* split up presets into more files

* fix include order (liking this less)

* add asan to presets

* fix copy paste

* add asan build preset

* move ubsan to presets

* replace mustache with a cmake script

* touch ups

* restore file to void merge conflicts

* fix order for args

* remove action wrapper

more quotes for inputs

* revert bad render of boost_json

* fix casing

* format changes

* test to see if output is working

* last try

* git revert moving mustache to cmake

* cmake not enough for presets

* drop ci prefix

* new test for `library_ROOT` search path

* install with presets

* cmake --install does not take a preset

* install with preset is not yet supported

https://gitlab.kitware.com/cmake/cmake/-/issues/23208

* add missing build step

* more global fuzzing flags to toolchain file

* fixup toolchain in presets

* fix pathing and limit configs to tested platforms

* clean up + use presets for clang-tidy

* dry up sanitizer flags

* try install with a preset

* remove duplicated test

* notes about hunter test

* skip checking the compiler since this causes errors with the same CMake uses in combinations with the fuzz sanitizer flag

https://github.com/prince-chrismc/jwt-cpp/actions/runs/7481024827/job/20361722539?pr=33

```
    /usr/bin/ld: /usr/bin/ld: DWARF error: invalid or unhandled FORM value: 0x22
    CMakeFiles/cmTC_ff7c6.dir/testCCompiler.c.o: in function `main':
    testCCompiler.c:(.text.main[main]+0x0): multiple definition of `main'; /usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.fuzzer-x86_64.a(FuzzerMain.cpp.o):(.text.main+0x0): first defined here
    /usr/bin/ld: /usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.fuzzer-x86_64.a(FuzzerMain.cpp.o): in function `main':
    (.text.main+0x12): undefined reference to `LLVMFuzzerTestOneInput'
```

* remove system name to disable cross compiling

* tests do not pass clang-tidy

* drop toolchain, use target specific flags

* leave flags in cmake

* missing deps + clean apt files

* remove some code duplication

* fix typo

* devcontainers fix, test and add wolfssl

refactored installer to have a script that could be reused

* add missing build context

* pass version to script

* dont use bash syntax

* fix typo

* upload tar.gz for new releases

* revert name to manually trigger it

* Update and rename nuget.yml to release.yml

* disable nuget for testing

* add a zip as well

* back to basics

* remove remove testing code

* update workflow name

* enable testing needs to be in the root

* add a test preset and use it in some workflows

* fix linting tests

* bump github actions

* bump python setup

* drop dev containers

* dont install openssl on windows

* revert merge conflicts

* fix whitespace

* fix private claims example to have a valid token

#351

* put back versioned format

I dont remember why, it looks like it was debugging
2024-06-15 07:52:23 -07:00

50 lines
1.6 KiB
C++

/// @file private-claims.cpp
#include <jwt-cpp/jwt.h>
#include <iostream>
#include <sstream>
using sec = std::chrono::seconds;
using min = std::chrono::minutes;
int main() {
jwt::claim from_raw_json;
std::istringstream iss{R"##({"api":{"array":[1,2,3],"null":null}})##"};
iss >> from_raw_json;
jwt::claim::set_t list{"once", "twice"};
std::vector<int64_t> big_numbers{727663072ULL, 770979831ULL, 427239169ULL, 525936436ULL};
const auto time = jwt::date::clock::now();
const auto token = jwt::create()
.set_type("JWT")
.set_issuer("auth.mydomain.io")
.set_audience("mydomain.io")
.set_issued_at(time)
.set_not_before(time - sec{15})
.set_expires_at(time + sec{15} + min{2})
.set_payload_claim("boolean", picojson::value(true))
.set_payload_claim("integer", picojson::value(int64_t{12345}))
.set_payload_claim("precision", picojson::value(12.345))
.set_payload_claim("strings", jwt::claim(list))
.set_payload_claim("array", jwt::claim(big_numbers.begin(), big_numbers.end()))
.set_payload_claim("object", from_raw_json)
.sign(jwt::algorithm::none{});
const auto decoded = jwt::decode(token);
const auto api_array = decoded.get_payload_claim("object").to_json().get("api").get("array");
std::cout << "api array = " << api_array << std::endl;
/* [verify exact claim] */
jwt::verify()
.allow_algorithm(jwt::algorithm::none{})
.with_issuer("auth.mydomain.io")
.with_audience("mydomain.io")
.with_claim("object", from_raw_json) // Match the exact JSON content
.verify(decoded);
/* [verify exact claim] */
return 0;
}