From d59d4b183c3484a978f197ad27e2491138775c92 Mon Sep 17 00:00:00 2001 From: Maurice Kalinowski Date: Wed, 24 Oct 2018 16:07:05 +0200 Subject: SensorTag: Prefer loading MQTT configuration from file To avoid accidently pushing credentials, they should not be patched into the header, but rather be placed in to a file co-located to the executable. Change-Id: I03950107f39a88b0f1c7deb49ff16d25ddf9df4d Reviewed-by: Oliver Wolff --- tradeshow/iot-sensortag/mqttdataproviderpool.cpp | 69 ++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) (limited to 'tradeshow/iot-sensortag/mqttdataproviderpool.cpp') diff --git a/tradeshow/iot-sensortag/mqttdataproviderpool.cpp b/tradeshow/iot-sensortag/mqttdataproviderpool.cpp index 24ce47c..55cd0f7 100644 --- a/tradeshow/iot-sensortag/mqttdataproviderpool.cpp +++ b/tradeshow/iot-sensortag/mqttdataproviderpool.cpp @@ -52,6 +52,8 @@ #include "mqttdataprovider.h" #include +#include +#include MqttDataProviderPool::MqttDataProviderPool(QObject *parent) : DataProviderPool(parent) @@ -66,10 +68,10 @@ void MqttDataProviderPool::startScanning() emit providersUpdated(); emit dataProvidersChanged(); - m_client->setHostname(QLatin1String(MQTT_BROKER)); - m_client->setPort(MQTT_PORT); - m_client->setUsername(QByteArray(MQTT_USERNAME)); - m_client->setPassword(QByteArray(MQTT_PASSWORD)); + m_client->setHostname(MqttCredentials::getBroker()); + m_client->setPort((quint16)MqttCredentials::getPort()); + m_client->setUsername(MqttCredentials::getUsername()); + m_client->setPassword(MqttCredentials::getPassword()); connect(m_client, &QMqttClient::connected, [this]() { auto sub = m_client->subscribe(QLatin1String("sensors/active")); @@ -121,3 +123,62 @@ void MqttDataProviderPool::deviceUpdate(const QMqttMessage &msg) emit dataProvidersChanged(); } } + +namespace { + static QString gBroker; + static int gPort{0}; + static QString gUser; + static QString gPassword; + bool resolveCredentials() { + static bool resolved = false; + if (resolved) + return true; + + QFile f(QLatin1String("broker.json")); + if (!f.open(QIODevice::ReadOnly)) { + qDebug() << "Could not find or open broker.json"; + return false; + } + const QByteArray data = f.readAll(); + if (data.isEmpty()) { + qDebug() << "broker.json file empty"; + return false; + } + QJsonDocument doc = QJsonDocument::fromJson(data); + if (doc.isNull()) { + qDebug() << "broker.json does not contain valid data"; + return false; + } + gBroker = doc["broker"].toString(); + gPort = doc["port"].toInt(); + gUser = doc["user"].toString(); + gPassword = doc["password"].toString(); + qDebug() << "broker.json parsed. Using broker:" << gBroker << ":" << gPort; + resolved = true; + return true; + } +} + +QString MqttCredentials::getBroker() +{ + resolveCredentials(); + return gBroker; +} + +int MqttCredentials::getPort() +{ + resolveCredentials(); + return gPort; +} + +QString MqttCredentials::getUsername() +{ + resolveCredentials(); + return gUser; +} + +QString MqttCredentials::getPassword() +{ + resolveCredentials(); + return gPassword; +} -- cgit v1.2.3