Skip to content

Commit 78f2f61

Browse files
XuleileonMohammod Al Amin Ashik
authored andcommitted
fix(gateway): restore disabled auth on auto-start
Signed-off-by: Xuleileon <xuleileon19932022@163.com>
1 parent 049de49 commit 78f2f61

2 files changed

Lines changed: 59 additions & 12 deletions

File tree

apps/desktop/src-tauri/src/commands/gateway.rs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,22 @@ pub(crate) async fn load_network_access(app_state: &AppState) -> bool {
217217
load_network_access_from_repo(&app_state.settings_repository).await
218218
}
219219

220+
pub(crate) async fn load_gateway_auth_disabled_from_repo(
221+
settings_repository: &Arc<dyn mcpmux_core::AppSettingsRepository>,
222+
) -> bool {
223+
settings_repository
224+
.get(GATEWAY_AUTH_DISABLED_KEY)
225+
.await
226+
.ok()
227+
.flatten()
228+
.map(|value| value == "true")
229+
.unwrap_or(false)
230+
}
231+
232+
pub(crate) async fn load_gateway_auth_disabled(app_state: &AppState) -> bool {
233+
load_gateway_auth_disabled_from_repo(&app_state.settings_repository).await
234+
}
235+
220236
pub(crate) fn advertised_base_url(public_base_url: Option<&str>, port: u16) -> String {
221237
public_base_url
222238
.map(str::trim)
@@ -1037,18 +1053,8 @@ pub async fn start_gateway(
10371053
// Seed the system-wide inbound-auth toggle into the running gateway from
10381054
// persisted settings (default: auth required). Live changes go through
10391055
// `set_gateway_auth_disabled`.
1040-
{
1041-
let disabled = app_state
1042-
.settings_repository
1043-
.get(GATEWAY_AUTH_DISABLED_KEY)
1044-
.await
1045-
.ok()
1046-
.flatten()
1047-
.map(|v| v == "true")
1048-
.unwrap_or(false);
1049-
if disabled {
1050-
gw_state.write().await.set_auth_disabled(true);
1051-
}
1056+
if load_gateway_auth_disabled(&app_state).await {
1057+
gw_state.write().await.set_auth_disabled(true);
10521058
}
10531059

10541060
// Subscribe to OAuth completions BEFORE spawn so we don't miss early
@@ -2041,3 +2047,37 @@ mod public_base_url_tests {
20412047
assert_eq!(super::bind_host_for(true), "0.0.0.0");
20422048
}
20432049
}
2050+
2051+
#[cfg(test)]
2052+
mod gateway_auth_settings_tests {
2053+
use super::{load_gateway_auth_disabled_from_repo, GATEWAY_AUTH_DISABLED_KEY};
2054+
use mcpmux_core::AppSettingsRepository;
2055+
use mcpmux_storage::{Database, SqliteAppSettingsRepository};
2056+
use std::sync::Arc;
2057+
use tokio::sync::Mutex;
2058+
2059+
fn settings_repo() -> Arc<dyn AppSettingsRepository> {
2060+
let database = Database::open_in_memory().expect("create in-memory database");
2061+
Arc::new(SqliteAppSettingsRepository::new(Arc::new(Mutex::new(
2062+
database,
2063+
))))
2064+
}
2065+
2066+
#[tokio::test]
2067+
async fn auth_remains_required_when_disable_setting_is_missing() {
2068+
let repository = settings_repo();
2069+
2070+
assert!(!load_gateway_auth_disabled_from_repo(&repository).await);
2071+
}
2072+
2073+
#[tokio::test]
2074+
async fn persisted_disable_setting_is_restored_on_gateway_start() {
2075+
let repository = settings_repo();
2076+
repository
2077+
.set(GATEWAY_AUTH_DISABLED_KEY, "true")
2078+
.await
2079+
.unwrap();
2080+
2081+
assert!(load_gateway_auth_disabled_from_repo(&repository).await);
2082+
}
2083+
}

apps/desktop/src-tauri/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ pub fn run() {
425425
// devices on the LAN can reach the gateway; loopback-only otherwise.
426426
let network_access =
427427
crate::commands::gateway::load_network_access_from_repo(&settings_repo).await;
428+
let auth_disabled =
429+
crate::commands::gateway::load_gateway_auth_disabled_from_repo(&settings_repo)
430+
.await;
428431
let local_url = format!("http://localhost:{}", final_port);
429432
info!("Auto-starting gateway on {} (advertising {})", local_url, url);
430433

@@ -484,6 +487,10 @@ pub fn run() {
484487
let server = mcpmux_gateway::GatewayServer::new(config, dependencies);
485488
let gw_inner_state = server.state();
486489

490+
if auth_disabled {
491+
gw_inner_state.write().await.set_auth_disabled(true);
492+
}
493+
487494
// Get services from gateway
488495
let pool_service = server.pool_service();
489496
let feature_service = server.feature_service();

0 commit comments

Comments
 (0)