diff --git a/include/jwt/exceptions.hpp b/include/jwt/exceptions.hpp index a614f15..d8aaa87 100644 --- a/include/jwt/exceptions.hpp +++ b/include/jwt/exceptions.hpp @@ -45,7 +45,7 @@ public: /** */ -class VerificationError final: public std::runtime_error +class VerificationError : public std::runtime_error { public: /** @@ -56,6 +56,84 @@ public: } }; +/** + */ +class InvalidAlgorithmError final: public VerificationError +{ +public: + /** + */ + InvalidAlgorithmError(std::string msg) + : VerificationError(std::move(msg)) + { + } +}; + +/** + */ +class TokenExpiredError final: public VerificationError +{ +public: + /** + */ + TokenExpiredError(std::string msg) + : VerificationError(std::move(msg)) + { + } +}; + +/** + */ +class InvalidIssuerError final: public VerificationError +{ +public: + /** + */ + InvalidIssuerError(std::string msg) + : VerificationError(std::move(msg)) + { + } +}; + +/** + */ +class InvalidAudienceError final: public VerificationError +{ +public: + /** + */ + InvalidAudienceError(std::string msg) + : VerificationError(std::move(msg)) + { + } +}; + +/** + */ +class ImmatureSignatureError final: public VerificationError +{ +public: + /** + */ + ImmatureSignatureError(std::string msg) + : VerificationError(std::move(msg)) + { + } +}; + +/** + */ +class InvalidSignatureError final: public VerificationError +{ +public: + /** + */ + InvalidSignatureError(std::string msg) + : VerificationError(std::move(msg)) + { + } +}; + } // END namespace jwt #endif diff --git a/include/jwt/impl/jwt.ipp b/include/jwt/impl/jwt.ipp index 6766a86..ea2eb69 100644 --- a/include/jwt/impl/jwt.ipp +++ b/include/jwt/impl/jwt.ipp @@ -557,6 +557,61 @@ jwt_object decode(const string_view enc_str, return jwt_obj; } + +void jwt_throw_exception(const std::error_code& ec) +{ + const auto& cat = ec.category(); + + if (&cat == &theVerificationErrorCategory) + { + switch (ec.value()) { + case VerificationErrc::InvalidAlgorithm: + { + throw InvalidAlgorithmError(ec.message()); + } + case VerificationErrc::TokenExpired: + { + throw TokenExpiredError(ec.message()); + } + case VerificationErrc::InvalidIssuer: + { + throw InvalidIssuerError(ec.message()); + } + case VerificationErrc::InvalidAudience: + { + throw InvalidAudienceError(ec.message()); + } + case VerificationErrc::ImmatureSignature: + { + throw ImmatureSignatureError(ec.message()); + } + case VerificationErrc::InvalidSignature: + { + throw InvalidSignatureError(ec.message()); + } + default: + assert (0 && "Unknown error code"); + }; + } + + if (&cat == &theDecodeErrorCategory) + { + throw DecodeError(ec.message()); + } + + if (&cat == &theAlgorithmErrCategory) + { + switch (ec.value()) { + case AlgorithmErrc::VerificationErr: + throw InvalidSignatureError(ec.message()); + default: + assert (0 && "Unknown error code or not to be treated as an error"); + }; + } + + assert (0 && "Unknown error code category"); +} + } // END namespace jwt