feat: central function for random string

This commit is contained in:
silver 2023-07-30 02:02:56 +01:00
parent 21f2a21609
commit 0023e52f79
3 changed files with 12 additions and 17 deletions

View file

@ -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<Sqlite>, 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<Response, lettre::transport::smtp::Error> {
let recipient = &record.name_first;

View file

@ -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<Vec<Record>, Box<dyn std::error::Erro
Ok(records)
}
// from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters
pub fn random_string(len: usize) -> String {
thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
}

View file

@ -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<Sqlite>) -> Result<Vec<AccountsPen
.await
}
fn create_random_string(length: usize) -> 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();