rename mcaptcha

This commit is contained in:
realaravinth 2021-07-19 16:21:48 +05:30
parent a0fefa5649
commit 8559ddcbe2
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
5 changed files with 67 additions and 11 deletions

2
Cargo.lock generated
View file

@ -312,7 +312,7 @@ dependencies = [
[[package]]
name = "libmcaptcha"
version = "0.1.4"
source = "git+https://github.com/mCaptcha/libmcaptcha?branch=master#0d8de9092b68fcdec74c7bee9e037aeda29c7a87"
source = "git+https://github.com/mCaptcha/libmcaptcha?branch=master#510606895a86285e7a70786b4c4d31e544c81aaa"
dependencies = [
"derive_builder",
"derive_more",

View file

@ -91,6 +91,7 @@ pub mod redis {
["MCAPTCHA_CACHE.GET", mcaptcha::MCaptcha::get_count, "readonly", 1, 1, 1],
["MCAPTCHA_CACHE.ADD_CAPTCHA", mcaptcha::MCaptcha::add_captcha, "readonly", 1, 1, 1],
["MCAPTCHA_CACHE.DELETE_CAPTCHA", mcaptcha::MCaptcha::delete_captcha, "write", 1, 1, 1],
["MCAPTCHA_CACHE.RENAME_CAPTCHA", mcaptcha::MCaptcha::rename, "write", 1, 1, 1],
["MCAPTCHA_CACHE.CAPTCHA_EXISTS", mcaptcha::MCaptcha::captcha_exists, "readonly", 1, 1, 1],
["MCAPTCHA_CACHE.ADD_CHALLENGE", challenge::Challenge::create_challenge, "write", 1, 1, 1],
["MCAPTCHA_CACHE.GET_CHALLENGE", challenge::Challenge::get_challenge, "write", 1, 1, 1],

View file

@ -139,9 +139,14 @@ impl MCaptcha {
let key_name = get_captcha_key(&args.next_string()?);
let json = args.next_string()?;
let mcaptcha: CreateMCaptcha = Format::Json.from_str(&json)?;
let duration = mcaptcha.duration;
let mcaptcha = Self::new(mcaptcha)?;
Self::add_captcha_runner(ctx, &key_name, mcaptcha)
}
#[inline]
fn add_captcha_runner(ctx: &Context, key_name: &str, mcaptcha: MCaptcha) -> RedisResult {
let duration = mcaptcha.get_duration();
let key = ctx.open_key_writable(&RedisString::create(ctx.ctx, &key_name));
if key.key_type() == KeyType::Empty {
key.set_value(&MCAPTCHA_MCAPTCHA_TYPE, mcaptcha)?;
@ -161,20 +166,52 @@ impl MCaptcha {
let key_name = get_captcha_key(&args.next_string()?);
let key = ctx.open_key(&RedisString::create(ctx.ctx, &key_name));
if key.key_type() == KeyType::Empty {
// 1 is false
Ok(RedisValue::Integer(1))
} else {
// 0 is true
if Self::captcha_exists_runner(&key) {
Ok(RedisValue::Integer(0))
} else {
Ok(RedisValue::Integer(1))
}
}
/// Add captcha to redis
#[inline]
fn captcha_exists_runner(key: &RedisKey) -> bool {
!(key.key_type() == KeyType::Empty)
}
/// implements mCaptcha rename: clones configuration from old name to new name and
/// deletes oldname
pub fn rename(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
let mut args = args.into_iter().skip(1);
let key_name = get_captcha_key(&args.next_string()?);
let new_name = get_captcha_key(&args.next_string()?);
let key = ctx.open_key(&RedisString::create(ctx.ctx, &key_name));
if Self::captcha_exists_runner(&key) {
if let Some(mcaptcha) = Self::get_mcaptcha(&key)? {
let mcaptcha = MCaptcha {
m: MCaptchaBuilder::default()
.defense(mcaptcha.m.get_defense())
.duration(mcaptcha.get_duration())
.build()?,
};
Self::add_captcha_runner(ctx, &new_name, mcaptcha)?;
Self::delete_captcha_runner(ctx, &key_name)?;
}
};
REDIS_OK
}
/// delete captcha
pub fn delete_captcha(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
let mut args = args.into_iter().skip(1);
let key_name = get_captcha_key(&args.next_string()?);
Self::delete_captcha_runner(ctx, &key_name)
}
#[inline]
fn delete_captcha_runner(ctx: &Context, key_name: &str) -> RedisResult {
let key = ctx.open_key_writable(&RedisString::create(ctx.ctx, &key_name));
if key.key_type() == KeyType::Empty {
Err(RedisError::nonexistent_key())

View file

@ -34,6 +34,7 @@ COMMANDS = {
"ADD_CAPTCHA": "MCAPTCHA_CACHE.ADD_CAPTCHA",
"DELETE_CAPTCHA": "MCAPTCHA_CACHE.DELETE_CAPTCHA",
"CAPTCHA_EXISTS": "MCAPTCHA_CACHE.CAPTCHA_EXISTS",
"RENAME_CAPTCHA": "MCAPTCHA_CACHE.RENAME_CAPTCHA",
}
payload = json.dumps(MCAPTCHA)
@ -42,9 +43,13 @@ def delete_captcha(key):
r.execute_command(COMMANDS["DELETE_CAPTCHA"], key)
def add_captcha(key, duration=5):
def add_captcha(key):
r.execute_command(COMMANDS["ADD_CAPTCHA"], key, payload)
def rename_captcha(key, new_key):
r.execute_command(COMMANDS["RENAME_CAPTCHA"], key, new_key)
def captcha_exists(key):
exists = r.execute_command(COMMANDS["CAPTCHA_EXISTS"], key)
@ -54,11 +59,11 @@ def captcha_exists(key):
if exists == 1:
return False
def register(key, duration=5):
def register(key):
if captcha_exists(key):
delete_captcha(key)
add_captcha(key, duration=5)
add_captcha(key)
async def captcha_exists_works():
key = "captcha_delete_works"
@ -83,3 +88,15 @@ async def delete_captcha_works():
delete_captcha(key)
assert captcha_exists(key) is False
print("[*] Delete captcha works")
async def rename_captcha_works():
key = "rename_captcha_works"
new_key = "new_key_rename_captcha_works"
register(key)
exists = captcha_exists(key)
assert exists is True
rename_captcha(key, new_key)
assert captcha_exists(key) is False
assert captcha_exists(new_key) is True
print("[*] Rename captcha works")

View file

@ -29,6 +29,7 @@ class Runner(object):
mcaptcha.delete_captcha_works,
mcaptcha.captcha_exists_works,
mcaptcha.register_captcha_works,
mcaptcha.rename_captcha_works,
challenge.add_challenge_works,
challenge.challenge_doesnt_exist,
challenge.challenge_ttl_works,