From 6a9465d579fe9e9ce6e4b74e927af86ee6dc922b Mon Sep 17 00:00:00 2001 From: neil Date: Wed, 16 Sep 2020 20:19:21 +0200 Subject: [PATCH] adding random token at the end of generated names. Fixes #14 --- src/account.rs | 15 ++++++++++++--- src/forward.rs | 5 +++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/account.rs b/src/account.rs index 22e6de7..4ad4b02 100644 --- a/src/account.rs +++ b/src/account.rs @@ -245,17 +245,26 @@ pub fn check_token(token: &str) -> bool { } // generates a new token -pub fn gen_token() -> String { +pub fn gen_token(size: usize) -> String { // Using /dev/random to generate random bytes let mut r = OsRng; - let mut my_secure_bytes = vec![0u8; 45]; + let mut my_secure_bytes = vec![0u8; size]; r.fill_bytes(&mut my_secure_bytes); base64::encode_config(my_secure_bytes, URL_SAFE_NO_PAD) } +// generates a random username composed of +// an adjective, a name and a 4-byte base64-encoded token. +// with the default list, that represents: +// 141 * 880 = 124 080 +// 255^4 / 2 = 2 114 125 312 (we lose approx. the half because of uppercase) +// 2 114 125 312 * 124 080 = 2.623206687*10^14 possible combinations?? pub fn gen_name() -> String { - format!("{}{}", list_rand(&ADJ_LIST), list_rand(&NAME_LIST)) + // uppercasing gen_token because NC would probably refuse two + // users with the same name but a different case + // and that'd be a pain to debug + format!("{}{}-{}", list_rand(&ADJ_LIST), list_rand(&NAME_LIST), gen_token(4).to_uppercase()) } pub fn list_rand(list: &[String]) -> &String { diff --git a/src/forward.rs b/src/forward.rs index 42d777d..04a32e7 100644 --- a/src/forward.rs +++ b/src/forward.rs @@ -218,7 +218,8 @@ pub async fn forward_register( } let nc_username = gen_name(); - let nc_password = gen_token(); + println!("gen_name: {}", nc_username); + let nc_password = gen_token(45); // attempts to create the account create_account(&client, &nc_username, &nc_password, lang.clone()).await?; @@ -229,7 +230,7 @@ pub async fn forward_register( crash(lang.clone(), "error_forwardregister_pool") })?; - let token = gen_token(); + let token = gen_token(45); // store the result in DB let form_result = Form::insert(