diff --git a/distant-auth/src/methods/none.rs b/distant-auth/src/methods/none.rs index 237598d..e22d4f5 100644 --- a/distant-auth/src/methods/none.rs +++ b/distant-auth/src/methods/none.rs @@ -5,11 +5,13 @@ use async_trait::async_trait; use crate::authenticator::Authenticator; use crate::methods::AuthenticationMethod; -/// Authenticaton method for a static secret key +/// Authenticaton method that skips authentication and approves anything. #[derive(Clone, Debug)] pub struct NoneAuthenticationMethod; impl NoneAuthenticationMethod { + pub const ID: &str = "none"; + #[inline] pub fn new() -> Self { Self @@ -26,7 +28,7 @@ impl Default for NoneAuthenticationMethod { #[async_trait] impl AuthenticationMethod for NoneAuthenticationMethod { fn id(&self) -> &'static str { - "none" + Self::ID } async fn authenticate(&self, _: &mut dyn Authenticator) -> io::Result<()> { diff --git a/distant-auth/src/methods/static_key.rs b/distant-auth/src/methods/static_key.rs index 56cd556..72da3ca 100644 --- a/distant-auth/src/methods/static_key.rs +++ b/distant-auth/src/methods/static_key.rs @@ -14,6 +14,8 @@ pub struct StaticKeyAuthenticationMethod { } impl StaticKeyAuthenticationMethod { + pub const ID: &str = "static_key"; + #[inline] pub fn new(key: T) -> Self { Self { key } @@ -26,7 +28,7 @@ where T: FromStr + PartialEq + Send + Sync, { fn id(&self) -> &'static str { - "static_key" + Self::ID } async fn authenticate(&self, authenticator: &mut dyn Authenticator) -> io::Result<()> { diff --git a/src/cli/common/client.rs b/src/cli/common/client.rs index 73dd481..793b688 100644 --- a/src/cli/common/client.rs +++ b/src/cli/common/client.rs @@ -4,7 +4,8 @@ use std::time::Duration; use async_trait::async_trait; use distant_core::net::auth::msg::*; use distant_core::net::auth::{ - AuthHandler, AuthMethodHandler, PromptAuthMethodHandler, SingleAuthHandler, + AuthHandler, AuthMethodHandler, NoneAuthenticationMethod, PromptAuthMethodHandler, + SingleAuthHandler, }; use distant_core::net::client::{Client as NetClient, ClientConfig, ReconnectStrategy}; use distant_core::net::manager::ManagerClient; @@ -145,11 +146,16 @@ impl Client { pub struct JsonAuthHandler { tx: MsgSender, rx: MsgReceiver, + skip: bool, } impl JsonAuthHandler { pub fn new(tx: MsgSender, rx: MsgReceiver) -> Self { - Self { tx, rx } + Self { + tx, + rx, + skip: false, + } } } @@ -165,6 +171,23 @@ impl AuthHandler for JsonAuthHandler { &mut self, initialization: Initialization, ) -> io::Result { + // NOTE: This is a hack to skip the need for authentication prompting when a "none" + // method is available as the server should then reply with the on_finished + // status automatically. + if initialization + .methods + .iter() + .any(|id| id == NoneAuthenticationMethod::ID) + { + self.skip = true; + + // NOTE: We only send back the none auth method to ensure that it is performed + // first to avoid blocking waiting on failing a different method. + return Ok(InitializationResponse { + methods: vec![NoneAuthenticationMethod::ID.to_string()], + }); + } + self.tx .send_blocking(&Authentication::Initialization(initialization))?; let response = self.rx.recv_blocking::()?; @@ -179,12 +202,20 @@ impl AuthHandler for JsonAuthHandler { } async fn on_start_method(&mut self, start_method: StartMethod) -> io::Result<()> { + if self.skip { + return Ok(()); + } + self.tx .send_blocking(&Authentication::StartMethod(start_method))?; Ok(()) } async fn on_finished(&mut self) -> io::Result<()> { + if self.skip { + return Ok(()); + } + self.tx.send_blocking(&Authentication::Finished)?; Ok(()) } diff --git a/tests/stress_tests.rs b/tests/stress_tests.rs index 487dd2f..629070a 100644 --- a/tests/stress_tests.rs +++ b/tests/stress_tests.rs @@ -17,7 +17,6 @@ fn should_handle_large_volume_of_requests(ctx: DistantManagerCtx) { // Perform many requests of writing a file and reading a file for i in 1..100 { - println!("Writing {i} to file {path:?}"); ctx.new_assert_cmd(["fs", "write"]) .arg(path.to_str().unwrap()) .write_stdin(format!("idx: {i}"))