Skip to content

Commit 937a5d9

Browse files
committed
Implement username/password authentication.
1 parent 5d385e1 commit 937a5d9

7 files changed

+56
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ ui_*
99
Makefile*
1010
*.pro.user
1111
*.stash
12+
*.Release
13+
*.Debug

MQTTCute.pro

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ win32-msvc:CONFIG(debug, debug|release): LIBS += -L$$PWD/mosquitto/msvc_2017_x64
8585
RESOURCES += \
8686
images.qrc
8787

88-
win32:RC_ICONS += img/mqtticon-large.png
88+
#win32:RC_ICONS += img/mqtticon-large.png

mainwindow.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ void MainWindow::connectRemote() {
164164
return;
165165
}
166166
}
167+
168+
if (usingDefaultSession && defaultSession.loginType == LOGIN_TYPE_PASSWORD) {
169+
if (!mqtt->setPassword(defaultSession.username, defaultSession.password)) {
170+
cerr << "Setting username/password failed.\n";
171+
QMessageBox::critical(this, tr("Setting username/password failed"),
172+
tr("Setting the username and/or password failed.\n\n\
173+
Please check the provided info and try again."));
174+
175+
return;
176+
}
177+
178+
}
179+
else if (loadedSession.loginType == LOGIN_TYPE_PASSWORD) {
180+
if (!mqtt->setPassword(loadedSession.username, loadedSession.password)) {
181+
cerr << "Setting username/password failed.\n";
182+
QMessageBox::critical(this, tr("Setting username/password failed"),
183+
tr("Setting the username and/or password failed.\n\n\
184+
Please check the provided info and try again."));
185+
186+
return;
187+
}
188+
}
167189

168190
// Try to connect. If successful, update local copy and stored remote server address.
169191
// The MQTT client class has to run on its own thread since it uses its own event loop.
@@ -216,6 +238,11 @@ void MainWindow::remoteConnected() {
216238
void MainWindow::errorHandler(QString err) {
217239
QMessageBox::warning(this, tr("Error"), err);
218240
connected = false;
241+
242+
// Reset UI elements.
243+
ui->actionConnect->setEnabled(true);
244+
ui->actionDisconnect->setEnabled(false);
245+
ui->mainToolBar->setDisabled(true);
219246
}
220247

221248

mosquitto/lib/mosquitto.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ and the Eclipse Distribution License is available at
2626
#else
2727
#include <winsock2.h>
2828
#include <windows.h>
29-
typedef int ssize_t;
29+
//typedef int ssize_t;
3030
#endif
3131

3232
#include "mosquitto.h"

mosquitto/lib/net_mosq.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ and the Eclipse Distribution License is available at
2020
#include <unistd.h>
2121
#else
2222
#include <winsock2.h>
23-
typedef int ssize_t;
23+
//typedef int ssize_t;
2424
#endif
2525

2626
#include "mosquitto_internal.h"

mqttlistener.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,31 @@ bool MqttListener::setTLS(string &ca, string &cert, string &key) {
5151
}
5252

5353

54+
// --- SET PASSWORD ---
55+
// Set the username and password for this MQTT connection.
56+
bool MqttListener::setPassword(string &username, string &password) {
57+
int res = username_pw_set(username.c_str(), password.c_str());
58+
if (res != MOSQ_ERR_SUCCESS) {
59+
if (res == MOSQ_ERR_INVAL) {
60+
cerr << "Invalid input parameters for Mosquitto PW.\n";
61+
}
62+
else if (res == MOSQ_ERR_NOMEM) {
63+
cerr << "Out of memory on Mosquitto PW.\n";
64+
}
65+
else {
66+
cerr << "Unknown Mosquitto PW error.\n";
67+
}
68+
69+
return false;
70+
}
71+
72+
return true;
73+
}
74+
75+
5476
// --- CONNECT BROKER ---
5577
void MqttListener::connectBroker() {
56-
cout << "Connecting to broker..." << host.toStdString() << ":" << port << "\n";
78+
cout << "Connecting to broker: " << host.toStdString() << ":" << port << "\n";
5779
int keepalive = 60;
5880
mosqpp::mosquittopp::connect(host.toStdString().c_str(), port, keepalive);
5981

mqttlistener.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MqttListener : public QObject, mosqpp::mosquittopp {
4040
~MqttListener();
4141

4242
bool setTLS(string &ca, string &cert, string &key);
43+
bool setPassword(string &username, string &password);
4344
void on_connect(int rc);
4445
void on_message(const struct mosquitto_message* message);
4546
void on_subscribe(int mid, int qos_count, const int* granted_qos);

0 commit comments

Comments
 (0)