Skip to content

Commit 10322dc

Browse files
feat(auth): bootstrap aw-webui token on launch (#216)
1 parent f12def5 commit 10322dc

2 files changed

Lines changed: 89 additions & 64 deletions

File tree

‎src-tauri/Cargo.lock‎

Lines changed: 26 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src-tauri/src/lib.rs‎

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use tauri::{
4242
menu::{Menu, MenuItem},
4343
tray::{TrayIconBuilder, TrayIconId},
4444
webview::WebviewWindowBuilder,
45-
AppHandle, Manager,
45+
AppHandle, Manager, Url,
4646
};
4747

4848
pub struct AppHandleWrapper(Mutex<AppHandle>);
@@ -191,6 +191,17 @@ pub fn handle_first_run() {
191191
}
192192
}
193193

194+
fn build_dashboard_url(port: u16, api_key: Option<&str>) -> Url {
195+
let mut url =
196+
Url::parse(&format!("http://localhost:{port}/")).expect("Failed to parse localhost url");
197+
198+
if let Some(api_key) = api_key.filter(|key| !key.is_empty()) {
199+
url.query_pairs_mut().append_pair("token", api_key);
200+
}
201+
202+
url
203+
}
204+
194205
pub fn listen_for_lockfile() {
195206
thread::spawn(|| {
196207
let runtime_path = get_runtime_path();
@@ -538,22 +549,31 @@ pub fn run() {
538549
if testing {
539550
info!("Running in testing mode (port {})", port);
540551
}
552+
let dashboard_api_key = aw_config
553+
.auth
554+
.api_key
555+
.as_deref()
556+
.filter(|key| !key.is_empty());
557+
if dashboard_api_key.is_some() {
558+
info!("Bootstrapping aw-webui API token into dashboard URL");
559+
}
560+
let dashboard_url = build_dashboard_url(port, dashboard_api_key);
541561
tauri::async_runtime::spawn(build_rocket(server_state, aw_config).launch());
542-
let url = format!("http://localhost:{}/", port)
543-
.parse()
544-
.expect("Failed to parse localhost url");
545562
// Create main window programmatically to attach initialization script.
546563
// The script intercepts clicks on external links and opens them in the system
547564
// browser via the open_external Tauri command. This approach works reliably for
548565
// SPA-generated links where on_navigation (which only fires for top-level
549566
// webview navigations) would miss JS-driven internal route changes.
550-
let _main_window =
551-
WebviewWindowBuilder::new(app, "main", tauri::WebviewUrl::External(url))
552-
.title("aw-tauri")
553-
.inner_size(800.0, 600.0)
554-
.visible(false)
555-
.initialization_script(
556-
r#"
567+
let _main_window = WebviewWindowBuilder::new(
568+
app,
569+
"main",
570+
tauri::WebviewUrl::External(dashboard_url),
571+
)
572+
.title("aw-tauri")
573+
.inner_size(800.0, 600.0)
574+
.visible(false)
575+
.initialization_script(
576+
r#"
557577
document.addEventListener('click', function(e) {
558578
var el = e.target;
559579
while (el && el.tagName !== 'A') { el = el.parentElement; }
@@ -564,9 +584,9 @@ pub fn run() {
564584
}
565585
}, true);
566586
"#,
567-
)
568-
.build()
569-
.expect("Failed to create main window");
587+
)
588+
.build()
589+
.expect("Failed to create main window");
570590
let manager_state = manager::start_manager();
571591

572592
let open = MenuItem::with_id(app, "open", "Open Dashboard", true, None::<&str>)
@@ -656,3 +676,32 @@ pub fn run() {
656676
.run(tauri::generate_context!())
657677
.expect("error while running tauri application");
658678
}
679+
680+
#[cfg(test)]
681+
mod tests {
682+
use super::build_dashboard_url;
683+
684+
#[test]
685+
fn build_dashboard_url_omits_token_when_auth_disabled() {
686+
assert_eq!(
687+
build_dashboard_url(5600, None).as_str(),
688+
"http://localhost:5600/"
689+
);
690+
}
691+
692+
#[test]
693+
fn build_dashboard_url_ignores_empty_api_keys() {
694+
assert_eq!(
695+
build_dashboard_url(5600, Some("")).as_str(),
696+
"http://localhost:5600/"
697+
);
698+
}
699+
700+
#[test]
701+
fn build_dashboard_url_appends_encoded_token() {
702+
assert_eq!(
703+
build_dashboard_url(5600, Some("secret+ /?=&")).as_str(),
704+
"http://localhost:5600/?token=secret%2B+%2F%3F%3D%26"
705+
);
706+
}
707+
}

0 commit comments

Comments
 (0)