mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-03 16:35:10 +00:00
perf: 支持自定义短信认证
This commit is contained in:
committed by
Jiangjie.Bai
parent
5e7d474bb7
commit
20cc4ea320
48
apps/common/sdk/sms/custom.py
Normal file
48
apps/common/sdk/sms/custom.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import requests
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from common.utils import get_logger
|
||||
from common.exceptions import JMSException
|
||||
|
||||
from .base import BaseSMSClient
|
||||
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
class CustomSMS(BaseSMSClient):
|
||||
@classmethod
|
||||
def new_from_settings(cls):
|
||||
return cls()
|
||||
|
||||
@staticmethod
|
||||
def need_pre_check():
|
||||
return False
|
||||
|
||||
def send_sms(self, phone_numbers: list, template_param: OrderedDict, **kwargs):
|
||||
phone_numbers_str = ','.join(phone_numbers)
|
||||
params = {}
|
||||
for k, v in settings.CUSTOM_SMS_API_PARAMS.items():
|
||||
params[k] = v.format(
|
||||
code=template_param.get('code'), phone_numbers=phone_numbers_str
|
||||
)
|
||||
|
||||
logger.info(f'Custom sms send: phone_numbers={phone_numbers}param={params}')
|
||||
if settings.CUSTOM_SMS_REQUEST_METHOD == 'post':
|
||||
action = requests.post
|
||||
kwargs = {'json': params}
|
||||
else:
|
||||
action = requests.get
|
||||
kwargs = {'params': params}
|
||||
try:
|
||||
response = action(url=settings.CUSTOM_SMS_URL, **kwargs)
|
||||
if response.reason != 'OK':
|
||||
raise JMSException(detail=response.text, code=response.status_code)
|
||||
except Exception as exc:
|
||||
logger.error('Custom sms error: {}'.format(exc))
|
||||
|
||||
|
||||
client = CustomSMS
|
@@ -17,6 +17,7 @@ class BACKENDS(TextChoices):
|
||||
TENCENT = 'tencent', _('Tencent cloud')
|
||||
HUAWEI = 'huawei', _('Huawei Cloud')
|
||||
CMPP2 = 'cmpp2', _('CMPP v2.0')
|
||||
Custom = 'custom', _('Custom type')
|
||||
|
||||
|
||||
class SMS:
|
||||
@@ -42,8 +43,9 @@ class SMS:
|
||||
)
|
||||
|
||||
def send_verify_code(self, phone_number, code):
|
||||
sign_name = getattr(settings, f'{self.client.SIGN_AND_TMPL_SETTING_FIELD_PREFIX}_VERIFY_SIGN_NAME')
|
||||
template_code = getattr(settings, f'{self.client.SIGN_AND_TMPL_SETTING_FIELD_PREFIX}_VERIFY_TEMPLATE_CODE')
|
||||
prefix = getattr(self.client, 'SIGN_AND_TMPL_SETTING_FIELD_PREFIX', '')
|
||||
sign_name = getattr(settings, f'{prefix}_VERIFY_SIGN_NAME', None)
|
||||
template_code = getattr(settings, f'{prefix}_VERIFY_TEMPLATE_CODE', None)
|
||||
|
||||
if self.client.need_pre_check() and not (sign_name and template_code):
|
||||
raise JMSException(
|
||||
|
Reference in New Issue
Block a user