Skip to content

Commit 4f8f220

Browse files
committed
find_key_id: If kid is empty, allow to use a single published key.
When kid is not set and only a single key is published, fall back to using this single key.
1 parent af87797 commit 4f8f220

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
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

0 commit comments

Comments
 (0)