Skip to content

Commit

Permalink
add client id to basic authentication interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalife committed Dec 25, 2024
1 parent c6334d0 commit facb929
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/backends/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class IBackend

/**
* Verifies a client credentials against its own store
* @param username The username the client passed
* @param password The password the client passed
* @param client_id The id associated with the mosquitto client making the connection
* @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, const std::string& client_id) = 0;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/backends/file/be_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void BE_File::loadFile(const std::string& filePath)
mosquitto_log_printf(MOSQ_LOG_INFO, "*** auth-plugin: loaded %i credentials from `%s`", m_credentials.size(), filePath.c_str());
}

bool BE_File::authenticate(const std::string& username, const std::string& password)
bool BE_File::authenticate(const std::string& username, const std::string& password, const std::string& /*client_id*/)
{
SHA256 hasher;
std::string input_hash = hasher(password);
Expand Down
5 changes: 4 additions & 1 deletion src/backends/file/be_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ class BE_File: public IBackend

/**
* Verifies a client credentials against the list of valid in-memory ones
* @param username The username the client passed
* @param password The password the client passed
* @param client_id The id associated with the mosquitto client making the connection
* @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, const std::string& client_id);

/**
* Identifier to use in the broker configuration to use a file-backed list
Expand Down
2 changes: 1 addition & 1 deletion src/backends/http/be_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void BE_Http::setupSubpaths(const std::map<std::string, std::string>& options) n
}
}

bool BE_Http::authenticate(const std::string& username, const std::string& password)
bool BE_Http::authenticate(const std::string& username, const std::string& password, const std::string& client_id)
{
return false;
}
5 changes: 4 additions & 1 deletion src/backends/http/be_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class BE_Http: public IBackend

/**
* Verifies a client credentials against the Http store
* @param username The username the client passed
* @param password The password the client passed
* @param client_id The id associated with the mosquitto client making the connection
* @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, const std::string& client_id);

/**
* Identifier to use in the broker configuration to connect to an Http backend
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::map<std::string, std::string>& 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, const std::string& /*client_id*/)
{
return false;
}
5 changes: 4 additions & 1 deletion src/backends/mysql/be_mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class BE_Mysql: public IBackend

/**
* Verifies a client credentials against the MySQL store
* @param username The username the client passed
* @param password The password the client passed
* @param client_id The id associated with the mosquitto client making the connection
* @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, const std::string& client_id);

/**
* Identifier to use in the broker configuration to connect to a MySQL service
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::map<std::string, std::string>& 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, const std::string& /*client_id*/)
{
return false;
}
5 changes: 4 additions & 1 deletion src/backends/sqlite/be_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class BE_Sqlite: public IBackend

/**
* Verifies a client credentials against the SQLite store
* @param username The username the client passed
* @param password The password the client passed
* @param client_id The id associated with the mosquitto client making the connection
* @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, const std::string& client_id);

/**
* Identifier to use in the broker configuration to connect to a SQLite database
Expand Down
3 changes: 2 additions & 1 deletion src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ int Plugin::onBasicAuth(const mosquitto_evt_basic_auth& event_data) noexcept
{
for (auto& backend: m_backends)
{
if (backend->authenticate(event_data.username, event_data.password))
const char* client_id = mosquitto_client_id(event_data.client);
if (backend->authenticate(event_data.username, event_data.password, client_id))
{
return MOSQ_ERR_SUCCESS;
}
Expand Down

0 comments on commit facb929

Please sign in to comment.