summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorChethan2k1 <40890937+Chethan2k1@users.noreply.github.com>2020-06-09 22:06:41 +0530
committerCH Chethan Reddy <40890937+Chethan2k1@users.noreply.github.com>2020-07-30 22:10:27 +0530
commit1eb162cb6fa81c7388725414e9c68f5991372d08 (patch)
treecdd97db814e36b1c34ea335d03ee9d51ae9f24e0 /src
parentAdd SAS Method choice and Add send_to_device API call (diff)
downloadnheko-1eb162cb6fa81c7388725414e9c68f5991372d08.tar.xz
Handle Device Verification related to_device messages
Diffstat (limited to 'src')
-rw-r--r--src/ChatPage.cpp1
-rw-r--r--src/ChatPage.h12
-rw-r--r--src/DeviceVerificationFlow.cpp105
-rw-r--r--src/DeviceVerificationFlow.h5
-rw-r--r--src/Olm.cpp23
5 files changed, 142 insertions, 4 deletions
diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp
index 518be31c..17ea2551 100644
--- a/src/ChatPage.cpp
+++ b/src/ChatPage.cpp
@@ -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"
diff --git a/src/ChatPage.h b/src/ChatPage.h
index 18bed289..b05a388d 100644
--- a/src/ChatPage.h
+++ b/src/ChatPage.h
@@ -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);
diff --git a/src/DeviceVerificationFlow.cpp b/src/DeviceVerificationFlow.cpp
index 5bbe2a71..c6652d03 100644
--- a/src/DeviceVerificationFlow.cpp
+++ b/src/DeviceVerificationFlow.cpp
@@ -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()
diff --git a/src/DeviceVerificationFlow.h b/src/DeviceVerificationFlow.h
index c7701196..561a3717 100644
--- a/src/DeviceVerificationFlow.h
+++ b/src/DeviceVerificationFlow.h
@@ -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)
\ No newline at end of file
diff --git a/src/Olm.cpp b/src/Olm.cpp
index 74fbac9a..6c1d3fdc 100644
--- a/src/Olm.cpp
+++ b/src/Olm.cpp
@@ -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));
                 }