feat: define interface for creating captcha

This commit is contained in:
realaravinth 2022-05-12 11:50:24 +05:30
parent 049f2b6eea
commit 00dca4a069
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
4 changed files with 27 additions and 1 deletions

View file

@ -13,6 +13,7 @@ async-trait = "0.1.51"
thiserror = "1.0.30"
serde = { version = "1", features = ["derive"]}
url = { version = "2.2.2", features = ["serde"] }
libmcaptcha = { branch = "master", git = "https://github.com/mCaptcha/libmcaptcha", features = ["minimal"], default-features = false }
[features]
default = []

View file

@ -35,6 +35,9 @@ pub enum DBError {
/// Secret is taken
#[error("Secret is taken")]
SecretTaken,
/// Captcha key is taken
#[error("Captcha key is taken")]
CaptchaKeyTaken,
}
/// Convenience type alias for grouping driver-specific errors

View file

@ -33,6 +33,8 @@
//! connection from pool
use serde::{Deserialize, Serialize};
use libmcaptcha::defense::Level;
pub mod errors;
pub mod ops;
#[cfg(feature = "test")]
@ -131,6 +133,20 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
/// update a user's secret
async fn update_secret(&self, username: &str, secret: &str) -> DBResult<()>;
/// create new captcha
async fn create_captcha(&self, username: &str, p: &CreateCaptcha) -> DBResult<()>;
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
/// data requried to create new captcha
pub struct CreateCaptcha<'a> {
/// cool down duration
pub duration: i32,
/// description of the captcha
pub description: &'a str,
/// secret key of the captcha
pub key: &'a str,
}
#[derive(Clone, Debug, Deserialize, Serialize)]

View file

@ -18,7 +18,11 @@
use crate::prelude::*;
/// test all database functions
pub async fn database_works<'a, T: MCDatabase>(db: &T, p: &Register<'a>) {
pub async fn database_works<'a, T: MCDatabase>(
db: &T,
p: &Register<'a>,
c: &CreateCaptcha<'a>,
) {
assert!(db.ping().await, "ping test");
if db.username_exists(p.username).await.unwrap() {
db.delete_user(p.username).await.unwrap();
@ -125,4 +129,6 @@ pub async fn database_works<'a, T: MCDatabase>(db: &T, p: &Register<'a>) {
db.email_exists(p.email.as_ref().unwrap()).await.unwrap(),
"user was with empty email but email is set; so email should exsit"
);
db.create_captcha(&p.username, c).await.unwrap();
}