From 49246db6d99ffd3bf9754b006e81a79e3bfc84b4 Mon Sep 17 00:00:00 2001 From: YuMo12268 <1404551459@qq.com> Date: Wed, 22 Jul 2026 12:39:13 +0800 Subject: [PATCH 1/3] fix(client): honor refresh during initial connect --- netmito/src/client/http.rs | 2 + netmito/src/client/mod.rs | 18 ++++----- netmito/src/service/auth/cred.rs | 69 +++++++++++++++++++++++--------- netmito/src/worker.rs | 1 + 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/netmito/src/client/http.rs b/netmito/src/client/http.rs index dfc7360..cc84fed 100644 --- a/netmito/src/client/http.rs +++ b/netmito/src/client/http.rs @@ -36,6 +36,7 @@ impl MitoHttpClient { credential_path: Option, user: Option, password: Option, + refresh: bool, ) -> crate::error::Result { let client_credential_path = credential_path .as_ref() @@ -56,6 +57,7 @@ impl MitoHttpClient { self.url.clone(), user, password, + refresh, ) .await?; self.credential_path = client_credential_path; diff --git a/netmito/src/client/mod.rs b/netmito/src/client/mod.rs index e77cdf2..9a5ae70 100644 --- a/netmito/src/client/mod.rs +++ b/netmito/src/client/mod.rs @@ -84,25 +84,23 @@ impl MitoClient { pub async fn setup(config: ClientConfig) -> crate::error::Result { tracing::debug!("Client is setting up"); - let refresh = config.refresh; let mut http_client = MitoHttpClient::new(config.coordinator_addr); let username = http_client - .connect(config.credential_path, config.user, config.password) + .connect( + config.credential_path, + config.user, + config.password, + config.refresh, + ) .await?; - let mut client = MitoClient { + Ok(MitoClient { http_client, username, redis_client: None, redis_pubsub_client: None, async_redis_client: None, - }; - - if refresh { - client.refresh_token().await?; - } - - Ok(client) + }) } pub fn http_client(&self) -> &http::MitoHttpClient { diff --git a/netmito/src/service/auth/cred.rs b/netmito/src/service/auth/cred.rs index a10c7d3..2061804 100644 --- a/netmito/src/service/auth/cred.rs +++ b/netmito/src/service/auth/cred.rs @@ -179,6 +179,7 @@ pub async fn get_user_credential( mut url: Url, user: Option, password: Option, + refresh: bool, ) -> crate::error::Result<(String, String)> { // Try to load credential from file let cred_path = cred_path @@ -197,34 +198,62 @@ pub async fn get_user_credential( if cred_path.exists() { if let Ok(mut lines) = read_lines(&cred_path).await { if let Some((username, cred)) = extract_credential(user.as_ref(), &mut lines).await? { - url.set_path("auth"); - let resp = client - .get(url.as_str()) - .bearer_auth(&cred) - .send() - .await - .map_err(|e| { - if e.is_request() && e.is_connect() { - url.set_path(""); - RequestError::ConnectionError(url.to_string()) - } else { - e.into() + if refresh { + url.set_path("refresh"); + let resp = client + .post(url.as_str()) + .bearer_auth(&cred) + .send() + .await + .map_err(|e| { + if e.is_request() && e.is_connect() { + url.set_path(""); + RequestError::ConnectionError(url.to_string()) + } else { + e.into() + } + })?; + if resp.status().is_success() { + let resp = resp + .json::() + .await + .map_err(RequestError::from)?; + let token = resp.token; + modify_or_append_credential(&cred_path, &username, &token).await?; + return Ok((username, token)); + } else if resp.status().is_server_error() { + return Err(ApiError::InternalServerError.into()); + } + } else { + url.set_path("auth"); + let resp = client + .get(url.as_str()) + .bearer_auth(&cred) + .send() + .await + .map_err(|e| { + if e.is_request() && e.is_connect() { + url.set_path(""); + RequestError::ConnectionError(url.to_string()) + } else { + e.into() + } + })?; + if resp.status().is_success() { + let resp_name = resp.text().await.map_err(RequestError::from)?; + if resp_name == username { + return Ok((username, cred)); } - })?; - if resp.status().is_success() { - let resp_name = resp.text().await.map_err(RequestError::from)?; - if resp_name == username { - return Ok((username, cred)); + } else if resp.status().is_server_error() { + return Err(ApiError::InternalServerError.into()); } - } else if resp.status().is_server_error() { - return Err(ApiError::InternalServerError.into()); } } } } // Local credential not found or invalid, need to login tracing::warn!("Local credential not found or invalid, need to login"); - let req = fill_user_login(user, password, false)?; + let req = fill_user_login(user, password, refresh)?; url.set_path("login"); let resp = client .post(url.as_str()) diff --git a/netmito/src/worker.rs b/netmito/src/worker.rs index 7b1bc69..b9607eb 100644 --- a/netmito/src/worker.rs +++ b/netmito/src/worker.rs @@ -257,6 +257,7 @@ impl MitoWorker { config.coordinator_addr.clone(), config.user.take(), config.password.take(), + false, ) .await?; let mut url = config.coordinator_addr.clone(); From ded9cfe5cf4e2da00c4128ec6354e0357aec541e Mon Sep 17 00:00:00 2001 From: YuMo12268 <1404551459@qq.com> Date: Sun, 26 Jul 2026 11:22:45 +0800 Subject: [PATCH 2/3] refactor(auth): combine credential request branches --- netmito/src/service/auth/cred.rs | 57 +++++++++++--------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/netmito/src/service/auth/cred.rs b/netmito/src/service/auth/cred.rs index 2061804..5133f5f 100644 --- a/netmito/src/service/auth/cred.rs +++ b/netmito/src/service/auth/cred.rs @@ -198,22 +198,23 @@ pub async fn get_user_credential( if cred_path.exists() { if let Ok(mut lines) = read_lines(&cred_path).await { if let Some((username, cred)) = extract_credential(user.as_ref(), &mut lines).await? { - if refresh { - url.set_path("refresh"); - let resp = client - .post(url.as_str()) - .bearer_auth(&cred) - .send() - .await - .map_err(|e| { - if e.is_request() && e.is_connect() { - url.set_path(""); - RequestError::ConnectionError(url.to_string()) - } else { - e.into() - } - })?; - if resp.status().is_success() { + url.set_path(if refresh { "refresh" } else { "auth" }); + let request = if refresh { + client.post(url.as_str()) + } else { + client.get(url.as_str()) + }; + let resp = request.bearer_auth(&cred).send().await.map_err(|e| { + if e.is_request() && e.is_connect() { + url.set_path(""); + RequestError::ConnectionError(url.to_string()) + } else { + e.into() + } + })?; + let status = resp.status(); + if status.is_success() { + if refresh { let resp = resp .json::() .await @@ -221,32 +222,14 @@ pub async fn get_user_credential( let token = resp.token; modify_or_append_credential(&cred_path, &username, &token).await?; return Ok((username, token)); - } else if resp.status().is_server_error() { - return Err(ApiError::InternalServerError.into()); - } - } else { - url.set_path("auth"); - let resp = client - .get(url.as_str()) - .bearer_auth(&cred) - .send() - .await - .map_err(|e| { - if e.is_request() && e.is_connect() { - url.set_path(""); - RequestError::ConnectionError(url.to_string()) - } else { - e.into() - } - })?; - if resp.status().is_success() { + } else { let resp_name = resp.text().await.map_err(RequestError::from)?; if resp_name == username { return Ok((username, cred)); } - } else if resp.status().is_server_error() { - return Err(ApiError::InternalServerError.into()); } + } else if status.is_server_error() { + return Err(ApiError::InternalServerError.into()); } } } From 487485dd5cd700ccfe2f148ac9425ace76e539bc Mon Sep 17 00:00:00 2001 From: YuMo12268 <1404551459@qq.com> Date: Mon, 27 Jul 2026 09:15:33 +0800 Subject: [PATCH 3/3] refactor(auth): combine request setup branches --- netmito/src/service/auth/cred.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netmito/src/service/auth/cred.rs b/netmito/src/service/auth/cred.rs index 5133f5f..0f0f57f 100644 --- a/netmito/src/service/auth/cred.rs +++ b/netmito/src/service/auth/cred.rs @@ -198,10 +198,11 @@ pub async fn get_user_credential( if cred_path.exists() { if let Ok(mut lines) = read_lines(&cred_path).await { if let Some((username, cred)) = extract_credential(user.as_ref(), &mut lines).await? { - url.set_path(if refresh { "refresh" } else { "auth" }); let request = if refresh { + url.set_path("refresh"); client.post(url.as_str()) } else { + url.set_path("auth"); client.get(url.as_str()) }; let resp = request.bearer_auth(&cred).send().await.map_err(|e| {