mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
* Add set_expires_in and set_issued_now methods * Add a Clock type parameter for builder * set_expires_in method sets exp from the current moment * Update default traits with the new builder interface * Add VerifyTokenExpirationInValid for all traits * Use the new methods in examples * Add create method to specify the clock type * Use JWT_CLAIM_EXPLICIT for explicit constructors * Fix builder factory method in mustache file * fixup whitespace * default template was missing a few args * fix whitespace * linter --------- Co-authored-by: Chris Mc <prince.chrismc@gmail.com>
41 lines
1.5 KiB
C++
41 lines
1.5 KiB
C++
#include <iostream>
|
|
#include <jwt-cpp/jwt.h>
|
|
|
|
int main() {
|
|
// openssl ecparam -name secp256k1 -genkey -noout -out ec-secp256k1-priv-key.pem
|
|
std::string es256k_priv_key = R"(-----BEGIN EC PRIVATE KEY-----
|
|
MHQCAQEEIArnQWnspKtjiVuZuzuZ/l1Uqqq8gb2unLJ/6U/Saf4ioAcGBSuBBAAK
|
|
oUQDQgAEfy03KCKUpIPMIJBtIG4xOwGm0Np/yHKaK9EDZi0mZ7VUeeNKq476CU5X
|
|
940yusahgneePQrDMF2nWFEtBCOiXQ==
|
|
-----END EC PRIVATE KEY-----)";
|
|
// openssl ec -in ec-secp256k1-priv-key.pem -pubout > ec-secp256k1-pub-key.pem
|
|
std::string es256k_pub_key = R"(-----BEGIN PUBLIC KEY-----
|
|
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEfy03KCKUpIPMIJBtIG4xOwGm0Np/yHKa
|
|
K9EDZi0mZ7VUeeNKq476CU5X940yusahgneePQrDMF2nWFEtBCOiXQ==
|
|
-----END PUBLIC KEY-----)";
|
|
|
|
auto token = jwt::create()
|
|
.set_issuer("auth0")
|
|
.set_type("JWT")
|
|
.set_id("es256k-create-example")
|
|
.set_issued_now()
|
|
.set_expires_in(std::chrono::seconds{36000})
|
|
.set_payload_claim("sample", jwt::claim(std::string{"test"}))
|
|
.sign(jwt::algorithm::es256k(es256k_pub_key, es256k_priv_key, "", ""));
|
|
|
|
std::cout << "token:\n" << token << std::endl;
|
|
|
|
auto verify = jwt::verify()
|
|
.allow_algorithm(jwt::algorithm::es256k(es256k_pub_key, es256k_priv_key, "", ""))
|
|
.with_issuer("auth0");
|
|
|
|
auto decoded = jwt::decode(token);
|
|
|
|
verify.verify(decoded);
|
|
|
|
for (auto& e : decoded.get_header_json())
|
|
std::cout << e.first << " = " << e.second << std::endl;
|
|
for (auto& e : decoded.get_payload_json())
|
|
std::cout << e.first << " = " << e.second << std::endl;
|
|
}
|