-
-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define a "jwt base class" for exceptions #252
Draft
prince-chrismc
wants to merge
10
commits into
Thalhammer:master
Choose a base branch
from
prince-chrismc:base-exception
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
67c26e3
define a base exception class
prince-chrismc c10724d
fix
prince-chrismc c437f82
add missing namespace
prince-chrismc bb6da77
fix ambiguity
prince-chrismc 01b7e20
fix copy pasting
prince-chrismc 180055f
missing namespace
prince-chrismc 9a67f42
linter
prince-chrismc ef502ee
traits
prince-chrismc 647449e
messing with exceptions some more
c465e4d
add missing error code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,19 +90,26 @@ namespace jwt { | |
* \brief Everything related to error codes issued by the library | ||
*/ | ||
namespace error { | ||
struct signature_verification_exception : public std::system_error { | ||
/** | ||
* \brief Generic base class for all JWT specific exceptions | ||
*/ | ||
struct exception : public std::exception { | ||
using std::exception::exception; | ||
}; | ||
|
||
struct signature_verification_exception : exception, public std::system_error { | ||
using system_error::system_error; | ||
}; | ||
struct signature_generation_exception : public std::system_error { | ||
struct signature_generation_exception : exception, public std::system_error { | ||
using system_error::system_error; | ||
}; | ||
struct rsa_exception : public std::system_error { | ||
struct rsa_exception : exception, public std::system_error { | ||
using system_error::system_error; | ||
}; | ||
struct ecdsa_exception : public std::system_error { | ||
struct ecdsa_exception : exception, public std::system_error { | ||
using system_error::system_error; | ||
}; | ||
struct token_verification_exception : public std::system_error { | ||
struct token_verification_exception : exception, public std::system_error { | ||
using system_error::system_error; | ||
}; | ||
/** | ||
|
@@ -362,15 +369,8 @@ namespace jwt { | |
} | ||
} | ||
} // namespace error | ||
|
||
// FIXME: Remove | ||
// Keep backward compat at least for a couple of revisions | ||
using error::ecdsa_exception; | ||
using error::rsa_exception; | ||
using error::signature_generation_exception; | ||
using error::signature_verification_exception; | ||
using error::token_verification_exception; | ||
} // namespace jwt | ||
|
||
namespace std { | ||
template<> | ||
struct is_error_code_enum<jwt::error::rsa_error> : true_type {}; | ||
|
@@ -383,6 +383,7 @@ namespace std { | |
template<> | ||
struct is_error_code_enum<jwt::error::token_verification_error> : true_type {}; | ||
} // namespace std | ||
|
||
namespace jwt { | ||
/** | ||
* \brief A collection for working with certificates | ||
|
@@ -974,7 +975,7 @@ namespace jwt { | |
} else if (!public_key.empty()) { | ||
pkey = helper::load_public_key_from_string(public_key, public_key_password); | ||
} else | ||
throw rsa_exception(error::rsa_error::no_key_provided); | ||
throw error::rsa_exception(error::rsa_error::no_key_provided); | ||
} | ||
/** | ||
* Sign jwt data | ||
|
@@ -1084,13 +1085,13 @@ namespace jwt { | |
pkey = helper::load_public_ec_key_from_string(public_key, public_key_password); | ||
check_public_key(pkey.get()); | ||
} else { | ||
throw ecdsa_exception(error::ecdsa_error::no_key_provided); | ||
throw error::ecdsa_exception(error::ecdsa_error::no_key_provided); | ||
} | ||
if (!pkey) throw ecdsa_exception(error::ecdsa_error::invalid_key); | ||
if (!pkey) throw error::ecdsa_exception(error::ecdsa_error::invalid_key); | ||
|
||
size_t keysize = EVP_PKEY_bits(pkey.get()); | ||
if (keysize != signature_length * 4 && (signature_length != 132 || keysize != 521)) | ||
throw ecdsa_exception(error::ecdsa_error::invalid_key_size); | ||
throw error::ecdsa_exception(error::ecdsa_error::invalid_key_size); | ||
} | ||
|
||
/** | ||
|
@@ -1190,25 +1191,29 @@ namespace jwt { | |
#ifdef JWT_OPENSSL_3_0 | ||
std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> ctx( | ||
EVP_PKEY_CTX_new_from_pkey(nullptr, pkey, nullptr), EVP_PKEY_CTX_free); | ||
if (!ctx) { throw ecdsa_exception(error::ecdsa_error::create_context_failed); } | ||
if (EVP_PKEY_public_check(ctx.get()) != 1) { throw ecdsa_exception(error::ecdsa_error::invalid_key); } | ||
if (!ctx) { throw error::ecdsa_exception(error::ecdsa_error::create_context_failed); } | ||
if (EVP_PKEY_public_check(ctx.get()) != 1) { | ||
throw error::ecdsa_exception(error::ecdsa_error::invalid_key); | ||
} | ||
#else | ||
std::unique_ptr<EC_KEY, decltype(&EC_KEY_free)> eckey(EVP_PKEY_get1_EC_KEY(pkey), EC_KEY_free); | ||
if (!eckey) { throw ecdsa_exception(error::ecdsa_error::invalid_key); } | ||
if (EC_KEY_check_key(eckey.get()) == 0) throw ecdsa_exception(error::ecdsa_error::invalid_key); | ||
if (!eckey) { throw error::ecdsa_exception(error::ecdsa_error::invalid_key); } | ||
if (EC_KEY_check_key(eckey.get()) == 0) throw error::ecdsa_exception(error::ecdsa_error::invalid_key); | ||
#endif | ||
} | ||
|
||
static void check_private_key(EVP_PKEY* pkey) { | ||
#ifdef JWT_OPENSSL_3_0 | ||
std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> ctx( | ||
EVP_PKEY_CTX_new_from_pkey(nullptr, pkey, nullptr), EVP_PKEY_CTX_free); | ||
if (!ctx) { throw ecdsa_exception(error::ecdsa_error::create_context_failed); } | ||
if (EVP_PKEY_private_check(ctx.get()) != 1) { throw ecdsa_exception(error::ecdsa_error::invalid_key); } | ||
if (!ctx) { throw error::ecdsa_exception(error::ecdsa_error::create_context_failed); } | ||
if (EVP_PKEY_private_check(ctx.get()) != 1) { | ||
throw error::ecdsa_exception(error::ecdsa_error::invalid_key); | ||
} | ||
#else | ||
std::unique_ptr<EC_KEY, decltype(&EC_KEY_free)> eckey(EVP_PKEY_get1_EC_KEY(pkey), EC_KEY_free); | ||
if (!eckey) { throw ecdsa_exception(error::ecdsa_error::invalid_key); } | ||
if (EC_KEY_check_key(eckey.get()) == 0) throw ecdsa_exception(error::ecdsa_error::invalid_key); | ||
if (!eckey) { throw error::ecdsa_exception(error::ecdsa_error::invalid_key); } | ||
if (EC_KEY_check_key(eckey.get()) == 0) throw error::ecdsa_exception(error::ecdsa_error::invalid_key); | ||
#endif | ||
} | ||
|
||
|
@@ -1314,7 +1319,7 @@ namespace jwt { | |
} else if (!public_key.empty()) { | ||
pkey = helper::load_public_key_from_string(public_key, public_key_password); | ||
} else | ||
throw ecdsa_exception(error::ecdsa_error::load_key_bio_read); | ||
throw error::ecdsa_exception(error::ecdsa_error::load_key_bio_read); | ||
} | ||
/** | ||
* Sign jwt data | ||
|
@@ -1449,7 +1454,7 @@ namespace jwt { | |
} else if (!public_key.empty()) { | ||
pkey = helper::load_public_key_from_string(public_key, public_key_password); | ||
} else | ||
throw rsa_exception(error::rsa_error::no_key_provided); | ||
throw error::rsa_exception(error::rsa_error::no_key_provided); | ||
} | ||
|
||
/** | ||
|
@@ -2316,13 +2321,13 @@ namespace jwt { | |
/** | ||
* Attempt to parse JSON was unsuccessful | ||
*/ | ||
struct invalid_json_exception : public std::runtime_error { | ||
struct invalid_json_exception : exception, public std::runtime_error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above, given that we dont actually set a dynamic error message, we should probably add a "invalid_json" error code and stick that in. |
||
invalid_json_exception() : runtime_error("invalid json") {} | ||
}; | ||
/** | ||
* Attempt to access claim was unsuccessful | ||
*/ | ||
struct claim_not_present_exception : public std::out_of_range { | ||
struct claim_not_present_exception : exception, public std::out_of_range { | ||
claim_not_present_exception() : out_of_range("claim not found") {} | ||
}; | ||
} // namespace error | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels really weird cause now signature_verification_exception inherits from std::exception twice.
Could we get away with inheriting
exception
fromstd::system_error
(which inheritsstd::runtime_error
, which in turn inheritsstd::exception
) ?