Debian Codegraph @

src/ui/UIA.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "UIA.h"
6
7 #include <algorithm>
8
9 #include <QInputDialog>
10 #include <QTimer>
11
12 #include <mtx/requests.hpp>
13 #include <mtx/responses/common.hpp>
14
15 #include "Logging.h"
16 #include "MatrixClient.h"
17
18 UIA *
19 UIA::instance()
20 {
21 static UIA uia{nullptr};
22 return &uia;
23 }
24
25 mtx::http::UIAHandler
26 UIA::genericHandler(QString context)
27 {
28 return mtx::http::UIAHandler([this, context](const mtx::http::UIAHandler &h,
29 const mtx::user_interactive::Unauthorized &u) {
30 QTimer::singleShot(0, this, [this, h, u, context]() {
31 this->currentHandler = h;
32 this->currentStatus = u;
33 this->title_ = context;
34 emit titleChanged();
35
36 std::vector<mtx::user_interactive::Flow> flows = u.flows;
37
38 nhlog::ui()->info("Completed stages: {}", u.completed.size());
39
40 if (!u.completed.empty()) {
41 // Get rid of all flows which don't start with the sequence of
42 // stages that have already been completed.
43 flows.erase(std::remove_if(flows.begin(),
44 flows.end(),
45 [completed_stages = u.completed](auto flow) {
46 if (completed_stages.size() > flow.stages.size())
47 return true;
48 for (size_t f = 0; f < completed_stages.size(); f++)
49 if (completed_stages[f] != flow.stages[f])
50 return true;
51 return false;
52 }),
53 flows.end());
54 }
55
56 if (flows.empty()) {
57 nhlog::ui()->error("No available registration flows!");
58 emit error(tr("No available registration flows!"));
59 return;
60 }
61
62 // sort flows with known stages first
63 std::sort(
64 flows.begin(),
65 flows.end(),
66 [](const mtx::user_interactive::Flow &a, const mtx::user_interactive::Flow &b) {
67 auto calcWeight = [](const mtx::user_interactive::Flow &f) {
68 using namespace mtx::user_interactive::auth_types;
69 const static std::map<std::string_view, int> weights{
70 {mtx::user_interactive::auth_types::password, 0},
71 {mtx::user_interactive::auth_types::email_identity, 0},
72 {mtx::user_interactive::auth_types::msisdn, 0},
73 {mtx::user_interactive::auth_types::dummy, 0},
74 {mtx::user_interactive::auth_types::registration_token, 0},
75 // recaptcha is known, but we'd like to avoid it, because it calls out to
76 // the browser
77 {mtx::user_interactive::auth_types::recaptcha, 1},
78 };
79 int weight = 0;
80 for (const auto &s : f.stages) {
81 if (!weights.count(s))
82 weight += 3;
83 else
84 weight += weights.at(s);
85 }
86 return weight;
87 };
88
89 return calcWeight(a) < calcWeight(b);
90 });
91
92 auto current_stage = flows.front().stages.at(u.completed.size());
93
94 if (current_stage == mtx::user_interactive::auth_types::password) {
95 emit password();
96 } else if (current_stage == mtx::user_interactive::auth_types::email_identity) {
97 emit email();
98 } else if (current_stage == mtx::user_interactive::auth_types::msisdn) {
99 emit phoneNumber();
100 } else if (current_stage == mtx::user_interactive::auth_types::recaptcha) {
101 auto captcha = new ReCaptcha(QString::fromStdString(u.session), context, nullptr);
102 QQmlEngine::setObjectOwnership(captcha, QQmlEngine::JavaScriptOwnership);
103 connect(captcha, &ReCaptcha::confirmation, this, [h, u]() {
104 h.next(mtx::user_interactive::Auth{u.session,
105 mtx::user_interactive::auth::Fallback{}});
106 });
107 connect(captcha, &ReCaptcha::cancelled, this, [this]() {
108 emit error(tr("Registration aborted"));
109 });
110 emit reCaptcha(captcha);
111 } else if (current_stage == mtx::user_interactive::auth_types::dummy) {
112 h.next(
113 mtx::user_interactive::Auth{u.session, mtx::user_interactive::auth::Dummy{}});
114
115 } else if (current_stage == mtx::user_interactive::auth_types::registration_token) {
116 bool ok;
117 QString token =
118 QInputDialog::getText(nullptr,
119 context,
120 tr("Please enter a valid registration token."),
121 QLineEdit::Normal,
122 QString(),
123 &ok);
124
125 if (ok) {
126 h.next(mtx::user_interactive::Auth{
127 u.session,
128 mtx::user_interactive::auth::RegistrationToken{token.toStdString()}});
129 } else {
130 emit error(tr("Registration aborted"));
131 }
132 } else {
133 // use fallback
134 auto fallback = new FallbackAuth(QString::fromStdString(u.session),
135 QString::fromStdString(current_stage),
136 nullptr);
137 QQmlEngine::setObjectOwnership(fallback, QQmlEngine::JavaScriptOwnership);
138 connect(fallback, &FallbackAuth::confirmation, this, [h, u]() {
139 h.next(mtx::user_interactive::Auth{u.session,
140 mtx::user_interactive::auth::Fallback{}});
141 });
142 connect(fallback, &FallbackAuth::cancelled, this, [this]() {
143 emit error(tr("Registration aborted"));
144 });
145 emit fallbackAuth(fallback);
146 }
147 });
148 });
149 }
150
151 void
152 UIA::continuePassword(const QString &password)
153 {
154 mtx::user_interactive::auth::Password p{};
155 p.identifier_type = mtx::user_interactive::auth::Password::UserId;
156 p.password = password.toStdString();
157 p.identifier_user = http::client()->user_id().to_string();
158
159 if (currentHandler)
160 currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, p});
161 }
162
163 void
164 UIA::continueEmail(const QString &email)
165 {
166 mtx::requests::RequestEmailToken r{};
167 r.client_secret = this->client_secret = mtx::client::utils::random_token(128, false);
168 r.email = email.toStdString();
169 r.send_attempt = 0;
170 http::client()->register_email_request_token(
171 r, [this](const mtx::responses::RequestToken &token, mtx::http::RequestErr e) {
172 if (!e) {
173 this->sid = token.sid;
174 this->submit_url = token.submit_url;
175 this->email_ = true;
176
177 if (submit_url.empty()) {
178 nhlog::ui()->debug("Got no submit url.");
179 emit confirm3pidToken();
180 } else {
181 nhlog::ui()->debug("Got submit url: {}", token.submit_url);
182 emit prompt3pidToken();
183 }
184 } else {
185 nhlog::ui()->debug("Registering email failed! ({},{},{},{})",
186 e->status_code,
187 e->status_code,
188 e->parse_error,
189 e->matrix_error.error);
190 emit error(QString::fromStdString(e->matrix_error.error));
191 }
192 });
193 }
194 void
195 UIA::continuePhoneNumber(const QString &countryCode, const QString &phoneNumber)
196 {
197 mtx::requests::RequestMSISDNToken r{};
198 r.client_secret = this->client_secret = mtx::client::utils::random_token(128, false);
199 r.country = countryCode.toStdString();
200 r.phone_number = phoneNumber.toStdString();
201 r.send_attempt = 0;
202 http::client()->register_phone_request_token(
203 r, [this](const mtx::responses::RequestToken &token, mtx::http::RequestErr e) {
204 if (!e) {
205 this->sid = token.sid;
206 this->submit_url = token.submit_url;
207 this->email_ = false;
208 if (submit_url.empty()) {
209 nhlog::ui()->debug("Got no submit url.");
210 emit confirm3pidToken();
211 } else {
212 nhlog::ui()->debug("Got submit url: {}", token.submit_url);
213 emit prompt3pidToken();
214 }
215 } else {
216 nhlog::ui()->debug("Registering phone number failed! ({},{},{},{})",
217 e->status_code,
218 e->status_code,
219 e->parse_error,
220 e->matrix_error.error);
221 emit error(QString::fromStdString(e->matrix_error.error));
222 }
223 });
224 }
225
226 void
227 UIA::continue3pidReceived()
228 {
229 mtx::user_interactive::auth::ThreePIDCred c{};
230 c.client_secret = this->client_secret;
231 c.sid = this->sid;
232
233 if (this->email_) {
234 mtx::user_interactive::auth::EmailIdentity i{};
235 i.threepidCred = c;
236 this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
237 } else {
238 mtx::user_interactive::auth::MSISDN i{};
239 i.threepidCred = c;
240 this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
241 }
242 }
243
244 void
245 UIA::submit3pidToken(const QString &token)
246 {
247 mtx::requests::IdentitySubmitToken t{};
248 t.client_secret = this->client_secret;
249 t.sid = this->sid;
250 t.token = token.toStdString();
251
252 http::client()->validate_submit_token(
253 submit_url, t, [this](const mtx::responses::Success &success, mtx::http::RequestErr e) {
254 if (!e && success.success) {
255 mtx::user_interactive::auth::ThreePIDCred c{};
256 c.client_secret = this->client_secret;
257 c.sid = this->sid;
258
259 nhlog::ui()->debug("Submit token success");
260
261 if (this->email_) {
262 mtx::user_interactive::auth::EmailIdentity i{};
263 i.threepidCred = c;
264 this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
265 } else {
266 mtx::user_interactive::auth::MSISDN i{};
267 i.threepidCred = c;
268 this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
269 }
270 } else {
271 if (e) {
272 nhlog::ui()->debug("Submit token invalid! ({},{},{},{})",
273 e->status_code,
274 e->status_code,
275 e->parse_error,
276 e->matrix_error.error);
277 emit error(QString::fromStdString(e->matrix_error.error));
278 } else {
279 nhlog::ui()->debug("Submit token invalid!");
280 emit error(tr("Invalid token"));
281 }
282 }
283
284 this->client_secret.clear();
285 this->sid.clear();
286 this->submit_url.clear();
287 });
288 }
289
290 #include "moc_UIA.cpp"