Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Support expiry of API keys.

kishorencc97ded0458e9
diff --git a/include/auth_manager.h b/include/auth_manager.h
index 501ae9dd..dbadbdec 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -13,16 +13,18 @@ struct api_key_t {
std::string description;
std::vector<std::string> actions;
std::vector<std::string> collections;
+ uint64_t expires_at;
static const size_t PREFIX_LEN = 4;
+ static const uint64_t FAR_FUTURE_TIMESTAMP = 64723363199; // year 4020
api_key_t() {
}
- api_key_t(const std::string& value, const std::string& description, const std::vector<std::string>& actions,
- const std::vector<std::string>& collections):
- value(value), description(description), actions(actions), collections(collections) {
+ api_key_t(const std::string &value, const std::string &description, const std::vector<std::string> &actions,
+ const std::vector<std::string> &collections, uint64_t expires_at) :
+ value(value), description(description), actions(actions), collections(collections), expires_at(expires_at) {
}
@@ -41,43 +43,19 @@ struct api_key_t {
actions = key_obj["actions"].get<std::vector<std::string>>();
collections = key_obj["collections"].get<std::vector<std::string>>();
- return Option<bool>(true);
- }
-
- static Option<uint32_t> validate(const nlohmann::json & key_obj) {
- auto mandatory_keys = {
- "description", "actions", "collections"
- };
-
- for(auto key: mandatory_keys) {
- if(key_obj.count(key) == 0) {
- return Option<uint32_t>(400, std::string("Could not find a `") + key + "` key.");
- }
- }
-
- 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.");
- }
-
- if(!key_obj["collections"].is_array() || key_obj["collections"].empty()) {
- return Option<uint32_t>(400,"Wrong format for `collections`. It should be an array of string.");
- }
-
- for(const nlohmann::json & item: key_obj["actions"]) {
- if(!item.is_string()) {
- return Option<uint32_t>(400,"Wrong format for `actions`. It should be an array of string.");
- }
- }
+ // handle optional fields
- for(const nlohmann::json & item: key_obj["collections"]) {
- if(!item.is_string()) {
- return Option<uint32_t>(400,"Wrong format for `collections`. It should be an array of string.");
- }
+ if(key_obj.count("expires_at") != 0) {
+ expires_at = key_obj["expires_at"];
+ } else {
+ expires_at = FAR_FUTURE_TIMESTAMP;
}
- return Option<uint32_t>(200);
+ return Option<bool>(true);
}
+ static Option<uint32_t> validate(const nlohmann::json & key_obj);
+
nlohmann::json to_json() const {
nlohmann::json obj;
obj["id"] = id;
@@ -85,6 +63,7 @@ struct api_key_t {
obj["description"] = description;
obj["actions"] = actions;
obj["collections"] = collections;
+ obj["expires_at"] = expires_at;
return obj;
}
@@ -113,6 +92,8 @@ private:
static constexpr const char* DOCUMENTS_SEARCH_ACTION = "documents:search";
+ static std::string fmt_error(std::string&& error, const std::string& key);
+
public:
AuthManager() = default;
@@ -134,12 +115,16 @@ public:
std::map<std::string, std::string>& params
);
- Option<std::string> params_from_scoped_key(
+ Option<bool> authenticate_parse_params(
const std::string& scoped_api_key,
const std::string& action,
- const std::string& collection
+ const std::string& collection,
+ nlohmann::json& embedded_params
);
static const size_t KEY_LEN = 32;
static const size_t HMAC_BASE64_LEN = 44;
+
+ bool auth_against_key(const std::string &collection, const std::string& action,
+ const api_key_t &api_key, const bool search_only) const;
};
\ No newline at end of file
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index 7a17fd1f..a98476b7 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -3,6 +3,7 @@
#include <regex>
constexpr const char* AuthManager::DOCUMENTS_SEARCH_ACTION;
+constexpr const uint64_t api_key_t::FAR_FUTURE_TIMESTAMP;
Option<bool> AuthManager::init(Store *store) {
// This function must be idempotent, i.e. when called multiple times, must produce the same state without leaks
@@ -136,29 +137,19 @@ bool AuthManager::authenticate(const std::string& req_api_key, const std::string
if(req_api_key.size() > KEY_LEN) {
// scoped API key: validate and if valid, extract params
- Option<std::string> params_op = params_from_scoped_key(req_api_key, action, collection);
+ nlohmann::json embedded_params;
+ Option<bool> params_op = authenticate_parse_params(req_api_key, action, collection, embedded_params);
if(!params_op.ok()) {
// authentication failed
return false;
}
- const std::string& embedded_params_str = params_op.get();
- nlohmann::json embedded_params;
-
- try {
- embedded_params = nlohmann::json::parse(embedded_params_str);
- } catch(const std::exception& e) {
- LOG(ERROR) << "JSON error: " << e.what();
- return false;
- }
-
- if(!embedded_params.is_object()) {
- LOG(ERROR) << "Scoped API key contains invalid search parameters.";
- return false;
- }
-
// enrich params with values from embedded_params
for(auto it = embedded_params.begin(); it != embedded_params.end(); ++it) {
+ if(it.key() == "expires_at") {
+ continue;
+ }
+
if(params.count(it.key()) == 0) {
params[it.key()] = it.value();
} else if(it.key() == "filter_by") {
@@ -178,26 +169,41 @@ bool AuthManager::authenticate(const std::string& req_api_key, const std::string
}
const api_key_t& api_key = api_keys[req_api_key];
+ return auth_against_key(collection, action, api_key, false);
+}
- // check if action is allowed
-
- bool action_is_allowed = false;
- for(const std::string& allowed_action: api_key.actions) {
- if(allowed_action == "*" || (action != "*" && allowed_action == action)) {
- action_is_allowed = true;
- break;
- }
- }
+bool AuthManager::auth_against_key(const std::string& collection, const std::string& action,
+ const api_key_t& api_key, const bool search_only) const {
- if(!action_is_allowed) {
+ if(std::time(0) > api_key.expires_at) {
+ LOG(ERROR) << fmt_error("Rejecting expired API key.", api_key.value);
return false;
}
- // check if action is allowed against a specific collection
+ if(search_only) {
+ // ensure that parent key has only search scope
+ if(api_key.actions.size() != 1 || api_key.actions[0] != DOCUMENTS_SEARCH_ACTION) {
+ LOG(ERROR) << fmt_error(std::string("Parent API key must allow only `") + DOCUMENTS_SEARCH_ACTION + "` action.",
+ api_key.value);
+ return false;
+ }
+ } else {
+ bool action_is_allowed = false;
+ for(const std::string& allowed_action: api_key.actions) {
+ if(allowed_action == "*" || (action != "*" && allowed_action == action)) {
+ action_is_allowed = true;
+ break;
+ }
+ }
+
+ if(!action_is_allowed) {
+ return false;
+ }
+ }
for(const std::string& allowed_collection: api_key.collections) {
- if(allowed_collection == "*" || (collection != "*" && allowed_collection == collection) ||
- std::regex_match (collection, std::regex(allowed_collection)) || collection.empty()) {
+ if(allowed_collection == "*" || (allowed_collection == collection) || collection.empty() ||
+ std::regex_match (collection, std::regex(allowed_collection))) {
return true;
}
}
@@ -205,12 +211,12 @@ bool AuthManager::authenticate(const std::string& req_api_key, const std::string
return false;
}
-Option<std::string> AuthManager::params_from_scoped_key(const std::string &scoped_api_key, const std::string& action,
- const std::string& collection) {
+Option<bool> AuthManager::authenticate_parse_params(const std::string& scoped_api_key, const std::string& action,
+ const std::string& collection, nlohmann::json& embedded_params) {
// allow only searches from scoped keys
if(action != DOCUMENTS_SEARCH_ACTION) {
LOG(ERROR) << "Scoped API keys can only be used for searches.";
- return Option<std::string>(403, "Forbidden.");
+ return Option<bool>(403, "Forbidden.");
}
const std::string& key_payload = StringUtils::base64_decode(scoped_api_key);
@@ -226,34 +232,94 @@ Option<std::string> AuthManager::params_from_scoped_key(const std::string &scope
if(kv.first.substr(0, api_key_t::PREFIX_LEN) == key_prefix) {
const api_key_t& api_key = kv.second;
- // ensure that parent key has only search scope
- if(api_key.actions.size() != 1 || api_key.actions[0] != DOCUMENTS_SEARCH_ACTION) {
- LOG(ERROR) << "Parent API key must allow only `" << DOCUMENTS_SEARCH_ACTION << "` action.";
- return Option<std::string>(403, "Forbidden.");
- }
-
// ensure that parent key collection filter matches queried collection
- bool collection_allowed = false;
- for(const std::string& allowed_collection: api_key.collections) {
- if(allowed_collection == "*" || (collection != "*" && allowed_collection == collection)) {
- collection_allowed = true;
- break;
- }
- }
+ bool auth_success = auth_against_key(collection, action, api_key, true);
- if(!collection_allowed) {
- LOG(ERROR) << "Parent key does not allow queries against queried collection.";
- return Option<std::string>(403, "Forbidden.");
+ if(!auth_success) {
+ LOG(ERROR) << fmt_error("Parent key does not allow queries against queried collection.", api_key.value);
+ return Option<bool>(403, "Forbidden.");
}
// finally verify hmac
std::string digest = StringUtils::hmac(kv.first, custom_params);
if(digest == hmacSHA256) {
- return Option<std::string>(custom_params);
+ try {
+ embedded_params = nlohmann::json::parse(custom_params);
+ } catch(const std::exception& e) {
+ LOG(ERROR) << "JSON error: " << e.what();
+ return Option<bool>(403, "Forbidden.");
+ }
+
+ if(!embedded_params.is_object()) {
+ LOG(ERROR) << fmt_error("Scoped API key contains invalid search parameters.", api_key.value);
+ return Option<bool>(403, "Forbidden.");
+ }
+
+ if(embedded_params.count("expires_at") != 0) {
+ if(!embedded_params["expires_at"].is_number_integer() || embedded_params["expires_at"].get<int64_t>() < 0) {
+ LOG(ERROR) << fmt_error("Wrong format for `expires_at`. It should be an unsigned integer.", api_key.value);
+ return Option<bool>(403, "Forbidden.");
+ }
+
+ // parent key's expiry timestamp always takes precedence
+ uint64_t expiry_ts = std::min(api_key.expires_at, embedded_params["expires_at"].get<uint64_t>());
+
+ if(std::time(0) > expiry_ts) {
+ LOG(ERROR) << fmt_error("Scoped API key has expired. ", api_key.value);
+ return Option<bool>(403, "Forbidden.");
+ }
+ }
+
+ return Option<bool>(true);
}
}
}
- return Option<std::string>(403, "Forbidden.");
+ return Option<bool>(403, "Forbidden.");
}
+
+std::string AuthManager::fmt_error(std::string&& error, const std::string& key) {
+ return error + " Key prefix: " + key.substr(0, api_key_t::PREFIX_LEN);
+}
+
+Option<uint32_t> api_key_t::validate(const nlohmann::json &key_obj) {
+ auto mandatory_keys = {
+ "description", "actions", "collections"
+ };
+
+ for(auto key: mandatory_keys) {
+ if(key_obj.count(key) == 0) {
+ return Option<uint32_t>(400, std::string("Could not find a `") + key + "` key.");
+ }
+ }
+
+ 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.");
+ }
+
+ if(!key_obj["collections"].is_array() || key_obj["collections"].empty()) {
+ return Option<uint32_t>(400,"Wrong format for `collections`. It should be an array of string.");
+ }
+
+ for(const nlohmann::json & item: key_obj["actions"]) {
+ if(!item.is_string()) {
+ return Option<uint32_t>(400,"Wrong format for `actions`. It should be an array of string.");
+ }
+ }
+
+ for(const nlohmann::json & item: key_obj["collections"]) {
+ if(!item.is_string()) {
+ return Option<uint32_t>(400,"Wrong format for `collections`. It should be an array of string.");
+ }
+ }
+
+ if(key_obj.count("expires_at") != 0) {
+ if(!key_obj["expires_at"].is_number_integer() || key_obj["expires_at"].get<int64_t>() < 0) {
+ return Option<uint32_t>(400,"Wrong format for `expires_at`. It should be an unsigned integer.");
+ }
+ }
+
+ return Option<uint32_t>(200);
+}
+
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 76644b85..dcba0a2f 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -1192,11 +1192,16 @@ bool post_create_key(http_req &req, http_res &res) {
const std::string &rand_key = req.metadata;
+ if(req_json.count("expires_at") == 0) {
+ req_json["expires_at"] = api_key_t::FAR_FUTURE_TIMESTAMP;
+ }
+
api_key_t api_key(
rand_key,
req_json["description"].get<std::string>(),
req_json["actions"].get<std::vector<std::string>>(),
- req_json["collections"].get<std::vector<std::string>>()
+ req_json["collections"].get<std::vector<std::string>>(),
+ req_json["expires_at"].get<uint64_t>()
);
const Option<api_key_t>& api_key_op = auth_manager.create_key(api_key);
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index ab464965..de32fce8 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -4,6 +4,8 @@
#include <http_data.h>
#include "auth_manager.h"
+static const size_t FUTURE_TS = 64723363199;
+
class AuthManagerTest : public ::testing::Test {
protected:
Store *store;
@@ -37,8 +39,8 @@ TEST_F(AuthManagerTest, CreateListDeleteAPIKeys) {
// test inserts
- api_key_t api_key1("abcd1", "test key 1", {"read", "write"}, {"collection1", "collection2"});
- api_key_t api_key2("abcd2", "test key 2", {"admin"}, {"*"});
+ api_key_t api_key1("abcd1", "test key 1", {"read", "write"}, {"collection1", "collection2"}, FUTURE_TS);
+ api_key_t api_key2("abcd2", "test key 2", {"admin"}, {"*"}, FUTURE_TS);
ASSERT_EQ("abcd1", api_key1.value);
ASSERT_EQ("abcd2", api_key2.value);
@@ -88,8 +90,8 @@ TEST_F(AuthManagerTest, CreateListDeleteAPIKeys) {
}
TEST_F(AuthManagerTest, CheckRestoreOfAPIKeys) {
- api_key_t api_key1("abcd1", "test key 1", {"read", "write"}, {"collection1", "collection2"});
- api_key_t api_key2("abcd2", "test key 2", {"admin"}, {"*"});
+ api_key_t api_key1("abcd1", "test key 1", {"read", "write"}, {"collection1", "collection2"}, FUTURE_TS);
+ api_key_t api_key2("abcd2", "test key 2", {"admin"}, {"*"}, FUTURE_TS);
std::string key_value1 = auth_manager.create_key(api_key1).get().value;
std::string key_value2 = auth_manager.create_key(api_key2).get().value;
@@ -105,9 +107,12 @@ TEST_F(AuthManagerTest, CheckRestoreOfAPIKeys) {
ASSERT_EQ("test key 1", list_op.get()[0].description);
ASSERT_EQ("abcd", list_op.get()[0].value);
ASSERT_STREQ(key_value1.substr(0, 4).c_str(), list_op.get()[0].value.c_str());
+ ASSERT_EQ(FUTURE_TS, list_op.get()[0].expires_at);
+
ASSERT_EQ("test key 2", list_op.get()[1].description);
ASSERT_EQ("abcd", list_op.get()[1].value);
ASSERT_STREQ(key_value2.substr(0, 4).c_str(), list_op.get()[1].value.c_str());
+ ASSERT_EQ(FUTURE_TS, list_op.get()[1].expires_at);
}
TEST_F(AuthManagerTest, VerifyAuthentication) {
@@ -116,14 +121,14 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
ASSERT_FALSE(auth_manager.authenticate("jdlaslasdasd", "", "", sparams));
// wildcard permission
- api_key_t wildcard_all_key = api_key_t("abcd1", "wildcard all key", {"*"}, {"*"});
+ api_key_t wildcard_all_key = api_key_t("abcd1", "wildcard all key", {"*"}, {"*"}, FUTURE_TS);
auth_manager.create_key(wildcard_all_key);
ASSERT_FALSE(auth_manager.authenticate("jdlaslasdasd", "documents:create", "collection1", sparams));
ASSERT_TRUE(auth_manager.authenticate(wildcard_all_key.value, "metrics:get", "", sparams));
// wildcard on a collection
- api_key_t wildcard_coll_key = api_key_t("abcd2", "wildcard coll key", {"*"}, {"collection1"});
+ api_key_t wildcard_coll_key = api_key_t("abcd2", "wildcard coll key", {"*"}, {"collection1"}, FUTURE_TS);
auth_manager.create_key(wildcard_coll_key);
ASSERT_FALSE(auth_manager.authenticate("adasda", "documents:create", "collection1", sparams));
@@ -131,7 +136,7 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
ASSERT_FALSE(auth_manager.authenticate(wildcard_coll_key.value, "documents:get", "collection2", sparams));
// wildcard on multiple collections
- api_key_t wildcard_colls_key = api_key_t("abcd3", "wildcard coll key", {"*"}, {"collection1", "collection2", "collection3"});
+ api_key_t wildcard_colls_key = api_key_t("abcd3", "wildcard coll key", {"*"}, {"collection1", "collection2", "collection3"}, FUTURE_TS);
auth_manager.create_key(wildcard_colls_key);
ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "collection1", sparams));
@@ -141,7 +146,7 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
ASSERT_FALSE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "*", sparams));
// only 1 action on multiple collections
- api_key_t one_action_key = api_key_t("abcd4", "one action key", {"documents:search"}, {"collection1", "collection2"});
+ api_key_t one_action_key = api_key_t("abcd4", "one action key", {"documents:search"}, {"collection1", "collection2"}, FUTURE_TS);
auth_manager.create_key(one_action_key);
ASSERT_TRUE(auth_manager.authenticate(one_action_key.value, "documents:search", "collection1", sparams));
@@ -151,7 +156,7 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
// multiple actions on multiple collections
api_key_t mul_acoll_key = api_key_t("abcd5", "multiple action/collection key",
- {"documents:get", "collections:list"}, {"metacollection", "collection2"});
+ {"documents:get", "collections:list"}, {"metacollection", "collection2"}, FUTURE_TS);
auth_manager.create_key(mul_acoll_key);
ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "documents:get", "metacollection", sparams));
@@ -163,16 +168,25 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
// regexp match
- api_key_t regexp_colls_key1 = api_key_t("abcd6", "regexp coll key", {"*"}, {"coll.*"});
+ api_key_t regexp_colls_key1 = api_key_t("abcd6", "regexp coll key", {"*"}, {"coll.*"}, FUTURE_TS);
auth_manager.create_key(regexp_colls_key1);
ASSERT_TRUE(auth_manager.authenticate(regexp_colls_key1.value, "collections:list", "collection2", sparams));
ASSERT_TRUE(auth_manager.authenticate(regexp_colls_key1.value, "documents:get", "collection5", sparams));
- api_key_t regexp_colls_key2 = api_key_t("abcd7", "regexp coll key", {"*"}, {".*meta.*"});
+ api_key_t regexp_colls_key2 = api_key_t("abcd7", "regexp coll key", {"*"}, {".*meta.*"}, FUTURE_TS);
auth_manager.create_key(regexp_colls_key2);
ASSERT_TRUE(auth_manager.authenticate(regexp_colls_key2.value, "collections:list", "metacollection", sparams));
ASSERT_TRUE(auth_manager.authenticate(regexp_colls_key2.value, "collections:list", "ametacollection", sparams));
+ // check for expiry
+
+ api_key_t expired_key1 = api_key_t("abcd8", "expiry key", {"*"}, {"*"}, 1606542716);
+ auth_manager.create_key(expired_key1);
+ ASSERT_FALSE(auth_manager.authenticate(expired_key1.value, "collections:list", "collection", sparams));
+
+ api_key_t unexpired_key1 = api_key_t("abcd9", "expiry key", {"*"}, {"*"}, 2237712220);
+ auth_manager.create_key(unexpired_key1);
+ ASSERT_TRUE(auth_manager.authenticate(unexpired_key1.value, "collections:list", "collection", sparams));
}
TEST_F(AuthManagerTest, GenerationOfAPIAction) {
@@ -196,7 +210,7 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
params["filter_by"] = "country:USA";
// create a API key bound to search scope and a given collection
- api_key_t key_search_coll1("KeyVal", "test key", {"documents:search"}, {"coll1"});
+ api_key_t key_search_coll1("KeyVal", "test key", {"documents:search"}, {"coll1"}, FUTURE_TS);
auth_manager.create_key(key_search_coll1);
std::string scoped_key = StringUtils::base64_encode(
@@ -221,17 +235,57 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
ASSERT_STREQ("user_id:1080", empty_params["filter_by"].c_str());
// when more than a single key prefix matches, must pick the correct underlying key
- api_key_t key_search_coll2("KeyVal2", "test key", {"documents:search"}, {"coll2"});
+ api_key_t key_search_coll2("KeyVal2", "test key", {"documents:search"}, {"coll2"}, FUTURE_TS);
auth_manager.create_key(key_search_coll2);
ASSERT_FALSE(auth_manager.authenticate(scoped_key, "documents:search", "coll2", empty_params));
// should only allow scoped API keys derived from parent key with documents:search action
- api_key_t key_search_admin("AdminKey", "admin key", {"*"}, {"*"});
+ api_key_t key_search_admin("AdminKey", "admin key", {"*"}, {"*"}, FUTURE_TS);
auth_manager.create_key(key_search_admin);
std::string scoped_key2 = StringUtils::base64_encode(
"BXbsk+xLT1gxOjDyip6+PE4MtOzOm/H7kbkN1d/j/s4=Admi{\"filter_by\": \"user_id:1080\"}"
);
ASSERT_FALSE(auth_manager.authenticate(scoped_key2, "documents:search", "coll", empty_params));
+
+ // expiration of scoped api key
+
+ // {"filter_by": "user_id:1080", "expires_at": 2237712220} (NOT expired)
+ api_key_t key_expiry("ExpireKey", "expire key", {"documents:search"}, {"*"}, FUTURE_TS);
+ auth_manager.create_key(key_expiry);
+
+ empty_params.clear();
+
+ std::string scoped_key3 = "K1M2STRDelZYNHpxNGVWUTlBTGpOWUl4dk8wNU8xdnVEZi9aSUcvZE5tcz1FeHBpeyJmaWx0ZXJfYnkiOi"
+ "AidXNlcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiAyMjM3NzEyMjIwfQ==";
+
+ ASSERT_TRUE(auth_manager.authenticate(scoped_key3, "documents:search", "coll1", empty_params));
+ ASSERT_STREQ("user_id:1080", empty_params["filter_by"].c_str());
+ ASSERT_EQ(1, empty_params.size());
+
+ // {"filter_by": "user_id:1080", "expires_at": 1606563316} (expired)
+
+ api_key_t key_expiry2("ExpireKey2", "expire key", {"documents:search"}, {"*"}, FUTURE_TS);
+ auth_manager.create_key(key_expiry2);
+
+ empty_params.clear();
+
+ std::string scoped_key4 = "SXFKNldZZWRiWkVKVmI2RCt3OTlKNHpBZ24yWlRUbEdJdERtTy9IZ2REZz1FeHBpeyJmaWx0ZXJfYnkiOiAidXN"
+ "lcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiAxNjA2NTYzMzE2fQ==";
+
+ ASSERT_FALSE(auth_manager.authenticate(scoped_key4, "documents:search", "coll1", empty_params));
+
+ // {"filter_by": "user_id:1080", "expires_at": 64723363200} (greater than parent key expiry)
+ // embedded key's param cannot exceed parent's expiry
+
+ api_key_t key_expiry3("ExpireKey3", "expire key", {"documents:search"}, {"*"}, 1606563841);
+ auth_manager.create_key(key_expiry3);
+
+ empty_params.clear();
+
+ std::string scoped_key5 = "V3JMNFJlZHRMVStrZHphNFVGZDh4MWltSmx6Yzk2R3QvS2ZwSE8weGRWQT1FeHBpeyJmaWx0ZXJfYnkiOiAidX"
+ "Nlcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiA2NDcyMzM2MzIwMH0=";
+
+ ASSERT_FALSE(auth_manager.authenticate(scoped_key5, "documents:search", "coll1", empty_params));
}
TEST_F(AuthManagerTest, ValidateBadKeyProperties) {
@@ -251,8 +305,29 @@ TEST_F(AuthManagerTest, ValidateBadKeyProperties) {
ASSERT_STREQ("Wrong format for `actions`. It should be an array of string.", validate_op.error().c_str());
key_obj1["actions"] = 1;
- key_obj1["collections"].push_back("*");
+ key_obj1["collections"] = {"*"};
validate_op = api_key_t::validate(key_obj1);
ASSERT_FALSE(validate_op.ok());
ASSERT_STREQ("Wrong format for `actions`. It should be an array of string.", validate_op.error().c_str());
+
+ nlohmann::json key_obj2;
+ key_obj2["description"] = "desc";
+ key_obj2["actions"] = {"*"};
+ key_obj2["collections"] = {"foobar"};
+ key_obj2["expires_at"] = -100;
+
+ validate_op = api_key_t::validate(key_obj2);
+ ASSERT_FALSE(validate_op.ok());
+ ASSERT_STREQ("Wrong format for `expires_at`. It should be an unsigned integer.", validate_op.error().c_str());
+
+ key_obj2["expires_at"] = "expiry_ts";
+
+ validate_op = api_key_t::validate(key_obj2);
+ ASSERT_FALSE(validate_op.ok());
+ ASSERT_STREQ("Wrong format for `expires_at`. It should be an unsigned integer.", validate_op.error().c_str());
+
+ key_obj2["expires_at"] = 1606539880;
+
+ validate_op = api_key_t::validate(key_obj2);
+ ASSERT_TRUE(validate_op.ok());
}
\ No newline at end of file
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 0201ef0b..503bf0aa 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2665,13 +2665,17 @@ TEST_F(CollectionTest, HighlightWithAccentedCharacters) {
ASSERT_TRUE(coll1->add(doc.dump()).ok());
}
- auto results = coll1->search("jour", {"title"}, "", {}, {}, 0, 10, 1, FREQUENCY).get();
+ auto results = coll1->search("à jour", {"title"}, "", {}, {}, 0, 10, 1, FREQUENCY).get();
ASSERT_EQ(1, results["found"].get<size_t>());
ASSERT_EQ(1, results["hits"].size());
- ASSERT_STREQ("Mise à <mark>jour</mark> Timy depuis PC",
+ ASSERT_STREQ("Mise <mark>à</mark> <mark>jour</mark> Timy depuis PC",
results["hits"][0]["highlights"][0]["snippet"].get<std::string>().c_str());
+ ASSERT_EQ(2, results["hits"][0]["highlights"][0]["matched_tokens"].size());
+ ASSERT_STREQ("à", results["hits"][0]["highlights"][0]["matched_tokens"][0].get<std::string>().c_str());
+ ASSERT_STREQ("jour", results["hits"][0]["highlights"][0]["matched_tokens"][1].get<std::string>().c_str());
+
collectionManager.drop_collection("coll1");
}