mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 09:18:33 +00:00
Added second example for readme
This commit is contained in:
parent
6302055db1
commit
aabf7e8546
5 changed files with 161 additions and 1 deletions
61
README.md
61
README.md
|
@ -23,6 +23,7 @@
|
||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
- [API Categories](#apicategories)
|
- [API Categories](#apicategories)
|
||||||
- [Advanced Examples](#advancedexamples)
|
- [Advanced Examples](#advancedexamples)
|
||||||
|
- [Parameters](#parameters)
|
||||||
- [JWS Verification](#jwsverification)
|
- [JWS Verification](#jwsverification)
|
||||||
- [Error Codes & Exceptions](#errorcodeexception)
|
- [Error Codes & Exceptions](#errorcodeexception)
|
||||||
- [Additional Header Data](#additionalheaderdata)
|
- [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!
|
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 <code>payload</code> function used in the above example to create <code>jwt_object</code> object can only take strings. For anything else, it will throw a compilation error.
|
||||||
|
|
||||||
|
For adding claims having values other than string, <code>jwt_object</code> class provides <code>add_claim</code> API. We will also see few other APIs in the next example. Make sure to read the comments :).
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <chrono>
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
2
|
4
|
||||||
|
|
|
@ -7,3 +7,6 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\
|
||||||
|
|
||||||
add_executable(simple_ex1 simple_ex1.cc)
|
add_executable(simple_ex1 simple_ex1.cc)
|
||||||
target_link_libraries(simple_ex1 ssl crypto gtest)
|
target_link_libraries(simple_ex1 ssl crypto gtest)
|
||||||
|
|
||||||
|
add_executable(simple_ex2 simple_ex2.cc)
|
||||||
|
target_link_libraries(simple_ex2 ssl crypto gtest)
|
||||||
|
|
|
@ -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
|
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
|
.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.
|
# Convenience name for target.
|
||||||
examples/CMakeFiles/simple_ex1.dir/rule:
|
examples/CMakeFiles/simple_ex1.dir/rule:
|
||||||
cd /Users/amuralid/dev_test/cpp-jwt && $(MAKE) -f CMakeFiles/Makefile2 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
|
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
|
.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 Target
|
||||||
help:
|
help:
|
||||||
@echo "The following are some of the valid targets for this Makefile:"
|
@echo "The following are some of the valid targets for this Makefile:"
|
||||||
|
@ -160,10 +202,14 @@ help:
|
||||||
@echo "... depend"
|
@echo "... depend"
|
||||||
@echo "... edit_cache"
|
@echo "... edit_cache"
|
||||||
@echo "... rebuild_cache"
|
@echo "... rebuild_cache"
|
||||||
|
@echo "... simple_ex2"
|
||||||
@echo "... simple_ex1"
|
@echo "... simple_ex1"
|
||||||
@echo "... simple_ex1.o"
|
@echo "... simple_ex1.o"
|
||||||
@echo "... simple_ex1.i"
|
@echo "... simple_ex1.i"
|
||||||
@echo "... simple_ex1.s"
|
@echo "... simple_ex1.s"
|
||||||
|
@echo "... simple_ex2.o"
|
||||||
|
@echo "... simple_ex2.i"
|
||||||
|
@echo "... simple_ex2.s"
|
||||||
.PHONY : help
|
.PHONY : help
|
||||||
|
|
||||||
|
|
||||||
|
|
50
examples/simple_ex2.cc
Normal file
50
examples/simple_ex2.cc
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
#include <chrono>
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue