mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-03-14 13:08:27 +03:00
feat: define interface for creating captcha
This commit is contained in:
parent
049f2b6eea
commit
00dca4a069
4 changed files with 27 additions and 1 deletions
|
@ -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 = []
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue