Skip to content

Commit f558630

Browse files
committed
Improved error messaging.
- It is not always clear what goes wrong during parsing.
1 parent d0fc2e4 commit f558630

File tree

5 files changed

+169
-157
lines changed

5 files changed

+169
-157
lines changed

src/include/jwt/jwt_error.h

+15-7
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
* Indicates that we failed to validate and parse the token.
3232
*/
3333
class InvalidTokenError : public std::runtime_error {
34-
public:
35-
explicit InvalidTokenError(std::string msg) : std::runtime_error(msg) {}
34+
public:
35+
explicit InvalidTokenError(std::string msg) : std::runtime_error(msg) {}
3636
};
3737

3838
/**
@@ -41,16 +41,24 @@ class InvalidTokenError : public std::runtime_error {
4141
* from this set of bytes.
4242
*/
4343
class TokenFormatError : public InvalidTokenError {
44-
public:
45-
explicit TokenFormatError(std::string msg) : InvalidTokenError(msg) {}
44+
public:
45+
explicit TokenFormatError(std::string msg) : InvalidTokenError(msg) {}
4646
};
4747

4848
/**
4949
* The token is not properly signed.
5050
*/
5151
class InvalidSignatureError : public InvalidTokenError {
52-
public:
53-
explicit InvalidSignatureError(std::string msg) : InvalidTokenError(msg) {}
52+
public:
53+
explicit InvalidSignatureError(std::string msg) : InvalidTokenError(msg) {}
5454
};
5555

56-
#endif // SRC_INCLUDE_JWT_JWT_ERROR_H_
56+
/**
57+
* Indicates that we are unable to construct the validator
58+
*/
59+
class InvalidValidatorError : public std::logic_error {
60+
public:
61+
explicit InvalidValidatorError(std::string msg) : std::logic_error(msg) {}
62+
};
63+
64+
#endif // SRC_INCLUDE_JWT_JWT_ERROR_H_

src/validators/claims/claimvalidatorfactory.cpp

+28-33
Original file line numberDiff line numberDiff line change
@@ -55,52 +55,47 @@ ClaimValidator *ClaimValidatorFactory::BuildInternal(const json &json) {
5555
}
5656

