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 auth against multiple collections.

kishorenc0175ac0df4a5
diff --git a/include/auth_manager.h b/include/auth_manager.h
index 2cb10262..db199395 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -94,6 +94,13 @@ private:
static std::string fmt_error(std::string&& error, const std::string& key);
+ Option<bool> authenticate_parse_params(const std::string& scoped_api_key, const std::string& action,
+ const std::vector<std::string>& collections,
+ nlohmann::json& embedded_params) const;
+
+ bool auth_against_key(const std::vector<std::string>& collections, const std::string& action,
+ const api_key_t &api_key, const bool search_only) const;
+
public:
AuthManager() = default;
@@ -108,26 +115,15 @@ public:
Option<api_key_t> remove_key(uint32_t id);
- bool authenticate(
- const std::string& req_api_key,
- const std::string& action,
- const std::string& collection,
- std::map<std::string, std::string>& params
- );
-
- Option<bool> authenticate_parse_params(
- const std::string& scoped_api_key,
- const std::string& action,
- 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;
+ bool authenticate(const std::string& req_api_key, const std::string& action,
+ const std::vector<std::string>& collections, std::map<std::string, std::string>& params) const;
+
static bool populate_req_params(std::map<std::string, std::string> &req_params,
nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item);
};
\ No newline at end of file
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 75a30172..560af3b2 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -73,7 +73,7 @@ public:
void add_to_collections(Collection* collection);
bool auth_key_matches(const std::string& auth_key_sent, const std::string& action,
- const std::string& collection, std::map<std::string, std::string>& params);
+ const std::vector<std::string>& collections, std::map<std::string, std::string>& params) const;
Option<Collection*> create_collection(const std::string& name, const size_t num_memory_shards,
const std::vector<field> & fields,
diff --git a/include/core_api.h b/include/core_api.h
index f3a54f18..6f0cc04b 100644
--- a/include/core_api.h
+++ b/include/core_api.h
@@ -2,7 +2,7 @@
#include "http_server.h"
-bool handle_authentication(std::map<std::string, std::string>& req_params, const route_path& rpath,
+bool handle_authentication(std::map<std::string, std::string>& req_params, const std::string& body, const route_path& rpath,
const std::string& auth_key);
// Collections
@@ -99,6 +99,9 @@ bool post_config(http_req& req, http_res& res);
// Misc helpers
+void get_collections_for_auth(std::map<std::string, std::string> &req_params, const std::string &body,
+ const route_path &rpath, std::vector<std::string> &collections);
+
bool raft_write_send_response(void *data);
static constexpr const char* SEND_RESPONSE_MSG = "send_response";
diff --git a/include/http_server.h b/include/http_server.h
index 914d58a2..fc75bd64 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -75,7 +75,7 @@ private:
bool cors_enabled;
- bool (*auth_handler)(std::map<std::string, std::string>& params, const route_path& rpath,
+ bool (*auth_handler)(std::map<std::string, std::string>& params, const std::string& body, const route_path& rpath,
const std::string& auth_key);
static void on_accept(h2o_socket_t *listener, const char *err);
@@ -120,8 +120,8 @@ public:
uint64_t node_state() const;
- void set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const route_path & rpath,
- const std::string & auth_key));
+ void set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const std::string& body,
+ const route_path & rpath, const std::string & auth_key));
void get(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index cb6fbb9f..db775889 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -131,14 +131,15 @@ uint32_t AuthManager::get_next_api_key_id() {
}
bool AuthManager::authenticate(const std::string& req_api_key, const std::string& action,
- const std::string& collection, std::map<std::string, std::string>& params) {
+ const std::vector<std::string>& collections,
+ std::map<std::string, std::string>& params) const {
//LOG(INFO) << "AuthManager::authenticate()";
if(req_api_key.size() > KEY_LEN) {
// scoped API key: validate and if valid, extract params
nlohmann::json embedded_params;
- Option<bool> params_op = authenticate_parse_params(req_api_key, action, collection, embedded_params);
+ Option<bool> params_op = authenticate_parse_params(req_api_key, action, collections, embedded_params);
if(!params_op.ok()) {
// authentication failed
return false;
@@ -162,17 +163,17 @@ bool AuthManager::authenticate(const std::string& req_api_key, const std::string
return true;
}
- //LOG(INFO) << "api_keys.size() = " << api_keys.size();
+ LOG(INFO) << "api_keys.size() = " << api_keys.size();
if(api_keys.count(req_api_key) == 0) {
return false;
}
- const api_key_t& api_key = api_keys[req_api_key];
- return auth_against_key(collection, action, api_key, false);
+ const api_key_t& api_key = api_keys.at(req_api_key);
+ return auth_against_key(collections, action, api_key, false);
}
-bool AuthManager::auth_against_key(const std::string& collection, const std::string& action,
+bool AuthManager::auth_against_key(const std::vector<std::string>& collections, const std::string& action,
const api_key_t& api_key, const bool search_only) const {
if(uint64_t(std::time(0)) > api_key.expires_at) {
@@ -201,18 +202,30 @@ bool AuthManager::auth_against_key(const std::string& collection, const std::str
}
}
- for(const std::string& allowed_collection: api_key.collections) {
- if(allowed_collection == "*" || (allowed_collection == collection) || collection.empty() ||
- std::regex_match (collection, std::regex(allowed_collection))) {
- return true;
+ for(const std::string& req_collection: collections) {
+ bool coll_allowed = false;
+
+ for(const std::string& allowed_collection: api_key.collections) {
+ if(allowed_collection == "*" || (allowed_collection == req_collection) || req_collection.empty() ||
+ std::regex_match (req_collection, std::regex(allowed_collection))) {
+ coll_allowed = true;
+ break;
+ }
+ }
+
+ if(!coll_allowed) {
+ // even if one collection is not allowed, we reject the entire request
+ return false;
}
}
- return false;
+ return true;
}
Option<bool> AuthManager::authenticate_parse_params(const std::string& scoped_api_key, const std::string& action,
- const std::string& collection, nlohmann::json& embedded_params) {
+ const std::vector<std::string>& collections,
+ nlohmann::json& embedded_params) const {
+
// allow only searches from scoped keys
if(action != DOCUMENTS_SEARCH_ACTION) {
LOG(ERROR) << "Scoped API keys can only be used for searches.";
@@ -238,7 +251,7 @@ Option<bool> AuthManager::authenticate_parse_params(const std::string& scoped_ap
const api_key_t& api_key = kv.second;
// ensure that parent key collection filter matches queried collection
- bool auth_success = auth_against_key(collection, action, api_key, true);
+ bool auth_success = auth_against_key(collections, action, api_key, true);
if(!auth_success) {
LOG(ERROR) << fmt_error("Parent key does not allow queries against queried collection.", api_key.value);
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 08153a2d..858f99db 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -263,8 +263,8 @@ void CollectionManager::dispose() {
bool CollectionManager::auth_key_matches(const std::string& auth_key_sent,
const std::string& action,
- const std::string& collection,
- std::map<std::string, std::string>& params) {
+ const std::vector<std::string>& collections,
+ std::map<std::string, std::string>& params) const {
if(auth_key_sent.empty()) {
return false;
}
@@ -275,7 +275,7 @@ bool CollectionManager::auth_key_matches(const std::string& auth_key_sent,
}
// finally, check managed auth keys
- return auth_manager.authenticate(auth_key_sent, action, collection, params);
+ return auth_manager.authenticate(auth_key_sent, action, collections, params);
}
Option<Collection*> CollectionManager::create_collection(const std::string& name,
diff --git a/src/core_api.cpp b/src/core_api.cpp
index ffbe8f38..42f303d0 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -11,22 +11,47 @@
#include "logger.h"
#include "core_api_utils.h"
-bool handle_authentication(std::map<std::string, std::string>& req_params, const route_path& rpath,
- const std::string& auth_key) {
+bool handle_authentication(std::map<std::string, std::string>& req_params, const std::string& body,
+ const route_path& rpath, const std::string& auth_key) {
CollectionManager & collectionManager = CollectionManager::get_instance();
- std::string collection = "*";
+ std::vector<std::string> collections;
- if(req_params.count("collection") != 0) {
- collection = req_params.at("collection");
- }
+ get_collections_for_auth(req_params, body, rpath, collections);
if(rpath.handler == get_health) {
// health endpoint requires no authentication
return true;
}
- return collectionManager.auth_key_matches(auth_key, rpath.action, collection, req_params);
+ return collectionManager.auth_key_matches(auth_key, rpath.action, collections, req_params);
+}
+
+void get_collections_for_auth(std::map<std::string, std::string> &req_params, const std::string &body,
+ const route_path &rpath, std::vector<std::string> &collections) {
+ if(req_params.count("collection") != 0) {
+ collections.emplace_back(req_params.at("collection"));
+ }
+
+ if(rpath.handler == post_multi_search) {
+ nlohmann::json obj = nlohmann::json::parse(body, nullptr, false);
+
+ if(obj == nlohmann::json::value_t::discarded) {
+ LOG(ERROR) << "Multi search request body is malformed.";
+ }
+
+ if(obj != nlohmann::json::value_t::discarded && obj.count("searches") != 0 && obj["searches"].is_array()) {
+ for(auto& el : obj["searches"]) {
+ if(el.is_object() && el.count("collection") != 0) {
+ collections.emplace_back(el["collection"].get<std::string>());
+ }
+ }
+ }
+ }
+
+ if(collections.empty()) {
+ collections.emplace_back("");
+ }
}
index_operation_t get_index_operation(const std::string& action) {
diff --git a/src/http_server.cpp b/src/http_server.cpp
index e9f71d8a..4e94e72f 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -397,15 +397,15 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
}
}
- bool authenticated = h2o_handler->http_server->auth_handler(query_map, *rpath, api_auth_key_sent);
+ const std::string & body = std::string(req->entity.base, req->entity.len);
+
+ bool authenticated = h2o_handler->http_server->auth_handler(query_map, body, *rpath, api_auth_key_sent);
if(!authenticated) {
std::string message = std::string("{\"message\": \"Forbidden - a valid `") + http_req::AUTH_HEADER +
"` header must be sent.\"}";
return send_response(req, 401, message);
}
- const std::string & body = std::string(req->entity.base, req->entity.len);
-
http_req* request = new http_req(req, rpath->http_method, path_without_query, route_hash, query_map, body);
http_res* response = new http_res();
@@ -728,8 +728,8 @@ void HttpServer::destroy_request_response(http_req* request, http_res* response)
}
}
-void HttpServer::set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const route_path& rpath,
- const std::string& auth_key)) {
+void HttpServer::set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const std::string& body,
+ const route_path& rpath, const std::string& auth_key)) {
auth_handler = handler;
}
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index 109d3f87..d4b7fa20 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -3,6 +3,7 @@
#include <iostream>
#include <http_data.h>
#include "auth_manager.h"
+#include "core_api.h"
static const size_t FUTURE_TS = 64723363199;
@@ -118,75 +119,123 @@ TEST_F(AuthManagerTest, CheckRestoreOfAPIKeys) {
TEST_F(AuthManagerTest, VerifyAuthentication) {
std::map<std::string, std::string> sparams;
// when no keys are present at all
- ASSERT_FALSE(auth_manager.authenticate("jdlaslasdasd", "", "", sparams));
+ ASSERT_FALSE(auth_manager.authenticate("jdlaslasdasd", "", {}, sparams));
// wildcard permission
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));
+ 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"}, FUTURE_TS);
auth_manager.create_key(wildcard_coll_key);
- ASSERT_FALSE(auth_manager.authenticate("adasda", "documents:create", "collection1", sparams));
- ASSERT_TRUE(auth_manager.authenticate(wildcard_coll_key.value, "documents:get", "collection1", sparams));
- ASSERT_FALSE(auth_manager.authenticate(wildcard_coll_key.value, "documents:get", "collection2", sparams));
+ ASSERT_FALSE(auth_manager.authenticate("adasda", "documents:create", {"collection1"}, sparams));
+ ASSERT_TRUE(auth_manager.authenticate(wildcard_coll_key.value, "documents:get", {"collection1"}, sparams));
+ 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"}, FUTURE_TS);
auth_manager.create_key(wildcard_colls_key);
- ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "collection1", sparams));
- ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:search", "collection2", sparams));
- ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:create", "collection3", sparams));
- ASSERT_FALSE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "collection4", sparams));
- ASSERT_FALSE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", "*", sparams));
+ ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", {"collection1"}, sparams));
+ ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:search", {"collection2"}, sparams));
+ ASSERT_TRUE(auth_manager.authenticate(wildcard_colls_key.value, "documents:create", {"collection3"}, sparams));
+ ASSERT_FALSE(auth_manager.authenticate(wildcard_colls_key.value, "documents:get", {"collection4"}, sparams));
+ 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"}, FUTURE_TS);
auth_manager.create_key(one_action_key);
- ASSERT_TRUE(auth_manager.authenticate(one_action_key.value, "documents:search", "collection1", sparams));
- ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "documents:get", "collection2", sparams));
- ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "documents:search", "collection5", sparams));
- ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "*", "collection2", sparams));
+ ASSERT_TRUE(auth_manager.authenticate(one_action_key.value, "documents:search", {"collection1"}, sparams));
+ ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "documents:get", {"collection2"}, sparams));
+ ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "documents:search", {{"collection5"}}, sparams));
+ ASSERT_FALSE(auth_manager.authenticate(one_action_key.value, "*", {"collection2"}, sparams));
// 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"}, FUTURE_TS);
auth_manager.create_key(mul_acoll_key);
- ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "documents:get", "metacollection", sparams));
- ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "collections:list", "collection2", sparams));
- ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "collections:list", "metacollection", sparams));
- ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "documents:search", "collection2", sparams));
- ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "documents:get", "collection5", sparams));
- ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "*", "*", sparams));
+ ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "documents:get", {"metacollection"}, sparams));
+ ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "collections:list", {"collection2"}, sparams));
+ ASSERT_TRUE(auth_manager.authenticate(mul_acoll_key.value, "collections:list", {"metacollection"}, sparams));
+ ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "documents:search", {"collection2"}, sparams));
+ ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "documents:get", {"collection5"}, sparams));
+ ASSERT_FALSE(auth_manager.authenticate(mul_acoll_key.value, "*", {"*"}, sparams));
// regexp match
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));
+ 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.*"}, 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));
+ 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));
+ 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));
+ ASSERT_TRUE(auth_manager.authenticate(unexpired_key1.value, "collections:list", {"collection"}, sparams));
+}
+
+TEST_F(AuthManagerTest, HandleAuthentication) {
+ route_path rpath_multi_search = route_path("POST", {"multi_search"}, post_multi_search, false, false);
+ std::map<std::string, std::string> req_params;
+
+ std::vector<std::string> collections;
+ get_collections_for_auth(req_params, "{]", rpath_multi_search, collections);
+
+ ASSERT_EQ(1, collections.size());
+ ASSERT_STREQ("", collections[0].c_str());
+
+ nlohmann::json sample_search_body;
+ sample_search_body["searches"] = nlohmann::json::array();
+ nlohmann::json search_query;
+ search_query["q"] = "aaa";
+ search_query["collection"] = "company1";
+
+ sample_search_body["searches"].push_back(search_query);
+
+ search_query["collection"] = "company2";
+ sample_search_body["searches"].push_back(search_query);
+
+ collections.clear();
+ get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, collections);
+
+ ASSERT_EQ(2, collections.size());
+ ASSERT_STREQ("company1", collections[0].c_str());
+ ASSERT_STREQ("company2", collections[1].c_str());
+
+ collections.clear();
+ req_params["collection"] = "foo";
+
+ get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, collections);
+
+ ASSERT_EQ(3, collections.size());
+ ASSERT_STREQ("foo", collections[0].c_str());
+ ASSERT_STREQ("company1", collections[1].c_str());
+ ASSERT_STREQ("company2", collections[2].c_str());
+
+ collections.clear();
+ req_params.clear();
+
+ route_path rpath_search = route_path("GET", {"collections", ":collection", "documents", "search"}, get_search, false, false);
+ get_collections_for_auth(req_params, sample_search_body.dump(), rpath_search, collections);
+
+ ASSERT_EQ(1, collections.size());
+ ASSERT_STREQ("", collections[0].c_str());
}
TEST_F(AuthManagerTest, GenerationOfAPIAction) {
@@ -223,27 +272,27 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
"IvjqWNZ5M5ElcvbMoXj45BxkQrZG4ZKEaNQoRioCx2s=KeyV{\"filter_by\": \"user_id:1080\"}"
);
- ASSERT_TRUE(auth_manager.authenticate(scoped_key, "documents:search", "coll1", params));
+ ASSERT_TRUE(auth_manager.authenticate(scoped_key, "documents:search", {"coll1"}, params));
ASSERT_STREQ("country:USA&&user_id:1080", params["filter_by"].c_str());
// should scope to collection bound by the parent key
- ASSERT_FALSE(auth_manager.authenticate(scoped_key, "documents:search", "coll2", params));
+ ASSERT_FALSE(auth_manager.authenticate(scoped_key, "documents:search", {"coll2"}, params));
// should scope to search action only
- ASSERT_FALSE(auth_manager.authenticate(scoped_key, "documents:create", "coll1", params));
+ ASSERT_FALSE(auth_manager.authenticate(scoped_key, "documents:create", {"coll1"}, params));
// check with corrupted key
- ASSERT_FALSE(auth_manager.authenticate("asdasasd", "documents:search", "coll1", params));
+ ASSERT_FALSE(auth_manager.authenticate("asdasasd", "documents:search", {"coll1"}, params));
// when params is empty, embedded param should be set
std::map<std::string, std::string> empty_params;
- ASSERT_TRUE(auth_manager.authenticate(scoped_key, "documents:search", "coll1", empty_params));
+ ASSERT_TRUE(auth_manager.authenticate(scoped_key, "documents:search", {"coll1"}, empty_params));
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"}, FUTURE_TS);
auth_manager.create_key(key_search_coll2);
- ASSERT_FALSE(auth_manager.authenticate(scoped_key, "documents:search", "coll2", empty_params));
+ 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", {"*"}, {"*"}, FUTURE_TS);
@@ -251,7 +300,7 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
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));
+ ASSERT_FALSE(auth_manager.authenticate(scoped_key2, "documents:search", {"coll2"}, empty_params));
// expiration of scoped api key
@@ -264,7 +313,7 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
std::string scoped_key3 = "K1M2STRDelZYNHpxNGVWUTlBTGpOWUl4dk8wNU8xdnVEZi9aSUcvZE5tcz1FeHBpeyJmaWx0ZXJfYnkiOi"
"AidXNlcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiAyMjM3NzEyMjIwfQ==";
- ASSERT_TRUE(auth_manager.authenticate(scoped_key3, "documents:search", "coll1", empty_params));
+ 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());
@@ -278,7 +327,7 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
std::string scoped_key4 = "SXFKNldZZWRiWkVKVmI2RCt3OTlKNHpBZ24yWlRUbEdJdERtTy9IZ2REZz1FeHBpeyJmaWx0ZXJfYnkiOiAidXN"
"lcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiAxNjA2NTYzMzE2fQ==";
- ASSERT_FALSE(auth_manager.authenticate(scoped_key4, "documents:search", "coll1", empty_params));
+ 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
@@ -291,12 +340,12 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
std::string scoped_key5 = "V3JMNFJlZHRMVStrZHphNFVGZDh4MWltSmx6Yzk2R3QvS2ZwSE8weGRWQT1FeHBpeyJmaWx0ZXJfYnkiOiAidX"
"Nlcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiA2NDcyMzM2MzIwMH0=";
- ASSERT_FALSE(auth_manager.authenticate(scoped_key5, "documents:search", "coll1", empty_params));
+ ASSERT_FALSE(auth_manager.authenticate(scoped_key5, "documents:search", {"coll1"}, empty_params));
// bad scoped API key
- ASSERT_FALSE(auth_manager.authenticate(" XhsdBdhehdDheruyhvbdhwjhHdhgyeHbfheR", "documents:search", "coll1", empty_params));
- ASSERT_FALSE(auth_manager.authenticate("cXYPvkNKRlQrBzVTEgY4a3FrZfZ2MEs4kFJ6all3eldwM GhKZnRId3Y3TT1RZmxZeYJmaWx0ZXJfYnkiOkJ1aWQ6OElVm1lUVm15SG9ZOHM4NUx2VFk4S2drNHJIMiJ9", "documents:search", "coll1", empty_params));
- ASSERT_FALSE(auth_manager.authenticate("SXZqcVdOWjVNNUVsY3ZiTW9YajQ1QnhrUXJaRzRaS0VhTlFvUmlvQ3gycz1LZXlWeyJmaWx0ZXJfYnkiOiAidXNlcl9pZDoxMDgw In0=", "documents:search", "coll1", empty_params));
+ ASSERT_FALSE(auth_manager.authenticate(" XhsdBdhehdDheruyhvbdhwjhHdhgyeHbfheR", "documents:search", {"coll1"}, empty_params));
+ ASSERT_FALSE(auth_manager.authenticate("cXYPvkNKRlQrBzVTEgY4a3FrZfZ2MEs4kFJ6all3eldwM GhKZnRId3Y3TT1RZmxZeYJmaWx0ZXJfYnkiOkJ1aWQ6OElVm1lUVm15SG9ZOHM4NUx2VFk4S2drNHJIMiJ9", "documents:search", {"coll1"}, empty_params));
+ ASSERT_FALSE(auth_manager.authenticate("SXZqcVdOWjVNNUVsY3ZiTW9YajQ1QnhrUXJaRzRaS0VhTlFvUmlvQ3gycz1LZXlWeyJmaWx0ZXJfYnkiOiAidXNlcl9pZDoxMDgw In0=", "documents:search", {"coll1"}, empty_params));
}
TEST_F(AuthManagerTest, ValidateBadKeyProperties) {