feat: migrate get captcha cooldown period to use db_*

This commit is contained in:
realaravinth 2022-05-14 16:27:44 +05:30
parent 7daffe767c
commit d061824660
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
2 changed files with 22 additions and 21 deletions

View file

@ -470,6 +470,26 @@
}, },
"query": "SELECT name FROM mcaptcha_config \n WHERE key = $1 \n AND user_id = (\n SELECT user_id FROM mcaptcha_users WHERE NAME = $2)" "query": "SELECT name FROM mcaptcha_config \n WHERE key = $1 \n AND user_id = (\n SELECT user_id FROM mcaptcha_users WHERE NAME = $2)"
}, },
"ad196ab3ef9dc32f6de2313577ccd6c26eae9ab19df5f71ce182651983efb99a": {
"describe": {
"columns": [
{
"name": "duration",
"ordinal": 0,
"type_info": "Int4"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "SELECT duration FROM mcaptcha_config \n WHERE key = $1"
},
"ad23588ee4bcbb13e208460ce21e2fa9f1373893934b530b339fea10360b34a8": { "ad23588ee4bcbb13e208460ce21e2fa9f1373893934b530b339fea10360b34a8": {
"describe": { "describe": {
"columns": [ "columns": [

View file

@ -41,13 +41,6 @@ pub async fn get_config(
payload: web::Json<GetConfigPayload>, payload: web::Json<GetConfigPayload>,
data: AppData, data: AppData,
) -> ServiceResult<impl Responder> { ) -> ServiceResult<impl Responder> {
// let res = sqlx::query!(
// "SELECT EXISTS (SELECT 1 from mcaptcha_config WHERE key = $1)",
// &payload.key,
// )
// .fetch_one(&data.db)
// .await?;
//if res.exists.is_none() { //if res.exists.is_none() {
if !data.dblib.captcha_exists(None, &payload.key).await? { if !data.dblib.captcha_exists(None, &payload.key).await? {
return Err(ServiceError::TokenNotFound); return Err(ServiceError::TokenNotFound);
@ -108,19 +101,7 @@ pub async fn get_config(
async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> { async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> {
// get levels // get levels
let levels = data.dblib.get_captcha_levels(None, key).await?; let levels = data.dblib.get_captcha_levels(None, key).await?;
struct DurationResp { let duration = data.dblib.get_captcha_cooldown(&key).await?;
duration: i32,
}
// get duration
let duration = sqlx::query_as!(
DurationResp,
"SELECT duration FROM mcaptcha_config
WHERE key = $1",
&key,
)
.fetch_one(&data.db)
.await?;
//let (levels, duration) = futures::try_join!(levels_fut, duration_fut).await?;
// build defense // build defense
let mut defense = DefenseBuilder::default(); let mut defense = DefenseBuilder::default();
@ -141,7 +122,7 @@ async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> {
let mcaptcha = MCaptchaBuilder::default() let mcaptcha = MCaptchaBuilder::default()
.defense(defense) .defense(defense)
// leaky bucket algorithm's emission interval // leaky bucket algorithm's emission interval
.duration(duration.duration as u64) .duration(duration as u64)
// .cache(cache) // .cache(cache)
.build() .build()
.unwrap(); .unwrap();