Skip to content

Commit 94823b2

Browse files
committed
chore: refactor captcha generation logic in login.py
1 parent fc05d26 commit 94823b2

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

apps/users/serializers/login.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,42 +142,43 @@ class CaptchaSerializer(serializers.Serializer):
142142
def generate(username: str, type: str = 'system'):
143143
auth_setting = LoginSerializer.get_auth_setting()
144144
max_attempts = auth_setting.get("max_attempts", 1)
145+
145146
need_captcha = False
146147
if max_attempts == -1:
147148
need_captcha = False
148149
elif max_attempts > 0:
149-
fail_count = cache.get(system_get_key(f'{type}_{username}'), version=system_version) or 0
150+
fail_count = cache.get(system_get_key(f'system_{username}'), version=system_version) or 0
150151
need_captcha = fail_count >= max_attempts
151152

152-
if need_captcha:
153-
chars = get_random_chars()
154-
image = ImageCaptcha()
155-
data = image.generate(chars)
156-
captcha = base64.b64encode(data.getbuffer())
157-
cache.set(Cache_Version.CAPTCHA.get_key(captcha=f'{type}_{username}'), chars.lower(),
158-
timeout=300, version=Cache_Version.CAPTCHA.get_version())
159-
return {'captcha': 'data:image/png;base64,' + captcha.decode()}
160-
return {'captcha': ''}
153+
return CaptchaSerializer._generate_captcha_if_needed(username, type, need_captcha)
161154

162155
@staticmethod
163156
def chat_generate(username: str, type: str = 'chat', access_token: str = ''):
164-
auth_setting = {}
165157
application_access_token = ApplicationAccessToken.objects.filter(
166158
access_token=access_token
167159
).first()
168160

169161
if not application_access_token:
170162
raise AppApiException(1005, _('Invalid access token'))
171-
if application_access_token:
172-
auth_setting = application_access_token.authentication_value
163+
164+
auth_setting = application_access_token.authentication_value
173165
max_attempts = auth_setting.get("max_attempts", 1)
166+
174167
need_captcha = False
175168
if max_attempts == -1:
176169
need_captcha = False
177170
elif max_attempts > 0:
178-
fail_count = cache.get(system_get_key(f'{type}_{username}'), version=system_version) or 0
171+
fail_count = cache.get(system_get_key(f'system_{username}'), version=system_version) or 0
179172
need_captcha = fail_count >= max_attempts
180173

174+
175+
return CaptchaSerializer._generate_captcha_if_needed(username, type, need_captcha)
176+
177+
@staticmethod
178+
def _generate_captcha_if_needed(username: str, type: str, need_captcha: bool):
179+
"""
180+
提取的公共验证码生成方法
181+
"""
181182
if need_captcha:
182183
chars = get_random_chars()
183184
image = ImageCaptcha()
@@ -187,6 +188,3 @@ def chat_generate(username: str, type: str = 'chat', access_token: str = ''):
187188
timeout=300, version=Cache_Version.CAPTCHA.get_version())
188189
return {'captcha': 'data:image/png;base64,' + captcha.decode()}
189190
return {'captcha': ''}
190-
191-
192-

0 commit comments

Comments
 (0)