Validate API key description during creation.

This commit is contained in:
Kishore Nallan 2021-05-20 15:00:51 +05:30
parent e6340dd645
commit 89409bb6d6
2 changed files with 15 additions and 0 deletions

View File

@ -330,6 +330,10 @@ Option<uint32_t> api_key_t::validate(const nlohmann::json &key_obj) {
return Option<uint32_t>(400, std::string("Key value must be a string."));
}
if(key_obj.count("description") != 0 && !key_obj["description"].is_string()) {
return Option<uint32_t>(400, std::string("Key description must be a string."));
}
if(!key_obj["actions"].is_array() || key_obj["actions"].empty()) {
return Option<uint32_t>(400,"Wrong format for `actions`. It should be an array of string.");
}

View File

@ -424,4 +424,15 @@ TEST_F(AuthManagerTest, ValidateBadKeyProperties) {
validate_op = api_key_t::validate(key_obj3);
ASSERT_FALSE(validate_op.ok());
ASSERT_STREQ("Key value must be a string.", validate_op.error().c_str());
// check for valid description
nlohmann::json key_obj4;
key_obj4["description"] = 42;
key_obj4["actions"] = {"*"};
key_obj4["collections"] = {"foobar"};
key_obj4["value"] = "abcd";
validate_op = api_key_t::validate(key_obj4);
ASSERT_FALSE(validate_op.ok());
ASSERT_STREQ("Key description must be a string.", validate_op.error().c_str());
}