2021-11-10 03:30:48 +00:00
|
|
|
from django.conf import settings
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-11-10 03:30:48 +00:00
|
|
|
|
2022-11-04 05:56:55 +00:00
|
|
|
from common.utils.verify_code import SendAndVerifyCodeUtil
|
2023-07-24 03:52:25 +00:00
|
|
|
from .base import BaseMFA
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
|
|
sms_failed_msg = _("SMS verify code invalid")
|
|
|
|
|
|
|
|
|
|
|
|
class MFASms(BaseMFA):
|
|
|
|
name = 'sms'
|
|
|
|
display_name = _("SMS")
|
|
|
|
placeholder = _("SMS verification code")
|
|
|
|
|
|
|
|
def __init__(self, user):
|
|
|
|
super().__init__(user)
|
|
|
|
phone = user.phone if self.is_authenticated() else ''
|
2022-11-04 05:56:55 +00:00
|
|
|
self.sms = SendAndVerifyCodeUtil(phone, backend=self.name)
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
|
|
def check_code(self, code):
|
|
|
|
assert self.is_authenticated()
|
2021-11-15 10:36:22 +00:00
|
|
|
ok = False
|
|
|
|
msg = ''
|
|
|
|
try:
|
|
|
|
ok = self.sms.verify(code)
|
|
|
|
except Exception as e:
|
|
|
|
msg = str(e)
|
2021-11-10 03:30:48 +00:00
|
|
|
return ok, msg
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
if not self.is_authenticated():
|
|
|
|
return True
|
|
|
|
return self.user.phone
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def challenge_required():
|
|
|
|
return True
|
|
|
|
|
|
|
|
def send_challenge(self):
|
2022-11-04 05:56:55 +00:00
|
|
|
self.sms.gen_and_send_async()
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def global_enabled():
|
|
|
|
return settings.SMS_ENABLED
|
|
|
|
|
|
|
|
def get_enable_url(self) -> str:
|
2022-03-15 07:42:10 +00:00
|
|
|
return '/ui/#/profile/setting?activeTab=ProfileUpdate'
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
|
|
def can_disable(self) -> bool:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def disable(self):
|
2022-03-15 07:42:10 +00:00
|
|
|
return '/ui/#/profile/setting?activeTab=ProfileUpdate'
|
2021-11-10 03:30:48 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def help_text_of_enable():
|
|
|
|
return _("Set phone number to enable")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def help_text_of_disable():
|
|
|
|
return _("Clear phone number to disable")
|
|
|
|
|
|
|
|
def get_disable_url(self) -> str:
|
2022-03-15 07:42:10 +00:00
|
|
|
return '/ui/#/profile/setting?activeTab=ProfileUpdate'
|