commit 813a0b35776f1228cd63ac585addb3aa3a37db08
parent dfb79fe39ccc307c980ed193db6f264eaa9f4ace
Author: Papayapaya <91221817+Papayapaya@users.noreply.github.com>
Date: Thu, 17 Nov 2022 14:28:31 -0800
make name chekcing function
Diffstat:
1 file changed, 22 insertions(+), 20 deletions(-)
diff --git a/scuttlebutt/src/main.rs b/scuttlebutt/src/main.rs
@@ -1,3 +1,4 @@
+use anyhow::Ok;
use chrono::{DateTime, Duration, Local, Utc};
use hmac::{Hmac, digest::typenum::array};
use jwt::{SignWithKey, VerifyWithKey};
@@ -88,6 +89,27 @@ pub fn gen_id() -> i64 {
.generate()
}
+pub fn check_name(name: String) -> String{
+ let disallowed_chars: [u32; 53] = [0x202e,0x0009,0x00AD,0x034F,0x061C,0x115F,0x1160,0x17B4,0x17B5,0x180E,0x2000,0x2001,0x2002,0x2003,0x2004,0x2005,0x2006,0x2007,0x2008,0x2009,0x200A,0x200B,0x200C,0x200D,0x200E,0x200F,0x202F,0x205F,0x2060,0x2061,0x2062,0x2063,0x2064,0x206A,0x206B,0x206C,0x206D,0x206E,0x206F,0x3000,0x2800,0x3164,0xFEFF,0xFFA0,0x1D159,0x1D173,0x1D174,0x1D175,0x1D176,0x1D177,0x1D178,0x1D179,0x1D17A];
+ let name_chars = name.chars();
+ let mut fixed_name = String::from("");
+ let mut index = 0;
+ let mut to_keep:[bool; 32] = [true; 32]; //assuming 32 is the max name size - TODO: figure out how to not hardcode this in rust
+ for i in name_chars{
+ for j in 0..disallowed_chars.len(){
+ if i == char::from_u32(disallowed_chars[j]).unwrap() {
+ to_keep[index] = false;
+ };
+ };
+ if to_keep[index]{
+ fixed_name.push(i)
+ };
+ index += 1;
+ };
+ assert(fixed_name != "", "name is empty or contains only illegal characters");
+ return fixed_name;
+}
+
#[OpenApi]
#[allow(unused_variables)]
impl Api {
@@ -153,26 +175,6 @@ impl Api {
if hash.0.len() != 64 {
return BadRequest(PlainText("Invalid hash provided.".to_string()));
}
-
- // name cleaning:
- let disallowed_chars: [u32; 53] = [0x202e,0x0009,0x00AD,0x034F,0x061C,0x115F,0x1160,0x17B4,0x17B5,0x180E,0x2000,0x2001,0x2002,0x2003,0x2004,0x2005,0x2006,0x2007,0x2008,0x2009,0x200A,0x200B,0x200C,0x200D,0x200E,0x200F,0x202F,0x205F,0x2060,0x2061,0x2062,0x2063,0x2064,0x206A,0x206B,0x206C,0x206D,0x206E,0x206F,0x3000,0x2800,0x3164,0xFEFF,0xFFA0,0x1D159,0x1D173,0x1D174,0x1D175,0x1D176,0x1D177,0x1D178,0x1D179,0x1D17A];
- let name_chars = name.chars();
- let mut fixed_name = String::from("");
- let mut index = 0;
- let mut to_keep:[bool; 32] = [true; 32]; //assuming 32 is the max name size - TODO: figure out how to not hardcode this in rust
- for i in name_chars{
- for j in 0..disallowed_chars.len(){
- if i == char::from_u32(disallowed_chars[j]).unwrap() {
- to_keep[index] = false;
- };
- };
- if to_keep[index]{
- fixed_name.push(i)
- };
- index += 1;
- };
-
-
let id = gen_id();
self.db.create_user(id, name.0.clone(), email.0.clone(), hash.0).unwrap();
self.db.create_user_groups(id).unwrap();