delete challenge

This commit is contained in:
Aravinth Manivannan 2021-06-10 18:31:59 +05:30
parent 3671eea67f
commit 43864cd991
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -41,6 +41,7 @@ const ADD_CAPTCHA: &str = "MCAPTCHA_CACHE.ADD_CAPTCHA";
const CAPTCHA_EXISTS: &str = "MCAPTCHA_CACHE.CAPTCHA_EXISTS"; const CAPTCHA_EXISTS: &str = "MCAPTCHA_CACHE.CAPTCHA_EXISTS";
const ADD_CHALLENGE: &str = "MCAPTCHA_CACHE.ADD_CHALLENGE"; const ADD_CHALLENGE: &str = "MCAPTCHA_CACHE.ADD_CHALLENGE";
const GET_CHALLENGE: &str = "MCAPTCHA_CACHE.GET_CHALLENGE"; const GET_CHALLENGE: &str = "MCAPTCHA_CACHE.GET_CHALLENGE";
const DELETE_CHALLENGE: &str = "MCAPTCHA_CACHE.DELETE_CHALLENGE";
const MODULE_NAME: &str = "mcaptcha_cahce"; const MODULE_NAME: &str = "mcaptcha_cahce";
@ -87,6 +88,7 @@ impl MCaptchaRedisConnection {
GET, GET,
ADD_CHALLENGE, ADD_CHALLENGE,
GET_CHALLENGE, GET_CHALLENGE,
DELETE_CHALLENGE,
]; ];
for cmd in commands.iter() { for cmd in commands.iter() {
@ -176,6 +178,15 @@ impl MCaptchaRedisConnection {
Ok(serde_json::from_str(&challege).unwrap()) Ok(serde_json::from_str(&challege).unwrap())
} }
/// Get PoW Challenge object from Redis
pub async fn delete_challenge(&self, msg: &VerifyCaptchaResult) -> CaptchaResult<()> {
let _: () = self
.0
.exec(redis::cmd(DELETE_CHALLENGE).arg(&[&msg.key, &msg.token]))
.await?;
Ok(())
}
/// Get number of visitors of an mCaptcha object from Redis /// Get number of visitors of an mCaptcha object from Redis
pub async fn get_visitors(&self, captcha: &str) -> CaptchaResult<usize> { pub async fn get_visitors(&self, captcha: &str) -> CaptchaResult<usize> {
let visitors: usize = self.0.exec(redis::cmd(GET).arg(&[captcha])).await?; let visitors: usize = self.0.exec(redis::cmd(GET).arg(&[captcha])).await?;
@ -279,6 +290,13 @@ pub mod tests {
assert_eq!(x.duration, add_challenge_msg.duration); assert_eq!(x.duration, add_challenge_msg.duration);
assert_eq!(x.difficulty_factor, add_challenge_msg.difficulty); assert_eq!(x.difficulty_factor, add_challenge_msg.difficulty);
assert!(r
.add_challenge(CAPTCHA_NAME, &add_challenge_msg)
.await
.is_ok());
assert!(r.delete_challenge(&verify_msg).await.is_ok());
let add_challenge_msg = CacheResult { let add_challenge_msg = CacheResult {
key: CAPTCHA_NAME.into(), key: CAPTCHA_NAME.into(),
token: CHALLENGE.into(), token: CHALLENGE.into(),