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
use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
};

use adw::prelude::*;
use futures_util::StreamExt;
use gettextrs::gettext;
use gtk::{
    gio,
    glib::{self, clone},
    subclass::prelude::*,
};
use search_provider::ResultMeta;

use crate::{
    config,
    models::{
        keyring, start as start_search_provider, Account, OTPUri, Provider, ProvidersModel,
        SearchProviderAction, FAVICONS_PATH, RUNTIME, SECRET_SERVICE, SETTINGS,
    },
    utils::{spawn, spawn_tokio_blocking},
    widgets::{KeyringErrorDialog, PreferencesWindow, ProvidersDialog, Window},
};

mod imp {
    use std::cell::{Cell, RefCell};

    use adw::subclass::prelude::*;

    use super::*;

    // The basic struct that holds our state and widgets
    // (Ref)Cells are used for members which need to be mutable
    #[derive(Default, glib::Properties)]
    #[properties(wrapper_type = super::Application)]
    pub struct Application {
        pub window: RefCell<Option<glib::WeakRef<Window>>>,
        pub model: ProvidersModel,
        #[property(get, set, construct)]
        pub is_locked: Cell<bool>,
        pub lock_timeout_id: RefCell<Option<glib::Source>>,
        #[property(get, set, construct)]
        pub can_be_locked: Cell<bool>,
        #[property(get, set, construct_only)]
        pub is_keyring_open: Cell<bool>,
    }

    // Sets up the basics for the GObject
    #[glib::object_subclass]
    impl ObjectSubclass for Application {
        const NAME: &'static str = "Application";
        type ParentType = adw::Application;
        type Type = super::Application;
    }

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

    // Overrides GApplication vfuncs
    impl ApplicationImpl for Application {
        fn startup(&self) {
            self.parent_startup();
            let app = self.obj();
            let quit_action = gio::ActionEntry::builder("quit")
                .activate(|app: &Self::Type, _, _| app.quit())
                .build();

            let preferences_action = gio::ActionEntry::builder("preferences")
                .activate(|app: &Self::Type, _, _| {
                    let model = &app.imp().model;
                    let window = app.active_window();
                    let preferences = PreferencesWindow::new(model);
                    preferences.set_has_set_password(app.can_be_locked());
                    preferences.connect_restore_completed(clone!(@weak window =>move |_| {
                        window.providers().refilter();
                        window.imp().toast_overlay.add_toast(adw::Toast::new(&gettext("Accounts restored successfully")));
                    }));
                    preferences.connect_has_set_password_notify(clone!(@weak app => move |pref| {
                        app.set_can_be_locked(pref.has_set_password());
                    }));
                    preferences.present(&window);
                }).build();

            // About
            let about_action = gio::ActionEntry::builder("about")
                .activate(|app: &Self::Type, _, _| {
                    let window = app.active_window();
                    adw::AboutDialog::builder()
                        .application_name(gettext("Authenticator"))
                        .version(config::VERSION)
                        .comments(gettext("Generate two-factor codes"))
                        .website("https://gitlab.gnome.org/World/Authenticator")
                        .developers(vec![
                            "Bilal Elmoussaoui",
                            "Maximiliano Sandoval",
                            "Christopher Davis",
                            "Julia Johannesen",
                        ])
                        .artists(vec!["Alexandros Felekidis", "Tobias Bernard"])
                        .translator_credits(gettext("translator-credits"))
                        .application_icon(config::APP_ID)
                        .license_type(gtk::License::Gpl30)
                        .build()
                        .present(&window);
                })
                .build();

            let providers_action = gio::ActionEntry::builder("providers")
                .activate(|app: &Self::Type, _, _| {
                    let model = &app.imp().model;
                    let window = app.active_window();
                    let providers = ProvidersDialog::new(model);
                    providers.connect_changed(clone!(@weak window => move |_| {
                        window.providers().refilter();
                    }));
                    providers.present(&window);
                })
                .build();

            let lock_action = gio::ActionEntry::builder("lock")
                .activate(|app: &Self::Type, _, _| app.set_is_locked(true))
                .build();

            app.add_action_entries([
                quit_action,
                about_action,
                lock_action,
                providers_action,
                preferences_action,
            ]);

            let lock_action = app.lookup_action("lock").unwrap();
            let preferences_action = app.lookup_action("preferences").unwrap();
            let providers_action = app.lookup_action("providers").unwrap();
            app.bind_property("can-be-locked", &lock_action, "enabled")
                .sync_create()
                .build();
            app.bind_property("is-locked", &preferences_action, "enabled")
                .invert_boolean()
                .sync_create()
                .build();
            app.bind_property("is-locked", &providers_action, "enabled")
                .invert_boolean()
                .sync_create()
                .build();

            app.connect_can_be_locked_notify(|app| {
                if !app.can_be_locked() {
                    app.cancel_lock_timeout();
                }
            });

            SETTINGS.connect_auto_lock_changed(clone!(@weak app => move |auto_lock| {
                if auto_lock {
                    app.restart_lock_timeout();
                } else {
                    app.cancel_lock_timeout();
                }
            }));

            SETTINGS.connect_auto_lock_timeout_changed(clone!(@weak app => move |_| {
                app.restart_lock_timeout()
            }));

            spawn(clone!(@strong app => async move {
                app.start_search_provider().await;
            }));
        }

