fractal/components/dialogs/
auth.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use std::{fmt::Debug, future::Future};

use adw::{prelude::*, subclass::prelude::*};
use futures_channel::oneshot;
use gettextrs::gettext;
use gtk::{glib, glib::clone, CompositeTemplate};
use matrix_sdk::{encryption::CrossSigningResetAuthType, Error};
use ruma::{
    api::client::{
        error::StandardErrorBody,
        uiaa::{
            AuthData, AuthType, Dummy, FallbackAcknowledgement, Password, UiaaInfo, UserIdentifier,
        },
    },
    assign,
};
use thiserror::Error;
use tracing::error;

use crate::{prelude::*, session::model::Session, spawn, spawn_tokio};

/// An error during UIAA interaction.
#[derive(Debug, Error)]
pub enum AuthError {
    /// The server returned a non-UIAA error.
    #[error(transparent)]
    ServerResponse(#[from] Error),

    /// The ID of the UIAA session is missing for a stage that requires it.
    #[error("The ID of the session is missing")]
    MissingSessionId,

    /// The available flows are empty or done but the endpoint still requires
    /// UIAA.
    #[error("There is no stage to choose from")]
    NoStageToChoose,

    /// The user cancelled the authentication.
    #[error("The user cancelled the authentication")]
    UserCancelled,

    /// The parent `Session` could not be upgraded.
    #[error("The session could not be upgraded")]
    NoSession,

    /// The parent `gtk::Widget` could not be upgraded.
    #[error("The parent widget could not be upgraded")]
    NoParentWidget,

    /// An unexpected error occurred.
    #[error("An unexpected error occurred")]
    Unknown,
}

mod imp {
    use std::cell::RefCell;

    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(resource = "/org/gnome/Fractal/ui/components/dialogs/auth.ui")]
    #[properties(wrapper_type = super::AuthDialog)]
    pub struct AuthDialog {
        #[template_child]
        password: TemplateChild<gtk::PasswordEntry>,
        #[template_child]
        open_browser_btn: TemplateChild<gtk::Button>,
        open_browser_btn_handler: RefCell<Option<glib::SignalHandlerId>>,
        #[template_child]
        error: TemplateChild<gtk::Label>,
        /// The parent session.
        #[property(get, set, construct_only)]
        session: glib::WeakRef<Session>,
        /// The parent widget.
        #[property(get)]
        parent: glib::WeakRef<gtk::Widget>,
        /// The sender for the response.
        sender: RefCell<Option<oneshot::Sender<String>>>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for AuthDialog {
        const NAME: &'static str = "AuthDialog";
        type Type = super::AuthDialog;
        type ParentType = adw::AlertDialog;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            Self::bind_template_callbacks(klass);
        }

        fn instance_init(obj: &InitializingObject<Self>) {
            obj.init_template();
        }
    }

    #[glib::derived_properties]
    impl ObjectImpl for AuthDialog {}

    impl WidgetImpl for AuthDialog {}
    impl AdwDialogImpl for AuthDialog {}

    impl AdwAlertDialogImpl for AuthDialog {
        fn response(&self, response: &str) {
            if let Some(sender) = self.sender.take() {
                if sender.send(response.to_owned()).is_err() {
                    error!("Could not send response");
                }
            }
        }
    }

    #[gtk::template_callbacks]
    impl AuthDialog {
        /// Authenticate the user to the server via an interactive
        /// authentication flow.
        ///
        /// The type of flow and the required stages are negotiated during the
        /// authentication. Returns the last server response on success.
        pub(super) async fn authenticate<
            Response: Send + 'static,
            F1: Future<Output = Result<Response, Error>> + Send + 'static,
            FN: Fn(matrix_sdk::Client, Option<AuthData>) -> F1 + Send + 'static + Sync + Clone,
        >(
            &self,
            parent: &gtk::Widget,
            callback: FN,
        ) -> Result<Response, AuthError> {
            let Some(client) = self.session.upgrade().map(|s| s.client()) else {
                return Err(AuthError::NoSession);
            };

            self.parent.set(Some(parent));

            let mut auth_data = None;

            loop {
                let callback_clone = callback.clone();
                let client_clone = client.clone();

                // Get the current state of the authentication.
                let handle =
                    spawn_tokio!(async move { callback_clone(client_clone, auth_data).await });
                let response = handle.await.expect("task was not aborted");

                let error = match response {
                    // Authentication is over.
                    Ok(result) => return Ok(result),
                    Err(error) => error,
                };
                // If this is a UIAA error, authentication continues.
                let Some(uiaa_info) = error.as_uiaa_response() else {
                    return Err(error.into());
                };

                let next_auth_data = self.perform_next_stage(uiaa_info).await?;
                auth_data = Some(next_auth_data);
            }
        }

