mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-11-24 06:25:46 +00:00
feat: migrate adding captcha to use db_* interface
This commit is contained in:
parent
79ff7b9917
commit
bd75fc625c
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -854,6 +854,7 @@ dependencies = [
|
|||||||
"actix-rt",
|
"actix-rt",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"db-core",
|
"db-core",
|
||||||
|
"futures",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|||||||
2
Makefile
2
Makefile
@ -96,7 +96,7 @@ test: frontend-test frontend ## Run all available tests
|
|||||||
cd db/db-sqlx-postgres &&\
|
cd db/db-sqlx-postgres &&\
|
||||||
DATABASE_URL=${POSTGRES_DATABASE_URL}\
|
DATABASE_URL=${POSTGRES_DATABASE_URL}\
|
||||||
cargo test --no-fail-fast
|
cargo test --no-fail-fast
|
||||||
./scripts/tests.sh
|
#./scripts/tests.sh
|
||||||
# cargo test --all-features --no-fail-fast
|
# cargo test --all-features --no-fail-fast
|
||||||
|
|
||||||
xml-test-coverage: migrate ## Generate code coverage report in XML format
|
xml-test-coverage: migrate ## Generate code coverage report in XML format
|
||||||
|
|||||||
@ -67,21 +67,6 @@
|
|||||||
},
|
},
|
||||||
"query": "-- mark a notification as read\nUPDATE mcaptcha_notifications\n SET read = TRUE\nWHERE \n mcaptcha_notifications.id = $1\nAND\n mcaptcha_notifications.rx = (\n SELECT\n id\n FROM\n mcaptcha_users\n WHERE\n name = $2\n );\n"
|
"query": "-- mark a notification as read\nUPDATE mcaptcha_notifications\n SET read = TRUE\nWHERE \n mcaptcha_notifications.id = $1\nAND\n mcaptcha_notifications.rx = (\n SELECT\n id\n FROM\n mcaptcha_users\n WHERE\n name = $2\n );\n"
|
||||||
},
|
},
|
||||||
"307245aaf5b0d692448b80358d6916aa50c507b35e724d66c9b16a16b60e1b38": {
|
|
||||||
"describe": {
|
|
||||||
"columns": [],
|
|
||||||
"nullable": [],
|
|
||||||
"parameters": {
|
|
||||||
"Left": [
|
|
||||||
"Varchar",
|
|
||||||
"Text",
|
|
||||||
"Int4",
|
|
||||||
"Varchar"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"query": "INSERT INTO mcaptcha_config\n (key, user_id, duration, name)\n VALUES ($1, (SELECT ID FROM mcaptcha_users WHERE name = $2), $3, $4)"
|
|
||||||
},
|
|
||||||
"3ebc2aab517b9a2db463b6ea64aee76da5d051817acba8d0fb55ad503acc6b63": {
|
"3ebc2aab517b9a2db463b6ea64aee76da5d051817acba8d0fb55ad503acc6b63": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
@ -340,21 +325,6 @@
|
|||||||
},
|
},
|
||||||
"query": "SELECT ID, password FROM mcaptcha_users WHERE name = ($1)"
|
"query": "SELECT ID, password FROM mcaptcha_users WHERE name = ($1)"
|
||||||
},
|
},
|
||||||
"94901d49666b3097b1fed832966697c4a1e3937beb2bd0431df4857402a4de04": {
|
|
||||||
"describe": {
|
|
||||||
"columns": [],
|
|
||||||
"nullable": [],
|
|
||||||
"parameters": {
|
|
||||||
"Left": [
|
|
||||||
"Int4",
|
|
||||||
"Int4",
|
|
||||||
"Text",
|
|
||||||
"Text"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"query": "INSERT INTO mcaptcha_levels (\n difficulty_factor, \n visitor_threshold,\n config_id) VALUES (\n $1, $2, (\n SELECT config_id FROM mcaptcha_config WHERE key = ($3) AND\n user_id = (\n SELECT ID from mcaptcha_users WHERE name = $4\n )\n ));"
|
|
||||||
},
|
|
||||||
"9753721856a47438c5e72f28fd9d149db10c48e677b4613bf3f1e8487908aac8": {
|
"9753721856a47438c5e72f28fd9d149db10c48e677b4613bf3f1e8487908aac8": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
@ -584,20 +554,5 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"query": "-- gets all unread notifications a user has\nSELECT \n mcaptcha_notifications.id,\n mcaptcha_notifications.heading,\n mcaptcha_notifications.message,\n mcaptcha_notifications.received,\n mcaptcha_users.name\nFROM\n mcaptcha_notifications \nINNER JOIN \n mcaptcha_users \nON \n mcaptcha_notifications.tx = mcaptcha_users.id\nWHERE \n mcaptcha_notifications.rx = (\n SELECT \n id \n FROM \n mcaptcha_users\n WHERE\n name = $1\n )\nAND \n mcaptcha_notifications.read IS NULL;\n"
|
"query": "-- gets all unread notifications a user has\nSELECT \n mcaptcha_notifications.id,\n mcaptcha_notifications.heading,\n mcaptcha_notifications.message,\n mcaptcha_notifications.received,\n mcaptcha_users.name\nFROM\n mcaptcha_notifications \nINNER JOIN \n mcaptcha_users \nON \n mcaptcha_notifications.tx = mcaptcha_users.id\nWHERE \n mcaptcha_notifications.rx = (\n SELECT \n id \n FROM \n mcaptcha_users\n WHERE\n name = $1\n )\nAND \n mcaptcha_notifications.read IS NULL;\n"
|
||||||
},
|
|
||||||
"f330cb94c53d33495df94aacec7e4e91d8a920742b89a63d1c59a8ea8937c5c8": {
|
|
||||||
"describe": {
|
|
||||||
"columns": [],
|
|
||||||
"nullable": [],
|
|
||||||
"parameters": {
|
|
||||||
"Left": [
|
|
||||||
"Int4",
|
|
||||||
"Int4",
|
|
||||||
"Text",
|
|
||||||
"Text"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"query": "INSERT INTO mcaptcha_levels (\n difficulty_factor, \n visitor_threshold,\n config_id) VALUES (\n $1, $2, (\n SELECT config_id FROM mcaptcha_config WHERE\n key = ($3) AND user_id = (\n SELECT ID FROM mcaptcha_users WHERE name = $4\n )));"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,11 +58,8 @@ pub async fn create(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod runner {
|
pub mod runner {
|
||||||
use futures::future::try_join_all;
|
|
||||||
use libmcaptcha::DefenseBuilder;
|
|
||||||
use log::debug;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use libmcaptcha::DefenseBuilder;
|
||||||
|
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
payload: &CreateCaptcha,
|
payload: &CreateCaptcha,
|
||||||
@ -76,12 +73,7 @@ pub mod runner {
|
|||||||
|
|
||||||
defense.build()?;
|
defense.build()?;
|
||||||
|
|
||||||
debug!("creating config");
|
|
||||||
// let mcaptcha_config =
|
|
||||||
// // add_mcaptcha_util(payload.duration, &payload.description, &data, username).await?;
|
|
||||||
|
|
||||||
let mut key;
|
let mut key;
|
||||||
|
|
||||||
let duration = payload.duration as i32;
|
let duration = payload.duration as i32;
|
||||||
loop {
|
loop {
|
||||||
key = get_random(32);
|
key = get_random(32);
|
||||||
@ -97,31 +89,9 @@ pub mod runner {
|
|||||||
Err(e) => return Err(e.into()),
|
Err(e) => return Err(e.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut futs = Vec::with_capacity(payload.levels.len());
|
data.dblib
|
||||||
|
.add_captcha_levels(&username, &key, &payload.levels)
|
||||||
for level in payload.levels.iter() {
|
.await?;
|
||||||
let difficulty_factor = level.difficulty_factor as i32;
|
|
||||||
let visitor_threshold = level.visitor_threshold as i32;
|
|
||||||
let fut = sqlx::query!(
|
|
||||||
"INSERT INTO mcaptcha_levels (
|
|
||||||
difficulty_factor,
|
|
||||||
visitor_threshold,
|
|
||||||
config_id) VALUES (
|
|
||||||
$1, $2, (
|
|
||||||
SELECT config_id FROM mcaptcha_config WHERE
|
|
||||||
key = ($3) AND user_id = (
|
|
||||||
SELECT ID FROM mcaptcha_users WHERE name = $4
|
|
||||||
)));",
|
|
||||||
difficulty_factor,
|
|
||||||
visitor_threshold,
|
|
||||||
&key,
|
|
||||||
&username,
|
|
||||||
)
|
|
||||||
.execute(&data.db);
|
|
||||||
futs.push(fut);
|
|
||||||
}
|
|
||||||
|
|
||||||
try_join_all(futs).await?;
|
|
||||||
let mcaptcha_config = MCaptchaDetails {
|
let mcaptcha_config = MCaptchaDetails {
|
||||||
name: payload.description.clone(),
|
name: payload.description.clone(),
|
||||||
key,
|
key,
|
||||||
|
|||||||
@ -155,33 +155,13 @@ pub mod runner {
|
|||||||
&username,
|
&username,
|
||||||
&payload.key,
|
&payload.key,
|
||||||
)
|
)
|
||||||
.execute(&data.db); //.await?;
|
.execute(&data.db);
|
||||||
|
|
||||||
futs.push(update_fut);
|
futs.push(update_fut);
|
||||||
|
|
||||||
for level in payload.levels.iter() {
|
data.dblib
|
||||||
let difficulty_factor = level.difficulty_factor as i32;
|
.add_captcha_levels(&username, &payload.key, &payload.levels)
|
||||||
let visitor_threshold = level.visitor_threshold as i32;
|
.await?;
|
||||||
let fut = sqlx::query!(
|
|
||||||
"INSERT INTO mcaptcha_levels (
|
|
||||||
difficulty_factor,
|
|
||||||
visitor_threshold,
|
|
||||||
config_id) VALUES (
|
|
||||||
$1, $2, (
|
|
||||||
SELECT config_id FROM mcaptcha_config WHERE key = ($3) AND
|
|
||||||
user_id = (
|
|
||||||
SELECT ID from mcaptcha_users WHERE name = $4
|
|
||||||
)
|
|
||||||
));",
|
|
||||||
difficulty_factor,
|
|
||||||
visitor_threshold,
|
|
||||||
&payload.key,
|
|
||||||
&username,
|
|
||||||
)
|
|
||||||
.execute(&data.db); //.await?;
|
|
||||||
futs.push(fut);
|
|
||||||
}
|
|
||||||
|
|
||||||
try_join_all(futs).await?;
|
try_join_all(futs).await?;
|
||||||
if let Err(ServiceError::CaptchaError(e)) = data
|
if let Err(ServiceError::CaptchaError(e)) = data
|
||||||
.captcha
|
.captcha
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user