5757
ClaimValidator *constructed = nullptr;
58-
if (json.count("iss")) {
59-
constructed = new ListClaimValidator("iss", BuildList(json["iss"]));
60-
} else if (json.count("sub")) {
61-
constructed = new ListClaimValidator("sub", BuildList(json["sub"]));
62-
} else if (json.count("aud")) {
63-
constructed = new ListClaimValidator("aud", BuildList(json["aud"]));
64-
} else if (json.count("exp")) {
65-
::json val = json["exp"];
66-
::json leeway = val["leeway"];
67-
constructed =
68-
new ExpValidator(leeway.is_null() ? 0 : leeway.get<int>());
69-
} else if (json.count("nbf")) {
70-
::json val = json["nbf"];
71-
::json leeway = val["leeway"];
72-
constructed =
73-
new NbfValidator(leeway.is_null() ? 0 : leeway.get<int>());
74-
} else if (json.count("iat")) {
75-
::json val = json["iat"];
76-
::json leeway = val["leeway"];
77-
constructed =
78-
new IatValidator(leeway.is_null() ? 0 : leeway.get<int>());
79-
}
80-
8158
try {
82-
if (json.count("all")) {
59+
if (json.count("iss")) {
60+
constructed = new ListClaimValidator("iss", BuildList(json["iss"]));
61+
} else if (json.count("sub")) {
62+
constructed = new ListClaimValidator("sub", BuildList(json["sub"]));
63+
} else if (json.count("aud")) {
64+
constructed = new ListClaimValidator("aud", BuildList(json["aud"]));
65+
} else if (json.count("exp")) {
66+
::json val = json["exp"];
67+
::json leeway = val["leeway"];
68+
constructed =
69+
new ExpValidator(leeway.is_null() ? 0 : leeway.get<int>());
70+
} else if (json.count("nbf")) {
71+
::json val = json["nbf"];
72+
::json leeway = val["leeway"];
73+
constructed =
74+
new NbfValidator(leeway.is_null() ? 0 : leeway.get<int>());
75+
} else if (json.count("iat")) {
76+
::json val = json["iat"];
77+
::json leeway = val["leeway"];
78+
constructed =
79+
new IatValidator(leeway.is_null() ? 0 : leeway.get<int>());
80+
} else if (json.count("all")) {
8381
constructed =
8482
new AllClaimValidator(BuildValidatorList(json["all"]));
8583
} else if (json.count("any")) {
8684
constructed =
8785
new AnyClaimValidator(BuildValidatorList(json["any"]));
8886
} else if (json.count("optional")) {
89-
::json val = json["optional"];
90-
ClaimValidator *inner = Build(val);
87+
ClaimValidator *inner = BuildInternal(json["optional"]);
9188
constructed = new OptionalClaimValidator(inner);
9289
}
9390
} catch (std::exception &le) {
94-
std::ostringstream msg;
95-
msg << "Json error inside: " << le.what();
96-
msg << ", at: " << json;
97-
throw std::logic_error(msg.str());
91+
throw std::logic_error(
92+
std::string("Failed to construct validator at: ") + json.dump() +
93+
", " + le.what());
9894
}
9995

10096
if (!constructed) {
101-
std::ostringstream msg;
102-
msg << "Missing property at: " << json;
103-
throw std::logic_error(msg.str());
97+
throw std::logic_error(std::string("No validator declared at: ") +
98+
json.dump());
10499
}
105100

106101
build_.push_back(constructed);

src/validators/messagevalidatorfactory.cpp

+27-31
Original file line numberDiff line numberDiff line change
@@ -99,48 +99,44 @@ MessageValidator *MessageValidatorFactory::BuildInternal(const json &json) {
9999
}
100100

101101
std::unique_ptr<MessageValidator> constructed = nullptr;
102-
if (json.count("none")) {
103-
constructed.reset(new NoneValidator());
104-
} else if (json.count("HS256")) {
105-
constructed.reset(
106-
new HS256Validator(ParseSecret("secret", json["HS256"])));
107-
} else if (json.count("HS384")) {
108-
constructed.reset(
109-
new HS384Validator(ParseSecret("secret", json["HS384"])));
110-
} else if (json.count("HS512")) {
111-
constructed.reset(
112-
new HS512Validator(ParseSecret("secret", json["HS512"])));
113-
} else if (json.count("RS256")) {
114-
constructed.reset(
115-
new RS256Validator(ParseSecret("public", json["RS256"])));
116-
} else if (json.count("RS384")) {
117-
constructed.reset(
118-
new RS384Validator(ParseSecret("public", json["RS384"])));
119-
} else if (json.count("RS512")) {
120-
constructed.reset(
121-
new RS512Validator(ParseSecret("public", json["RS512"])));
122-
}
123-
124102
try {
125-
if (json.count("set")) {
103+
if (json.count("none")) {
104+
constructed.reset(new NoneValidator());
105+
} else if (json.count("HS256")) {
106+
constructed.reset(
107+
new HS256Validator(ParseSecret("secret", json["HS256"])));
108+
} else if (json.count("HS384")) {
109+
constructed.reset(
110+
new HS384Validator(ParseSecret("secret", json["HS384"])));
111+
} else if (json.count("HS512")) {
112+
constructed.reset(
113+
new HS512Validator(ParseSecret("secret", json["HS512"])));
114+
} else if (json.count("RS256")) {
115+
constructed.reset(
116+
new RS256Validator(ParseSecret("public", json["RS256"])));
117+
} else if (json.count("RS384")) {
118+
constructed.reset(
119+
new RS384Validator(ParseSecret("public", json["RS384"])));
120+
} else if (json.count("RS512")) {
121+
constructed.reset(
122+
new RS512Validator(ParseSecret("public", json["RS512"])));
123+
} else if (json.count("set")) {
126124
auto lst = BuildValidatorList(json["set"]);
127125
constructed.reset(new SetValidator(lst));
128126
} else if (json.count("kid")) {
129127
KidValidator *kid = new KidValidator();
130128
constructed.reset(kid);
131129
BuildKid(kid, json["kid"]);
132130
}
133-
} catch (std::exception &le) {
134-
std::ostringstream msg;
135-
msg << "Json error inside: " << le.what();
136-
msg << ", at: " << json;
137-
throw std::logic_error(msg.str());
131+
} catch (std::exception &e) {
132+
throw std::logic_error(
133+
std::string("Failed to construct validator at: ") + json.dump() +
134+
", " + e.what());
138135
}
139136

140137
if (!constructed.get()) {
141-
std::ostringstream msg;
142-
msg << "Missing validator property at: " << json;
143-
throw std::logic_error(msg.str());
138+
throw std::logic_error(std::string("No validator declared at: ") +
139+
json.dump());
144140
}
145141

146142
build_.push_back(constructed.get());

0 commit comments

Comments
 (0)