MCPcopy Create free account
hub / github.com/Thalhammer/jwt-cpp / main

Function main

example/es256k.cpp:5–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include <jwt-cpp/jwt.h>
4
5int main() {
6 // openssl ecparam -name secp256k1 -genkey -noout -out ec-secp256k1-priv-key.pem
7 std::string es256k_priv_key = R"(-----BEGIN EC PRIVATE KEY-----
8MHQCAQEEIArnQWnspKtjiVuZuzuZ/l1Uqqq8gb2unLJ/6U/Saf4ioAcGBSuBBAAK
9oUQDQgAEfy03KCKUpIPMIJBtIG4xOwGm0Np/yHKaK9EDZi0mZ7VUeeNKq476CU5X
10940yusahgneePQrDMF2nWFEtBCOiXQ==
11-----END EC PRIVATE KEY-----)";
12 // openssl ec -in ec-secp256k1-priv-key.pem -pubout > ec-secp256k1-pub-key.pem
13 std::string es256k_pub_key = R"(-----BEGIN PUBLIC KEY-----
14MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEfy03KCKUpIPMIJBtIG4xOwGm0Np/yHKa
15K9EDZi0mZ7VUeeNKq476CU5X940yusahgneePQrDMF2nWFEtBCOiXQ==
16-----END PUBLIC KEY-----)";
17
18 auto token = jwt::create()
19 .set_issuer("auth0")
20 .set_type("JWT")
21 .set_id("es256k-create-example")
22 .set_issued_now()
23 .set_expires_in(std::chrono::seconds{36000})
24 .set_payload_claim("sample", jwt::claim(std::string{"test"}))
25 .sign(jwt::algorithm::es256k(es256k_pub_key, es256k_priv_key, "", ""));
26
27 std::cout << "token:\n" << token << '\n';
28
29 auto verify = jwt::verify()
30 .allow_algorithm(jwt::algorithm::es256k(es256k_pub_key, es256k_priv_key, "", ""))
31 .with_issuer("auth0");
32
33 auto decoded = jwt::decode(token);
34
35 verify.verify(decoded);
36
37 for (auto& e : decoded.get_header_json())
38 std::cout << e.first << " = " << e.second << '\n';
39 for (auto& e : decoded.get_payload_json())
40 std::cout << e.first << " = " << e.second << '\n';
41}

Callers

nothing calls this directly

Calls 8

es256kClass · 0.85
get_header_jsonMethod · 0.80
get_payload_jsonMethod · 0.80
createFunction · 0.50
verifyFunction · 0.50
decodeFunction · 0.50
signMethod · 0.45
verifyMethod · 0.45

Tested by

no test coverage detected