aboutsummaryrefslogtreecommitdiff
path: root/lib/keyverificationsession.cpp
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-08-25 19:28:10 +0200
committerAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-08-25 19:48:33 +0200
commit1c94d1b41eb352b31b2dc915fea95e26f6138284 (patch)
treeac3aa4c3b067fe1d004cad3f35443047ea961a0a /lib/keyverificationsession.cpp
parent82f4efb0227e7e22e831733fae3952818b063ac2 (diff)
downloadlibquotient-1c94d1b41eb352b31b2dc915fea95e26f6138284.tar.gz
libquotient-1c94d1b41eb352b31b2dc915fea95e26f6138284.zip
KeyVerificationSession: cleanup
- Use std::chrono for the timeout (it's more readable and less ambiguous) and make it a local variable - Only pass a Connection object once to constructors - Ensure buildability even without E2EE (key verification is disabled in that case) - Reorder #includes - Other cleanup following clang-tidy warnings
Diffstat (limited to 'lib/keyverificationsession.cpp')
-rw-r--r--lib/keyverificationsession.cpp59
1 files changed, 31 insertions, 28 deletions
diff --git a/lib/keyverificationsession.cpp b/lib/keyverificationsession.cpp
index d889b465..1ee489ea 100644
--- a/lib/keyverificationsession.cpp
+++ b/lib/keyverificationsession.cpp
@@ -1,53 +1,59 @@
// SPDX-FileCopyrightText: 2022 Tobias Fella <fella@posteo.de>
// SPDX-License-Identifier: LGPL-2.1-or-later
-#include "connection.h"
#include "keyverificationsession.h"
-#include "olm/sas.h"
+
+#include "connection.h"
+#include "database.h"
#include "e2ee/qolmaccount.h"
#include "e2ee/qolmutils.h"
+#include "olm/sas.h"
+
#include "events/event.h"
+
#include <QtCore/QCryptographicHash>
-#include <QtCore/QUuid>
#include <QtCore/QTimer>
-#include "database.h"
+#include <QtCore/QUuid>
+
+#include <chrono>
using namespace Quotient;
+using namespace std::chrono;
-KeyVerificationSession::KeyVerificationSession(const QString& remoteUserId, const KeyVerificationRequestEvent& event, Connection *connection, bool encrypted, QObject* parent)
- : QObject(parent)
- , m_remoteUserId(remoteUserId)
+KeyVerificationSession::KeyVerificationSession(
+ QString remoteUserId, const KeyVerificationRequestEvent& event,
+ Connection* connection, bool encrypted)
+ : QObject(connection)
+ , m_remoteUserId(std::move(remoteUserId))
, m_remoteDeviceId(event.fromDevice())
, m_transactionId(event.transactionId())
, m_connection(connection)
, m_encrypted(encrypted)
, m_remoteSupportedMethods(event.methods())
{
- auto timeoutTime = std::min(event.timestamp().addSecs(600),
- QDateTime::currentDateTime().addSecs(120));
- m_timeout =
- timeoutTime.toMSecsSinceEpoch() - QDateTime::currentMSecsSinceEpoch();
- if (m_timeout <= 5000) {
- return;
- }
- init();
- setState(INCOMING);
+ const auto& currentTime = QDateTime::currentDateTime();
+ const auto timeoutTime =
+ std::min(event.timestamp().addSecs(600), currentTime.addSecs(120));
+ const milliseconds timeout{ currentTime.msecsTo(timeoutTime) };
+ if (timeout > 5s)
+ init(timeout);
+ // Otherwise don't even bother starting up
}
-KeyVerificationSession::KeyVerificationSession(const QString& userId, const QString& deviceId, Connection* connection, QObject* parent)
- : QObject(parent)
- , m_remoteUserId(userId)
- , m_remoteDeviceId(deviceId)
+KeyVerificationSession::KeyVerificationSession(QString userId, QString deviceId,
+ Connection* connection)
+ : QObject(connection)
+ , m_remoteUserId(std::move(userId))
+ , m_remoteDeviceId(std::move(deviceId))
, m_transactionId(QUuid::createUuid().toString())
, m_connection(connection)
, m_encrypted(false)
{
- m_timeout = 600000;
- init();
+ init(600s);
QMetaObject::invokeMethod(this, &KeyVerificationSession::sendRequest);
}
-void KeyVerificationSession::init()
+void KeyVerificationSession::init(milliseconds timeout)
{
connect(m_connection, &Connection::incomingKeyVerificationReady, this, [this](const KeyVerificationReadyEvent& event) {
if (event.transactionId() == m_transactionId && event.fromDevice() == m_remoteDeviceId) {
@@ -85,10 +91,7 @@ void KeyVerificationSession::init()
}
});
- QTimer::singleShot(m_timeout, this, [this] {
- cancelVerification(TIMEOUT);
- });
-
+ QTimer::singleShot(timeout, this, [this] { cancelVerification(TIMEOUT); });
m_sas = olm_sas(new uint8_t[olm_sas_size()]);
auto randomSize = olm_create_sas_random_length(m_sas);
@@ -380,7 +383,7 @@ void KeyVerificationSession::handleMac(const KeyVerificationMacEvent& event)
}
}
-void KeyVerificationSession::handleDone(const KeyVerificationDoneEvent& event)
+void KeyVerificationSession::handleDone(const KeyVerificationDoneEvent&)
{
if (state() != DONE) {
cancelVerification(UNEXPECTED_MESSAGE);