WIP account registration

This commit is contained in:
Reid 2023-08-26 19:49:43 -07:00
parent f4d9c1a3c4
commit 2ba4aa8c8e
Signed by: reidlab
GPG key ID: 6C9EAA3364F962C8
14 changed files with 419 additions and 19 deletions

14
src/db.rs Normal file
View file

@ -0,0 +1,14 @@
use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;
pub mod models;
pub mod schema;
pub fn establish_connection_pg() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}

33
src/db/models.rs Normal file
View file

@ -0,0 +1,33 @@
use diesel::prelude::*;
use serde::{Serialize, Deserialize};
use super::schema::accounts;
#[derive(Queryable, Serialize)]
pub struct Account {
pub id: i32,
pub username: String,
pub gjp2: String,
pub email: String,
pub is_admin: i32,
pub messages_enabled: i32,
pub comments_enabled: i32,
pub friend_requests_enabled: i32,
pub youtube_url: Option<String>,
pub twitter_url: Option<String>,
pub twitch_url: Option<String>,
pub created_at: String
}
#[derive(Insertable, Deserialize)]
#[diesel(table_name = accounts)]
pub struct NewAccount {
pub username: String,
pub gjp2: String,
pub email: String
}

23
src/db/schema.rs Normal file
View file

@ -0,0 +1,23 @@
// @generated automatically by Diesel CLI.
diesel::table! {
accounts (id) {
id -> Int4,
#[max_length = 20]
username -> Varchar,
gjp2 -> Text,
#[max_length = 254]
email -> Varchar,
is_admin -> Int4,
messages_enabled -> Int4,
comments_enabled -> Int4,
friend_requests_enabled -> Int4,
#[max_length = 30]
youtube_url -> Nullable<Varchar>,
#[max_length = 20]
twitter_url -> Nullable<Varchar>,
#[max_length = 20]
twitch_url -> Nullable<Varchar>,
created_at -> Timestamp,
}
}

2
src/helpers.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod gjp2;
pub mod clean;

6
src/helpers/clean.rs Normal file
View file

@ -0,0 +1,6 @@
use regex::Regex;
pub fn clean(string: &str) -> String {
let regex = Regex::new(r"[^a-zA-z0-9_-]").unwrap();
return regex.replace_all(string, "").to_string();
}

12
src/helpers/gjp2.rs Normal file
View file

@ -0,0 +1,12 @@
use sha::sha1::Sha1;
use sha::utils::{Digest, DigestExt};
use password_auth::generate_hash;
pub fn get_gjp2(password: String) -> String {
return Sha1::default().digest(String::from(password + "mI29fmAnxgTs").as_bytes()).to_hex();
}
pub fn get_gjp2_hashed(password: String) -> String {
return generate_hash(get_gjp2(password))
}

View file

@ -1,13 +1,77 @@
#![feature(decl_macro)]
#[macro_use] extern crate rocket;
use rocket::form::Form;
use rocket::http::Status;
use rocket::response::status;
use diesel::prelude::*;
use diesel::result::Error;
mod db;
use db::*;
mod helpers;
use helpers::*;
#[get("/")]
fn index() -> String {
return String::from("index | coming soon to a localhost:8000 near u");
}
#[derive(FromForm)]
struct FormRegisterGJAccount {
userName: String,
password: String,
email: String
}
#[post("/memaddrefix/accounts/registerGJAccount.php", data = "<input>")]
fn register_gj_account(input: Form<FormRegisterGJAccount>) -> status::Custom<&'static str> {
use crate::schema::accounts::dsl::*;
use crate::models::NewAccount;
let connection = &mut establish_connection_pg();
if input.userName != clean::clean(input.userName.as_ref()) {
return status::Custom(Status::BadRequest, "-4")
}
if input.password.len() < 6 {
return status::Custom(Status::BadRequest, "-8")
}
if input.userName.len() < 3 {
return status::Custom(Status::BadRequest, "-9")
}
if input.userName.len() > 20 {
return status::Custom(Status::BadRequest, "-4")
}
if input.userName.len() > 254 {
return status::Custom(Status::BadRequest, "-6")
}
let account_name_usage = accounts.filter(username.eq(input.userName.clone())).count().get_result::<i64>(connection) as Result<i64, Error>;
let account_name_used = account_name_usage.expect("Fatal database name query error") != 0;
if account_name_used {
return status::Custom(Status::Conflict, "-2")
}
let new_account = NewAccount {
username: input.userName.clone(),
gjp2: helpers::gjp2::get_gjp2_hashed(input.password.clone()),
email: input.email.clone()
};
diesel::insert_into(accounts)
.values(&new_account)
.execute(connection)
.expect("Fatal error saving the new account");
return status::Custom(Status::Created, "1")
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
rocket::build().mount("/", routes![index, register_gj_account])
}

View file

@ -1,2 +0,0 @@
// @generated automatically by Diesel CLI.