Skip to content

Commit

Permalink
Add DeviceVerificationList to keep track of all flows and Popup on
Browse files Browse the repository at this point in the history
recieving start or request
  • Loading branch information
Chethan2k1 committed Jun 18, 2020
1 parent bbaf15b commit 8da77bc
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 68 deletions.
12 changes: 6 additions & 6 deletions resources/qml/Avatar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ Rectangle {
height: avatar.height / 6
width: height
radius: settings.avatarCircles ? height / 2 : height / 4
color: switch (timelineManager.userPresence(userid)) {
case "online": return "#00cc66"
case "unavailable": return "#ff9933"
case "offline": return "#a82353"
default: "transparent"
}
// color: switch (timelineManager.userPresence(userid)) {
// case "online": return "#00cc66"
// case "unavailable": return "#ff9933"
// case "offline": return "#a82353"
// default: "transparent"
// }
}

color: colors.base
Expand Down
18 changes: 10 additions & 8 deletions resources/qml/TimelineView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,22 @@ Page {
id: deviceVerificationDialog
DeviceVerification {}
}
Component{
id: deviceVerificationFlow
DeviceVerificationFlow {}
}
Connections {
target: timelineManager
onDeviceVerificationRequest: {
var dialog = deviceVerificationDialog.createObject(timelineRoot, {flow: deviceVerificationFlow});
onNewDeviceVerificationRequest: {
var newFlow = deviceVerificationFlow.createObject(timelineRoot,
{userId : userId,sender: false,deviceId : deviceId,tranId:transactionId});
deviceVerificationList.add(newFlow.tranId);
var dialog = deviceVerificationDialog.createObject(timelineRoot,
{flow: newFlow,sender: false});
dialog.show();
}
}

Button {
text: "test device verification"
onClicked: timelineManager.startDummyVerification()
z: 5
}

Label {
visible: !timelineManager.timeline && !timelineManager.isInitialSync
anchors.centerIn: parent
Expand Down
10 changes: 6 additions & 4 deletions resources/qml/UserProfile.qml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ ApplicationWindow{
id: deviceVerificationDialog
DeviceVerification {}
}
DeviceVerificationFlow {
Component{
id: deviceVerificationFlow
DeviceVerificationFlow {}
}

background: Item{
Expand Down Expand Up @@ -111,10 +112,11 @@ ApplicationWindow{
id: verifyButton
text:"Verify"
onClicked: {
deviceVerificationFlow.userId = user_data.userId
deviceVerificationFlow.deviceId = model.deviceID
var newFlow = deviceVerificationFlow.createObject(userProfileDialog,
{userId : user_data.userId,sender: true,deviceId : model.deviceID});
deviceVerificationList.add(newFlow.tranId);
var dialog = deviceVerificationDialog.createObject(userProfileDialog,
{flow: deviceVerificationFlow,sender: true});
{flow: newFlow,sender: true});
dialog.show();
}
contentItem: Text {
Expand Down
60 changes: 51 additions & 9 deletions resources/qml/device-verification/DeviceVerification.qml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignLeft
text: "Cancel"
onClicked: { dialog.close(); flow.cancelVerification(); }
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Layout.fillWidth: true
Expand Down Expand Up @@ -127,7 +132,12 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignLeft
text: "Deny"
onClicked: { dialog.close(); flow.cancelVerification(); }
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Layout.fillWidth: true
Expand Down Expand Up @@ -166,7 +176,12 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignLeft
text: "Cancel"
onClicked: { dialog.close(); flow.cancelVerification(); }
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Layout.fillWidth: true
Expand Down Expand Up @@ -212,7 +227,12 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignLeft
text: "They do not match!"
onClicked: { dialog.close(); flow.cancelVerification(); }
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Layout.fillWidth: true
Expand Down Expand Up @@ -346,7 +366,12 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignLeft
text: "They do not match!"
onClicked: { dialog.close(); flow.cancelVerification(); }
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Layout.fillWidth: true
Expand Down Expand Up @@ -385,7 +410,12 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignLeft
text: "Cancel"
onClicked: { dialog.close(); flow.cancelVerification(); }
onClicked: {
dialog.close();
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Layout.fillWidth: true
Expand Down Expand Up @@ -419,7 +449,11 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignRight
text: "Close"
onClicked: dialog.close()
onClicked: {
dialog.close()
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
}
}
Expand Down Expand Up @@ -450,7 +484,11 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignRight
text: "Close"
onClicked: dialog.close()
onClicked: {
dialog.close()
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
}
}
Expand Down Expand Up @@ -481,7 +519,11 @@ ApplicationWindow {
Button {
Layout.alignment: Qt.AlignRight
text: "Close"
onClicked: dialog.close()
onClicked: {
dialog.close()
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ChatPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#include "Cache.h"
#include "Cache_p.h"
#include "ChatPage.h"
#include "EventAccessors.h"
#include "DeviceVerificationFlow.h"
#include "EventAccessors.h"
#include "Logging.h"
#include "MainWindow.h"
#include "MatrixClient.h"
Expand Down
69 changes: 47 additions & 22 deletions src/DeviceVerificationFlow.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "DeviceVerificationFlow.h"
#include "ChatPage.h"

#include "Logging.h"

#include <QDateTime>
#include <QDebug> // only for debugging
#include <QTimer>
Expand All @@ -13,9 +13,10 @@ namespace msgs = mtx::events::msg;

DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
{
qRegisterMetaType<mtx::events::collections::DeviceEvents>();
timeout = new QTimer(this);
timeout->setSingleShot(true);
if (this->sender == true)
this->transaction_id = http::client()->generate_txn_id();
connect(timeout, &QTimer::timeout, this, [this]() {
emit timedout();
this->deleteLater();
Expand Down Expand Up @@ -73,6 +74,12 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
timeout->start(TIMEOUT);
}

QString
DeviceVerificationFlow::getTransactionId()
{
return QString::fromStdString(this->transaction_id);
}

QString
DeviceVerificationFlow::getUserId()
{
Expand All @@ -91,10 +98,22 @@ DeviceVerificationFlow::getMethod()
return this->method;
}

bool
DeviceVerificationFlow::getSender()
{
return this->sender;
}

void
DeviceVerificationFlow::setTransactionId(QString transaction_id_)
{
this->transaction_id = transaction_id_.toStdString();
}

void
DeviceVerificationFlow::setUserId(QString userID)
{
this->userId = userID;
this->userId = userID;
this->toClient = mtx::identifiers::parse<mtx::identifiers::User>(userID.toStdString());
}

Expand All @@ -110,6 +129,12 @@ DeviceVerificationFlow::setMethod(DeviceVerificationFlow::Method method_)
this->method = method_;
}

void
DeviceVerificationFlow::setSender(bool sender_)
{
this->sender = sender_;
}

//! accepts a verification
void
DeviceVerificationFlow::acceptVerificationRequest()
Expand All @@ -119,11 +144,12 @@ DeviceVerificationFlow::acceptVerificationRequest()

req.transaction_id = this->transaction_id;
req.method = mtx::events::msg::VerificationMethods::SASv1;
req.key_agreement_protocol = "curve25519";
req.key_agreement_protocol = "curve25519-hkdf-sha256";
req.hash = "sha256";
req.message_authentication_code = "";
// req.short_authentication_string = "";
req.commitment = "";
req.message_authentication_code = "hkdf-hmac-sha256";
req.short_authentication_string = {mtx::events::msg::SASMethods::Decimal,
mtx::events::msg::SASMethods::Emoji};
req.commitment = "";

emit this->verificationRequestAccepted(this->method);

Expand All @@ -132,12 +158,12 @@ DeviceVerificationFlow::acceptVerificationRequest()
http::client()
->send_to_device<mtx::events::msg::KeyVerificationAccept,
mtx::events::EventType::KeyVerificationAccept>(
"m.key.verification.accept", body, [](mtx::http::RequestErr err) {
this->transaction_id, body, [this](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to accept verification request: {} {}",
err->matrix_error.error,
static_cast<int>(err->status_code));
// emit this->verificationRequestAccepted(rand() % 2 ? Emoji : Decimal);
emit this->verificationRequestAccepted(rand() % 2 ? Emoji : Decimal);
});
}
//! starts the verification flow
Expand All @@ -150,22 +176,23 @@ DeviceVerificationFlow::startVerificationRequest()
req.from_device = http::client()->device_id();
req.transaction_id = this->transaction_id;
req.method = mtx::events::msg::VerificationMethods::SASv1;
req.key_agreement_protocols = {};
req.hashes = {};
req.message_authentication_codes = {};
// req.short_authentication_string = "";
qDebug()<<"Inside Start Verification";
qDebug()<<this->userId;
req.key_agreement_protocols = {"curve25519-hkdf-sha256"};
req.hashes = {"sha256"};
req.message_authentication_codes = {"hkdf-hmac-sha256", "hmac-sha256"};
req.short_authentication_string = {mtx::events::msg::SASMethods::Decimal,
mtx::events::msg::SASMethods::Emoji};

body[this->toClient][this->deviceId.toStdString()] = req;

http::client()
->send_to_device<mtx::events::msg::KeyVerificationStart,
mtx::events::EventType::KeyVerificationStart>(
"m.key.verification.start", body, [](mtx::http::RequestErr err) {
this->transaction_id, body, [body](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to start verification request: {} {}",
err->matrix_error.error,
static_cast<int>(err->status_code));
std::cout << nlohmann::json(body).dump(2) << std::endl;
});
}
//! sends a verification request
Expand All @@ -177,8 +204,6 @@ DeviceVerificationFlow::sendVerificationRequest()
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationRequest> body;
mtx::events::msg::KeyVerificationRequest req;

this->transaction_id = http::client()->generate_txn_id();

req.from_device = http::client()->device_id();
req.transaction_id = this->transaction_id;
req.methods.resize(1);
Expand All @@ -190,7 +215,7 @@ DeviceVerificationFlow::sendVerificationRequest()
http::client()
->send_to_device<mtx::events::msg::KeyVerificationRequest,
mtx::events::EventType::KeyVerificationRequest>(
"m.key.verification.request", body, [](mtx::http::RequestErr err) {
this->transaction_id, body, [](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to send verification request: {} {}",
err->matrix_error.error,
Expand All @@ -214,7 +239,7 @@ DeviceVerificationFlow::cancelVerification()
http::client()
->send_to_device<mtx::events::msg::KeyVerificationCancel,
mtx::events::EventType::KeyVerificationCancel>(
"m.key.verification.cancel", body, [this](mtx::http::RequestErr err) {
this->transaction_id, body, [this](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to cancel verification request: {} {}",
err->matrix_error.error,
Expand All @@ -237,7 +262,7 @@ DeviceVerificationFlow::sendVerificationKey()
http::client()
->send_to_device<mtx::events::msg::KeyVerificationKey,
mtx::events::EventType::KeyVerificationKey>(
"m.key.verification.cancel", body, [](mtx::http::RequestErr err) {
this->transaction_id, body, [](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to send verification key: {} {}",
err->matrix_error.error,
Expand All @@ -260,7 +285,7 @@ DeviceVerificationFlow::sendVerificationMac()
http::client()
->send_to_device<mtx::events::msg::KeyVerificationMac,
mtx::events::EventType::KeyVerificationMac>(
"m.key.verification.cancel", body, [](mtx::http::RequestErr err) {
this->transaction_id, body, [](mtx::http::RequestErr err) {
if (err)
nhlog::net()->warn("failed to send verification MAC: {} {}",
err->matrix_error.error,
Expand Down
Loading

0 comments on commit 8da77bc

Please sign in to comment.