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

Allow long custom API keys.

Kishore Nallan37b78072fae5
diff --git a/include/auth_manager.h b/include/auth_manager.h
index 873df7a2..d32872e7 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -107,7 +107,7 @@ private:
public:
- static const size_t KEY_LEN = 32;
+ static const size_t GENERATED_KEY_LEN = 32;
static const size_t HMAC_BASE64_LEN = 44;
AuthManager() = default;
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index ee3f87bc..f3408261 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -145,36 +145,31 @@ bool AuthManager::authenticate(const std::string& req_api_key, const std::string
std::shared_lock lock(mutex);
//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, collections, embedded_params);
- if(!params_op.ok()) {
- // authentication failed
- return false;
- }
+ const auto& key_it = api_keys.find(req_api_key);
+ if(key_it != api_keys.end()) {
+ const api_key_t& api_key = key_it->second;
+ return auth_against_key(collections, action, api_key, false);
+ }
- // enrich params with values from embedded_params
- for(auto& item: embedded_params.items()) {
- if(item.key() == "expires_at") {
- continue;
- }
+ // could be a scoped API key
+ nlohmann::json embedded_params;
+ Option<bool> auth_op = authenticate_parse_params(req_api_key, action, collections, embedded_params);
+ if(!auth_op.ok()) {
+ return false;
+ }
- // overwrite = true as embedded params have higher priority
- AuthManager::add_item_to_params(params, item, true);
+ // enrich params with values from embedded_params
+ for(auto& item: embedded_params.items()) {
+ if(item.key() == "expires_at") {
+ continue;
}
- return true;
+ // overwrite = true as embedded params have higher priority
+ AuthManager::add_item_to_params(params, item, true);
}
//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.at(req_api_key);
- return auth_against_key(collections, action, api_key, false);
+ return true;
}
bool AuthManager::auth_against_key(const std::vector<std::string>& collections, const std::string& action,
diff --git a/src/http_server.cpp b/src/http_server.cpp
index f2b602ac..08e642a4 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -379,7 +379,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
// do any additional pre-request middleware operations here
if(rpath->action == "keys:create") {
// we enrich incoming request with a random API key here so that leader and replicas will use the same key
- request->metadata = StringUtils::randstring(AuthManager::KEY_LEN);
+ request->metadata = StringUtils::randstring(AuthManager::GENERATED_KEY_LEN);
}
if(req->proceed_req == nullptr) {
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index 053f7f66..ae5a9273 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -128,6 +128,13 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
ASSERT_FALSE(auth_manager.authenticate("jdlaslasdasd", "documents:create", {"collection1"}, sparams));
ASSERT_TRUE(auth_manager.authenticate(wildcard_all_key.value, "metrics:get", {""}, sparams));
+ // long API key
+ std::string long_api_key_str = StringUtils::randstring(50);
+ api_key_t long_api_key = api_key_t(long_api_key_str, "long api key", {"*"}, {"*"}, FUTURE_TS);
+ auth_manager.create_key(long_api_key);
+
+ ASSERT_TRUE(auth_manager.authenticate(long_api_key_str, "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);