configuration: Add smtp config values.

This commit is contained in:
Valentin Tolmer 2021-11-03 17:09:19 +09:00 committed by nitnelave
parent 350fdcdf9b
commit 18e3892e55
5 changed files with 71 additions and 5 deletions

1
Cargo.lock generated
View File

@ -1718,6 +1718,7 @@ dependencies = [
"quoted_printable", "quoted_printable",
"r2d2", "r2d2",
"regex", "regex",
"serde",
"tokio", "tokio",
"tokio-native-tls", "tokio-native-tls",
] ]

View File

@ -66,3 +66,25 @@ database_url = "sqlite:///data/users.db?mode=rwc"
## each password. ## each password.
## Randomly generated on first run if it doesn't exist. ## Randomly generated on first run if it doesn't exist.
key_file = "/data/private_key" key_file = "/data/private_key"
## Options to configure SMTP parameters, to send password reset emails.
## To set these options from environment variables, use the following format
## (example with "password"): LLDAP_SMTP_OPTIONS__PASSWORD
#[smtp_options]
## Whether to enabled password reset via email, from LLDAP.
#enable_password_reset=true
## The SMTP server.
#server="smtp.gmail.com"
## The SMTP port.
#port=587
## Whether to connect with TLS.
#tls_required=true
## The SMTP user, usually your email address.
#user="sender@gmail.com"
## The SMTP password.
#password="password"
## The header field, optional: how the sender appears in the email. The first
## is a free-form name, followed by an email between <>.
#from="LLDAP Admin <sender@gmail.com>"
## Same for reply-to, optional.
#reply_to="Do not reply <noreply@localhost>"

View File

@ -57,6 +57,7 @@ rev = "eb59676a940b15f77871aefe1e46d7b5bf85f40a"
version = "0.10.0-rc.3" version = "0.10.0-rc.3"
features = [ features = [
"builder", "builder",
"serde",
"smtp-transport", "smtp-transport",
"tokio1-native-tls", "tokio1-native-tls",
"tokio1", "tokio1",

View File

@ -21,7 +21,7 @@ pub enum Command {
#[derive(Debug, Clap, Clone)] #[derive(Debug, Clap, Clone)]
pub struct RunOpts { pub struct RunOpts {
/// Change config file name /// Change config file name.
#[clap(short, long, default_value = "lldap_config.toml")] #[clap(short, long, default_value = "lldap_config.toml")]
pub config_file: String, pub config_file: String,
@ -33,7 +33,11 @@ pub struct RunOpts {
#[clap(long)] #[clap(long)]
pub ldaps_port: Option<u16>, pub ldaps_port: Option<u16>,
/// Set verbose logging /// Change HTTP API port. Default: 17170
#[clap(long)]
pub http_port: Option<u16>,
/// Set verbose logging.
#[clap(short, long)] #[clap(short, long)]
pub verbose: bool, pub verbose: bool,
} }

View File

@ -1,13 +1,39 @@
use crate::infra::cli::RunOpts;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use figment::{ use figment::{
providers::{Env, Format, Serialized, Toml}, providers::{Env, Format, Serialized, Toml},
Figment, Figment,
}; };
use lettre::message::Mailbox;
use lldap_auth::opaque::{server::ServerSetup, KeyPair}; use lldap_auth::opaque::{server::ServerSetup, KeyPair};
use log::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::infra::cli::RunOpts; #[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)]
#[builder(pattern = "owned")]
pub struct MailOptions {
#[builder(default = "false")]
pub enable_password_reset: bool,
#[builder(default = "None")]
pub from: Option<Mailbox>,
#[builder(default = "None")]
pub reply_to: Option<Mailbox>,
#[builder(default = r#""localhost".to_string()"#)]
pub server: String,
#[builder(default = "587")]
pub port: u16,
#[builder(default = r#""admin".to_string()"#)]
pub user: String,
#[builder(default = r#""".to_string()"#)]
pub password: String,
#[builder(default = "true")]
pub tls_required: bool,
}
impl std::default::Default for MailOptions {
fn default() -> Self {
MailOptionsBuilder::default().build().unwrap()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)] #[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)]
#[builder( #[builder(
@ -35,11 +61,19 @@ pub struct Configuration {
pub verbose: bool, pub verbose: bool,
#[builder(default = r#"String::from("server_key")"#)] #[builder(default = r#"String::from("server_key")"#)]
pub key_file: String, pub key_file: String,
#[builder(default)]
pub smtp_options: MailOptions,
#[serde(skip)] #[serde(skip)]
#[builder(field(private), setter(strip_option))] #[builder(field(private), setter(strip_option))]
server_setup: Option<ServerSetup>, server_setup: Option<ServerSetup>,
} }
impl std::default::Default for Configuration {
fn default() -> Self {
ConfigurationBuilder::default().build().unwrap()
}
}
impl ConfigurationBuilder { impl ConfigurationBuilder {
pub fn build(self) -> Result<Configuration> { pub fn build(self) -> Result<Configuration> {
let server_setup = get_server_setup(self.key_file.as_deref().unwrap_or("server_key"))?; let server_setup = get_server_setup(self.key_file.as_deref().unwrap_or("server_key"))?;
@ -77,6 +111,10 @@ impl Configuration {
self.ldaps_port = port; self.ldaps_port = port;
} }
if let Some(port) = cli_opts.http_port {
self.http_port = port;
}
self self
} }
} }
@ -108,7 +146,7 @@ pub fn init(cli_opts: RunOpts) -> Result<Configuration> {
ConfigurationBuilder::default().build().unwrap(), ConfigurationBuilder::default().build().unwrap(),
)) ))
.merge(Toml::file(config_file)) .merge(Toml::file(config_file))
.merge(Env::prefixed("LLDAP_")) .merge(Env::prefixed("LLDAP_").split("__"))
.extract()?; .extract()?;
let mut config = config.merge_with_cli(cli_opts); let mut config = config.merge_with_cli(cli_opts);