Skip to content

Commit 6aabc2e

Browse files
🏟️ Add verify context trait template specialization (#229)
* Add verify context template specialization Co-authored-by: sirzooro <sirzooro@users.noreply.github.com> * add `verify_context` to all defaults Co-authored-by: sirzooro <sirzooro@users.noreply.github.com>
1 parent d7e0936 commit 6aabc2e

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

example/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ target_link_libraries(jwks-verify jwt-cpp::jwt-cpp)
2828

2929
add_executable(es256k es256k.cpp)
3030
target_link_libraries(es256k jwt-cpp::jwt-cpp)
31+
32+
add_executable(partial-claim-verifier partial-claim-verifier.cpp)
33+
target_link_libraries(partial-claim-verifier jwt-cpp::jwt-cpp)

example/partial-claim-verifier.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "jwt-cpp/traits/nlohmann-json/defaults.h"
2+
3+
#include <iostream>
4+
5+
int main() {
6+
std::string rsa_priv_key = R"(-----BEGIN PRIVATE KEY-----
7+
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC4ZtdaIrd1BPIJ
8+
tfnF0TjIK5inQAXZ3XlCrUlJdP+XHwIRxdv1FsN12XyMYO/6ymLmo9ryoQeIrsXB
9+
XYqlET3zfAY+diwCb0HEsVvhisthwMU4gZQu6TYW2s9LnXZB5rVtcBK69hcSlA2k
10+
ZudMZWxZcj0L7KMfO2rIvaHw/qaVOE9j0T257Z8Kp2CLF9MUgX0ObhIsdumFRLaL
11+
DvDUmBPr2zuh/34j2XmWwn1yjN/WvGtdfhXW79Ki1S40HcWnygHgLV8sESFKUxxQ
12+
mKvPUTwDOIwLFL5WtE8Mz7N++kgmDcmWMCHc8kcOIu73Ta/3D4imW7VbKgHZo9+K
13+
3ESFE3RjAgMBAAECggEBAJTEIyjMqUT24G2FKiS1TiHvShBkTlQdoR5xvpZM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QDWFg6S2je0KtbV5PYe
30+
RultUEe2C0jYMDQx+JYxbPmtcopvZQrFEur3WKVuLy5UAy7EBvwMnZwIG7OOohJb
31+
vkSpADK6VPn9lbqq7O8cTedEHttm6otmLt8ZyEl3hZMaL3hbuRj6ysjmoFKx6CrX
32+
rK0/Ikt5ybqUzKCMJZg2VKGTxg==
33+
-----END PRIVATE KEY-----)";
34+
35+
auto role_claim = nlohmann::json{{"my-service", {{"roles", {"foo", "bar", "baz"}}}}};
36+
37+
auto token = jwt::create()
38+
.set_issuer("auth0")
39+
.set_type("JWT")
40+
.set_id("rsa-create-example")
41+
.set_issued_at(std::chrono::system_clock::now())
42+
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{36000})
43+
.set_payload_claim("resource-access", role_claim)
44+
.sign(jwt::algorithm::rs256("", rsa_priv_key, "", ""));
45+
46+
std::cout << "token: " << token << std::endl;
47+
48+
std::string rsa_pub_key = R"(-----BEGIN PUBLIC KEY-----
49+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuGbXWiK3dQTyCbX5xdE4
50+
yCuYp0AF2d15Qq1JSXT/lx8CEcXb9RbDddl8jGDv+spi5qPa8qEHiK7FwV2KpRE9
51+
83wGPnYsAm9BxLFb4YrLYcDFOIGULuk2FtrPS512Qea1bXASuvYXEpQNpGbnTGVs
52+
WXI9C+yjHztqyL2h8P6mlThPY9E9ue2fCqdgixfTFIF9Dm4SLHbphUS2iw7w1JgT
53+
69s7of9+I9l5lsJ9cozf1rxrXX4V1u/SotUuNB3Fp8oB4C1fLBEhSlMcUJirz1E8
54+
AziMCxS+VrRPDM+zfvpIJg3JljAh3PJHDiLu902v9w+Iplu1WyoB2aPfitxEhRN0
55+
YwIDAQAB
56+
-----END PUBLIC KEY-----)";
57+
58+
auto decoded = jwt::decode(token);
59+
60+
for (const auto& e : decoded.get_payload_claims())
61+
std::cout << e.first << " = " << e.second << std::endl;
62+
63+
std::cout << std::endl;
64+
65+
auto role_verifier = [](const jwt::verify_context& ctx, std::error_code& ec) {
66+
using error = jwt::error::token_verification_error;
67+
68+
auto c = ctx.get_claim(false, ec);
69+
if (ec) return;
70+
if (c.get_type() == jwt::json::type::object) {
71+
auto obj = c.to_json();
72+
try {
73+
auto roles = obj["my-service"]["roles"].get<nlohmann::json::array_t>();
74+
if (roles.end() == std::find(roles.begin(), roles.end(), "foo")) ec = error::claim_value_missmatch;
75+
} catch (const std::exception& ex) { ec = error::claim_value_missmatch; }
76+
} else
77+
ec = error::claim_type_missmatch;
78+
};
79+
80+
auto verifier = jwt::verify()
81+
.allow_algorithm(jwt::algorithm::rs256(rsa_pub_key, "", "", ""))
82+
.with_issuer("auth0")
83+
.with_claim("resource-access", role_verifier);
84+
85+
try {
86+
verifier.verify(decoded);
87+
std::cout << "Success!" << std::endl;
88+
} catch (const std::exception& ex) { std::cout << "Error: " << ex.what() << std::endl; }
89+
90+
return 0;
91+
}

