make SMTP authentication optionnal, let lettre pick the better auth mechanism

This commit is contained in:
Jean-Christophe BEGUE 2018-08-15 17:00:55 +02:00
parent d68f57cbba
commit 401aa7c699
2 changed files with 32 additions and 26 deletions

View file

@ -3,7 +3,7 @@ use native_tls::TlsConnector;
use native_tls::{Protocol};
use lettre::{EmailTransport, SmtpTransport, ClientTlsParameters, ClientSecurity};
use lettre::smtp::{ConnectionReuseParameters, SmtpTransportBuilder};
use lettre::smtp::authentication::{Credentials, Mechanism};
use lettre::smtp::authentication::Credentials;
use lettre_email::EmailBuilder;
use MailConfig;
@ -11,10 +11,7 @@ use MailConfig;
fn mailer(config: &MailConfig) -> SmtpTransport {
let client_security = if config.smtp_ssl {
let mut tls_builder = TlsConnector::builder().unwrap();
tls_builder.supported_protocols(&[
Protocol::Tlsv10, Protocol::Tlsv11, Protocol::Tlsv12
]).unwrap();
tls_builder.supported_protocols(&[Protocol::Tlsv11, Protocol::Tlsv12]).unwrap();
ClientSecurity::Required(
ClientTlsParameters::new(config.smtp_host.to_owned(), tls_builder.build().unwrap())
)
@ -22,12 +19,21 @@ fn mailer(config: &MailConfig) -> SmtpTransport {
ClientSecurity::None
};
SmtpTransportBuilder::new((config.smtp_host.to_owned().as_str(), config.smtp_port), client_security)
.unwrap()
.credentials(Credentials::new(config.smtp_username.to_owned(), config.smtp_password.to_owned()))
.authentication_mechanism(Mechanism::Login)
let smtp_transport = SmtpTransportBuilder::new(
(config.smtp_host.to_owned().as_str(), config.smtp_port),
client_security
).unwrap();
let smtp_transport = match (&config.smtp_username, &config.smtp_password) {
(Some(username), Some(password)) => {
smtp_transport.credentials(Credentials::new(username.to_owned(), password.to_owned()))
},
(_, _) => smtp_transport,
};
smtp_transport
.smtp_utf8(true)
.connection_reuse(ConnectionReuseParameters::ReuseUnlimited)
.connection_reuse(ConnectionReuseParameters::NoReuse)
.build()
}

View file

@ -163,13 +163,13 @@ pub struct MailConfig {
smtp_port: u16,
smtp_ssl: bool,
smtp_from: String,
smtp_username: String,
smtp_password: String,
smtp_username: Option<String>,
smtp_password: Option<String>,
}
impl MailConfig {
fn load() -> Option<Self> {
let smtp_host = util::parse_option_string(env::var("SMTP_HOST").ok());
let smtp_host = env::var("SMTP_HOST").ok();
// When SMTP_HOST is absent, we assume the user does not want to enable it.
if smtp_host.is_none() {
@ -186,24 +186,24 @@ impl MailConfig {
}
});
let smtp_username = env::var("SMTP_USERNAME").ok();
let smtp_password = env::var("SMTP_PASSWORD").ok().or_else(|| {
if smtp_username.as_ref().is_some() {
println!("Please specify SMTP_PASSWORD to enable SMTP support.");
exit(1);
} else {
None
}
});
Some(MailConfig {
smtp_host: smtp_host.unwrap(),
smtp_port: smtp_port,
smtp_ssl: smtp_ssl,
smtp_from: util::parse_option_string(env::var("SMTP_FROM").ok())
.unwrap_or("bitwarden@localhost".to_string()),
// If username or password is not specified and SMTP support seems to be wanted,
// don't let the app start: the configuration is clearly incomplete.
smtp_username: util::parse_option_string(env::var("SMTP_USERNAME").ok())
.unwrap_or_else(|| {
println!("Please specify SMTP_USERNAME to enable SMTP support.");
exit(1);
}),
smtp_password: util::parse_option_string(env::var("SMTP_PASSWORD").ok())
.unwrap_or_else(|| {
println!("Please specify SMTP_PASSWORD to enable SMTP support.");
exit(1);
}),
.unwrap_or("bitwarden-rs@localhost".to_string()),
smtp_username: smtp_username,
smtp_password: smtp_password,
})
}
}