1
0
Fork 0
mirror of https://git.42l.fr/neil/sncf.git synced 2024-04-25 02:50:27 +02:00

adding random token at the end of generated names. Fixes #14

This commit is contained in:
neil 2020-09-16 20:19:21 +02:00
parent e4cab99f9e
commit 6a9465d579
2 changed files with 15 additions and 5 deletions

View file

@ -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 {

View file

@ -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(