[fractal] Fix error handling of token request verification



commit 05ecf239b56dc612ff6a9987ce38def2e8d8ea2b
Author: sonjita <sonjaleaheinze gmail com>
Date:   Thu Oct 17 17:56:26 2019 +0200

    Fix error handling of token request verification
    
    Same situation for email token request verification and phone token request verification:
    Before, the user was always shown the error message "Couldn’t add the <email address / phone number.>",
    also when the error was due to the fact that the <email / phone> was already in use.
    
    With this commit, that bug is fixed: when the <email / phone> is already in use,
    the user is shown the error message "<Email / Phone number> is already in use"

 fractal-gtk/src/app/backend_loop.rs                | 11 +++++++
 fractal-matrix-api/src/backend/user.rs             | 36 ++++++++++++----------
 fractal-matrix-api/src/error.rs                    |  1 +
 .../r0/contact/request_verification_token_email.rs | 15 ++++++++-
 .../contact/request_verification_token_msisdn.rs   | 15 ++++++++-
 5 files changed, 60 insertions(+), 18 deletions(-)
---
diff --git a/fractal-gtk/src/app/backend_loop.rs b/fractal-gtk/src/app/backend_loop.rs
index cbb0cc67..5576d6f8 100644
--- a/fractal-gtk/src/app/backend_loop.rs
+++ b/fractal-gtk/src/app/backend_loop.rs
@@ -54,10 +54,21 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
                     let error = i18n("Email is already in use");
                     APPOP!(show_error_dialog_in_settings, (error));
                 }
+                Ok(BKResponse::GetTokenEmail(Err(Error::Denied))) => {
+                    let error = i18n("Please enter a valid email adress.");
+                    APPOP!(show_error_dialog_in_settings, (error));
+                }
                 Ok(BKResponse::GetTokenPhone(Err(Error::TokenUsed))) => {
                     let error = i18n("Phone number is already in use");
                     APPOP!(show_error_dialog_in_settings, (error));
                 }
+                Ok(BKResponse::GetTokenPhone(Err(Error::Denied))) => {
+                    let error = i18n(
+                        "Please enter your phone number in the format: \n 
+                        + your country code and your phone number.",
+                    );
+                    APPOP!(show_error_dialog_in_settings, (error));
+                }
                 Ok(BKResponse::SubmitPhoneToken(Ok((sid, secret)))) => {
                     let secret = Some(secret);
                     APPOP!(valid_phone_token, (sid, secret));
diff --git a/fractal-matrix-api/src/backend/user.rs b/fractal-matrix-api/src/backend/user.rs
index 83d19777..e990f782 100644
--- a/fractal-matrix-api/src/backend/user.rs
+++ b/fractal-matrix-api/src/backend/user.rs
@@ -185,15 +185,17 @@ pub fn get_email_token(bk: &Backend, identity: String, email: String, client_sec
                     .json::<EmailTokenResponse>()
                     .map_err(Into::into)
             })
-            .map(|response| (response.sid, client_secret))
-            .map_err(|error| match error {
-                Error::MatrixError(ref js)
-                    if js["errcode"].as_str().unwrap_or_default() == "M_THREEPID_IN_USE" =>
-                {
-                    Error::TokenUsed
+            .and_then(|response| match response {
+                EmailTokenResponse::Passed(info) => Ok(info.sid),
+                EmailTokenResponse::Failed(info) => {
+                    if info.errcode == "M_THREEPID_IN_USE" {
+                        Err(Error::TokenUsed)
+                    } else {
+                        Err(Error::Denied)
+                    }
                 }
-                err => err,
-            });
+            })
+            .map(|response| (response, client_secret));
 
         tx.send(BKResponse::GetTokenEmail(query))
             .expect_log("Connection closed");
@@ -225,15 +227,17 @@ pub fn get_phone_token(bk: &Backend, identity: String, phone: String, client_sec
                     .json::<PhoneTokenResponse>()
                     .map_err(Into::into)
             })
-            .map(|response| (response.sid, client_secret))
-            .map_err(|error| match error {
-                Error::MatrixError(ref js)
-                    if js["errcode"].as_str().unwrap_or_default() == "M_THREEPID_IN_USE" =>
-                {
-                    Error::TokenUsed
+            .and_then(|response| match response {
+                PhoneTokenResponse::Passed(info) => Ok(info.sid),
+                PhoneTokenResponse::Failed(info) => {
+                    if info.errcode == "M_THREEPID_IN_USE" {
+                        Err(Error::TokenUsed)
+                    } else {
+                        Err(Error::Denied)
+                    }
                 }
-                err => err,
-            });
+            })
+            .map(|response| (response, client_secret));
 
         tx.send(BKResponse::GetTokenPhone(query))
             .expect_log("Connection closed");
diff --git a/fractal-matrix-api/src/error.rs b/fractal-matrix-api/src/error.rs
index ea73e087..7c83938a 100644
--- a/fractal-matrix-api/src/error.rs
+++ b/fractal-matrix-api/src/error.rs
@@ -13,6 +13,7 @@ pub enum Error {
     SendMsgError(String),
     SendMsgRedactionError(String),
     TokenUsed,
+    Denied,
 }
 
 impl From<reqwest::Error> for Error {
diff --git a/fractal-matrix-api/src/r0/contact/request_verification_token_email.rs 
b/fractal-matrix-api/src/r0/contact/request_verification_token_email.rs
index 7ae47cc3..7776950a 100644
--- a/fractal-matrix-api/src/r0/contact/request_verification_token_email.rs
+++ b/fractal-matrix-api/src/r0/contact/request_verification_token_email.rs
@@ -20,10 +20,23 @@ pub struct Body {
 }
 
 #[derive(Clone, Debug, Deserialize)]
-pub struct Response {
+#[serde(rename_all = "lowercase")]
+#[serde(untagged)]
+pub enum Response {
+    Passed(InfoPassed),
+    Failed(InfoFailed),
+}
+
+#[derive(Clone, Debug, Deserialize)]
+pub struct InfoPassed {
     pub sid: String,
 }
 
+#[derive(Clone, Debug, Deserialize)]
+pub struct InfoFailed {
+    pub errcode: String,
+}
+
 pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
     let url = base
         .join("/_matrix/client/r0/account/3pid/email/requestToken")
diff --git a/fractal-matrix-api/src/r0/contact/request_verification_token_msisdn.rs 
b/fractal-matrix-api/src/r0/contact/request_verification_token_msisdn.rs
index c1dae91e..b95bbea0 100644
--- a/fractal-matrix-api/src/r0/contact/request_verification_token_msisdn.rs
+++ b/fractal-matrix-api/src/r0/contact/request_verification_token_msisdn.rs
@@ -21,10 +21,23 @@ pub struct Body {
 }
 
 #[derive(Clone, Debug, Deserialize)]
-pub struct Response {
+#[serde(rename_all = "lowercase")]
+#[serde(untagged)]
+pub enum Response {
+    Passed(InfoPassed),
+    Failed(InfoFailed),
+}
+
+#[derive(Clone, Debug, Deserialize)]
+pub struct InfoPassed {
     pub sid: String,
 }
 
+#[derive(Clone, Debug, Deserialize)]
+pub struct InfoFailed {
+    pub errcode: String,
+}
+
 pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
     let url = base
         .join("/_matrix/client/r0/account/3pid/msisdn/requestToken")


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]