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

Function main

example/private-claims.cpp:10–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8using min = std::chrono::minutes;
9
10int main() {
11 jwt::claim from_raw_json;
12 std::istringstream iss{R"##({"api":{"array":[1,2,3],"null":null}})##"};
13 iss >> from_raw_json;
14
15 jwt::claim::set_t list{"once", "twice"};
16 std::vector<int64_t> big_numbers{727663072ULL, 770979831ULL, 427239169ULL, 525936436ULL};
17
18 const auto time = jwt::date::clock::now();
19 const auto token = jwt::create()
20 .set_type("JWT")
21 .set_issuer("auth.mydomain.io")
22 .set_audience("mydomain.io")
23 .set_issued_at(time)
24 .set_not_before(time - sec{15})
25 .set_expires_at(time + sec{15} + min{2})
26 .set_payload_claim("boolean", picojson::value(true))
27 .set_payload_claim("integer", picojson::value(int64_t{12345}))
28 .set_payload_claim("precision", picojson::value(12.345))
29 .set_payload_claim("strings", jwt::claim(list))
30 .set_payload_claim("array", jwt::claim(big_numbers.begin(), big_numbers.end()))
31 .set_payload_claim("object", from_raw_json)
32 .sign(jwt::algorithm::none{});
33
34 const auto decoded = jwt::decode(token);
35
36 const auto api_array = decoded.get_payload_claim("object").to_json().get("api").get("array");
37 std::cout << "api array = " << api_array << '\n';
38
39 /* [verify exact claim] */
40 jwt::verify()
41 .allow_algorithm(jwt::algorithm::none{})
42 .with_issuer("auth.mydomain.io")
43 .with_audience("mydomain.io")
44 .with_claim("object", from_raw_json) // Match the exact JSON content
45 .verify(decoded);
46 /* [verify exact claim] */
47
48 return 0;
49}

Callers

nothing calls this directly

Calls 11

valueClass · 0.85
getMethod · 0.80
createFunction · 0.50
decodeFunction · 0.50
verifyFunction · 0.50
signMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
to_jsonMethod · 0.45
get_payload_claimMethod · 0.45
verifyMethod · 0.45

Tested by

no test coverage detected