mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 10:05:41 +00:00
feat: migrate create captcha to use db_*
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -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",
|
||||||
|
|||||||
@@ -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};
|
||||||
|
|||||||
@@ -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)]
|
||||||
|
|||||||
@@ -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 {
|
loop {
|
||||||
key = get_random(32);
|
key = get_random(32);
|
||||||
|
let p = DBCreateCaptcha {
|
||||||
|
description: &payload.description,
|
||||||
|
key: &key,
|
||||||
|
duration,
|
||||||
|
};
|
||||||
|
|
||||||
let res = sqlx::query!(
|
match data.dblib.create_captcha(&username, &p).await {
|
||||||
"INSERT INTO mcaptcha_config
|
Ok(_) => break,
|
||||||
(key, user_id, duration, name)
|
Err(DBError::SecretTaken) => continue,
|
||||||
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()),
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user