Skip to content

Commit

Permalink
Implement username/password authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
MayaPosch committed Apr 24, 2018
1 parent 5d385e1 commit 937a5d9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -9,3 +9,5 @@ ui_*
Makefile*
*.pro.user
*.stash
*.Release
*.Debug
2 changes: 1 addition & 1 deletion MQTTCute.pro
Expand Up @@ -85,4 +85,4 @@ win32-msvc:CONFIG(debug, debug|release): LIBS += -L$$PWD/mosquitto/msvc_2017_x64
RESOURCES += \
images.qrc

win32:RC_ICONS += img/mqtticon-large.png
#win32:RC_ICONS += img/mqtticon-large.png
27 changes: 27 additions & 0 deletions mainwindow.cpp
Expand Up @@ -164,6 +164,28 @@ void MainWindow::connectRemote() {
return;
}
}

if (usingDefaultSession && defaultSession.loginType == LOGIN_TYPE_PASSWORD) {
if (!mqtt->setPassword(defaultSession.username, defaultSession.password)) {
cerr << "Setting username/password failed.\n";
QMessageBox::critical(this, tr("Setting username/password failed"),
tr("Setting the username and/or password failed.\n\n\
Please check the provided info and try again."));

return;
}

}
else if (loadedSession.loginType == LOGIN_TYPE_PASSWORD) {
if (!mqtt->setPassword(loadedSession.username, loadedSession.password)) {
cerr << "Setting username/password failed.\n";
QMessageBox::critical(this, tr("Setting username/password failed"),
tr("Setting the username and/or password failed.\n\n\
Please check the provided info and try again."));

return;
}
}

// Try to connect. If successful, update local copy and stored remote server address.
// The MQTT client class has to run on its own thread since it uses its own event loop.
Expand Down Expand Up @@ -216,6 +238,11 @@ void MainWindow::remoteConnected() {
void MainWindow::errorHandler(QString err) {
QMessageBox::warning(this, tr("Error"), err);
connected = false;

// Reset UI elements.
ui->actionConnect->setEnabled(true);
ui->actionDisconnect->setEnabled(false);
ui->mainToolBar->setDisabled(true);
}


Expand Down
2 changes: 1 addition & 1 deletion mosquitto/lib/mosquitto.c
Expand Up @@ -26,7 +26,7 @@ and the Eclipse Distribution License is available at
#else
#include <winsock2.h>
#include <windows.h>
typedef int ssize_t;
//typedef int ssize_t;
#endif

#include "mosquitto.h"
Expand Down
2 changes: 1 addition & 1 deletion mosquitto/lib/net_mosq.h
Expand Up @@ -20,7 +20,7 @@ and the Eclipse Distribution License is available at
#include <unistd.h>
#else
#include <winsock2.h>
typedef int ssize_t;
//typedef int ssize_t;
#endif

#include "mosquitto_internal.h"
Expand Down
24 changes: 23 additions & 1 deletion mqttlistener.cpp
Expand Up @@ -51,9 +51,31 @@ bool MqttListener::setTLS(string &ca, string &cert, string &key) {
}


// --- SET PASSWORD ---
// Set the username and password for this MQTT connection.
bool MqttListener::setPassword(string &username, string &password) {
int res = username_pw_set(username.c_str(), password.c_str());
if (res != MOSQ_ERR_SUCCESS) {
if (res == MOSQ_ERR_INVAL) {
cerr << "Invalid input parameters for Mosquitto PW.\n";
}
else if (res == MOSQ_ERR_NOMEM) {
cerr << "Out of memory on Mosquitto PW.\n";
}
else {
cerr << "Unknown Mosquitto PW error.\n";
}

return false;
}

return true;
}


// --- CONNECT BROKER ---
void MqttListener::connectBroker() {
cout << "Connecting to broker..." << host.toStdString() << ":" << port << "\n";
cout << "Connecting to broker: " << host.toStdString() << ":" << port << "\n";
int keepalive = 60;
mosqpp::mosquittopp::connect(host.toStdString().c_str(), port, keepalive);

Expand Down
1 change: 1 addition & 0 deletions mqttlistener.h
Expand Up @@ -40,6 +40,7 @@ class MqttListener : public QObject, mosqpp::mosquittopp {
~MqttListener();

bool setTLS(string &ca, string &cert, string &key);
bool setPassword(string &username, string &password);
void on_connect(int rc);
void on_message(const struct mosquitto_message* message);
void on_subscribe(int mid, int qos_count, const int* granted_qos);
Expand Down

0 comments on commit 937a5d9

Please sign in to comment.