new users no longer contain optional fields

only using completely necessary information
This commit is contained in:
shockrah 2020-03-08 19:20:08 -07:00
parent 23166a9e99
commit 8cbfa9520e
3 changed files with 22 additions and 46 deletions

View File

@ -8,6 +8,7 @@
#[derive(Serialize)] #[derive(Serialize)]
pub struct NewUserInfo { pub struct NewUserInfo {
pub userid: u64, // userid which later will be discoverable via the pub userid: u64, // userid which later will be discoverable via the
pub key_hash: Option<String>, pub username: String,
pub key: String,
pub valid: bool pub valid: bool
} }

View File

@ -1,4 +1,5 @@
// This modules mainly deals with creating various types of random data // This modules mainly deals with creating various types of random data
use getrandom;
pub fn newUserID() -> u64 { pub fn newUserID() -> u64 {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
@ -6,22 +7,21 @@ pub fn newUserID() -> u64 {
Ok(_val) => {u64::from_ne_bytes(buf)} Ok(_val) => {u64::from_ne_bytes(buf)}
Err(_e) => {0} // if this really happens we're fucked anyway Err(_e) => {0} // if this really happens we're fucked anyway
} }
} }
pub fn newKey() -> String { pub fn newKey() -> Option<String> { // Returns a random string which we later hash with bcrypt
let mut buf = [0u8; 32]; let mut raw_slice = [0u8; 32];
getrandom::getrandom(&mut buf)?; if let Some(_ignored) = getrandom(raw_slice) {
let mut string = String::new(); let mut buf = String::new();
for i in buf.iter() { for i in raw_slice {
// if the value isn't in the proper range then we place it in the proper range let mut cv: u8 = *i;
// [33-126] if cv > 126 { cv %= 126; }
if i < 33 { if cv < 33 { cv += 34; }
i += 33; buf.push(cv as char);
} }
else if i > 126 { Some(buf)
i %= 126; }
else {
None
} }
} }
string
}

View File

@ -1,42 +1,17 @@
use crate::rand_utils::{newUserID, newKey};
use chrono:Utc; use chrono:Utc;
use payload::NewUserInfo; use payload::NewUserInfo;
use getrandom::getrandom; use getrandom::getrandom;
use bcrypt::{hash, DEFAULT_COST};
fn rand_string() -> Option<String> {
// Returns a random string which we later hash with bcrypt
let mut raw_slice = [0u8; 32];
if let Some(_ignored) = getrandom(raw_slice) {
let mut buf = String::new();
for i in raw_slice {
let mut cv: u8 = *i;
if cv > 126 { cv %= 126; }
if cv < 33 { cv += 34; }
buf.push(cv as char);
}
Some(buf)
}
else {
None
}
}
fn rand_uid() -> u64 {
let mut buf = [0u8;8];
let _r = getrandom(&mut buf); // this basically never fails hehexd
u64::from_ne_bytes(buf)
}
// Returns a struct of payload::NewUserInfo // Returns a struct of payload::NewUserInfo
pub fn createNewUser() -> NewUserInfo { pub fn createNewUser() -> NewUserInfo {
let uid = rand_id(); let uid = newUserID();
let uname = format!("User: {}", uid); let uname = format!("User:#{}", uid);
let hashkey = bcrypt(rand_string(), DEFAULT_COST); let rstring = newKey();
// Fuck it
NewUserInfo { NewUserInfo {
userid: uid, userid: uid,
username: uname, username: uname,
key_hash: hashkey, key: rstring,
valid: true valid: true,
} }
} }