2020-03-13 23:05:18 +03:00
|
|
|
#include "DeviceVerificationFlow.h"
|
|
|
|
|
2020-06-04 16:44:15 +03:00
|
|
|
#include <MatrixClient.h>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug> // only for debugging
|
2020-03-13 23:05:18 +03:00
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
static constexpr int TIMEOUT = 2 * 60 * 1000; // 2 minutes
|
|
|
|
|
|
|
|
DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
|
|
|
|
{
|
|
|
|
timeout = new QTimer(this);
|
|
|
|
timeout->setSingleShot(true);
|
|
|
|
connect(timeout, &QTimer::timeout, this, [this]() {
|
|
|
|
emit timedout();
|
|
|
|
this->deleteLater();
|
|
|
|
});
|
|
|
|
timeout->start(TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2020-06-04 16:44:15 +03:00
|
|
|
std::string
|
|
|
|
DeviceVerificationFlow::generate_txn_id()
|
|
|
|
{
|
|
|
|
this->transaction_id = mtx::client::utils::random_token(32, false);
|
|
|
|
return this->transaction_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! accepts a verification
|
2020-03-13 23:05:18 +03:00
|
|
|
void
|
|
|
|
DeviceVerificationFlow::acceptVerificationRequest()
|
|
|
|
{
|
2020-06-04 16:44:15 +03:00
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationAccept> body;
|
|
|
|
mtx::events::msg::KeyVerificationAccept req;
|
|
|
|
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
req.method = mtx::events::msg::VerificationMethods::SASv1;
|
|
|
|
req.key_agreement_protocol = "";
|
|
|
|
req.hash = "";
|
|
|
|
req.message_authentication_code = "";
|
|
|
|
// req.short_authentication_string = "";
|
|
|
|
req.commitment = "";
|
|
|
|
|
2020-03-13 23:05:18 +03:00
|
|
|
emit verificationRequestAccepted(rand() % 2 ? Emoji : Decimal);
|
2020-06-04 16:44:15 +03:00
|
|
|
|
|
|
|
// Yet to add send to_device message
|
|
|
|
}
|
|
|
|
//! starts the verification flow
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::startVerificationRequest()
|
|
|
|
{
|
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationAccept> body;
|
|
|
|
mtx::events::msg::KeyVerificationAccept req;
|
|
|
|
|
|
|
|
// req.from_device = "";
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
req.method = mtx::events::msg::VerificationMethods::SASv1;
|
|
|
|
req.key_agreement_protocol = {};
|
|
|
|
// req.hashes = {};
|
|
|
|
req.message_authentication_code = {};
|
|
|
|
// req.short_authentication_string = "";
|
|
|
|
|
|
|
|
// Yet to add send to_device message
|
|
|
|
}
|
|
|
|
//! sends a verification request
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::sendVerificationRequest()
|
|
|
|
{
|
|
|
|
QDateTime CurrentTime = QDateTime::currentDateTimeUtc();
|
|
|
|
|
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationRequest> body;
|
|
|
|
mtx::events::msg::KeyVerificationRequest req;
|
|
|
|
|
|
|
|
req.from_device = "";
|
|
|
|
req.transaction_id = generate_txn_id();
|
|
|
|
req.methods.resize(1);
|
|
|
|
req.methods[0] = mtx::events::msg::VerificationMethods::SASv1;
|
|
|
|
req.timestamp = (uint64_t)CurrentTime.toTime_t();
|
|
|
|
|
|
|
|
// Yet to add send to_device message
|
2020-03-13 23:05:18 +03:00
|
|
|
}
|
|
|
|
//! cancels a verification flow
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::cancelVerification()
|
|
|
|
{
|
2020-06-04 16:44:15 +03:00
|
|
|
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationCancel> body;
|
|
|
|
mtx::events::msg::KeyVerificationCancel req;
|
|
|
|
|
|
|
|
req.transaction_id = this->transaction_id;
|
|
|
|
req.reason = "";
|
|
|
|
req.code = "";
|
|
|
|
|
2020-03-13 23:05:18 +03:00
|
|
|
this->deleteLater();
|
2020-06-04 16:44:15 +03:00
|
|
|
|
|
|
|
// Yet to add send to_device message
|
2020-03-13 23:05:18 +03:00
|
|
|
}
|
|
|
|
//! Completes the verification flow
|
|
|
|
void
|
|
|
|
DeviceVerificationFlow::acceptDevice()
|
|
|
|
{
|
|
|
|
emit deviceVerified();
|
|
|
|
this->deleteLater();
|
2020-06-04 16:44:15 +03:00
|
|
|
|
|
|
|
// Yet to add send to_device message
|
2020-03-13 23:05:18 +03:00
|
|
|
}
|