Skip to content

Commit

Permalink
lowercase authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalife committed Nov 24, 2024
1 parent fdaa073 commit e85a414
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/backends/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class IBackend
* Verifies a client credentials against its own store
* @return True if the client should be granted access by the broker
*/
virtual bool Authenticate(const std::string& username, const std::string& password) = 0;
virtual bool authenticate(const std::string& username, const std::string& password) = 0;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/backends/mysql/be_mysql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BE_Mysql::BE_Mysql(const std::vector<mosquitto_opt>& options)
mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: backend %s initializing", BE_Mysql::kind);
}

bool BE_Mysql::Authenticate(const std::string& username, const std::string& password)
bool BE_Mysql::authenticate(const std::string& username, const std::string& password)
{
return false;
}
2 changes: 1 addition & 1 deletion src/backends/mysql/be_mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BE_Mysql: public IBackend
* Verifies a client credentials against the MySQL store
* @return True if the client should be granted access by the broker
*/
bool Authenticate(const std::string& username, const std::string& password);
bool authenticate(const std::string& username, const std::string& password);

/**
* Identifier to use in the broker configuration to connect to a MySQL service
Expand Down
3 changes: 2 additions & 1 deletion src/backends/passwd/be_passwd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ BE_Passwd::BE_Passwd(const std::vector<mosquitto_opt>& options)
mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: backend %s initializing", BE_Passwd::kind);
}

bool BE_Passwd::Authenticate(const std::string& username, const std::string& password)
bool BE_Passwd::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());
return false;
}
2 changes: 1 addition & 1 deletion src/backends/passwd/be_passwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BE_Passwd: public IBackend
* Verifies a client credentials against the list of valid in-memory ones
* @return True if the client should be granted access by the broker
*/
bool Authenticate(const std::string& username, const std::string& password);
bool authenticate(const std::string& username, const std::string& password);

/**
* Identifier to use in the broker configuration to use a file-backed list
Expand Down
2 changes: 1 addition & 1 deletion src/backends/sqlite/be_sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BE_Sqlite::BE_Sqlite(const std::vector<mosquitto_opt>& options)
mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: backend %s initializing", BE_Sqlite::kind);
}

bool BE_Sqlite::Authenticate(const std::string& username, const std::string& password)
bool BE_Sqlite::authenticate(const std::string& username, const std::string& password)
{
return false;
}
2 changes: 1 addition & 1 deletion src/backends/sqlite/be_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BE_Sqlite: public IBackend
* Verifies a client credentials against the SQLite store
* @return True if the client should be granted access by the broker
*/
bool Authenticate(const std::string& username, const std::string& password);
bool authenticate(const std::string& username, const std::string& password);

/**
* Identifier to use in the broker configuration to connect to a SQLite database
Expand Down
14 changes: 0 additions & 14 deletions src/mosquitto-plugin-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,3 @@ int mosquitto_plugin_cleanup(void* userdata, struct mosquitto_opt* options, int

return 0;
}

/**
* Called when the broker is trying to validate basic authentication.
* @param user_data The pointer provided in `mosquitto_plugin_init`.
* @param client The broker instance that is attempting to authenticate a client.
* @param username The client's username
* @param password The client's password
* @return This function returns MOSQ_ERR_SUCCESS or MOSQ_ERR_AUTH for successful/failed authentication.
*/
int mosquitto_auth_unpwd_check(void* user_data, struct mosquitto* client, const char* username, const char* password)
{
Plugin* self = reinterpret_cast<Plugin*>(user_data);
return self->onBasicAuth(username, password);
}
47 changes: 45 additions & 2 deletions src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ Plugin::Plugin(mosquitto_plugin_id_t* identifier, std::vector<mosquitto_opt> opt
, m_options(std::move(options))
{
initializeBackends();
registerEvents();
}

Plugin::~Plugin()
{
unregisterEvents();
}

void Plugin::initializeBackends() noexcept
Expand Down Expand Up @@ -55,11 +57,52 @@ void Plugin::initializeBackends() noexcept
}
}

int Plugin::onBasicAuth(const std::string& username, const std::string& password) noexcept
void Plugin::registerEvents() noexcept
{
mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: registering events");

int hr = mosquitto_callback_register(m_identifier, MOSQ_EVT_BASIC_AUTH, Plugin::onEvent, nullptr, this);
if (hr != MOSQ_ERR_SUCCESS)
{
mosquitto_log_printf(MOSQ_LOG_ERR, "*** auth-plugin: unable to register for basic auth events, hr = %s", hr);
}
}

void Plugin::unregisterEvents() noexcept
{
mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: unregistering events");

int hr = mosquitto_callback_unregister(m_identifier, MOSQ_EVT_BASIC_AUTH, Plugin::onEvent, nullptr);
if (hr != MOSQ_ERR_SUCCESS)
{
mosquitto_log_printf(MOSQ_LOG_ERR, "*** auth-plugin: unable to unregister basic auth callback");
}
}

int Plugin::onEvent(int event_id, void* event_data, void* user_data) noexcept
{
Plugin* self = reinterpret_cast<Plugin*>(user_data);

if (event_id == MOSQ_EVT_BASIC_AUTH)
{
mosquitto_log_printf(MOSQ_LOG_DEBUG, "*** auth-plugin: received a basic auth event");
mosquitto_evt_basic_auth* ed = reinterpret_cast<mosquitto_evt_basic_auth*>(event_data);
return self->onBasicAuth(*ed);
}
else
{
mosquitto_log_printf(MOSQ_LOG_ERR, "*** auth-plugin: received an unexpected event, event_id = %i", event_id);
return MOSQ_ERR_UNKNOWN;
}

return MOSQ_ERR_SUCCESS;
}

int Plugin::onBasicAuth(const mosquitto_evt_basic_auth& event_data) noexcept
{
for (auto& backend: m_backends)
{
if (backend->Authenticate(username, password))
if (backend->authenticate(event_data.username, event_data.password))
{
return MOSQ_ERR_SUCCESS;
}
Expand Down
7 changes: 6 additions & 1 deletion src/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ class Plugin
* given a username and password combination. As long as one backend accepts the credentials, the
* authentication is considered successful. The backends are assigned priority based on the order they
* are listed in the configuration file. The first one listed is the first responder, and so on.
* @param event_data The raw data packet sent from the broker
* @return MOSQ_ERR_SUCCESS for successful combination, MOSQ_ERR_AUTH otherwise
*/
int onBasicAuth(const std::string& username, const std::string& password) noexcept;
int onBasicAuth(const mosquitto_evt_basic_auth& event_data) noexcept;

static int onEvent(int event_id, void* event_data, void* user_data) noexcept;

private:
/**
* Initializes the backends in the order they were listed in the configuration file. The first one
* listed is the first responder, and so on.
*/
void initializeBackends() noexcept;
void registerEvents() noexcept;
void unregisterEvents() noexcept;

std::vector<mosquitto_opt> m_options;
mosquitto_plugin_id_t* m_identifier;
Expand Down

0 comments on commit e85a414

Please sign in to comment.