-
-
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.
🏟️ Add verify context trait template specialization (#229)
* Add verify context template specialization Co-authored-by: sirzooro <[email protected]> * add `verify_context` to all defaults Co-authored-by: sirzooro <[email protected]>
- Loading branch information
1 parent
d7e0936
commit 6aabc2e
Showing
7 changed files
with
124 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
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,91 @@ | ||
#include "jwt-cpp/traits/nlohmann-json/defaults.h" | ||
|
||
#include <iostream> | ||
|
||
int main() { | ||
std::string rsa_priv_key = R"(-----BEGIN PRIVATE KEY----- | ||
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC4ZtdaIrd1BPIJ | ||
tfnF0TjIK5inQAXZ3XlCrUlJdP+XHwIRxdv1FsN12XyMYO/6ymLmo9ryoQeIrsXB | ||
XYqlET3zfAY+diwCb0HEsVvhisthwMU4gZQu6TYW2s9LnXZB5rVtcBK69hcSlA2k | ||
ZudMZWxZcj0L7KMfO2rIvaHw/qaVOE9j0T257Z8Kp2CLF9MUgX0ObhIsdumFRLaL | ||
DvDUmBPr2zuh/34j2XmWwn1yjN/WvGtdfhXW79Ki1S40HcWnygHgLV8sESFKUxxQ | ||
mKvPUTwDOIwLFL5WtE8Mz7N++kgmDcmWMCHc8kcOIu73Ta/3D4imW7VbKgHZo9+K | ||
3ESFE3RjAgMBAAECggEBAJTEIyjMqUT24G2FKiS1TiHvShBkTlQdoR5xvpZMlYbN | ||
tVWxUmrAGqCQ/TIjYnfpnzCDMLhdwT48Ab6mQJw69MfiXwc1PvwX1e9hRsc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7EBvwMnZwIG7OOohJb | ||
vkSpADK6VPn9lbqq7O8cTedEHttm6otmLt8ZyEl3hZMaL3hbuRj6ysjmoFKx6CrX | ||
rK0/Ikt5ybqUzKCMJZg2VKGTxg== | ||
-----END PRIVATE KEY-----)"; | ||
|
||
auto role_claim = nlohmann::json{{"my-service", {{"roles", {"foo", "bar", "baz"}}}}}; | ||
|
||
auto token = jwt::create() | ||
.set_issuer("auth0") | ||
.set_type("JWT") | ||
.set_id("rsa-create-example") | ||
.set_issued_at(std::chrono::system_clock::now()) | ||
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{36000}) | ||
.set_payload_claim("resource-access", role_claim) | ||
.sign(jwt::algorithm::rs256("", rsa_priv_key, "", "")); | ||
|
||
std::cout << "token: " << token << std::endl; | ||
|
||
std::string rsa_pub_key = R"(-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuGbXWiK3dQTyCbX5xdE4 | ||
yCuYp0AF2d15Qq1JSXT/lx8CEcXb9RbDddl8jGDv+spi5qPa8qEHiK7FwV2KpRE9 | ||
83wGPnYsAm9BxLFb4YrLYcDFOIGULuk2FtrPS512Qea1bXASuvYXEpQNpGbnTGVs | ||
WXI9C+yjHztqyL2h8P6mlThPY9E9ue2fCqdgixfTFIF9Dm4SLHbphUS2iw7w1JgT | ||
69s7of9+I9l5lsJ9cozf1rxrXX4V1u/SotUuNB3Fp8oB4C1fLBEhSlMcUJirz1E8 | ||
AziMCxS+VrRPDM+zfvpIJg3JljAh3PJHDiLu902v9w+Iplu1WyoB2aPfitxEhRN0 | ||
YwIDAQAB | ||
-----END PUBLIC KEY-----)"; | ||
|
||
auto decoded = jwt::decode(token); | ||
|
||
for (const auto& e : decoded.get_payload_claims()) | ||
std::cout << e.first << " = " << e.second << std::endl; | ||
|
||
std::cout << std::endl; | ||
|
||
auto role_verifier = [](const jwt::verify_context& ctx, std::error_code& ec) { | ||
using error = jwt::error::token_verification_error; | ||
|
||
auto c = ctx.get_claim(false, ec); | ||
if (ec) return; | ||
if (c.get_type() == jwt::json::type::object) { | ||
auto obj = c.to_json(); | ||
try { | ||
auto roles = obj["my-service"]["roles"].get<nlohmann::json::array_t>(); | ||
if (roles.end() == std::find(roles.begin(), roles.end(), "foo")) ec = error::claim_value_missmatch; | ||
} catch (const std::exception& ex) { ec = error::claim_value_missmatch; } | ||
} else | ||
ec = error::claim_type_missmatch; | ||
}; | ||
|
||
auto verifier = jwt::verify() | ||
.allow_algorithm(jwt::algorithm::rs256(rsa_pub_key, "", "", "")) | ||
.with_issuer("auth0") | ||
.with_claim("resource-access", role_verifier); | ||
|
||
try { | ||
verifier.verify(decoded); | ||
std::cout << "Success!" << std::endl; | ||
} catch (const std::exception& ex) { std::cout << "Error: " << ex.what() << std::endl; } | ||
|
||
return 0; | ||
} |
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
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
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
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
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