Fix registration on matrix.org

This was a bit of a journey:
https://github.com/matrix-org/matrix-doc/pull/3471
But it should work now and we now use the UIAHandler everywhere.

fixes #670
This commit is contained in:
Nicolas Werner 2021-11-03 18:36:12 +01:00
parent ea371130f2
commit 211fd9d76c
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
9 changed files with 587 additions and 124 deletions

View file

@ -9,6 +9,8 @@
#include <QInputDialog>
#include <QTimer>
#include <mtx/responses/common.hpp>
#include "Logging.h"
#include "MainWindow.h"
#include "dialogs/FallbackAuth.h"
@ -54,6 +56,7 @@ UIA::genericHandler(QString context)
if (flows.empty()) {
nhlog::ui()->error("No available registration flows!");
emit error(tr("No available registration flows!"));
return;
}
@ -61,6 +64,10 @@ UIA::genericHandler(QString context)
if (current_stage == mtx::user_interactive::auth_types::password) {
emit password();
} else if (current_stage == mtx::user_interactive::auth_types::email_identity) {
emit email();
} else if (current_stage == mtx::user_interactive::auth_types::msisdn) {
emit phoneNumber();
} else if (current_stage == mtx::user_interactive::auth_types::recaptcha) {
auto captchaDialog =
new dialogs::ReCaptcha(QString::fromStdString(u.session), MainWindow::instance());
@ -74,8 +81,9 @@ UIA::genericHandler(QString context)
mtx::user_interactive::auth::Fallback{}});
});
// connect(
// captchaDialog, &dialogs::ReCaptcha::cancel, this, &RegisterPage::errorOccurred);
connect(captchaDialog, &dialogs::ReCaptcha::cancel, this, [this]() {
emit error(tr("Registration aborted"));
});
QTimer::singleShot(0, this, [captchaDialog]() { captchaDialog->show(); });
@ -98,7 +106,7 @@ UIA::genericHandler(QString context)
u.session,
mtx::user_interactive::auth::RegistrationToken{token.toStdString()}});
} else {
// emit errorOccurred();
emit error(tr("Registration aborted"));
}
} else {
// use fallback
@ -114,8 +122,9 @@ UIA::genericHandler(QString context)
mtx::user_interactive::auth::Fallback{}});
});
// connect(dialog, &dialogs::FallbackAuth::cancel, this,
// &RegisterPage::errorOccurred);
connect(dialog, &dialogs::FallbackAuth::cancel, this, [this]() {
emit error(tr("Registration aborted"));
});
dialog->show();
}
@ -134,3 +143,130 @@ UIA::continuePassword(QString password)
if (currentHandler)
currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, p});
}
void
UIA::continueEmail(QString email)
{
mtx::requests::RequestEmailToken r{};
r.client_secret = this->client_secret = mtx::client::utils::random_token(128, false);
r.email = email.toStdString();
r.send_attempt = 0;
http::client()->register_email_request_token(
r, [this](const mtx::responses::RequestToken &token, mtx::http::RequestErr e) {
if (!e) {
this->sid = token.sid;
this->submit_url = token.submit_url;
this->email_ = true;
if (submit_url.empty()) {
nhlog::ui()->debug("Got no submit url.");
emit confirm3pidToken();
} else {
nhlog::ui()->debug("Got submit url: {}", token.submit_url);
emit prompt3pidToken();
}
} else {
nhlog::ui()->debug("Registering email failed! ({},{},{},{})",
e->status_code,
e->status_code,
e->parse_error,
e->matrix_error.error);
emit error(QString::fromStdString(e->matrix_error.error));
}
});
}
void
UIA::continuePhoneNumber(QString countryCode, QString phoneNumber)
{
mtx::requests::RequestMSISDNToken r{};
r.client_secret = this->client_secret = mtx::client::utils::random_token(128, false);
r.country = countryCode.toStdString();
r.phone_number = phoneNumber.toStdString();
r.send_attempt = 0;
http::client()->register_phone_request_token(
r, [this](const mtx::responses::RequestToken &token, mtx::http::RequestErr e) {
if (!e) {
this->sid = token.sid;
this->submit_url = token.submit_url;
this->email_ = false;
if (submit_url.empty()) {
nhlog::ui()->debug("Got no submit url.");
emit confirm3pidToken();
} else {
nhlog::ui()->debug("Got submit url: {}", token.submit_url);
emit prompt3pidToken();
}
} else {
nhlog::ui()->debug("Registering phone number failed! ({},{},{},{})",
e->status_code,
e->status_code,
e->parse_error,
e->matrix_error.error);
emit error(QString::fromStdString(e->matrix_error.error));
}
});
}
void
UIA::continue3pidReceived()
{
mtx::user_interactive::auth::ThreePIDCred c{};
c.client_secret = this->client_secret;
c.sid = this->sid;
if (this->email_) {
mtx::user_interactive::auth::EmailIdentity i{};
i.threepidCred = c;
this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
} else {
mtx::user_interactive::auth::MSISDN i{};
i.threepidCred = c;
this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
}
}
void
UIA::submit3pidToken(QString token)
{
mtx::requests::IdentitySubmitToken t{};
t.client_secret = this->client_secret;
t.sid = this->sid;
t.token = token.toStdString();
http::client()->validate_submit_token(
submit_url, t, [this](const mtx::responses::Success &success, mtx::http::RequestErr e) {
if (!e && success.success) {
mtx::user_interactive::auth::ThreePIDCred c{};
c.client_secret = this->client_secret;
c.sid = this->sid;
nhlog::ui()->debug("Submit token success");
if (this->email_) {
mtx::user_interactive::auth::EmailIdentity i{};
i.threepidCred = c;
this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
} else {
mtx::user_interactive::auth::MSISDN i{};
i.threepidCred = c;
this->currentHandler->next(mtx::user_interactive::Auth{currentStatus.session, i});
}
} else {
if (e) {
nhlog::ui()->debug("Submit token invalid! ({},{},{},{})",
e->status_code,
e->status_code,
e->parse_error,
e->matrix_error.error);
emit error(QString::fromStdString(e->matrix_error.error));
} else {
nhlog::ui()->debug("Submit token invalid!");
emit error(tr("Invalid token"));
}
}
this->client_secret.clear();
this->sid.clear();
this->submit_url.clear();
});
}