diff --git a/fir_ser/api/tasks.py b/fir_ser/api/tasks.py index 419b215..879c39a 100644 --- a/fir_ser/api/tasks.py +++ b/fir_ser/api/tasks.py @@ -6,6 +6,8 @@ import logging +from captcha.models import CaptchaStore + from api.utils.ctasks import sync_download_times, auto_clean_upload_tmp_file from api.views.login import get_login_type from common.core.sysconfig import Config, invalid_config_cache @@ -48,6 +50,11 @@ def auto_clean_upload_tmp_file_job(): auto_clean_upload_tmp_file() +@app.task +def auto_clean_captcha_store_job(): + CaptchaStore.remove_expired() + + @app.task def auto_delete_tmp_file_job(): auto_delete_ios_mobile_tmp_file() diff --git a/fir_ser/api/utils/utils.py b/fir_ser/api/utils/utils.py index f5fb85f..3b8fd3f 100644 --- a/fir_ser/api/utils/utils.py +++ b/fir_ser/api/utils/utils.py @@ -23,27 +23,32 @@ from common.utils.sendmsg import SendMessage from common.utils.storage import Storage from common.utils.token import generate_numeric_token_of_length, generate_alphanumeric_token_of_length, make_token, \ verify_token -from fir_ser.settings import CAPTCHA_LENGTH, MEDIA_ROOT +from fir_ser.settings import MEDIA_ROOT logger = logging.getLogger(__name__) def get_captcha(): - captcha_key = CaptchaStore.generate_key() + # 随机字符串 + random_char_fun = 'captcha.helpers.random_char_challenge' + + # 数学运算 + math_fun = 'captcha.helpers.math_challenge' + + captcha_key = CaptchaStore.generate_key(random_char_fun) captcha_image = captcha_image_url(captcha_key) - CaptchaStore.remove_expired() local_storage = LocalStorage(**Config.IOS_PMFILE_DOWNLOAD_DOMAIN) return {"captcha_image": "/".join([local_storage.get_base_url(), captcha_image.strip("/"), '']), "captcha_key": captcha_key, - "length": CAPTCHA_LENGTH} + "length": len(CaptchaStore.objects.filter(hashkey=captcha_key).first().response)} def valid_captcha(captcha_key, code, username): if username: - challenge = CaptchaStore.objects.filter(hashkey=captcha_key).values("challenge").first() - logger.info(f"captcha_key:{captcha_key} code:{code} challenge:{challenge}") - if challenge: - if captcha_key and code and code.strip(" ").lower() == challenge.get("challenge").lower(): + captcha_obj = CaptchaStore.objects.filter(hashkey=captcha_key).values("response").first() + logger.info(f"captcha_key:{captcha_key} code:{code} challenge:{captcha_obj}") + if captcha_obj: + if captcha_key and code and code.strip(" ").lower() == captcha_obj.get("response").lower(): return True return False diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index a68cb8b..2563cda 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -292,6 +292,7 @@ SYNC_CACHE_TO_DATABASE = { 'wx_get_access_token_times': 60 * 10, # 微信access_token 自动获取时间 'try_login_times': (10, 12 * 60 * 60), # 当天登录失败次数,超过该失败次数,锁定24小时 'auto_clean_tmp_file_times': 60 * 30, # 定时清理上传失误生成的临时文件 + 'auto_clean_captcha_store_times': 60 * 60, # 定时清理临时验证码数据 'auto_clean_local_tmp_file_times': 60 * 30, # 定时清理临时文件,现在包含超级签名描述临时文件 'try_send_msg_over_limit_times': (3, 60 * 60), # 每小时用户发送信息次数 'clean_local_tmp_file_from_mtime': 60 * 60, # 清理最后一次修改时间超过限制时间的临时文件,单位秒 @@ -452,6 +453,11 @@ CELERY_BEAT_SCHEDULE = { 'schedule': SYNC_CACHE_TO_DATABASE.get("auto_clean_local_tmp_file_times"), 'args': () }, + 'auto_clean_captcha_store_job': { + 'task': 'api.tasks.auto_clean_captcha_store_job', + 'schedule': SYNC_CACHE_TO_DATABASE.get("auto_clean_captcha_store_times"), + 'args': () + }, 'auto_check_ios_developer_active_job': { 'task': 'xsign.tasks.auto_check_ios_developer_active_job', # 'schedule': SYNC_CACHE_TO_DATABASE.get("auto_check_ios_developer_active_times"),