        fn activate(&self) {
            let app = self.obj();

            if !app.is_keyring_open() {
                app.present_error_window();
                return;
            }

            if let Some(ref win) = *self.window.borrow() {
                let window = win.upgrade().unwrap();
                window.present();
                return;
            }

            let window = Window::new(&self.model, &app);
            window.present();
            self.window.replace(Some(window.downgrade()));

            app.set_accels_for_action("app.quit", &["<primary>q"]);
            app.set_accels_for_action("app.lock", &["<primary>l"]);
            app.set_accels_for_action("app.providers", &["<primary>p"]);
            app.set_accels_for_action("app.preferences", &["<primary>comma"]);
            app.set_accels_for_action("win.show-help-overlay", &["<primary>question"]);
            app.set_accels_for_action("win.search", &["<primary>f"]);
            app.set_accels_for_action("win.add_account", &["<primary>n"]);
            // Start the timeout to lock the app if the auto-lock
            // setting is enabled.
            app.restart_lock_timeout();
        }

        fn open(&self, files: &[gio::File], _hint: &str) {
            self.activate();
            let uris = files
                .iter()
                .filter_map(|f| f.uri().parse::<OTPUri>().ok())
                .collect::<Vec<OTPUri>>();
            // We only handle a single URI (see the desktop file)
            if let Some(uri) = uris.first() {
                let window = self.obj().active_window();
                window.open_add_account(Some(uri))
            }
        }
    }
    // This is empty, but we still need to provide an
    // empty implementation for each type we subclass.
    impl GtkApplicationImpl for Application {}

    impl AdwApplicationImpl for Application {}
}

// Creates a wrapper struct that inherits the functions
// from objects listed it @extends or interfaces it @implements.
// This is what allows us to do e.g. application.quit() on
// Application without casting.
glib::wrapper! {
    pub struct Application(ObjectSubclass<imp::Application>)
        @extends gio::Application, gtk::Application, adw::Application,
        @implements gio::ActionMap, gio::ActionGroup;
}

impl Application {
    pub fn run() -> glib::ExitCode {
        tracing::info!("Authenticator ({})", config::APP_ID);
        tracing::info!("Version: {} ({})", config::VERSION, config::PROFILE);
        tracing::info!("Datadir: {}", config::PKGDATADIR);

        std::fs::create_dir_all(&*FAVICONS_PATH).ok();

        // To be removed in the upcoming release
        if !SETTINGS.keyrings_migrated() {
            tracing::info!("Migrating the secrets to the file backend");
            let output: oo7::Result<()> = RUNTIME.block_on(async {
                oo7::migrate(
                    vec![
                        HashMap::from([("application", config::APP_ID), ("type", "token")]),
                        HashMap::from([("application", config::APP_ID), ("type", "password")]),
                    ],
                    false,
                )
                .await?;
                Ok(())
            });
            match output {
                Ok(_) => {
                    SETTINGS
                        .set_keyrings_migrated(true)
                        .expect("Failed to update settings");
                    tracing::info!("Secrets were migrated successfully");
                }
                Err(err) => {
                    tracing::error!("Failed to migrate your data {err}");
                }
            }
        }

        let is_keyring_open = spawn_tokio_blocking(async {
            match oo7::Keyring::new().await {
                Ok(keyring) => {
                    if let Err(err) = keyring.unlock().await {
                        tracing::error!("Could not unlock keyring: {err}");
                        false
                    } else {
                        SECRET_SERVICE.set(keyring).unwrap();
                        true
                    }
                }
                Err(err) => {
                    tracing::error!("Could not open keyring: {err}");
                    false
                }
            }
        });

        let has_set_password = if is_keyring_open {
            spawn_tokio_blocking(async { keyring::has_set_password().await.unwrap_or(false) })
        } else {
            false
        };
        let app = glib::Object::builder::<Application>()
            .property("application-id", config::APP_ID)
            .property("flags", gio::ApplicationFlags::HANDLES_OPEN)
            .property("resource-base-path", "/com/belmoussaoui/Authenticator")
            .property("is-locked", has_set_password)
            .property("can-be-locked", has_set_password)
            .property("is-keyring-open", is_keyring_open)
            .build();
        // Only load the model if the app is not locked
        if !has_set_password && is_keyring_open {
            app.imp().model.load();
        }

        app.run()
    }

