Handle Device Verification related to_device messages

This commit is contained in:
Chethan2k1 2020-06-09 22:06:41 +05:30 committed by CH Chethan Reddy
parent cd5dd0e39b
commit 1eb162cb6f
7 changed files with 153 additions and 14 deletions

View File

@ -29,6 +29,14 @@ ApplicationWindow{
}
}
Component {
id: deviceVerificationDialog
DeviceVerification {}
}
DeviceVerificationFlow {
id: deviceVerificationFlow
}
background: Item{
id: userProfileItem
width: userProfileDialog.width
@ -98,22 +106,15 @@ ApplicationWindow{
Layout.alignment: Qt.AlignRight
text: displayName
}
Component {
id: deviceVerificationDialog
DeviceVerification {}
}
DeviceVerificationFlow {
id: deviceVerificationFlow
}
}
Button{
id: verifyButton
text:"Verify"
onClicked: {
var dialog = deviceVerificationDialog.createObject(userProfileDialog,
{flow: deviceVerificationFlow,sender: false});
deviceVerificationFlow.userId = user_data.userId
deviceVerificationFlow.deviceId = model.deviceID
var dialog = deviceVerificationDialog.createObject(userProfileDialog,
{flow: deviceVerificationFlow,sender: true});
dialog.show();
}
contentItem: Text {

View File

@ -83,7 +83,7 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignRight
text: "Start verification"
onClicked: { stack.replace(awaitingVerificationRequestAccept); flow.sendVerificationRequest(); }
onClicked: { stack.replace(awaitingVerificationRequestAccept); flow.startVerificationRequest(); }
}
}
}

View File

@ -27,6 +27,7 @@
#include "Cache_p.h"
#include "ChatPage.h"
#include "EventAccessors.h"
#include "DeviceVerificationFlow.h"
#include "Logging.h"
#include "MainWindow.h"
#include "MatrixClient.h"

View File

@ -19,6 +19,7 @@
#include <atomic>
#include <optional>
#include <stack>
#include <variant>
#include <mtx/common.hpp>
@ -164,6 +165,17 @@ signals:
void themeChanged();
void decryptSidebarChanged();
//! Signals for device verificaiton
void recievedDeviceVerificationAccept(
const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationRequest(
const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationCancel(
const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationKey(const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationMac(const mtx::events::collections::DeviceEvents &message);
void recievedDeviceVerificationStart(const mtx::events::collections::DeviceEvents &message);
private slots:
void showUnreadMessageNotification(int count);
void updateTopBarAvatar(const QString &roomid, const QString &img);

View File

@ -1,4 +1,5 @@
#include "DeviceVerificationFlow.h"
#include "ChatPage.h"
#include "Logging.h"
#include <QDateTime>
@ -8,22 +9,73 @@
static constexpr int TIMEOUT = 2 * 60 * 1000; // 2 minutes
namespace msgs = mtx::events::msg;
DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
{
qRegisterMetaType<mtx::events::collections::DeviceEvents>();
timeout = new QTimer(this);
timeout->setSingleShot(true);
connect(timeout, &QTimer::timeout, this, [this]() {
emit timedout();
this->deleteLater();
});
connect(ChatPage::instance(),
&ChatPage::recievedDeviceVerificationAccept,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationAccept>>(message);
if (msg.content.transaction_id == this->transaction_id) {
std::cout << "Recieved Event Accept" << std::endl;
}
});
connect(ChatPage::instance(),
&ChatPage::recievedDeviceVerificationRequest,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationRequest>>(message);
if (msg.content.transaction_id == this->transaction_id) {
std::cout << "Recieved Event Request" << std::endl;
}
});
connect(ChatPage::instance(),
&ChatPage::recievedDeviceVerificationCancel,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationCancel>>(message);
if (msg.content.transaction_id == this->transaction_id) {
std::cout << "Recieved Event Cancel" << std::endl;
}
});
connect(ChatPage::instance(),
&ChatPage::recievedDeviceVerificationKey,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationKey>>(message);
if (msg.content.transaction_id == this->transaction_id) {
std::cout << "Recieved Event Key" << std::endl;
}
});
connect(ChatPage::instance(),
&ChatPage::recievedDeviceVerificationMac,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationMac>>(message);
if (msg.content.transaction_id == this->transaction_id) {
std::cout << "Recieved Event Mac" << std::endl;
}
});
timeout->start(TIMEOUT);
}
QString
DeviceVerificationFlow::getUserId()
{
toClient = mtx::identifiers::parse<mtx::identifiers::User>((this->userId).toStdString());
std::cout << http::client()->device_id() << std::endl;
return this->userId;
}
@ -43,6 +95,7 @@ void
DeviceVerificationFlow::setUserId(QString userID)
{
this->userId = userID;
this->toClient = mtx::identifiers::parse<mtx::identifiers::User>(userID.toStdString());
}
void
@ -101,7 +154,8 @@ DeviceVerificationFlow::startVerificationRequest()
req.hashes = {};
req.message_authentication_codes = {};
// req.short_authentication_string = "";
qDebug()<<"Inside Start Verification";
qDebug()<<this->userId;
body[this->toClient][this->deviceId.toStdString()] = req;
http::client()
@ -168,6 +222,51 @@ DeviceVerificationFlow::cancelVerification()
this->deleteLater();
});
}
//! sends the verification key
void
DeviceVerificationFlow::sendVerificationKey()
{
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationKey> body;
mtx::events::msg::KeyVerificationKey req;
req.key = "";
req.transaction_id = this->transaction_id;
body[this->toClient][deviceId.toStdString()] = req;
http::client()
->send_to_device<mtx::events::msg::KeyVerificationKey,
mtx::events::EventType::KeyVerificationKey>(
"m.key.verification.cancel", body, [](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to send verification key: {} {}",
err->matrix_error.error,
static_cast<int>(err->status_code));
});
}
//! sends the mac of the keys
void
DeviceVerificationFlow::sendVerificationMac()
{
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationMac> body;
mtx::events::msg::KeyVerificationMac req;
req.transaction_id = this->transaction_id;
// req.mac = "";
req.keys = "";
body[this->toClient][deviceId.toStdString()] = req;
http::client()
->send_to_device<mtx::events::msg::KeyVerificationMac,
mtx::events::EventType::KeyVerificationMac>(
"m.key.verification.cancel", body, [](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to send verification MAC: {} {}",
err->matrix_error.error,
static_cast<int>(err->status_code));
});
}
//! Completes the verification flow
void
DeviceVerificationFlow::acceptDevice()

