1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "VerificationManager.h"
#include <chrono>
#include "Cache.h"
#include "ChatPage.h"
#include "DeviceVerificationFlow.h"
#include "timeline/TimelineViewManager.h"
VerificationManager::VerificationManager(TimelineViewManager *o)
: QObject(o)
, rooms_(o->rooms())
{
instance_ = this;
}
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;
}
void
VerificationManager::receivedRoomDeviceVerificationRequest(
const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &message,
TimelineModel *model)
{
if (this->isInitialSync_)
return;
if (!isValidTime(message.origin_server_ts))
return;
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;
if (!isValidTime(msg.timestamp))
return;
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;
// can't do this for start messages sent as to_device...
// if (!isValidTime(msg.timestamp))
// return;
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);
for (const std::string &room_id : joined_rooms) {
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);
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();
}
});
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)
{
nhlog::crypto()->debug("Removing verification flow {}", (void *)flow);
for (auto it = dvList.keyValueBegin(); it != dvList.keyValueEnd(); ++it) {
if (it->second == flow) {
dvList.remove((*it).first);
return;
}
}
}
void
VerificationManager::verifyDevice(QString userid, QString deviceid)
{
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));
this->dvList[flow->transactionId()] = flow;
emit newDeviceVerificationRequest(flow.data());
}
#include "moc_VerificationManager.cpp"
|