        /// Reset the cross-signing keys while handling the interactive
        /// authentication flow.
        ///
        /// The type of flow and the required stages are negotiated during the
        /// authentication. Returns the last server response on success.
        pub(super) async fn reset_cross_signing(
            &self,
            parent: &gtk::Widget,
        ) -> Result<(), AuthError> {
            let Some(encryption) = self.session.upgrade().map(|s| s.client().encryption()) else {
                return Err(AuthError::NoSession);
            };

            self.parent.set(Some(parent));

            let handle = spawn_tokio!(async move { encryption.reset_cross_signing().await })
                .await
                .expect("task was not aborted")?;

            if let Some(handle) = handle {
                match handle.auth_type() {
                    CrossSigningResetAuthType::Uiaa(uiaa_info) => {
                        let auth_data = self.perform_next_stage(uiaa_info).await?;

                        spawn_tokio!(async move { handle.auth(Some(auth_data)).await })
                            .await
                            .expect("task was not aborted")?;
                    }
                    CrossSigningResetAuthType::Oidc(_) => {
                        // According to the code, this is only used with the `experimental-oidc`
                        // feature. Return an error in case this changes.
                        error!(
                            "Could not perform cross-signing reset: received unexpected OIDC stage"
                        );
                        return Err(AuthError::Unknown);
                    }
                }
            }

            Ok(())
        }

        /// Performs the preferred next stage in the given UIAA info.
        ///
        /// Stages that are actually supported are preferred. If no stages are
        /// supported, we use the web-based fallback.
        async fn perform_next_stage(&self, uiaa_info: &UiaaInfo) -> Result<AuthData, AuthError> {
            // Show the authentication error, if there is one.
            self.show_auth_error(uiaa_info.auth_error.as_ref());

            // Find and perform the next stage.
            let stages = uiaa_info
                .flows
                .iter()
                .filter_map(|flow| flow.stages.strip_prefix(uiaa_info.completed.as_slice()))
                .filter_map(|stages_left| stages_left.first());

            let mut first_stage = None;
            for stage in stages {
                if let Some(auth_result) = self
                    .try_perform_stage(uiaa_info.session.as_ref(), stage)
                    .await
                {
                    return auth_result;
                }

                if first_stage.is_none() {
                    first_stage = Some(stage);
                }
            }

            // Default to first stage if no stages are supported.
            let first_stage = first_stage.ok_or(AuthError::NoStageToChoose)?;
            self.perform_fallback(uiaa_info.session.clone(), first_stage)
                .await
        }

        /// Tries to perform the given stage.
        ///
        /// Returns `None` if the stage is not implemented.
        async fn try_perform_stage(
            &self,
            uiaa_session: Option<&String>,
            stage: &AuthType,
        ) -> Option<Result<AuthData, AuthError>> {
            match stage {
                AuthType::Password => {
                    Some(self.perform_password_stage(uiaa_session.cloned()).await)
                }
                AuthType::Sso => Some(self.perform_fallback(uiaa_session.cloned(), stage).await),
                AuthType::Dummy => Some(Ok(Self::perform_dummy_stage(uiaa_session.cloned()))),
                _ => None,
            }
        }

        /// Performs the password stage.
        async fn perform_password_stage(
            &self,
            uiaa_session: Option<String>,
        ) -> Result<AuthData, AuthError> {
            let Some(session) = self.session.upgrade() else {
                return Err(AuthError::NoSession);
            };
            let obj = self.obj();

            self.password.set_visible(true);
            self.open_browser_btn.set_visible(false);
            obj.set_body(&gettext(
                "Please authenticate the operation with your password",
            ));
            obj.set_response_enabled("confirm", false);

            self.show_and_wait_for_response().await?;

            let user_id = session.user_id().to_string();
            let password = self.password.text().into();

            let data = assign!(
                Password::new(UserIdentifier::UserIdOrLocalpart(user_id), password),
                { session: uiaa_session }
            );

            Ok(AuthData::Password(data))
        }

        /// Performs the dummy stage.
        fn perform_dummy_stage(uiaa_session: Option<String>) -> AuthData {
            AuthData::Dummy(assign!(Dummy::new(), { session: uiaa_session }))
        }