View File

@ -38,6 +38,10 @@ public slots:
void startVerificationRequest();
//! cancels a verification flow
void cancelVerification();
//! sends the verification key
void sendVerificationKey();
//! sends the mac of the keys
void sendVerificationMac();
//! Completes the verification flow
void acceptDevice();
@ -56,3 +60,4 @@ private:
std::string transaction_id;
mtx::identifiers::User toClient;
};
Q_DECLARE_METATYPE(mtx::events::collections::DeviceEvents)

View File

@ -1,11 +1,15 @@
#include <QObject>
#include <variant>
#include "Olm.h"
#include "Cache.h"
#include "ChatPage.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "Utils.h"
#include <DeviceVerificationFlow.h>
#include <iostream> // only for debugging
static const std::string STORAGE_SECRET_KEY("secret");
constexpr auto MEGOLM_ALGO = "m.megolm.v1.aes-sha2";
@ -27,7 +31,6 @@ handle_to_device_messages(const std::vector<mtx::events::collections::DeviceEven
{
if (msgs.empty())
return;
nhlog::crypto()->info("received {} to_device messages", msgs.size());
nlohmann::json j_msg;
@ -74,6 +77,24 @@ handle_to_device_messages(const std::vector<mtx::events::collections::DeviceEven
e.what(),
j_msg.dump(2));
}
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationAccept)) {
ChatPage::instance()->recievedDeviceVerificationAccept(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationRequest)) {
ChatPage::instance()->recievedDeviceVerificationRequest(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationCancel)) {
ChatPage::instance()->recievedDeviceVerificationCancel(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationKey)) {
ChatPage::instance()->recievedDeviceVerificationKey(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationMac)) {
ChatPage::instance()->recievedDeviceVerificationMac(msg);
std::cout << j_msg.dump(2) << std::endl;
} else if (msg_type == to_string(mtx::events::EventType::KeyVerificationStart)) {
ChatPage::instance()->recievedDeviceVerificationStart(msg);
std::cout << j_msg.dump(2) << std::endl;
} else {
nhlog::crypto()->warn("unhandled event: {}", j_msg.dump(2));
}