Skip to content

Commit 79ea377

Browse files
authored
Merge pull request #55 from olifre/remove-kid-requirement2
Allow empty Key-ID / kid if only a single key is published
2 parents ff0c4fd + e4cd39a commit 79ea377

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/scitokens_internal.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,26 @@ picojson::value::object find_key_id(const picojson::value json, const std::strin
172172
throw JsonException("Metadata resource is missing 'keys' array value");
173173
}
174174
auto keys_array = iter->second.get<picojson::array>();
175-
for (auto &key : keys_array) {
176-
if (!key.is<picojson::object>()) {continue;}
175+
if (kid.empty()) {
176+
if (keys_array.size() != 1) {
177+
throw JsonException("Key ID empty but multiple keys published.");
178+
}
179+
auto &key = keys_array.at(0);
180+
return key.get<picojson::object>();
181+
} else {
182+
for (auto &key : keys_array) {
183+
if (!key.is<picojson::object>()) {continue;}
177184

178-
auto key_obj = key.get<picojson::object>();
179-
iter = key_obj.find("kid");
180-
if (iter == key_obj.end() || (!iter->second.is<std::string>())) {continue;}
185+
auto key_obj = key.get<picojson::object>();
186+
iter = key_obj.find("kid");
187+
if (iter == key_obj.end() || (!iter->second.is<std::string>())) {continue;}
181188

182-
std::string cur_kid = iter->second.get<std::string>();
189+
std::string cur_kid = iter->second.get<std::string>();
183190

184-
if (cur_kid == kid) {return key_obj;}
191+
if (cur_kid == kid) {return key_obj;}
192+
}
193+
throw JsonException("Key ID is not published by the issuer.");
185194
}
186-
throw JsonException("Key ID is not published by the issuer.");
187195
}
188196

189197

src/scitokens_internal.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,6 @@ class Validator {
270270
if (!jwt.has_payload_claim("iss")) {
271271
throw jwt::token_verification_exception("'iss' claim is mandatory");
272272
}
273-
if (!jwt.has_header_claim("kid")) {
274-
throw jwt::token_verification_exception("'kid' claim is mandatory");
275-
}
276273
if (!m_allowed_issuers.empty()) {
277274
std::string issuer = jwt.get_issuer();
278275
bool permitted = false;
@@ -297,9 +294,17 @@ class Validator {
297294

298295
std::string public_pem;
299296
std::string algorithm;
300-
get_public_key_pem(jwt.get_issuer(), jwt.get_key_id(), public_pem, algorithm);
297+
// Key id is optional in the RFC, set to blank if it doesn't exist
298+
std::string key_id;
299+
try {
300+
key_id = jwt.get_key_id();
301+
} catch (const std::runtime_error&) {
302+
// Don't do anything, key_id is empty, as it should be.
303+
}
304+
305+
get_public_key_pem(jwt.get_issuer(), key_id, public_pem, algorithm);
301306
// std::cout << "Public PEM: " << public_pem << std::endl << "Algorithm: " << algorithm << std::endl;
302-
SciTokenKey key(jwt.get_key_id(), algorithm, public_pem, "");
307+
SciTokenKey key(key_id, algorithm, public_pem, "");
303308
auto verifier = jwt::verify()
304309
.allow_algorithm(key);
305310

0 commit comments

Comments
 (0)