From e83adea4fef6622e4d027d8de9a1a8062f8a3ba0 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 4 Jun 2023 14:21:12 +0100 Subject: [PATCH] feat: now check local db for pending usernames --- src/lib.rs | 30 +++++++++++++++++++++--------- src/methods/account_new.rs | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6b2e417..b4a4ff0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,19 @@ use sqlx::{Error, Pool, Sqlite}; use std::env; use std::str::FromStr; use std::time::{SystemTime, UNIX_EPOCH}; +use tide::prelude::*; + +#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] +pub struct AccountsPending { + user: String, + mail: String, + name_first : String, + name_second : String, + auth_code : String, + discord: Option, + // will only last for a few hours + expiry: i64 +} pub async fn db_init(database: &str) -> Result, Error> { let pool = SqlitePoolOptions::new() @@ -12,21 +25,20 @@ pub async fn db_init(database: &str) -> Result, Error> { .connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true)) .await?; - /* - // https://store.steampowered.com/api/appdetails?appids=1258740 sqlx::query( - "CREATE TABLE IF NOT EXISTS store_details ( - id integer primary key, - name text not null, - item_type text not null, - last_timestamp integer not null + "CREATE TABLE IF NOT EXISTS accounts_pending ( + user text primary key, + mail text not null, + name_first text not null, + name_second text not null, + auth_code text not null, + discord text, + expiry integer not null )", ) .execute(&pool) .await?; - */ - // set up indexes? /* sqlx::query("CREATE INDEX IF NOT EXISTS index_estimate ON bus_results (valid_estimate)") diff --git a/src/methods/account_new.rs b/src/methods/account_new.rs index 44f0c4d..684d1c5 100644 --- a/src/methods/account_new.rs +++ b/src/methods/account_new.rs @@ -1,9 +1,10 @@ -use crate::State; +use crate::{AccountsPending, get_now, State}; use ldap3::exop::PasswordModify; use ldap3::{LdapConn, Mod, Scope, SearchEntry}; use std::collections::HashSet; use tide::prelude::{json, Deserialize}; use tide::Request; +use sqlx::{Pool, Sqlite}; #[derive(Debug, Deserialize)] pub struct LdapNewUser { @@ -56,11 +57,40 @@ pub async fn post_new_account(mut req: Request) -> tide::Result { // done with ldap ldap.unbind()?; + + // setup the pool, going to need it for the rest of it + let pool = &req.state().db; + + db_pending_clear_expired(pool).await; // now check local - - + if let Ok(results) = sqlx::query_as::<_, AccountsPending>( + r#" + SELECT * + FROM accounts_pending + WHERE user == ? + "#, + ).bind(user).fetch_all(pool).await { + if !results.is_empty(){ + return Ok(json!({"result": "error", "error": "username not available"}).into()) + } + } Ok(json!({"result": "success"}).into()) } + + +// clear the db of expired ones before checking for username and validating inputs +async fn db_pending_clear_expired(pool: &Pool){ + let now = get_now(); + if let Ok(results) = sqlx::query_as::<_, AccountsPending>( + r#" + DELETE + FROM accounts_pending + WHERE expiry < ? + "#, + ).bind(now).fetch_all(pool).await { + println!("{:?}", results) + } +} \ No newline at end of file