Skip to content
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
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions include/jwt-cpp/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#endif

namespace jwt {
namespace error {
/**
* An issue ocurred encoding or decoding
*/
struct cryptographic_exception : public std::runtime_error {
using runtime_error::runtime_error
};
}
/**
* \brief character maps when encoding and decoding
*/
Expand Down Expand Up @@ -90,7 +98,7 @@ namespace jwt {

inline uint32_t index(const std::array<char, 64>& alphabet, char symbol) {
auto itr = std::find_if(alphabet.cbegin(), alphabet.cend(), [symbol](char c) { return c == symbol; });
if (itr == alphabet.cend()) { throw std::runtime_error("Invalid input: not within alphabet"); }
if (itr == alphabet.cend()) { throw error::cryptographic_exception("Invalid input: symbol '" + &symbol + "' not within alphabet"); }

return std::distance(alphabet.cbegin(), itr);
}
Expand All @@ -100,7 +108,6 @@ namespace jwt {
* \brief A collection of fellable functions for working with base64 and base64url
*/
namespace base {

namespace details {
struct padding {
size_t count = 0;
Expand Down Expand Up @@ -181,10 +188,10 @@ namespace jwt {
inline std::string decode(const std::string& base, const std::array<char, 64>& alphabet,
const std::vector<std::string>& fill) {
const auto pad = count_padding(base, fill);
if (pad.count > 2) throw std::runtime_error("Invalid input: too much fill");
if (pad.count > 2) throw error::cryptographic_exception("Invalid input: too much fill");

const size_t size = base.size() - pad.length;
if ((size + pad.count) % 4 != 0) throw std::runtime_error("Invalid input: incorrect total size");
if ((size + pad.count) % 4 != 0) throw error::cryptographic_exception("Invalid input: incorrect total size");

size_t out_size = size / 4 * 3;
std::string res;
Expand Down Expand Up @@ -251,6 +258,14 @@ namespace jwt {
std::string encode(const std::string& bin) {
return details::encode(bin, T::data(), T::fill());
}
/**
* \brief base64 decode a string using any alphabet
*
* \tparam T the alphabet to convert back into
* \param base the base64 encoded to decode
* \return std::string containing the decoded contents
* \throw cryptographic_exception
*/
template<typename T>
std::string decode(const std::string& base) {
return details::decode(base, T::data(), T::fill());
Expand Down
Loading