Skip to content

Commit

Permalink
Error Handling and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan2k1 committed Jun 26, 2020
1 parent 2edbf57 commit 2f1d026
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 96 deletions.
16 changes: 7 additions & 9 deletions resources/qml/UserProfile.qml
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,28 @@ ApplicationWindow{
// userProfileList.ignoreUser()
// }
// }

ImageButton{
image:":/icons/icons/ui/round-remove-button.png"
image:":/icons/icons/ui/black-bubble-speech.png"
Layout.margins: {
left: 5
right: 5
}
ToolTip.visible: hovered
ToolTip.text: qsTr("Kick the user")
ToolTip.text: qsTr("Start a private chat")
onClicked : {
userProfileList.kickUser()
userProfileList.startChat()
}
}

ImageButton{
image:":/icons/icons/ui/black-bubble-speech.png"
image:":/icons/icons/ui/round-remove-button.png"
Layout.margins: {
left: 5
right: 5
}
ToolTip.visible: hovered
ToolTip.text: qsTr("Start a conversation")
ToolTip.text: qsTr("Kick the user")
onClicked : {
userProfileList.startChat()
userProfileList.kickUser()
}
}
}
Expand Down Expand Up @@ -205,7 +203,7 @@ ApplicationWindow{
text:"OK"
onClicked: userProfileDialog.close()

Layout.alignment: Qt.AlignRight
Layout.alignment: Qt.AlignRight | Qt.AlignBottom

Layout.margins : {
right : 10
Expand Down
52 changes: 10 additions & 42 deletions resources/qml/device-verification/DeviceVerification.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ ApplicationWindow {
implicitHeight: currentItem.implicitHeight
}

onClosing: {
flow.cancelVerification();
deviceVerificationList.remove(flow.tranId);
delete flow;
}

property var flow
Connections {
target: flow
Expand Down Expand Up @@ -123,33 +117,6 @@ ApplicationWindow {
color:colors.text
verticalAlignment: Text.AlignVCenter
}

RowLayout {
RadioButton {
id: decimalRadio
Layout.alignment: Qt.AlignLeft
text: qsTr("Decimal")
contentItem: Text {
text: decimalRadio.text
color: colors.text
}
onClicked: { flow.method = DeviceVerificationFlow.Decimal }
}
Item {
Layout.fillWidth: true
}
RadioButton {
id: emojiRadio
Layout.alignment: Qt.AlignRight
text: qsTr("Emoji")
contentItem: Text {
text: emojiRadio.text
color: colors.text
}
onClicked: { flow.method = DeviceVerificationFlow.Emoji }
}
}

RowLayout {
Button {
Layout.alignment: Qt.AlignLeft
Expand All @@ -165,9 +132,8 @@ ApplicationWindow {
}
onClicked: {
dialog.close();
flow.cancelVerification();
flow.cancelVerification(DeviceVerificationFlow.User);
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Expand Down Expand Up @@ -227,9 +193,8 @@ ApplicationWindow {
}
onClicked: {
dialog.close();
flow.cancelVerification();
flow.cancelVerification(DeviceVerificationFlow.User);
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Expand Down Expand Up @@ -261,14 +226,17 @@ ApplicationWindow {
Label {
font.pixelSize: Qt.application.font.pixelSize * 2
text: flow.sasList[0]
color:colors.text
}
Label {
font.pixelSize: Qt.application.font.pixelSize * 2
text: flow.sasList[1]
color:colors.text
}
Label {
font.pixelSize: Qt.application.font.pixelSize * 2
text: flow.sasList[2]
color:colors.text
}
}

Expand All @@ -287,9 +255,8 @@ ApplicationWindow {
}
onClicked: {
dialog.close();
flow.cancelVerification();
flow.cancelVerification(DeviceVerificationFlow.MismatchedSAS);
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Expand Down Expand Up @@ -411,6 +378,7 @@ ApplicationWindow {
implicitWidth: col.width
ColumnLayout {
id: col
Layout.fillWidth: true
anchors.bottom: parent.bottom
property var emoji: emojis.mapping[flow.sasList[index]]
Label {
Expand All @@ -424,6 +392,7 @@ ApplicationWindow {
Label {
Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
text: col.emoji.description
color:colors.text
}
}
}
Expand All @@ -445,9 +414,8 @@ ApplicationWindow {
}
onClicked: {
dialog.close();
flow.cancelVerification();
flow.cancelVerification(DeviceVerificationFlow.MismatchedSAS);
deviceVerificationList.remove(flow.tranId);
delete flow;
}
}
Item {
Expand Down Expand Up @@ -507,7 +475,7 @@ ApplicationWindow {
}
onClicked: {
dialog.close();
flow.cancelVerification();
flow.cancelVerification(DeviceVerificationFlow.User);
deviceVerificationList.remove(flow.tranId);
delete flow;
}
Expand Down
109 changes: 70 additions & 39 deletions src/DeviceVerificationFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,53 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
this->isMacVerified = false;
connect(timeout, &QTimer::timeout, this, [this]() {
emit timedout();
this->cancelVerification(DeviceVerificationFlow::Error::Timeout);
this->deleteLater();
});

connect(ChatPage::instance(),
&ChatPage::recievedDeviceVerificationStart,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationStart>>(message);
if (msg.content.transaction_id == this->transaction_id) {
if ((std::find(msg.content.key_agreement_protocols.begin(),
msg.content.key_agreement_protocols.end(),
"curve25519-hkdf-sha256") !=
msg.content.key_agreement_protocols.end()) &&
(std::find(msg.content.hashes.begin(),
msg.content.hashes.end(),
"sha256") != msg.content.hashes.end()) &&
(std::find(msg.content.message_authentication_codes.begin(),
msg.content.message_authentication_codes.end(),
"hmac-sha256") !=
msg.content.message_authentication_codes.end()) &&
((std::find(msg.content.short_authentication_string.begin(),
connect(
ChatPage::instance(),
&ChatPage::recievedDeviceVerificationStart,
this,
[this](const mtx::events::collections::DeviceEvents &message) {
auto msg =
std::get<mtx::events::DeviceEvent<msgs::KeyVerificationStart>>(message);
if (msg.content.transaction_id == this->transaction_id) {
if ((std::find(msg.content.key_agreement_protocols.begin(),
msg.content.key_agreement_protocols.end(),
"curve25519-hkdf-sha256") !=
msg.content.key_agreement_protocols.end()) &&
(std::find(msg.content.hashes.begin(),
msg.content.hashes.end(),
"sha256") != msg.content.hashes.end()) &&
(std::find(msg.content.message_authentication_codes.begin(),
msg.content.message_authentication_codes.end(),
"hmac-sha256") !=
msg.content.message_authentication_codes.end())) {
if (std::find(msg.content.short_authentication_string.begin(),
msg.content.short_authentication_string.end(),
mtx::events::msg::SASMethods::Decimal) !=
msg.content.short_authentication_string.end()) ||
(std::find(msg.content.short_authentication_string.begin(),
msg.content.short_authentication_string.end(),
mtx::events::msg::SASMethods::Emoji) !=
msg.content.short_authentication_string.end()))) {
this->acceptVerificationRequest();
this->canonical_json = nlohmann::json(msg.content);
} else {
this->cancelVerification();
}
}
});
msg.content.short_authentication_string.end()) {
this->method = DeviceVerificationFlow::Method::Emoji;
} else if (std::find(
msg.content.short_authentication_string.begin(),
msg.content.short_authentication_string.end(),
mtx::events::msg::SASMethods::Emoji) !=
msg.content.short_authentication_string.end()) {
this->method = DeviceVerificationFlow::Method::Decimal;
} else {
this->cancelVerification(
DeviceVerificationFlow::Error::UnknownMethod);
return;
}
this->acceptVerificationRequest();
this->canonical_json = nlohmann::json(msg.content);
} else {
this->cancelVerification(
DeviceVerificationFlow::Error::UnknownMethod);
}
}
});
connect(
ChatPage::instance(),
&ChatPage::recievedDeviceVerificationAccept,
Expand All @@ -76,7 +87,8 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
this->mac_method = msg.content.message_authentication_code;
this->sendVerificationKey();
} else {
this->cancelVerification();
this->cancelVerification(
DeviceVerificationFlow::Error::UnknownMethod);
}
}
});
Expand Down Expand Up @@ -130,7 +142,8 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
msg.content.key + this->canonical_json.dump()))) {
emit this->verificationRequestAccepted(this->method);
} else {
this->cancelVerification();
this->cancelVerification(
DeviceVerificationFlow::Error::MismatchedCommitment);
}
}
}
Expand All @@ -156,7 +169,8 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
this->sas->calculate_mac(this->device_keys[mac.first],
info + mac.first)) {
} else {
this->cancelVerification();
this->cancelVerification(
DeviceVerificationFlow::Error::KeyMismatch);
return;
}
}
Expand All @@ -172,7 +186,8 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
else
this->isMacVerified = true;
} else {
this->cancelVerification();
this->cancelVerification(
DeviceVerificationFlow::Error::KeyMismatch);
}
}
});
Expand Down Expand Up @@ -429,15 +444,31 @@ DeviceVerificationFlow::sendVerificationRequest()
}
//! cancels a verification flow
void
DeviceVerificationFlow::cancelVerification()
DeviceVerificationFlow::cancelVerification(DeviceVerificationFlow::Error error_code)
{
mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationCancel> body;
mtx::events::msg::KeyVerificationCancel req;

req.transaction_id = this->transaction_id;
// TODO: Add Proper Error Messages and Code
req.reason = "Device Verification Cancelled";
req.code = "400";
if (error_code == DeviceVerificationFlow::Error::UnknownMethod) {
req.code = "m.unknown_method";
req.reason = "unknown method recieved";
} else if (error_code == DeviceVerificationFlow::Error::MismatchedCommitment) {
req.code = "m.mismatched_commitment";
req.reason = "commitment didn't match";
} else if (error_code == DeviceVerificationFlow::Error::MismatchedSAS) {
req.code = "m.mismatched_sas";
req.reason = "sas didn't match";
} else if (error_code == DeviceVerificationFlow::Error::KeyMismatch) {
req.code = "m.key_match";
req.reason = "keys did not match";
} else if (error_code == DeviceVerificationFlow::Error::Timeout) {
req.code = "m.timeout";
req.reason = "timed out";
} else if (error_code == DeviceVerificationFlow::Error::User) {
req.code = "m.user";
req.reason = "user cancelled the verification";
}

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

Expand Down
12 changes: 11 additions & 1 deletion src/DeviceVerificationFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class DeviceVerificationFlow : public QObject
Emoji
};
Q_ENUM(Method)
enum Error
{
UnknownMethod,
MismatchedCommitment,
MismatchedSAS,
KeyMismatch,
Timeout,
User
};
Q_ENUM(Error)

DeviceVerificationFlow(QObject *parent = nullptr);
QString getTransactionId();
Expand Down Expand Up @@ -56,7 +66,7 @@ public slots:
//! starts the verification flow
void startVerificationRequest();
//! cancels a verification flow
void cancelVerification();
void cancelVerification(DeviceVerificationFlow::Error error_code);
//! sends the verification key
void sendVerificationKey();
//! sends the mac of the keys
Expand Down
Loading

0 comments on commit 2f1d026

Please sign in to comment.