2021-08-24 06:20:54 +00:00
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
from django.core.cache import cache
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
from .endpoint import SMS
|
2021-08-24 06:20:54 +00:00
|
|
|
|
from common.utils import get_logger
|
|
|
|
|
from common.exceptions import JMSException
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CodeExpired(JMSException):
|
|
|
|
|
default_code = 'verify_code_expired'
|
|
|
|
|
default_detail = _('The verification code has expired. Please resend it')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CodeError(JMSException):
|
|
|
|
|
default_code = 'verify_code_error'
|
|
|
|
|
default_detail = _('The verification code is incorrect')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CodeSendTooFrequently(JMSException):
|
|
|
|
|
default_code = 'code_send_too_frequently'
|
|
|
|
|
default_detail = _('Please wait {} seconds before sending')
|
|
|
|
|
|
|
|
|
|
def __init__(self, ttl):
|
|
|
|
|
super().__init__(detail=self.default_detail.format(ttl))
|
|
|
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
class SendAndVerifySMSUtil:
|
|
|
|
|
KEY_TMPL = 'auth-verify-code-{}'
|
2021-08-24 06:20:54 +00:00
|
|
|
|
TIMEOUT = 60
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
def __init__(self, phone, key_suffix=None, timeout=None):
|
|
|
|
|
self.phone = phone
|
2021-08-24 06:20:54 +00:00
|
|
|
|
self.code = ''
|
2021-11-10 03:30:48 +00:00
|
|
|
|
self.timeout = timeout or self.TIMEOUT
|
|
|
|
|
self.key_suffix = key_suffix or str(phone)
|
2021-11-19 02:34:19 +00:00
|
|
|
|
self.key = self.KEY_TMPL.format(self.key_suffix)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
def gen_and_send(self):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
"""
|
|
|
|
|
生成,保存,发送
|
|
|
|
|
"""
|
2021-11-19 02:34:19 +00:00
|
|
|
|
ttl = self.ttl()
|
|
|
|
|
if ttl > 0:
|
|
|
|
|
logger.error('Send sms too frequently, delay {}'.format(ttl))
|
|
|
|
|
raise CodeSendTooFrequently(ttl)
|
|
|
|
|
|
2021-09-10 06:39:02 +00:00
|
|
|
|
try:
|
2021-11-10 03:30:48 +00:00
|
|
|
|
code = self.generate()
|
|
|
|
|
self.send(code)
|
2021-09-10 06:39:02 +00:00
|
|
|
|
except JMSException:
|
|
|
|
|
self.clear()
|
|
|
|
|
raise
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
def generate(self):
|
|
|
|
|
code = ''.join(random.sample('0123456789', 4))
|
|
|
|
|
self.code = code
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
|
cache.delete(self.key)
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
def send(self, code):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
"""
|
|
|
|
|
发送信息的方法,如果有错误直接抛出 api 异常
|
|
|
|
|
"""
|
|
|
|
|
sms = SMS()
|
2021-11-10 03:30:48 +00:00
|
|
|
|
sms.send_verify_code(self.phone, code)
|
|
|
|
|
cache.set(self.key, self.code, self.timeout)
|
|
|
|
|
logger.info(f'Send sms verify code to {self.phone}: {code}')
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
def verify(self, code):
|
|
|
|
|
right = cache.get(self.key)
|
|
|
|
|
if not right:
|
|
|
|
|
raise CodeExpired
|
|
|
|
|
|
|
|
|
|
if right != code:
|
|
|
|
|
raise CodeError
|
|
|
|
|
|
|
|
|
|
self.clear()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def ttl(self):
|
|
|
|
|
return cache.ttl(self.key)
|
|
|
|
|
|
|
|
|
|
def get_code(self):
|
|
|
|
|
return cache.get(self.key)
|