From 8570e80d06fcfcedf7bdda7af2cb353d4f9258b3 Mon Sep 17 00:00:00 2001 From: Dany Khalife Date: Sun, 24 Nov 2024 18:08:12 +0000 Subject: [PATCH] initial impl for file --- src/backends/file/be_file.cpp | 53 ++++++++++++++++++++++++++++++++++- src/backends/file/be_file.h | 12 ++++++++ src/plugin.cpp | 3 -- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/backends/file/be_file.cpp b/src/backends/file/be_file.cpp index d3e6cbc..a4a8295 100644 --- a/src/backends/file/be_file.cpp +++ b/src/backends/file/be_file.cpp @@ -1,14 +1,65 @@ #include "be_file.h" +#include #include +#include +#include + +constexpr const char* c_file_opt_key = "creds_file"; BE_File::BE_File(const std::map& options) { mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: backend %s initializing", BE_File::kind); + + if (options.count(c_file_opt_key) == 0) + { + mosquitto_log_printf(MOSQ_LOG_ERR, "*** auth-plugin: required config `%s` is missing", c_file_opt_key); + return; + } + + const char* credentialsFilePath = options.at(c_file_opt_key); + loadFile(credentialsFilePath); +} + +void BE_File::loadFile(const char* filePath) +{ + mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: loading credentials from `%s`", filePath); + + auto file = std::ifstream(filePath); + if (file.eof()) + { + mosquitto_log_printf(MOSQ_LOG_ERR, "*** auth-plugin: file not found: `%s`", filePath); + return;; + } + + std::string line, username, password; + int lineNb = 1; + while (getline(file, line)) + { + std::stringstream lineStream(line); + + if (getline(lineStream, username, ':') && getline(lineStream, password)) + { + m_credentials.emplace_back(make_pair(username, password)); + } + else + { + mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: line %i is malformed, skipping it", lineNb); + } + + ++lineNb; + } } bool BE_File::authenticate(const std::string& username, const std::string& password) { - mosquitto_log_printf(MOSQ_LOG_ERR, "*** auth-plugin: username %s with password %s", username.c_str(), password.c_str()); + for (const auto& item: m_credentials) + { + if (item.first == username && item.second == password) + { + return true; + } + } + return false; } diff --git a/src/backends/file/be_file.h b/src/backends/file/be_file.h index 6dd523d..cbb56e9 100644 --- a/src/backends/file/be_file.h +++ b/src/backends/file/be_file.h @@ -2,6 +2,8 @@ #include "../backend.h" +#include + /** * Represents a file based store. * Stores valid username and encrypted password combinations in a file. @@ -26,4 +28,14 @@ class BE_File: public IBackend * of credentials */ static constexpr const char* kind = "file"; + +private: + /** + * Loads the given path and stores valid credentials listed in it, in memory + * @param filePath The path to the file containing the credentials + */ + void loadFile(const char* filePath); + + using Credentials = std::pair; + std::vector m_credentials; }; diff --git a/src/plugin.cpp b/src/plugin.cpp index bb29a3c..04737db 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -3,9 +3,7 @@ #include #include #include -#include #include -#include #include constexpr const char* c_backends_opt_key = "backends"; @@ -47,7 +45,6 @@ void Plugin::initializeBackends() noexcept continue; } - mosquitto_log_printf(MOSQ_LOG_INFO, "*** auth-plugin: moving ownership"); m_backends.push_back(std::move(backend)); }