[fractal/fractal-next] auth_dialog: Split out stage handlers



commit 5a8c85b2ff528d11904310ffb8289d8f1016dca6
Author: Kai A. Hiller <V02460 gmail com>
Date:   Sat Feb 12 09:38:38 2022 +0100

    auth_dialog: Split out stage handlers

 src/components/auth_dialog.rs | 83 ++++++++++++++++++++++---------------------
 1 file changed, 43 insertions(+), 40 deletions(-)
---
diff --git a/src/components/auth_dialog.rs b/src/components/auth_dialog.rs
index 05dda36a..12666569 100644
--- a/src/components/auth_dialog.rs
+++ b/src/components/auth_dialog.rs
@@ -233,7 +233,6 @@ impl AuthDialog {
         &self,
         callback: FN,
     ) -> Result<Response, AuthError> {
-        let priv_ = self.imp();
         let client = self.session().client();
         let mut auth_data = None;
 
@@ -262,54 +261,58 @@ impl AuthDialog {
 
             match flow.stages[uiaa_info.completed.len()] {
                 AuthType::Password => {
-                    let stack = &priv_.stack;
-                    stack.set_visible_child_name(AuthType::Password.as_ref());
-                    if self.show_and_wait_for_response().await.is_ok() {
-                        let user_id = self.session().user().unwrap().user_id().to_string();
-                        let password = priv_.password.text().to_string();
-                        let session = uiaa_info.session;
-
-                        auth_data = Some(AuthData::Password(Password {
-                            user_id,
-                            password,
-                            session,
-                        }));
-
-                        continue;
-                    }
+                    auth_data = Some(self.perform_password_stage(uiaa_info.session).await?);
+                    continue;
                 }
                 // TODO implement other authentication types
                 // See: https://gitlab.gnome.org/GNOME/fractal/-/issues/835
                 _ => {
-                    if let Some(session) = uiaa_info.session {
-                        priv_.stack.set_visible_child_name("fallback");
-                        let client_clone = client.clone();
-                        let homeserver =
-                            spawn_tokio!(async move { client_clone.homeserver().await })
-                                .await
-                                .unwrap();
-                        let first_stage = flow.stages.first();
-                        self.setup_fallback_page(
-                            homeserver.as_str(),
-                            first_stage.ok_or(AuthError::NoStageToChoose)?.as_ref(),
-                            &session,
-                        );
-                        if self.show_and_wait_for_response().await.is_ok() {
-                            auth_data =
-                                Some(AuthData::FallbackAcknowledgement(FallbackAcknowledgement {
-                                    session,
-                                }));
-
-                            continue;
-                        }
-                    }
+                    let session = uiaa_info.session.ok_or(AuthError::MalformedResponse)?;
+                    let first_stage = flow.stages.first();
+                    let stage = first_stage.ok_or(AuthError::NoStageToChoose)?;
+                    auth_data = Some(self.perform_fallback(session, stage).await?);
+
+                    continue;
                 }
             }
-
-            return Err(AuthError::UserCancelled);
         }
     }
 
+    /// Performs the password stage.
+    async fn perform_password_stage(&self, session: Option<String>) -> Result<AuthData, AuthError> {
+        let stack = &self.imp().stack;
+        stack.set_visible_child_name(AuthType::Password.as_ref());
+        self.show_and_wait_for_response().await?;
+
+        let user_id = self.session().user().unwrap().user_id().to_string();
+        let password = self.imp().password.text().to_string();
+
+        Ok(AuthData::Password(Password {
+            user_id,
+            password,
+            session,
+        }))
+    }
+
+    /// Performs a web-based fallback for the given stage.
+    async fn perform_fallback(
+        &self,
+        session: String,
+        stage: &AuthType,
+    ) -> Result<AuthData, AuthError> {
+        let client = self.session().client();
+        let homeserver = spawn_tokio!(async move { client.homeserver().await })
+            .await
+            .unwrap();
+        self.imp().stack.set_visible_child_name("fallback");
+        self.setup_fallback_page(homeserver.as_str(), stage.as_ref(), &session);
+        self.show_and_wait_for_response().await?;
+
+        Ok(AuthData::FallbackAcknowledgement(FallbackAcknowledgement {
+            session,
+        }))
+    }
+
     /// Lets the user complete the current stage.
     async fn show_and_wait_for_response(&self) -> Result<(), AuthError> {
         let (sender, receiver) = futures::channel::oneshot::channel();


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