Refactor reset_prepare() to reduce indents

This commit is contained in:
finga 2020-08-28 22:27:54 +02:00
parent 5b3e56954e
commit b155c17337

View file

@ -58,49 +58,40 @@ fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Res
ldap.unbind()?;
let (rs, _res) = result.success()?;
if rs.len() == 1 {
// generate key
let mut rng = rand::thread_rng();
let key: String = (0..64)
.map(|_| BASE62[rng.gen::<usize>() % 62] as char)
.collect();
// store key with id
let keys = Arc::clone(&keys.keys);
if let Ok(mut keys) = keys.lock() {
keys.insert(key.to_string(), email_address.to_string());
debug!("Generated new key for '{}'", email_address);
// generate email
let email = EmailBuilder::new()
.to(email_address)
.from("ldap0r@example.com")
.subject("LDAP password reset")
.text(format!(
"Use following url to set a new password: {}/reset/{}",
config.domain, key
))
.build()?;
// send email
let mut mailer = SmtpClient::new_unencrypted_localhost()?.transport();
let result = mailer.send(email.into());
if result.is_ok() {
info!("Password reset email was sent to '{}'", email_address);
} else {
bail!(
"Sending password reset email with reset URL to '{}' failed",
email_address
);
}
} else {
bail!("Could not aquire lock for keys");
};
} else {
if rs.len() != 1 {
bail!("Invalid password reset request for '{}'", email_address);
}
// generate key
let mut rng = rand::thread_rng();
let key: String = (0..64)
.map(|_| BASE62[rng.gen::<usize>() % 62] as char)
.collect();
// store key with id
let keys = Arc::clone(&keys.keys);
let mut keys = keys
.lock()
.map_err(|e| anyhow!("Could not aquire lock for keys: {}", e))?;
keys.insert(key.to_string(), email_address.to_string());
debug!("Generated new key for '{}'", email_address);
// generate email
let email = EmailBuilder::new()
.to(email_address)
.from("ldap0r@example.com")
.subject("LDAP password reset")
.text(format!(
"Use following url to set a new password: {}/reset/{}",
config.domain, key
))
.build()?;
// send email
let mut mailer = SmtpClient::new_unencrypted_localhost()?.transport();
mailer.send(email.into())?;
info!("Password reset email was sent to '{}'", email_address);
Ok(())
}