feat: migrate captcha exists to use db_* interface

This commit is contained in:
realaravinth 2022-05-12 19:32:08 +05:30
parent 0bb975a230
commit 81ad030338
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
4 changed files with 56 additions and 56 deletions

View file

@ -108,26 +108,6 @@
},
"query": "SELECT time FROM mcaptcha_pow_confirmed_stats \n WHERE \n config_id = (\n SELECT config_id FROM mcaptcha_config \n WHERE \n key = $1\n AND\n user_id = (\n SELECT \n ID FROM mcaptcha_users WHERE name = $2))\n ORDER BY time DESC"
},
"4a5dfbc5aeb2bab290a09640cc25223d484fbc7549e5bc54f33bab8616725031": {
"describe": {
"columns": [
{
"name": "exists",
"ordinal": 0,
"type_info": "Bool"
}
],
"nullable": [
null
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "SELECT EXISTS (SELECT 1 from mcaptcha_config WHERE key = $1)"
},
"4c3a9fe30a4c6bd49ab1cb8883c4495993aa05f2991483b4f04913b2e5043a63": {
"describe": {
"columns": [

View file

@ -14,8 +14,6 @@
* 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/>.
*/
use std::borrow::Cow;
use actix_identity::Identity;
use actix_web::{web, HttpResponse, Responder};
use libmcaptcha::defense::Level;
@ -83,14 +81,14 @@ pub mod runner {
duration,
};
match data.dblib.create_captcha(&username, &p).await {
match data.dblib.create_captcha(username, &p).await {
Ok(_) => break,
Err(DBError::SecretTaken) => continue,
Err(e) => return Err(e.into()),
}
}
data.dblib
.add_captcha_levels(&username, &key, &payload.levels)
.add_captcha_levels(username, &key, &payload.levels)
.await?;
let mcaptcha_config = MCaptchaDetails {
name: payload.description.clone(),

View file

@ -160,7 +160,7 @@ pub mod runner {
futs.push(update_fut);
data.dblib
.add_captcha_levels(&username, &payload.key, &payload.levels)
.add_captcha_levels(username, &payload.key, &payload.levels)
.await?;
try_join_all(futs).await?;
if let Err(ServiceError::CaptchaError(e)) = data

View file

@ -42,43 +42,65 @@ pub async fn get_config(
payload: web::Json<GetConfigPayload>,
data: AppData,
) -> ServiceResult<impl Responder> {
let res = sqlx::query!(
"SELECT EXISTS (SELECT 1 from mcaptcha_config WHERE key = $1)",
&payload.key,
)
.fetch_one(&data.db)
.await?;
// 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? {
return Err(ServiceError::TokenNotFound);
}
let payload = payload.into_inner();
match res.exists {
Some(true) => {
match data.captcha.get_pow(payload.key.clone()).await {
Ok(Some(config)) => {
record_fetch(&payload.key, &data.db).await;
Ok(HttpResponse::Ok().json(config))
}
Ok(None) => {
init_mcaptcha(&data, &payload.key).await?;
let config = data
.captcha
.get_pow(payload.key.clone())
.await
.expect("mcaptcha should be initialized and ready to go");
// background it. would require data::Data to be static
// to satidfy lifetime
record_fetch(&payload.key, &data.db).await;
Ok(HttpResponse::Ok().json(config))
}
Err(e) => Err(e.into()),
}
}
Some(false) => Err(ServiceError::TokenNotFound),
None => Err(ServiceError::TokenNotFound),
match data.captcha.get_pow(payload.key.clone()).await {
Ok(Some(config)) => {
record_fetch(&payload.key, &data.db).await;
Ok(HttpResponse::Ok().json(config))
}
Ok(None) => {
init_mcaptcha(&data, &payload.key).await?;
let config = data
.captcha
.get_pow(payload.key.clone())
.await
.expect("mcaptcha should be initialized and ready to go");
// background it. would require data::Data to be static
// to satidfy lifetime
record_fetch(&payload.key, &data.db).await;
Ok(HttpResponse::Ok().json(config))
}
Err(e) => Err(e.into()),
}
// match res.exists {
// Some(true) => {
// match data.captcha.get_pow(payload.key.clone()).await {
// Ok(Some(config)) => {
// record_fetch(&payload.key, &data.db).await;
// Ok(HttpResponse::Ok().json(config))
// }
// Ok(None) => {
// init_mcaptcha(&data, &payload.key).await?;
// let config = data
// .captcha
// .get_pow(payload.key.clone())
// .await
// .expect("mcaptcha should be initialized and ready to go");
// // background it. would require data::Data to be static
// // to satidfy lifetime
// record_fetch(&payload.key, &data.db).await;
// Ok(HttpResponse::Ok().json(config))
// }
// Err(e) => Err(e.into()),
// }
// }
//
// Some(false) => Err(ServiceError::TokenNotFound),
// None => Err(ServiceError::TokenNotFound),
// }
}
/// Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.
///