From 0023e52f7941380ea38ff87037b430eb6f657369 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 30 Jul 2023 02:02:56 +0100 Subject: [PATCH] feat: central function for random string --- src/bin/new_users.rs | 10 ++-------- src/lib.rs | 9 ++++++++- src/methods/account_new.rs | 10 ++-------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/bin/new_users.rs b/src/bin/new_users.rs index 09bb5ac..05725f4 100644 --- a/src/bin/new_users.rs +++ b/src/bin/new_users.rs @@ -4,8 +4,7 @@ use lettre::{ Message, SmtpTransport, Transport, }; use maud::html; -use rand::{distributions::Alphanumeric, thread_rng, Rng}; -use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, AccountsNew, Config, Record, get_now_iso}; +use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, AccountsNew, Config, Record, get_now_iso, random_string}; use sqlx::{Pool, Sqlite}; #[async_std::main] @@ -26,7 +25,7 @@ async fn main() { } // generate a auth key - let auth = generate_auth(); + let auth = random_string(50); match send_mail(&config, &record, &auth) { Ok(_) => match save_to_db(&db, &record, &auth).await { @@ -75,11 +74,6 @@ async fn check_pending(db: &Pool, mail: &str) -> bool { .is_empty() } -// from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters -fn generate_auth() -> String { - thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect() -} - // using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs fn send_mail(config: &Config, record: &Record, auth: &str) -> Result { let recipient = &record.name_first; diff --git a/src/lib.rs b/src/lib.rs index f363e03..3a78225 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,9 +7,11 @@ use std::env; use std::str::FromStr; use std::time::{SystemTime, UNIX_EPOCH}; use chrono::{Datelike, SecondsFormat, Utc}; +use rand::distributions::Alphanumeric; +use rand::{Rng, thread_rng}; use tide::prelude::*; -#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] +#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)] pub struct AccountsNew { pub mail: String, pub auth_code: String, @@ -295,3 +297,8 @@ pub fn read_csv(config: &Config) -> Result, Box String { + thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect() +} diff --git a/src/methods/account_new.rs b/src/methods/account_new.rs index e1c850d..aa63564 100644 --- a/src/methods/account_new.rs +++ b/src/methods/account_new.rs @@ -1,4 +1,4 @@ -use crate::{get_now, Accounts, AccountsPending, State}; +use crate::{Accounts, AccountsPending, State, get_now_iso, AccountsNew, random_string}; use ldap3::exop::PasswordModify; use ldap3::result::ExopResult; use ldap3::{LdapConn, Scope}; @@ -155,12 +155,6 @@ async fn db_pending_clear_expired(pool: &Pool) -> Result String { - use rand::distributions::Alphanumeric; - use rand::{thread_rng, Rng}; - - thread_rng().sample_iter(&Alphanumeric).take(length).map(char::from).collect() -} #[derive(Debug, Deserialize)] pub struct LdapUserVerify { @@ -267,7 +261,7 @@ async fn account_verification_new_account(ldap: &mut LdapConn, user_details: &Ac let dn = format!("uid={},ou=users,dc=skynet,dc=ie", user); let home_directory = format!("/home/{}", user); - let password_tmp = create_random_string(50); + let password_tmp = random_string(50); let labeled_uri = format!("ldap:///ou=groups,dc=skynet,dc=ie??sub?(&(objectclass=posixgroup)(memberuid={}))", user); let sk_mail = format!("{}@skynet.ie", user); let sk_created = get_sk_created();