diff --git a/db/db-core/Cargo.toml b/db/db-core/Cargo.toml index 3a7c6ee0..d0d3350f 100644 --- a/db/db-core/Cargo.toml +++ b/db/db-core/Cargo.toml @@ -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 = [] diff --git a/db/db-core/src/errors.rs b/db/db-core/src/errors.rs index 3ecb09c3..07e3b0f6 100644 --- a/db/db-core/src/errors.rs +++ b/db/db-core/src/errors.rs @@ -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 diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 0ec37061..75770cf8 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -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)] diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index 3ef9548d..58e92bc8 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -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(); }