From aabf7e85461ce2979ae09fb311ad701d5790e33d Mon Sep 17 00:00:00 2001 From: Arun M Date: Fri, 29 Dec 2017 17:29:24 +0530 Subject: [PATCH] Added second example for readme --- README.md | 61 ++++++++++++++++++++++++++++++ examples/CMakeFiles/progress.marks | 2 +- examples/CMakeLists.txt | 3 ++ examples/Makefile | 46 ++++++++++++++++++++++ examples/simple_ex2.cc | 50 ++++++++++++++++++++++++ 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 examples/simple_ex2.cc diff --git a/README.md b/README.md index 51a541e..3ef9009 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ - [Installation](#installation) - [API Categories](#apicategories) - [Advanced Examples](#advancedexamples) +- [Parameters](#parameters) - [JWS Verification](#jwsverification) - [Error Codes & Exceptions](#errorcodeexception) - [Additional Header Data](#additionalheaderdata) @@ -86,3 +87,63 @@ Few good resources on this material which I found useful are: ``` Almost the same API, except for some ugliness here and there. But close enough! + + Lets take another example in which we will see to add payload claim having type other than string. + The payload function used in the above example to create jwt_object object can only take strings. For anything else, it will throw a compilation error. + + For adding claims having values other than string, jwt_object class provides add_claim API. We will also see few other APIs in the next example. Make sure to read the comments :). + + ```cpp + #include + #include + #include + #include "jwt/jwt.hpp" + + int main() { + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"user", "admin"}})}; + + //Use add_claim API to add claim values which are + // _not_ strings. + // For eg: `iat` and `exp` claims below. + // Other claims could have been added in the payload + // function above as they are just stringy things. + obj.add_claim("iss", "arun.muralidharan") + .add_claim("sub", "test") + .add_claim("id", "a-b-c-d-e-f-1-2-3") + .add_claim("iat", 1513862371) + .add_claim("exp", std::chrono::system_clock::now() + std::chrono::seconds{10}) + ; + + //Use `has_claim` to check if the claim exists or not + assert (obj.has_claim("iss")); + assert (obj.has_claim("exp")); + + //Use `has_claim_with_value` to check if the claim exists + //with a specific value or not. + assert (obj.payload().has_claim_with_value("id", "a-b-c-d-e-f-1-2-3")); + assert (obj.payload().has_claim_with_value("iat", 1513862371)); + + //Remove a claim using `remove_claim` API. + //Most APIs have an overload which takes enum class type as well + //It can be used interchangeably with strings. + obj.remove_claim(jwt::registered_claims::expiration); + assert (not obj.has_claim("exp")); + + //Using `add_claim` with extra features. + //Check return status and overwrite + bool ret = obj.payload().add_claim("sub", "new test", false/*overwrite*/); + assert (not ret); + + // Overwrite an existing claim + ret = obj.payload().add_claim("sub", "new test", true/*overwrite*/); + assert ( ret ); + + assert (obj.payload().has_claim_with_value("sub", "new test")); + + return 0; + } + ``` + + diff --git a/examples/CMakeFiles/progress.marks b/examples/CMakeFiles/progress.marks index 0cfbf08..b8626c4 100644 --- a/examples/CMakeFiles/progress.marks +++ b/examples/CMakeFiles/progress.marks @@ -1 +1 @@ -2 +4 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b2d024b..e225f14 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,3 +7,6 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\ add_executable(simple_ex1 simple_ex1.cc) target_link_libraries(simple_ex1 ssl crypto gtest) + +add_executable(simple_ex2 simple_ex2.cc) +target_link_libraries(simple_ex2 ssl crypto gtest) diff --git a/examples/Makefile b/examples/Makefile index 546b4b1..d6ae8b6 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -110,6 +110,21 @@ depend: cd /Users/amuralid/dev_test/cpp-jwt && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 .PHONY : depend +# Convenience name for target. +examples/CMakeFiles/simple_ex2.dir/rule: + cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f CMakeFiles/Makefile2 examples/CMakeFiles/simple_ex2.dir/rule +.PHONY : examples/CMakeFiles/simple_ex2.dir/rule + +# Convenience name for target. +simple_ex2: examples/CMakeFiles/simple_ex2.dir/rule + +.PHONY : simple_ex2 + +# fast build rule for target. +simple_ex2/fast: + cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f examples/CMakeFiles/simple_ex2.dir/build.make examples/CMakeFiles/simple_ex2.dir/build +.PHONY : simple_ex2/fast + # Convenience name for target. examples/CMakeFiles/simple_ex1.dir/rule: cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f CMakeFiles/Makefile2 examples/CMakeFiles/simple_ex1.dir/rule @@ -152,6 +167,33 @@ simple_ex1.cc.s: cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f examples/CMakeFiles/simple_ex1.dir/build.make examples/CMakeFiles/simple_ex1.dir/simple_ex1.cc.s .PHONY : simple_ex1.cc.s +simple_ex2.o: simple_ex2.cc.o + +.PHONY : simple_ex2.o + +# target to build an object file +simple_ex2.cc.o: + cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f examples/CMakeFiles/simple_ex2.dir/build.make examples/CMakeFiles/simple_ex2.dir/simple_ex2.cc.o +.PHONY : simple_ex2.cc.o + +simple_ex2.i: simple_ex2.cc.i + +.PHONY : simple_ex2.i + +# target to preprocess a source file +simple_ex2.cc.i: + cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f examples/CMakeFiles/simple_ex2.dir/build.make examples/CMakeFiles/simple_ex2.dir/simple_ex2.cc.i +.PHONY : simple_ex2.cc.i + +simple_ex2.s: simple_ex2.cc.s + +.PHONY : simple_ex2.s + +# target to generate assembly for a file +simple_ex2.cc.s: + cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f examples/CMakeFiles/simple_ex2.dir/build.make examples/CMakeFiles/simple_ex2.dir/simple_ex2.cc.s +.PHONY : simple_ex2.cc.s + # Help Target help: @echo "The following are some of the valid targets for this Makefile:" @@ -160,10 +202,14 @@ help: @echo "... depend" @echo "... edit_cache" @echo "... rebuild_cache" + @echo "... simple_ex2" @echo "... simple_ex1" @echo "... simple_ex1.o" @echo "... simple_ex1.i" @echo "... simple_ex1.s" + @echo "... simple_ex2.o" + @echo "... simple_ex2.i" + @echo "... simple_ex2.s" .PHONY : help diff --git a/examples/simple_ex2.cc b/examples/simple_ex2.cc new file mode 100644 index 0000000..bdc70d1 --- /dev/null +++ b/examples/simple_ex2.cc @@ -0,0 +1,50 @@ +#include +#include +#include +#include "jwt/jwt.hpp" + +int main() { + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"user", "admin"}})}; + + //Use add_claim API to add claim values which are + // _not_ strings. + // For eg: `iat` and `exp` claims below. + // Other claims could have been added in the payload + // function above as they are just stringy things. + obj.add_claim("iss", "arun.muralidharan") + .add_claim("sub", "test") + .add_claim("id", "a-b-c-d-e-f-1-2-3") + .add_claim("iat", 1513862371) + .add_claim("exp", std::chrono::system_clock::now() + std::chrono::seconds{10}) + ; + + //Use `has_claim` to check if the claim exists or not + assert (obj.has_claim("iss")); + assert (obj.has_claim("exp")); + + //Use `has_claim_with_value` to check if the claim exists + //with a specific value or not. + assert (obj.payload().has_claim_with_value("id", "a-b-c-d-e-f-1-2-3")); + assert (obj.payload().has_claim_with_value("iat", 1513862371)); + + //Remove a claim using `remove_claim` API. + //Most APIs have an overload which takes enum class type as well + //It can be used interchangeably with strings. + obj.remove_claim(jwt::registered_claims::expiration); + assert (not obj.has_claim("exp")); + + //Using `add_claim` with extra features. + //Check return status and overwrite + bool ret = obj.payload().add_claim("sub", "new test", false/*overwrite*/); + assert (not ret); + + // Overwrite an existing claim + ret = obj.payload().add_claim("sub", "new test", true/*overwrite*/); + assert ( ret ); + + assert (obj.payload().has_claim_with_value("sub", "new test")); + + return 0; +}