Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
7.5 KB
Raw Blame
pub mod attachments;
pub mod badge;
pub mod commands;
pub mod deep_link;
pub mod menu;
pub mod notifications;
pub mod servers;
pub mod sso;
pub mod state;
pub mod window;

use state::AppState;

pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_notification::init())
        .plugin(tauri_plugin_decorum::init())
        .plugin(tauri_plugin_autostart::init(
            tauri_plugin_autostart::MacosLauncher::LaunchAgent,
            None,
        ))
        .plugin(tauri_plugin_deep_link::init())
        .manage(AppState::default())
        .invoke_handler(tauri::generate_handler![
            commands::list_servers,
            commands::add_server,
            commands::remove_server,
            commands::check_server,
            commands::active_server,
            commands::set_active_server,
            commands::get_launch_at_login,
            commands::set_launch_at_login,
            commands::start_sso,
        ])
        .setup(|app| {
            window::setup(app)?;
            let menu = menu::build(app.handle())?;
            app.set_menu(menu)?;
            app.on_menu_event(|app, event| menu::on_event(app, event.id().as_ref()));
            notifications::register(app.handle());
            badge::register(app.handle());
            // Grant runtime IPC capabilities for every already-known server
            // origin (saved + active) so the bridge works when the app resumes
            // or switches to them — instead of a static wildcard capability.
            for entry in servers::ServerStore::load() {
                commands::grant_server_capability(app.handle(), &entry.url);
            }
            if let Some(active) = servers::load_active() {
                commands::grant_server_capability(app.handle(), &active);
            }
            {
                // Hide windows on close instead of destroying them, so reopening
                // keeps working (a destroyed webview makes get_webview_window
                // return None). For "main" this also lets the dock icon re-show
                // it via the RunEvent::Reopen handler below.
                use tauri::Manager;
                for label in ["main", "prefs"] {
                    if let Some(win) = app.get_webview_window(label) {
                        let win_for_event = win.clone();
                        win.on_window_event(move |event| {
                            if let tauri::WindowEvent::CloseRequested { api, .. } = event {
                                api.prevent_close();
                                let _ = win_for_event.hide();
                            }
                        });
                    }
                }
            }
            {
                // The remote Sure page can emit events but cannot invoke custom
                // commands, so SSO is triggered via an event instead of invoke.
                use tauri::Listener;
                let handle = app.handle().clone();
                app.listen_any("sure://start-sso", move |event| {
                    #[derive(serde::Deserialize)]
                    struct StartSso {
                        server: String,
                        provider: String,
                    }
                    if let Ok(p) = serde_json::from_str::<StartSso>(event.payload()) {
                        if let Err(e) = commands::begin_sso(&handle, p.server, p.provider) {
                            eprintln!("[sure] start-sso failed: {e}");
                        }
                    }
                });
            }
            {
                use tauri::Manager;
                use tauri_plugin_deep_link::DeepLinkExt;
                let handle = app.handle().clone();
                app.deep_link().on_open_url(move |event| {
                    for url in event.urls() {
                        let u = url.as_str();
                        // SSO handoff first: exchange the one-time code (bound to
                        // our stored PKCE verifier) for a session in the webview.
                        // POST it via a form so the verifier never appears in a
                        // URL / server log (RFC 7636 keeps the verifier secret).
                        if let Some(cb) = deep_link::parse_sso_callback(u) {
                            let pending = handle.state::<AppState>().pending_sso.lock().unwrap().take();
                            if let (deep_link::SsoCallback::Code(code), Some(p)) = (cb, pending) {
                                if let Some(w) = handle.get_webview_window("main") {
                                    let action = format!("{}/sessions/desktop_exchange", p.server);
                                    let js = format!(
                                        "(function(){{var f=document.createElement('form');f.method='POST';f.action={};\
                                         var c=document.createElement('input');c.type='hidden';c.name='code';c.value={};f.appendChild(c);\
                                         var v=document.createElement('input');v.type='hidden';v.name='code_verifier';v.value={};f.appendChild(v);\
                                         document.body.appendChild(f);f.submit();}})();",
                                        serde_json::to_string(&action).unwrap_or_default(),
                                        serde_json::to_string(&code).unwrap_or_default(),
                                        serde_json::to_string(&p.verifier).unwrap_or_default(),
                                    );
                                    let _ = w.eval(&js);
                                }
                            }
                            continue;
                        }
                        // Generic sure://{host}/{path} navigation — only to a
                        // server the user has saved, so a malicious deep link
                        // can't load an arbitrary origin into the main webview.
                        if let Some(target) = deep_link::parse(u) {
                            // Gate on the destination, not the bare origin: a
                            // saved server may be mounted under a path, and the
                            // link's own path is what lands inside it.
                            let dest = format!("{}{}", target.server, target.path);
                            if servers::is_known_server(&dest) {
                                if let Some(w) = handle.get_webview_window("main") {
                                    let _ = w.eval(&format!("window.location.assign({:?})", dest));
                                }
                            }
                        }
                    }
                });
            }
            Ok(())
        })
        .on_page_load(|window, payload| {
            if matches!(window.label(), "main" | "prefs")
                && payload.event() == tauri::webview::PageLoadEvent::Finished
            {
                const BRIDGE: &str = include_str!("../../dist/bridge.js");
                let _ = window.eval(BRIDGE);
            }
        })
        .build(tauri::generate_context!())
        .expect("error while running Sure Desktop")
        .run(|app, event| {
            // Clicking the dock icon (while the main window is hidden, not
            // destroyed) fires Reopen — re-show and focus the main window.
            if let tauri::RunEvent::Reopen { .. } = event {
                use tauri::Manager;
                if let Some(w) = app.get_webview_window("main") {
                    let _ = w.show();
                    let _ = w.set_focus();
                }
            }
        });
}