-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e3d6a1
commit 8a1d95a
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Signing Tokens | ||
|
||
## Custom Signature Algorithms | ||
|
||
The libraries design is open so you can implement your own algorithms, see [existing examples](https://github.com/Thalhammer/jwt-cpp/blob/73f23419235661e89a304ba5ab09d6714fb8dd94/include/jwt-cpp/jwt.h#L874) for ideas. | ||
|
||
```cpp | ||
struct your_algorithm{ | ||
std::string sign(const std::string& /*unused*/, std::error_code& ec) const { | ||
ec.clear(); | ||
// CALL YOUR METHOD HERE | ||
return {}; | ||
} | ||
void verify(const std::string& /*unused*/, const std::string& signature, std::error_code& ec) const { | ||
ec.clear(); | ||
if (!signature.empty()) { ec = error::signature_verification_error::invalid_signature; } | ||
// CALL YOUR METHOD HERE | ||
} | ||
std::string name() const { return "your_algorithm"; } | ||
}; | ||
``` | ||
|
||
Then everything else is the same, just pass in your implementation such as: | ||
|
||
|
||
```cpp | ||
auto token = jwt::create() | ||
.set_id("custom-algo-example") | ||
.set_issued_at(std::chrono::system_clock::now()) | ||
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{36000}) | ||
.set_payload_claim("sample", jwt::claim(std::string{"test"})) | ||
.sign(your_algorithm{/* what ever you want */}); | ||
``` |