From 0d3d552ae07e4eadfb9b6fbef766a444a211c82d Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 12 May 2022 11:59:10 +0530 Subject: [PATCH] feat: migrate create captcha to use db_* --- Cargo.lock | 1 + src/api/v1/account/username.rs | 2 - src/api/v1/auth.rs | 2 - src/api/v1/mcaptcha/create.rs | 68 ++++++++++++---------------------- 4 files changed, 24 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 97cd4f59..9a0f1a07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -840,6 +840,7 @@ name = "db-core" version = "0.1.0" dependencies = [ "async-trait", + "libmcaptcha", "serde 1.0.137", "serde_json", "thiserror", diff --git a/src/api/v1/account/username.rs b/src/api/v1/account/username.rs index d3ca7f4d..cc046c3f 100644 --- a/src/api/v1/account/username.rs +++ b/src/api/v1/account/username.rs @@ -14,8 +14,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -use std::borrow::Cow; - use actix_identity::Identity; use actix_web::{web, HttpResponse, Responder}; use serde::{Deserialize, Serialize}; diff --git a/src/api/v1/auth.rs b/src/api/v1/auth.rs index db21a02c..9234d56d 100644 --- a/src/api/v1/auth.rs +++ b/src/api/v1/auth.rs @@ -63,8 +63,6 @@ pub mod routes { } pub mod runners { - use std::borrow::Cow; - use super::*; #[derive(Clone, Debug, Deserialize, Serialize)] diff --git a/src/api/v1/mcaptcha/create.rs b/src/api/v1/mcaptcha/create.rs index 455c6161..9180df86 100644 --- a/src/api/v1/mcaptcha/create.rs +++ b/src/api/v1/mcaptcha/create.rs @@ -21,6 +21,9 @@ use actix_web::{web, HttpResponse, Responder}; use libmcaptcha::defense::Level; use serde::{Deserialize, Serialize}; +use db_core::errors::DBError; +use db_core::CreateCaptcha as DBCreateCaptcha; + use super::get_random; use crate::errors::*; use crate::AppData; @@ -74,55 +77,26 @@ pub mod runner { defense.build()?; debug!("creating config"); - let mcaptcha_config = - // add_mcaptcha_util(payload.duration, &payload.description, &data, username).await?; + // let mcaptcha_config = + // // add_mcaptcha_util(payload.duration, &payload.description, &data, username).await?; - { - let mut key; + let mut key; - let resp; + let duration = payload.duration as i32; + loop { + key = get_random(32); + let p = DBCreateCaptcha { + description: &payload.description, + key: &key, + duration, + }; - loop { - key = get_random(32); - - let res = sqlx::query!( - "INSERT INTO mcaptcha_config - (key, user_id, duration, name) - VALUES ($1, (SELECT ID FROM mcaptcha_users WHERE name = $2), $3, $4)", - &key, - &username, - payload.duration as i32, - &payload.description, - ) - .execute(&data.db) - .await; - - match res { - Err(sqlx::Error::Database(err)) => { - if err.code() == Some(Cow::from("23505")) - && err.message().contains("mcaptcha_config_key_key") - { - continue; - } else { - return Err(sqlx::Error::Database(err).into()); - } - } - Err(e) => return Err(e.into()), - - Ok(_) => { - resp = MCaptchaDetails { - key, - name: payload.description.to_owned(), - }; - break; + match data.dblib.create_captcha(&username, &p).await { + Ok(_) => break, + Err(DBError::SecretTaken) => continue, + Err(e) => return Err(e.into()), } } - } - resp - }; - - debug!("config created"); - let mut futs = Vec::with_capacity(payload.levels.len()); for level in payload.levels.iter() { @@ -140,7 +114,7 @@ pub mod runner { )));", difficulty_factor, visitor_threshold, - &mcaptcha_config.key, + &key, &username, ) .execute(&data.db); @@ -148,6 +122,10 @@ pub mod runner { } try_join_all(futs).await?; + let mcaptcha_config = MCaptchaDetails { + name: payload.description.clone(), + key, + }; Ok(mcaptcha_config) } }