Skip to content

Commit 7ba5d17

Browse files
authored
Merge pull request #57 from encryption4all/fix/issue-56-smtp-credentials-separate-fields
feat: accept smtp_username and smtp_password as separate config fields
2 parents 614dfc7 + e622153 commit 7ba5d17

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

conf/config.dev.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ email_from = "noreply@postguard.local"
77
smtp_url = "mailcrab"
88
smtp_port = 1025
99
smtp_tls = false
10-
# smtp_credentials = ["user", "pw"]
10+
# smtp_username = "user"
11+
# smtp_password = "pw"
1112
allowed_origins = "^https?://(localhost|127\\.0\\.0\\.1)(:[0-9]+)?$"
1213
# pkg_url = "https://postguard-main.cs.ru.nl/pkg"
1314
# pkg_url = "https://pkg.staging.yivi.app"

conf/config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ data_dir = "/tmp/data"
55
email_from = "noreply@postguard.nl"
66
smtp_url = "mailcrab"
77
smtp_port = 1025
8-
# smtp_credentials = ["user", "pw"]
8+
# smtp_username = "user"
9+
# smtp_password = "pw"
910
allowed_origins = "^https://postguard.(eu|nl)$"
1011
pkg_url = "https://postguard-main.cs.ru.nl/pkg"

cryptify/src/config.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ pub struct RawCryptifyConfig {
77
email_from: String,
88
smtp_url: String,
99
smtp_port: u16,
10-
smtp_credentials: Option<(String, String)>,
10+
smtp_username: Option<String>,
11+
smtp_password: Option<String>,
1112
smtp_tls: Option<bool>,
1213
allowed_origins: String,
1314
pkg_url: String,
@@ -21,7 +22,8 @@ pub struct CryptifyConfig {
2122
email_from: lettre::message::Mailbox,
2223
smtp_url: String,
2324
smtp_port: u16,
24-
smtp_credentials: Option<(String, String)>,
25+
smtp_username: Option<String>,
26+
smtp_password: Option<String>,
2527
smtp_tls: bool,
2628
allowed_origins: String,
2729
pkg_url: String,
@@ -38,7 +40,8 @@ impl From<RawCryptifyConfig> for CryptifyConfig {
3840
}),
3941
smtp_url: config.smtp_url,
4042
smtp_port: config.smtp_port,
41-
smtp_credentials: config.smtp_credentials,
43+
smtp_username: config.smtp_username,
44+
smtp_password: config.smtp_password,
4245
smtp_tls: config.smtp_tls.unwrap_or(true),
4346
allowed_origins: config.allowed_origins,
4447
pkg_url: config.pkg_url,
@@ -67,8 +70,12 @@ impl CryptifyConfig {
6770
self.smtp_port
6871
}
6972

70-
pub fn smtp_credentials(&self) -> Option<&(String, String)> {
71-
self.smtp_credentials.as_ref()
73+
pub fn smtp_username(&self) -> Option<&str> {
74+
self.smtp_username.as_deref()
75+
}
76+
77+
pub fn smtp_password(&self) -> Option<&str> {
78+
self.smtp_password.as_deref()
7279
}
7380

7481
pub fn smtp_tls(&self) -> bool {

cryptify/src/email.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub async fn send_email(
172172
config.smtp_url(),
173173
config.smtp_port(),
174174
config.smtp_tls(),
175-
config.smtp_credentials().is_some()
175+
config.smtp_username().is_some()
176176
);
177177
let mut mailer_builder = if config.smtp_tls() {
178178
SmtpTransport::starttls_relay(config.smtp_url())?.port(config.smtp_port())
@@ -183,7 +183,7 @@ pub async fn send_email(
183183
mailer_builder = mailer_builder.timeout(Some(std::time::Duration::from_secs(10)));
184184

185185
// add credentials, if present
186-
if let Some((username, password)) = config.smtp_credentials() {
186+
if let (Some(username), Some(password)) = (config.smtp_username(), config.smtp_password()) {
187187
let credentials = Credentials::new(username.to_owned(), password.to_owned());
188188
mailer_builder = mailer_builder.credentials(credentials);
189189
}

0 commit comments

Comments
 (0)