    pub fn active_window(&self) -> Window {
        self.imp()
            .window
            .borrow()
            .as_ref()
            .unwrap()
            .upgrade()
            .unwrap()
    }

    /// Starts or restarts the lock timeout.
    pub fn restart_lock_timeout(&self) {
        let imp = self.imp();
        let auto_lock = SETTINGS.auto_lock();
        let timeout = SETTINGS.auto_lock_timeout() * 60;

        if !auto_lock {
            return;
        }

        self.cancel_lock_timeout();

        if !self.is_locked() && self.can_be_locked() {
            let (tx, rx) = futures_channel::oneshot::channel::<()>();
            let tx = Arc::new(Mutex::new(Some(tx)));
            let id = glib::source::timeout_source_new_seconds(
                timeout,
                None,
                glib::Priority::HIGH,
                clone!(@strong tx => move || {
                    let Some(tx) = tx.lock().unwrap().take() else {
                        return glib::ControlFlow::Break;
                    };
                    tx.send(()).unwrap();
                    glib::ControlFlow::Break
                }),
            );
            spawn(clone!(@strong self as app => async move {
                if let Ok(()) = rx.await {
                    app.set_is_locked(true);
                }
            }));
            id.attach(Some(&glib::MainContext::default()));
            imp.lock_timeout_id.replace(Some(id));
        }
    }

    fn cancel_lock_timeout(&self) {
        if let Some(id) = self.imp().lock_timeout_id.borrow_mut().take() {
            id.destroy();
        }
    }

    fn account_provider_by_identifier(&self, id: &str) -> Option<(Provider, Account)> {
        let identifier = id.split(':').collect::<Vec<&str>>();
        let provider_id = identifier.first()?.parse::<u32>().ok()?;
        let account_id = identifier.get(1)?.parse::<u32>().ok()?;

        let provider = self.imp().model.find_by_id(provider_id)?;
        let account = provider.accounts_model().find_by_id(account_id)?;

        Some((provider, account))
    }

    async fn start_search_provider(&self) {
        let mut receiver = match start_search_provider().await {
            Err(err) => {
                tracing::error!("Failed to start search provider {err}");
                return;
            }
            Ok(receiver) => receiver,
        };
        loop {
            let response = receiver.next().await.unwrap();
            match response {
                SearchProviderAction::LaunchSearch(terms, _) => {
                    self.activate();
                    let window = self.active_window();
                    window.imp().search_entry.set_text(&terms.join(" "));
                    window.imp().search_btn.set_active(true);
                    window.present();
                }
                SearchProviderAction::ActivateResult(id) => {
                    let notification = gio::Notification::new(&gettext("One-Time password copied"));
                    notification.set_body(Some(&gettext("Password was copied successfully")));
                    self.send_notification(Some(&id), &notification);
                    let Some((provider, _)) = self.account_provider_by_identifier(&id) else {
                        return;
                    };
                    glib::timeout_add_seconds_local_once(
                        provider.period(),
                        glib::clone!(@weak self as app => move || {
                            app.withdraw_notification(&id);
                        }),
                    );
                }
                SearchProviderAction::InitialResultSet(terms, sender) => {
                    // don't show any results if the application is locked
                    let response = if self.is_locked() {
                        vec![]
                    } else {
                        self.imp()
                            .model
                            .find_accounts(&terms)
                            .into_iter()
                            .map(|account| format!("{}:{}", account.provider().id(), account.id()))
                            .collect::<Vec<_>>()
                    };
                    sender.send(response).unwrap();
                }
                SearchProviderAction::ResultMetas(identifiers, sender) => {
                    let metas = identifiers
                        .iter()
                        .filter_map(|id| {
                            self.account_provider_by_identifier(id)
                                .map(|(provider, account)| {
                                    ResultMeta::builder(id.to_owned(), &account.name())
                                        .description(&provider.name())
                                        .clipboard_text(&account.code().replace(' ', ""))
                                        .build()
                                })
                        })
                        .collect::<Vec<_>>();
                    sender.send(metas).unwrap();
                }
            }
        }
    }

    fn present_error_window(&self) {
        let dialog = KeyringErrorDialog::new(self);
        dialog.present();
    }
}