From 1830f084c0ea0d116814e9344c783d91ad49059a Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sun, 2 Aug 2015 19:12:59 +0200 Subject: [PATCH] Let the user configure the authentication mecanisms --- src/sender.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/sender.rs b/src/sender.rs index b676999..48f4858 100644 --- a/src/sender.rs +++ b/src/sender.rs @@ -25,6 +25,8 @@ pub struct SenderBuilder { credentials: Option<(String, String)>, /// Socket we are connecting to server_addr: SocketAddr, + /// List of authentication mecanism, sorted by priority + authentication_mecanisms: Vec, } /// Builder for the SMTP Sender @@ -41,6 +43,7 @@ impl SenderBuilder { connection_reuse_count_limit: 100, enable_connection_reuse: false, hello_name: "localhost".to_string(), + authentication_mecanisms: vec![Mecanism::CramMd5, Mecanism::Plain], }), None => Err(From::from("Could nor resolve hostname")), } @@ -74,6 +77,12 @@ impl SenderBuilder { self.credentials = Some((username.to_string(), password.to_string())); self } + + /// Set the authentication mecanisms + pub fn authentication_mecanisms(mut self, mecanisms: Vec) -> SenderBuilder { + self.authentication_mecanisms = mecanisms; + self + } /// Build the SMTP client /// @@ -191,20 +200,23 @@ impl Sender { debug!("server {}", self.server_info.as_ref().unwrap()); } - // TODO: Use PLAIN AUTH in encrypted connections, CRAM-MD5 otherwise if self.client_info.credentials.is_some() && self.state.connection_reuse_count == 0 { let (username, password) = self.client_info.credentials.clone().unwrap(); - if self.server_info.as_ref().unwrap().supports_auth_mecanism(Mecanism::CramMd5) { - let result = self.client.auth(Mecanism::CramMd5, &username, &password); - try_smtp!(result, self); - } else if self.server_info.as_ref().unwrap().supports_auth_mecanism(Mecanism::Plain) { - let result = self.client.auth(Mecanism::Plain, &username, &password); - try_smtp!(result, self); - } else { - debug!("No supported authentication mecanisms available"); - } + let mut found = false; + + for mecanism in self.client_info.authentication_mecanisms.clone() { + if self.server_info.as_ref().unwrap().supports_auth_mecanism(mecanism) { + found = true; + let result = self.client.auth(mecanism, &username, &password); + try_smtp!(result, self); + } + } + + if !found { + debug!("No supported authentication mecanisms available"); + } } let current_message = try!(email.message_id().ok_or("Missing Message-ID"));