include/jwt-cpp/traits/boost-json/defaults.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ namespace jwt {
7777
inline jwks<traits::boost_json> parse_jwks(const traits::boost_json::string_type& token) {
7878
return jwks<traits::boost_json>(token);
7979
}
80+
81+
/**
82+
* This type is the specialization of the \ref verify_ops::verify_context class which
83+
* uses the standard template types.
84+
*/
85+
using verify_context = verify_ops::verify_context<traits::boost_json>;
8086
} // namespace jwt
8187

8288
#endif // JWT_CPP_BOOST_JSON_DEFAULTS_H

include/jwt-cpp/traits/danielaparker-jsoncons/defaults.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ namespace jwt {
7777
inline jwks<traits::danielaparker_jsoncons> parse_jwks(const traits::danielaparker_jsoncons::string_type& token) {
7878
return jwks<traits::danielaparker_jsoncons>(token);
7979
}
80+
81+
/**
82+
* This type is the specialization of the \ref verify_ops::verify_context class which
83+
* uses the standard template types.
84+
*/
85+
using verify_context = verify_ops::verify_context<traits::danielaparker_jsoncons>;
8086
} // namespace jwt
8187

8288
#endif // JWT_CPP_DANIELAPARKER_JSONCONS_DEFAULTS_H

include/jwt-cpp/traits/defaults.h.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ namespace jwt {
7979
inline jwks<traits::{{traits_name}}> parse_jwks(const traits::{{traits_name}}::string_type& token) {
8080
return jwks<traits::{{traits_name}}>(token);
8181
}
82+
83+
/**
84+
* This type is the specialization of the \ref verify_ops::verify_context class which
85+
* uses the standard template types.
86+
*/
87+
using verify_context = verify_ops::verify_context<traits::{{traits_name}}>;
8288
} // namespace jwt
8389

8490
#endif // JWT_CPP_{{traits_name_upper}}_DEFAULTS_H

include/jwt-cpp/traits/kazuho-picojson/defaults.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ namespace jwt {
7373
inline jwks<traits::kazuho_picojson> parse_jwks(const traits::kazuho_picojson::string_type& token) {
7474
return jwks<traits::kazuho_picojson>(token);
7575
}
76+
77+
/**
78+
* This type is the specialization of the \ref verify_ops::verify_context class which
79+
* uses the standard template types.
80+
*/
81+
using verify_context = verify_ops::verify_context<traits::kazuho_picojson>;
7682
} // namespace jwt
7783

7884
#endif // JWT_CPP_KAZUHO_PICOJSON_DEFAULTS_H

include/jwt-cpp/traits/nlohmann-json/defaults.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ namespace jwt {
7777
inline jwks<traits::nlohmann_json> parse_jwks(const traits::nlohmann_json::string_type& token) {
7878
return jwks<traits::nlohmann_json>(token);
7979
}
80+
81+
/**
82+
* This type is the specialization of the \ref verify_ops::verify_context class which
83+
* uses the standard template types.
84+
*/
85+
using verify_context = verify_ops::verify_context<traits::nlohmann_json>;
8086
} // namespace jwt
8187

8288
#endif // JWT_CPP_NLOHMANN_JSON_DEFAULTS_H

0 commit comments

Comments
 (0)