Skip to content

Commit e0251c9

Browse files
olifredjw8605github-actions[bot]
authored
scitokens_internal: catch matching exception type after jwt-cpp update (#125)
After updating the vendored jwt-cpp version in: a8c5977 the exception type if a claim is not found has changed, breaking the "no kid claim" use case. The vendored jwt-cpp version offers a convenience function to check whether the header claim is present, use that instead of accessing it and catching the exception. Co-authored-by: Derek Weitzel <djw8605@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 3d17d51 commit e0251c9

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

src/scitokens_internal.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ class SciTokenKey {
152152
m_private(private_contents) {}
153153

154154
std::string serialize(jwt::builder<jwt::traits::kazuho_picojson> &builder) {
155-
std::error_code ec;
156-
builder.set_key_id(m_kid);
155+
if (m_kid != "none") {
156+
builder.set_key_id(m_kid);
157+
}
157158
return builder.sign(*this);
158159
}
159160

@@ -496,10 +497,8 @@ class Validator {
496497
std::string algorithm;
497498
// Key id is optional in the RFC, set to blank if it doesn't exist
498499
std::string key_id;
499-
try {
500+
if (jwt.has_key_id()) {
500501
key_id = jwt.get_key_id();
501-
} catch (const std::runtime_error &) {
502-
// Don't do anything, key_id is empty, as it should be.
503502
}
504503
auto status =
505504
get_public_key_pem(jwt.get_issuer(), key_id, public_pem, algorithm);

test/main.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,86 @@ TEST_F(SerializeTest, ExplicitTime) {
697697
enforcer_destroy(enforcer);
698698
}
699699

700+
class SerializeNoKidTest : public ::testing::Test {
701+
protected:
702+
void SetUp() override {
703+
char *err_msg;
704+
m_key = KeyPtr(scitoken_key_create("none", "ES256", ec_public,
705+
ec_private, &err_msg),
706+
scitoken_key_destroy);
707+
ASSERT_TRUE(m_key.get() != nullptr);
708+
709+
m_token = TokenPtr(scitoken_create(m_key.get()), scitoken_destroy);
710+
ASSERT_TRUE(m_token.get() != nullptr);
711+
712+
auto rv = scitoken_set_claim_string(
713+
m_token.get(), "iss", "https://demo.scitokens.org/gtest", &err_msg);
714+
ASSERT_TRUE(rv == 0);
715+
716+
rv = scitoken_store_public_ec_key("https://demo.scitokens.org/gtest",
717+
"1", ec_public, &err_msg);
718+
ASSERT_TRUE(rv == 0);
719+
720+
scitoken_set_lifetime(m_token.get(), 60);
721+
722+
m_audiences_array.push_back("https://demo.scitokens.org/");
723+
m_audiences_array.push_back(nullptr);
724+
725+
const char *groups[3] = {nullptr, nullptr, nullptr};
726+
const char group0[] = "group0";
727+
const char group1[] = "group1";
728+
groups[0] = group0;
729+
groups[1] = group1;
730+
rv = scitoken_set_claim_string_list(m_token.get(), "groups", groups,
731+
&err_msg);
732+
ASSERT_TRUE(rv == 0);
733+
734+
m_read_token.reset(scitoken_create(nullptr));
735+
ASSERT_TRUE(m_read_token.get() != nullptr);
736+
}
737+
738+
using KeyPtr = std::unique_ptr<void, decltype(&scitoken_key_destroy)>;
739+
KeyPtr m_key{nullptr, scitoken_key_destroy};
740+
741+
using TokenPtr = std::unique_ptr<void, decltype(&scitoken_destroy)>;
742+
TokenPtr m_token{nullptr, scitoken_destroy};
743+
744+
std::vector<const char *> m_audiences_array;
745+
746+
TokenPtr m_read_token{nullptr, scitoken_destroy};
747+
};
748+
749+
TEST_F(SerializeNoKidTest, VerifyATJWTTest) {
750+
751+
char *err_msg = nullptr;
752+
753+
// Serialize as at+jwt token.
754+
char *token_value = nullptr;
755+
scitoken_set_serialize_profile(m_token.get(), SciTokenProfile::AT_JWT);
756+
auto rv = scitoken_serialize(m_token.get(), &token_value, &err_msg);
757+
ASSERT_TRUE(rv == 0);
758+
std::unique_ptr<char, decltype(&free)> token_value_ptr(token_value, free);
759+
760+
// Accepts any profile.
761+
rv = scitoken_deserialize_v2(token_value, m_read_token.get(), nullptr,
762+
&err_msg);
763+
ASSERT_TRUE(rv == 0);
764+
765+
// Accepts only an at+jwt token, should work with at+jwt token
766+
scitoken_set_deserialize_profile(m_read_token.get(),
767+
SciTokenProfile::AT_JWT);
768+
rv = scitoken_deserialize_v2(token_value, m_read_token.get(), nullptr,
769+
&err_msg);
770+
ASSERT_TRUE(rv == 0);
771+
772+
// Accepts only SciToken 2.0; should fail
773+
scitoken_set_deserialize_profile(m_read_token.get(),
774+
SciTokenProfile::SCITOKENS_2_0);
775+
rv = scitoken_deserialize_v2(token_value, m_read_token.get(), nullptr,
776+
&err_msg);
777+
ASSERT_FALSE(rv == 0);
778+
}
779+
700780
int main(int argc, char **argv) {
701781
::testing::InitGoogleTest(&argc, argv);
702782
return RUN_ALL_TESTS();

0 commit comments

Comments
 (0)