Skip to content

Commit 519b2cb

Browse files
authored
Merge pull request #21 from msgflo/fix-auth
Actually use authization details on MQTT
2 parents c7acf56 + b8cdeda commit 519b2cb

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

include/msgflo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <string>
44
#include <memory>
5+
#include <functional>
6+
57
#include "json11.hpp"
68

79
namespace msgflo {

src/mqtt_support.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ class mqtt_client : public waitable, private mqtt_lib {
283283
}
284284

285285
public:
286+
int setUsernamePassword(std::string user, std::string pass) {
287+
return mosquitto_username_pw_set(mosquitto, user.c_str(), pass.c_str());
288+
}
289+
286290
int unacked_messages() {
287291
guard lock(this_mutex);
288292
return unacked_messages_;

src/msgflo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,15 @@ class MosquittoEngine final : public Engine, protected mqtt_event_listener, prot
359359

360360
public:
361361
MosquittoEngine(const EngineConfig config, const string &host, const int port,
362-
const int keep_alive, const string &client_id, const bool clean_session)
362+
const int keep_alive, const string &client_id, const bool clean_session, const std::string &user, const std::string &pw)
363363
: _debugOutput(config.debugOutput())
364364
, client(this, host, port, keep_alive, client_id, clean_session)
365365
, discoveryLastSent(0)
366366
, discoveryPeriod(config.discoveryPeriod/3)
367367
{
368+
if (user.size()) {
369+
client.setUsernamePassword(user, pw);
370+
}
368371
client.connect();
369372
}
370373

@@ -567,8 +570,7 @@ shared_ptr<Engine> createEngine(const EngineConfig config) {
567570
cout << "keep_alive: " << keep_alive << endl;
568571
cout << "clean_session: " << clean_session << endl;
569572
}
570-
571-
return make_shared<MosquittoEngine>(config, host, port, keep_alive, client_id, clean_session);
573+
return make_shared<MosquittoEngine>(config, host, port, keep_alive, client_id, clean_session, username, password);
572574
} else if (string_starts_with(url, "amqp://")) {
573575
return make_shared<AmqpEngine>(url, config);
574576
}

0 commit comments

Comments
 (0)