2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-10-15 03:44:48 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "VerificationManager.h"
|
2022-02-14 17:25:55 +03:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
2021-10-15 03:44:48 +03:00
|
|
|
#include "Cache.h"
|
|
|
|
#include "ChatPage.h"
|
|
|
|
#include "DeviceVerificationFlow.h"
|
|
|
|
#include "timeline/TimelineViewManager.h"
|
|
|
|
|
|
|
|
VerificationManager::VerificationManager(TimelineViewManager *o)
|
|
|
|
: QObject(o)
|
|
|
|
, rooms_(o->rooms())
|
2022-09-25 21:05:08 +03:00
|
|
|
{
|
2023-06-19 02:38:40 +03:00
|
|
|
instance_ = this;
|
2022-09-25 21:05:08 +03:00
|
|
|
}
|
2021-10-15 03:44:48 +03:00
|
|
|
|
2022-02-14 17:25:55 +03:00
|
|
|
static bool
|
|
|
|
isValidTime(std::optional<uint64_t> t)
|
|
|
|
{
|
|
|
|
if (!t)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> time{std::chrono::milliseconds(*t)};
|
|
|
|
auto diff = std::chrono::system_clock::now() - time;
|
|
|
|
|
|
|
|
return diff < 10min && diff > -5min;
|
|
|
|
}
|
|
|
|
|
2021-10-15 03:44:48 +03:00
|
|
|
void
|
|
|
|
VerificationManager::receivedRoomDeviceVerificationRequest(
|
|
|
|
const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &message,
|
|
|
|
TimelineModel *model)
|
|
|
|
{
|
|
|
|
if (this->isInitialSync_)
|
|
|
|
return;
|
|
|
|
|
2022-02-14 17:25:55 +03:00
|
|
|
if (!isValidTime(message.origin_server_ts))
|
|
|
|
return;
|
|
|
|
|
2021-10-15 03:44:48 +03:00
|
|
|
auto event_id = QString::fromStdString(message.event_id);
|
|
|
|
if (!this->dvList.contains(event_id)) {
|
|
|
|
if (auto flow = DeviceVerificationFlow::NewInRoomVerification(
|
|
|
|
this, model, message.content, QString::fromStdString(message.sender), event_id)) {
|
|
|
|
dvList[event_id] = flow;
|
|
|
|
emit newDeviceVerificationRequest(flow.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VerificationManager::receivedDeviceVerificationRequest(
|
|
|
|
const mtx::events::msg::KeyVerificationRequest &msg,
|
|
|
|
std::string sender)
|
|
|
|
{
|
|
|
|
if (this->isInitialSync_)
|
|
|
|
return;
|
|
|
|
|
2022-02-14 17:25:55 +03:00
|
|
|
if (!isValidTime(msg.timestamp))
|
|
|
|
return;
|
|
|
|
|
2021-10-15 03:44:48 +03:00
|
|
|
if (!msg.transaction_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto txnid = QString::fromStdString(msg.transaction_id.value());
|
|
|
|
if (!this->dvList.contains(txnid)) {
|
|
|
|
if (auto flow = DeviceVerificationFlow::NewToDeviceVerification(
|
|
|
|
this, msg, QString::fromStdString(sender), txnid)) {
|
|
|
|
dvList[txnid] = flow;
|
|
|
|
emit newDeviceVerificationRequest(flow.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VerificationManager::receivedDeviceVerificationStart(
|
|
|
|
const mtx::events::msg::KeyVerificationStart &msg,
|
|
|
|
std::string sender)
|
|
|
|
{
|
|
|
|
if (this->isInitialSync_)
|
|
|
|
return;
|
|
|
|
|
2022-02-14 17:25:55 +03:00
|
|
|
// can't do this for start messages sent as to_device...
|
|
|
|
// if (!isValidTime(msg.timestamp))
|
|
|
|
// return;
|
|
|
|
|
2021-10-15 03:44:48 +03:00
|
|
|
if (!msg.transaction_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto txnid = QString::fromStdString(msg.transaction_id.value());
|
|
|
|
if (!this->dvList.contains(txnid)) {
|
|
|
|
if (auto flow = DeviceVerificationFlow::NewToDeviceVerification(
|
|
|
|
this, msg, QString::fromStdString(sender), txnid)) {
|
|
|
|
dvList[txnid] = flow;
|
|
|
|
emit newDeviceVerificationRequest(flow.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VerificationManager::verifyUser(QString userid)
|
|
|
|
{
|
|
|
|
auto joined_rooms = cache::joinedRooms();
|
|
|
|
auto room_infos = cache::getRoomInfo(joined_rooms);
|
|
|
|
|
2021-12-29 00:30:12 +03:00
|
|
|
for (const std::string &room_id : joined_rooms) {
|
2021-10-15 03:44:48 +03:00
|
|
|
if ((room_infos[QString::fromStdString(room_id)].member_count == 2) &&
|
|
|
|
cache::isRoomEncrypted(room_id)) {
|
|
|
|
auto room_members = cache::roomMembers(room_id);
|
|
|
|
if (std::find(room_members.begin(), room_members.end(), (userid).toStdString()) !=
|
|
|
|
room_members.end()) {
|
|
|
|
if (auto model = rooms_->getRoomById(QString::fromStdString(room_id))) {
|
|
|
|
auto flow =
|
|
|
|
DeviceVerificationFlow::InitiateUserVerification(this, model.data(), userid);
|
2022-07-22 12:46:22 +03:00
|
|
|
std::unique_ptr<QObject> context{new QObject(flow.get())};
|
|
|
|
QObject *pcontext = context.get();
|
|
|
|
connect(
|
|
|
|
model.data(),
|
|
|
|
&TimelineModel::updateFlowEventId,
|
|
|
|
pcontext,
|
|
|
|
[this, flow, context = std::move(context)](std::string eventId) mutable {
|
|
|
|
if (context->parent() == flow.get()) {
|
|
|
|
dvList[QString::fromStdString(eventId)] = flow;
|
|
|
|
context.reset();
|
|
|
|
}
|
|
|
|
});
|
2021-10-15 03:44:48 +03:00
|
|
|
emit newDeviceVerificationRequest(flow.data());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
tr("No encrypted private chat found with this user. Create an "
|
|
|
|
"encrypted private chat with this user and try again."));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VerificationManager::removeVerificationFlow(DeviceVerificationFlow *flow)
|
|
|
|
{
|
2022-07-22 12:46:22 +03:00
|
|
|
nhlog::crypto()->debug("Removing verification flow {}", (void *)flow);
|
2021-10-15 03:44:48 +03:00
|
|
|
for (auto it = dvList.keyValueBegin(); it != dvList.keyValueEnd(); ++it) {
|
2022-07-22 12:46:22 +03:00
|
|
|
if (it->second == flow) {
|
2021-10-15 03:44:48 +03:00
|
|
|
dvList.remove((*it).first);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VerificationManager::verifyDevice(QString userid, QString deviceid)
|
|
|
|
{
|
2021-10-30 01:22:47 +03:00
|
|
|
auto flow = DeviceVerificationFlow::InitiateDeviceVerification(this, userid, {deviceid});
|
|
|
|
this->dvList[flow->transactionId()] = flow;
|
|
|
|
emit newDeviceVerificationRequest(flow.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VerificationManager::verifyOneOfDevices(QString userid, std::vector<QString> deviceids)
|
|
|
|
{
|
|
|
|
auto flow =
|
|
|
|
DeviceVerificationFlow::InitiateDeviceVerification(this, userid, std::move(deviceids));
|
2021-10-15 03:44:48 +03:00
|
|
|
this->dvList[flow->transactionId()] = flow;
|
|
|
|
emit newDeviceVerificationRequest(flow.data());
|
|
|
|
}
|
2024-03-16 03:24:33 +03:00
|
|
|
|
|
|
|
#include "moc_VerificationManager.cpp"
|