|
6 | 6 | @date:2023/9/5 16:32 |
7 | 7 | @desc: |
8 | 8 | """ |
| 9 | +import base64 |
9 | 10 | import datetime |
10 | 11 | import os |
11 | 12 | import random |
12 | 13 | import re |
13 | 14 | import uuid |
14 | 15 |
|
| 16 | +from captcha.image import ImageCaptcha |
15 | 17 | from django.conf import settings |
16 | 18 | from django.core import validators, signing, cache |
17 | 19 | from django.core.mail import send_mail |
18 | 20 | from django.core.mail.backends.smtp import EmailBackend |
19 | 21 | from django.db import transaction |
20 | 22 | from django.db.models import Q, QuerySet, Prefetch |
| 23 | +from django.utils.translation import get_language |
| 24 | +from django.utils.translation import gettext_lazy as _, to_locale |
21 | 25 | from drf_yasg import openapi |
22 | 26 | from rest_framework import serializers |
23 | 27 |
|
|
30 | 34 | from common.mixins.api_mixin import ApiMixin |
31 | 35 | from common.models.db_model_manage import DBModelManage |
32 | 36 | from common.response.result import get_api_response |
33 | | -from common.util.common import valid_license |
| 37 | +from common.util.common import valid_license, get_random_chars |
34 | 38 | from common.util.field_message import ErrMessage |
35 | 39 | from common.util.lock import lock |
36 | 40 | from dataset.models import DataSet, Document, Paragraph, Problem, ProblemParagraphMapping |
|
39 | 43 | from setting.models import Team, SystemSetting, SettingType, Model, TeamMember, TeamMemberPermission |
40 | 44 | from smartdoc.conf import PROJECT_DIR |
41 | 45 | from users.models.user import User, password_encrypt, get_user_dynamics_permission |
42 | | -from django.utils.translation import gettext_lazy as _, gettext, to_locale |
43 | | -from django.utils.translation import get_language |
| 46 | + |
44 | 47 | user_cache = cache.caches['user_cache'] |
| 48 | +captcha_cache = cache.caches['captcha_cache'] |
| 49 | + |
| 50 | + |
| 51 | +class CaptchaSerializer(ApiMixin, serializers.Serializer): |
| 52 | + @staticmethod |
| 53 | + def get_response_body_api(): |
| 54 | + return get_api_response(openapi.Schema( |
| 55 | + type=openapi.TYPE_STRING, |
| 56 | + title="captcha", |
| 57 | + default="xxxx", |
| 58 | + description="captcha" |
| 59 | + )) |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def generate(): |
| 63 | + chars = get_random_chars() |
| 64 | + image = ImageCaptcha() |
| 65 | + data = image.generate(chars) |
| 66 | + captcha = base64.b64encode(data.getbuffer()) |
| 67 | + captcha_cache.set(f"LOGIN:{chars}", chars, timeout=5 * 60) |
| 68 | + return 'data:image/png;base64,' + captcha.decode() |
45 | 69 |
|
46 | 70 |
|
47 | 71 | class SystemSerializer(ApiMixin, serializers.Serializer): |
@@ -71,13 +95,19 @@ class LoginSerializer(ApiMixin, serializers.Serializer): |
71 | 95 |
|
72 | 96 | password = serializers.CharField(required=True, error_messages=ErrMessage.char(_("Password"))) |
73 | 97 |
|
| 98 | + captcha = serializers.CharField(required=True, error_messages=ErrMessage.char(_("captcha"))) |
| 99 | + |
74 | 100 | def is_valid(self, *, raise_exception=False): |
75 | 101 | """ |
76 | 102 | 校验参数 |
77 | 103 | :param raise_exception: Whether to throw an exception can only be True |
78 | 104 | :return: User information |
79 | 105 | """ |
80 | 106 | super().is_valid(raise_exception=True) |
| 107 | + captcha = self.data.get('captcha') |
| 108 | + captcha_value = captcha_cache.get(f"LOGIN:{captcha}") |
| 109 | + if captcha_value is None: |
| 110 | + raise AppApiException(1005, _("Captcha code error or expiration")) |
81 | 111 | username = self.data.get("username") |
82 | 112 | password = password_encrypt(self.data.get("password")) |
83 | 113 | user = QuerySet(User).filter(Q(username=username, |
@@ -109,7 +139,8 @@ def get_request_body_api(self): |
109 | 139 | required=['username', 'password'], |
110 | 140 | properties={ |
111 | 141 | 'username': openapi.Schema(type=openapi.TYPE_STRING, title=_("Username"), description=_("Username")), |
112 | | - 'password': openapi.Schema(type=openapi.TYPE_STRING, title=_("Password"), description=_("Password")) |
| 142 | + 'password': openapi.Schema(type=openapi.TYPE_STRING, title=_("Password"), description=_("Password")), |
| 143 | + 'captcha': openapi.Schema(type=openapi.TYPE_STRING, title=_("captcha"), description=_("captcha")) |
113 | 144 | } |
114 | 145 | ) |
115 | 146 |
|
|
0 commit comments