summary refs log tree commit diff
path: root/src/ui
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-11-03 18:36:12 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2021-11-03 18:39:51 +0100
commit211fd9d76cfd691eceb8c77297ee20fd0fde4bbb (patch)
tree287451ffac2b9eb556dd6d57578a6fbdee591913 /src/ui
parentMerge branch 'macos_api_updates' into 'master' (diff)
downloadnheko-211fd9d76cfd691eceb8c77297ee20fd0fde4bbb.tar.xz
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
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/UIA.cpp146
-rw-r--r--src/ui/UIA.h17
2 files changed, 158 insertions, 5 deletions
diff --git a/src/ui/UIA.cpp b/src/ui/UIA.cpp
index 29161382..c157ea0f 100644
--- a/src/ui/UIA.cpp
+++ b/src/ui/UIA.cpp
@@ -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();
+      });
+}
diff --git a/src/ui/UIA.h b/src/ui/UIA.h
index fb047451..0db23897 100644
--- a/src/ui/UIA.h
+++ b/src/ui/UIA.h
@@ -27,14 +27,31 @@ public:
 
 public slots:
     void continuePassword(QString password);
+    void continueEmail(QString email);
+    void continuePhoneNumber(QString countryCode, QString phoneNumber);
+    void submit3pidToken(QString token);
+    void continue3pidReceived();
 
 signals:
     void password();
+    void email();
+    void phoneNumber();
+
+    void confirm3pidToken();
+    void prompt3pidToken();
+    void tokenAccepted();
 
     void titleChanged();
+    void error(QString msg);
 
 private:
     std::optional<mtx::http::UIAHandler> currentHandler;
     mtx::user_interactive::Unauthorized currentStatus;
     QString title_;
+
+    // for 3pids like email and phone number
+    std::string client_secret;
+    std::string sid;
+    std::string submit_url;
+    bool email_ = true;
 };