Use a more unique nonce for the remote connection

This commit is contained in:
Michael Zanetti 2020-01-08 13:24:12 +01:00
parent 583cd927b8
commit e7584509c3
4 changed files with 10 additions and 8 deletions

View File

@ -846,7 +846,7 @@ bool AWSClient::tokensExpired() const
return (m_accessTokenExpiry.addSecs(-10) < QDateTime::currentDateTime()) || (m_sessionTokenExpiry.addSecs(-10) < QDateTime::currentDateTime());
}
bool AWSClient::postToMQTT(const QString &coreId, const QString &timestamp, QObject* sender, std::function<void (bool)> callback)
bool AWSClient::postToMQTT(const QString &coreId, const QString &nonce, QObject* sender, std::function<void (bool)> callback)
{
if (!isLoggedIn()) {
qWarning() << "Cannot post to MQTT. Not logged in to AWS";
@ -855,7 +855,7 @@ bool AWSClient::postToMQTT(const QString &coreId, const QString &timestamp, QObj
if (tokensExpired()) {
qDebug() << "Cannot post to MQTT. Need to refresh the tokens first";
refreshAccessToken();
QueuedCall::enqueue(m_callQueue, QueuedCall("postToMQTT", coreId, timestamp, sender, callback));
QueuedCall::enqueue(m_callQueue, QueuedCall("postToMQTT", coreId, nonce, sender, callback));
return true; // So far it looks we're doing ok... let's return true
}
QString topic = QString("%1/%2/proxy").arg(coreId).arg(QString(m_identityId));
@ -873,7 +873,9 @@ bool AWSClient::postToMQTT(const QString &coreId, const QString &timestamp, QObj
QVariantMap params;
params.insert("token", m_idToken);
params.insert("timestamp", timestamp);
params.insert("nonce", nonce);
// FIXME: Old (nymea < 0.18) protocol spec had "timestamp" instead of "nonce", keeping it for backwards compatibility for a bit
params.insert("timestamp", nonce);
QByteArray payload = QJsonDocument::fromVariant(params).toJson(QJsonDocument::Compact);

View File

@ -119,7 +119,7 @@ public:
Q_INVOKABLE void fetchDevices();
Q_INVOKABLE bool postToMQTT(const QString &coreId, const QString &timestamp, QObject* sender, std::function<void(bool)> callback);
Q_INVOKABLE bool postToMQTT(const QString &coreId, const QString &nonce, QObject* sender, std::function<void(bool)> callback);
Q_INVOKABLE void getId();
Q_INVOKABLE void registerPushNotificationEndpoint(const QString &registrationId, const QString &deviceDisplayName, const QString mobileDeviceId, const QString &mobileDeviceManufacturer, const QString &mobileDeviceModel);

View File

@ -30,7 +30,7 @@ CloudTransport::CloudTransport(AWSClient *awsClient, QObject *parent):
QObject::connect(m_remoteproxyConnection, &RemoteProxyConnection::ready, this,[this]() {
qDebug() << "Proxy ready. Authenticating channel.";
m_remoteproxyConnection->authenticate(m_awsClient->idToken(), QString::number(m_timestamp.toMSecsSinceEpoch()));
m_remoteproxyConnection->authenticate(m_awsClient->idToken(), m_nonce);
});
QObject::connect(m_remoteproxyConnection, &RemoteProxyConnection::dataReady, this, [this](const QByteArray &data) {
emit dataReady(data);
@ -52,8 +52,8 @@ bool CloudTransport::connect(const QUrl &url)
qDebug() << "Connecting to" << url;
m_url = url;
m_timestamp = QDateTime::currentDateTime();
bool postResult = m_awsClient->postToMQTT(url.host(), QString::number(m_timestamp.toMSecsSinceEpoch()), QPointer<QObject>(this), [this](bool success) {
m_nonce = QUuid::createUuid().toString();
bool postResult = m_awsClient->postToMQTT(url.host(), m_nonce, QPointer<QObject>(this), [this](bool success) {
if (success) {
qDebug() << "MQTT Post done. Connecting to remote proxy";
m_remoteproxyConnection->connectServer(QUrl("wss://remoteproxy.nymea.io"));

View File

@ -36,7 +36,7 @@ private:
QUrl m_url;
AWSClient *m_awsClient = nullptr;
remoteproxyclient::RemoteProxyConnection *m_remoteproxyConnection = nullptr;
QDateTime m_timestamp;
QString m_nonce;
};
#endif // CLOUDTRANSPORT_H