Skip to content

Commit

Permalink
Handle Device Verification related to_device messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan2k1 committed Jun 10, 2020
1 parent e0df0cd commit 6479a12
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 9 deletions.
17 changes: 9 additions & 8 deletions resources/qml/UserProfile.qml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ ApplicationWindow{
}
}

Component {
id: deviceVerificationDialog
DeviceVerification {}
}
DeviceVerificationFlow {
id: deviceVerificationFlow
}

background: Item{
id: userProfileItem
width: userProfileDialog.width
Expand Down Expand Up @@ -98,20 +106,13 @@ 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});
{flow: deviceVerificationFlow,sender: true});
deviceVerificationFlow.userId = user_data.userId
deviceVerificationFlow.deviceId = model.deviceID
dialog.show();
Expand Down
1 change: 1 addition & 0 deletions src/ChatPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "Cache.h"
#include "Cache_p.h"
#include "ChatPage.h"
#include "DeviceVerificationFlow.h"
#include "Logging.h"
#include "MainWindow.h"
#include "MatrixClient.h"
Expand Down
12 changes: 12 additions & 0 deletions src/ChatPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <atomic>
#include <optional>
#include <stack>
#include <variant>

#include <mtx/common.hpp>
Expand Down Expand Up @@ -155,6 +156,17 @@ public slots:
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);
Expand Down
99 changes: 99 additions & 0 deletions src/DeviceVerificationFlow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "DeviceVerificationFlow.h"
#include "ChatPage.h"

#include "Logging.h"
#include <QDateTime>
Expand All @@ -8,14 +9,67 @@

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);
}

Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/DeviceVerificationFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -56,3 +60,4 @@ public slots:
std::string transaction_id;
mtx::identifiers::User toClient;
};
Q_DECLARE_METATYPE(mtx::events::collections::DeviceEvents)
23 changes: 22 additions & 1 deletion src/Olm.cpp
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;

Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 6479a12

Please sign in to comment.