1
0
mirror of https://github.com/dani-garcia/vaultwarden.git synced 2024-11-11 13:32:38 +01:00

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

View File

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