feat: migrate create captcha to use db_*

This commit is contained in:
realaravinth
2022-05-12 11:59:10 +05:30
parent d64b05c84f
commit 0d3d552ae0
4 changed files with 24 additions and 49 deletions

1
Cargo.lock generated
View File

@@ -840,6 +840,7 @@ name = "db-core"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"libmcaptcha",
"serde 1.0.137", "serde 1.0.137",
"serde_json", "serde_json",
"thiserror", "thiserror",

View File

@@ -14,8 +14,6 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::borrow::Cow;
use actix_identity::Identity; use actix_identity::Identity;
use actix_web::{web, HttpResponse, Responder}; use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@@ -63,8 +63,6 @@ pub mod routes {
} }
pub mod runners { pub mod runners {
use std::borrow::Cow;
use super::*; use super::*;
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]

View File

@@ -21,6 +21,9 @@ use actix_web::{web, HttpResponse, Responder};
use libmcaptcha::defense::Level; use libmcaptcha::defense::Level;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use db_core::errors::DBError;
use db_core::CreateCaptcha as DBCreateCaptcha;
use super::get_random; use super::get_random;
use crate::errors::*; use crate::errors::*;
use crate::AppData; use crate::AppData;
@@ -74,55 +77,26 @@ pub mod runner {
defense.build()?; defense.build()?;
debug!("creating config"); debug!("creating config");
let mcaptcha_config = // let mcaptcha_config =
// add_mcaptcha_util(payload.duration, &payload.description, &data, username).await?; // // 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 { match data.dblib.create_captcha(&username, &p).await {
key = get_random(32); Ok(_) => break,
Err(DBError::SecretTaken) => continue,
let res = sqlx::query!( Err(e) => return Err(e.into()),
"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;
} }
} }
}
resp
};
debug!("config created");
let mut futs = Vec::with_capacity(payload.levels.len()); let mut futs = Vec::with_capacity(payload.levels.len());
for level in payload.levels.iter() { for level in payload.levels.iter() {
@@ -140,7 +114,7 @@ pub mod runner {
)));", )));",
difficulty_factor, difficulty_factor,
visitor_threshold, visitor_threshold,
&mcaptcha_config.key, &key,
&username, &username,
) )
.execute(&data.db); .execute(&data.db);
@@ -148,6 +122,10 @@ pub mod runner {
} }
try_join_all(futs).await?; try_join_all(futs).await?;
let mcaptcha_config = MCaptchaDetails {
name: payload.description.clone(),
key,
};
Ok(mcaptcha_config) Ok(mcaptcha_config)
} }
} }