Skip to content

Commit

Permalink
initial impl for file
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalife committed Nov 24, 2024
1 parent bbd9486 commit 8570e80
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
53 changes: 52 additions & 1 deletion src/backends/file/be_file.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
#include "be_file.h"

#include <fstream>
#include <mosquitto.h>
#include <sstream>
#include <string>

constexpr const char* c_file_opt_key = "creds_file";

BE_File::BE_File(const std::map<const char*, const char*, KeysEqual>& 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;
}
12 changes: 12 additions & 0 deletions src/backends/file/be_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../backend.h"

#include <vector>

/**
* Represents a file based store.
* Stores valid username and encrypted password combinations in a file.
Expand All @@ -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::string, std::string>;
std::vector<Credentials> m_credentials;
};
3 changes: 0 additions & 3 deletions src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <algorithm>
#include <iostream>
#include <mosquitto.h>
#include <ranges>
#include <sstream>
#include <string_view>
#include <string>

constexpr const char* c_backends_opt_key = "backends";
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 8570e80

Please sign in to comment.