        /// Performs a web-based fallback for the given stage.
        async fn perform_fallback(
            &self,
            uiaa_session: Option<String>,
            stage: &AuthType,
        ) -> Result<AuthData, AuthError> {
            let Some(client) = self.session.upgrade().map(|s| s.client()) else {
                return Err(AuthError::NoSession);
            };
            let uiaa_session = uiaa_session.ok_or(AuthError::MissingSessionId)?;
            let obj = self.obj();

            self.password.set_visible(false);
            self.open_browser_btn.set_visible(true);
            obj.set_body(&gettext(
                "Please authenticate the operation via the browser and, once completed, press confirm",
            ));
            obj.set_response_enabled("confirm", false);

            let homeserver = client.homeserver();
            self.set_up_fallback(homeserver.as_str(), stage.as_ref(), &uiaa_session);

            self.show_and_wait_for_response().await?;

            Ok(AuthData::FallbackAcknowledgement(
                FallbackAcknowledgement::new(uiaa_session),
            ))
        }

        /// Let the user complete the current stage.
        async fn show_and_wait_for_response(&self) -> Result<(), AuthError> {
            let Some(parent) = self.parent.upgrade() else {
                return Err(AuthError::NoParentWidget);
            };
            let obj = self.obj();

            let (sender, receiver) = futures_channel::oneshot::channel();
            self.sender.replace(Some(sender));

            // Show this dialog.
            obj.present(Some(&parent));

            // Wait for the response.
            let result = receiver.await;

            // Close this dialog.
            obj.close();

            match result.as_deref() {
                Ok("confirm") => Ok(()),
                Ok(_) => Err(AuthError::UserCancelled),
                Err(_) => {
                    error!("Could not get the response, the channel was closed");
                    Err(AuthError::Unknown)
                }
            }
        }

        /// Show the given error.
        fn show_auth_error(&self, auth_error: Option<&StandardErrorBody>) {
            if let Some(auth_error) = auth_error {
                self.error.set_label(&auth_error.message);
            }

            self.error.set_visible(auth_error.is_some());
        }

        /// Prepare the button to open the web-based fallback with the given
        /// settings.
        fn set_up_fallback(&self, homeserver: &str, auth_type: &str, uiaa_session: &str) {
            if let Some(handler) = self.open_browser_btn_handler.take() {
                self.open_browser_btn.disconnect(handler);
            }

            let uri = format!(
                "{homeserver}_matrix/client/r0/auth/{auth_type}/fallback/web?session={uiaa_session}"
            );

            let handler = self.open_browser_btn.connect_clicked(clone!(
                #[weak(rename_to = imp)]
                self,
                move |_| {
                    let uri = uri.clone();
                    spawn!(async move {
                        let Some(parent) = imp.parent.upgrade() else {
                            return;
                        };

                        if let Err(error) = gtk::UriLauncher::new(&uri)
                            .launch_future(parent.root().and_downcast_ref::<gtk::Window>())
                            .await
                        {
                            error!("Could not launch URI: {error}");
                        }

                        imp.obj().set_response_enabled("confirm", true);
                    });
                }
            ));

            self.open_browser_btn_handler.replace(Some(handler));
        }

        /// Update the confirm response for the current state.
        #[template_callback]
        fn update_confirm(&self) {
            self.obj()
                .set_response_enabled("confirm", !self.password.text().is_empty());
        }
    }
}

glib::wrapper! {
    /// Dialog to guide the user through the [User-Interactive Authentication API] (UIAA).
    ///
    /// [User-Interactive Authentication API]: https://spec.matrix.org/latest/client-server-api/#user-interactive-authentication-api
    pub struct AuthDialog(ObjectSubclass<imp::AuthDialog>)
        @extends gtk::Widget, adw::Dialog, adw::AlertDialog, @implements gtk::Accessible;
}

impl AuthDialog {
    pub fn new(session: &Session) -> Self {
        glib::Object::builder().property("session", session).build()
    }

    /// Authenticate the user to the server via an interactive authentication
    /// flow.
    ///
    /// The type of flow and the required stages are negotiated during the
    /// authentication. Returns the last server response on success.
    pub(crate) async fn authenticate<
        Response: Send + 'static,
        F1: Future<Output = Result<Response, Error>> + Send + 'static,
        FN: Fn(matrix_sdk::Client, Option<AuthData>) -> F1 + Send + 'static + Sync + Clone,
    >(
        &self,
        parent: &impl IsA<gtk::Widget>,
        callback: FN,
    ) -> Result<Response, AuthError> {
        self.imp().authenticate(parent.upcast_ref(), callback).await
    }

    /// Reset the cross-signing keys while handling the interactive
    /// authentication flow.
    ///
    /// The type of flow and the required stages are negotiated during the
    /// authentication. Returns the last server response on success.
    pub(crate) async fn reset_cross_signing(
        &self,
        parent: &impl IsA<gtk::Widget>,
    ) -> Result<(), AuthError> {
        self.imp().reset_cross_signing(parent.upcast_ref()).await
    }
}