2021-08-24 06:20:54 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2023-05-24 09:32:57 +00:00
|
|
|
|
from django.db import models
|
2021-08-24 06:20:54 +00:00
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from common.serializers.fields import EncryptedField
|
2022-08-15 09:39:54 +00:00
|
|
|
|
from common.validators import PhoneValidator
|
2021-10-21 09:08:17 +00:00
|
|
|
|
from common.sdk.sms import BACKENDS
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2022-08-09 08:09:20 +00:00
|
|
|
|
__all__ = [
|
|
|
|
|
'SMSSettingSerializer', 'AlibabaSMSSettingSerializer', 'TencentSMSSettingSerializer',
|
2023-05-24 09:32:57 +00:00
|
|
|
|
'HuaweiSMSSettingSerializer', 'CMPP2SMSSettingSerializer', 'CustomSMSSettingSerializer',
|
2022-08-09 08:09:20 +00:00
|
|
|
|
]
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
2021-09-09 12:59:26 +00:00
|
|
|
|
class SMSSettingSerializer(serializers.Serializer):
|
2021-09-09 12:12:52 +00:00
|
|
|
|
SMS_ENABLED = serializers.BooleanField(default=False, label=_('Enable SMS'))
|
2021-09-13 12:15:59 +00:00
|
|
|
|
SMS_BACKEND = serializers.ChoiceField(
|
2022-08-09 08:09:20 +00:00
|
|
|
|
choices=BACKENDS.choices, default=BACKENDS.ALIBABA, label=_('SMS provider / Protocol')
|
2021-09-13 12:15:59 +00:00
|
|
|
|
)
|
2021-09-09 13:11:57 +00:00
|
|
|
|
|
|
|
|
|
|
2021-09-13 12:15:59 +00:00
|
|
|
|
class SignTmplPairSerializer(serializers.Serializer):
|
|
|
|
|
SIGN_NAME = serializers.CharField(max_length=256, required=True, label=_('Signature'))
|
|
|
|
|
TEMPLATE_CODE = serializers.CharField(max_length=256, required=True, label=_('Template code'))
|
2021-09-09 12:59:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BaseSMSSettingSerializer(serializers.Serializer):
|
2022-11-04 06:22:38 +00:00
|
|
|
|
PREFIX_TITLE = _('SMS')
|
|
|
|
|
|
2022-08-15 09:39:54 +00:00
|
|
|
|
SMS_TEST_PHONE = serializers.CharField(
|
|
|
|
|
max_length=256, required=False, validators=[PhoneValidator(), ],
|
|
|
|
|
allow_blank=True, label=_('Test phone')
|
|
|
|
|
)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
|
data = super().to_representation(instance)
|
2021-09-09 12:59:26 +00:00
|
|
|
|
# data['SMS_BACKEND'] = self.fields['SMS_BACKEND'].default
|
2021-08-24 06:20:54 +00:00
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AlibabaSMSSettingSerializer(BaseSMSSettingSerializer):
|
2023-05-26 07:15:21 +00:00
|
|
|
|
ALIBABA_ACCESS_KEY_ID = serializers.CharField(max_length=256, required=True, label='Access Key ID')
|
2022-05-20 02:01:41 +00:00
|
|
|
|
ALIBABA_ACCESS_KEY_SECRET = EncryptedField(
|
2023-05-26 07:15:21 +00:00
|
|
|
|
max_length=256, required=False, label='Access Key Secret',
|
2021-08-24 06:20:54 +00:00
|
|
|
|
)
|
2021-09-13 12:15:59 +00:00
|
|
|
|
ALIBABA_VERIFY_SIGN_NAME = serializers.CharField(max_length=256, required=True, label=_('Signature'))
|
|
|
|
|
ALIBABA_VERIFY_TEMPLATE_CODE = serializers.CharField(max_length=256, required=True, label=_('Template code'))
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TencentSMSSettingSerializer(BaseSMSSettingSerializer):
|
2023-05-26 07:15:21 +00:00
|
|
|
|
TENCENT_SECRET_ID = serializers.CharField(max_length=256, required=True, label='Secret ID')
|
|
|
|
|
TENCENT_SECRET_KEY = EncryptedField(max_length=256, required=False, label='Secret Key')
|
|
|
|
|
TENCENT_SDKAPPID = serializers.CharField(max_length=256, required=True, label='SDK APP ID')
|
2021-09-13 12:15:59 +00:00
|
|
|
|
TENCENT_VERIFY_SIGN_NAME = serializers.CharField(max_length=256, required=True, label=_('Signature'))
|
|
|
|
|
TENCENT_VERIFY_TEMPLATE_CODE = serializers.CharField(max_length=256, required=True, label=_('Template code'))
|
2022-08-09 08:09:20 +00:00
|
|
|
|
|
|
|
|
|
|
2022-09-06 02:02:51 +00:00
|
|
|
|
class HuaweiSMSSettingSerializer(BaseSMSSettingSerializer):
|
2023-05-26 07:15:21 +00:00
|
|
|
|
HUAWEI_APP_KEY = serializers.CharField(max_length=256, required=True, label='App Key')
|
|
|
|
|
HUAWEI_APP_SECRET = EncryptedField(max_length=256, required=False, label='App Secret')
|
2022-09-06 02:02:51 +00:00
|
|
|
|
HUAWEI_SMS_ENDPOINT = serializers.CharField(max_length=1024, required=True, label=_('App Access Address'))
|
|
|
|
|
HUAWEI_SIGN_CHANNEL_NUM = serializers.CharField(max_length=1024, required=True, label=_('Signature channel number'))
|
|
|
|
|
HUAWEI_VERIFY_SIGN_NAME = serializers.CharField(max_length=256, required=True, label=_('Signature'))
|
|
|
|
|
HUAWEI_VERIFY_TEMPLATE_CODE = serializers.CharField(max_length=256, required=True, label=_('Template code'))
|
|
|
|
|
|
|
|
|
|
|
2022-08-09 08:09:20 +00:00
|
|
|
|
class CMPP2SMSSettingSerializer(BaseSMSSettingSerializer):
|
|
|
|
|
CMPP2_HOST = serializers.CharField(max_length=256, required=True, label=_('Host'))
|
|
|
|
|
CMPP2_PORT = serializers.IntegerField(default=7890, label=_('Port'))
|
2022-08-10 09:32:28 +00:00
|
|
|
|
CMPP2_SP_ID = serializers.CharField(max_length=128, required=True, label=_('Enterprise code(SP id)'))
|
|
|
|
|
CMPP2_SP_SECRET = EncryptedField(max_length=256, required=False, label=_('Shared secret(Shared secret)'))
|
2022-08-09 08:09:20 +00:00
|
|
|
|
CMPP2_SRC_ID = serializers.CharField(max_length=256, required=False, label=_('Original number(Src id)'))
|
|
|
|
|
CMPP2_SERVICE_ID = serializers.CharField(max_length=256, required=True, label=_('Business type(Service id)'))
|
|
|
|
|
CMPP2_VERIFY_SIGN_NAME = serializers.CharField(max_length=256, required=True, label=_('Signature'))
|
|
|
|
|
CMPP2_VERIFY_TEMPLATE_CODE = serializers.CharField(
|
|
|
|
|
max_length=69, required=True, label=_('Template'),
|
|
|
|
|
help_text=_('Template need contain {code} and Signature + template length does not exceed 67 words. '
|
|
|
|
|
'For example, your verification code is {code}, which is valid for 5 minutes. '
|
|
|
|
|
'Please do not disclose it to others.')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
|
sign_name = attrs.get('CMPP2_VERIFY_SIGN_NAME', '')
|
|
|
|
|
template_code = attrs.get('CMPP2_VERIFY_TEMPLATE_CODE', '')
|
|
|
|
|
if template_code.find('{code}') == -1:
|
|
|
|
|
raise serializers.ValidationError(_('The template needs to contain {code}'))
|
|
|
|
|
if len(sign_name + template_code) > 65:
|
|
|
|
|
# 保证验证码内容在一条短信中(长度小于70字), 签名两边的括号和空格占3个字,再减去2个即可(验证码占用4个但占位符6个
|
|
|
|
|
raise serializers.ValidationError(_('Signature + Template must not exceed 65 words'))
|
|
|
|
|
return attrs
|
2023-05-24 09:32:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CustomSMSSettingSerializer(BaseSMSSettingSerializer):
|
|
|
|
|
class RequestType(models.TextChoices):
|
|
|
|
|
get = 'get', 'Get'
|
|
|
|
|
post = 'post', 'Post'
|
|
|
|
|
|
|
|
|
|
CUSTOM_SMS_URL = serializers.URLField(required=True, label=_("URL"))
|
2023-06-08 02:06:14 +00:00
|
|
|
|
CUSTOM_SMS_API_PARAMS = serializers.JSONField(
|
2023-05-24 09:32:57 +00:00
|
|
|
|
label=_('Parameters'), default={'phone_number': '{phone_number}', 'code': '{code}'}
|
|
|
|
|
)
|
|
|
|
|
CUSTOM_SMS_REQUEST_METHOD = serializers.ChoiceField(
|
|
|
|
|
default=RequestType.get, choices=RequestType.choices, label=_("Request method")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def validate(attrs):
|
|
|
|
|
need_params = {'{phone_numbers}', '{code}'}
|
|
|
|
|
params = attrs.get('CUSTOM_SMS_API_PARAMS', {})
|
|
|
|
|
if len(set(params.values()) & need_params) != len(need_params):
|
|
|
|
|
raise serializers.ValidationError(
|
|
|
|
|
_('The value in the parameter must contain %s') % ','.join(need_params)
|
|
|
|
|
)
|
|
|
|
|
return attrs
|