jumpserver/apps/authentication/mfa/face.py

59 lines
1.6 KiB
Python
Raw Normal View History

2024-11-12 09:28:43 +00:00
from authentication.mfa.base import BaseMFA
from django.utils.translation import gettext_lazy as _
2024-12-06 10:19:36 +00:00
from authentication.mixins import AuthFaceMixin
2024-11-20 12:05:31 +00:00
from common.const import LicenseEditionChoices
2024-11-12 09:28:43 +00:00
from settings.api import settings
2024-12-06 10:19:36 +00:00
class MFAFace(BaseMFA, AuthFaceMixin):
2024-11-12 09:28:43 +00:00
name = "face"
display_name = _('Face Recognition')
placeholder = 'Face Recognition'
def check_code(self, code):
assert self.is_authenticated()
try:
code = self.get_face_code()
if not self.user.check_face(code):
return False, _('Facial comparison failed')
except Exception as e:
return False, "{}:{}".format(_('Facial comparison failed'), str(e))
return True, ''
def is_active(self):
if not self.is_authenticated():
return True
return bool(self.user.face_vector)
@staticmethod
def global_enabled():
2024-11-20 12:05:31 +00:00
return settings.XPACK_LICENSE_IS_VALID \
and LicenseEditionChoices.ULTIMATE == \
LicenseEditionChoices.from_key(settings.XPACK_LICENSE_EDITION) \
and settings.FACE_RECOGNITION_ENABLED
2024-11-12 09:28:43 +00:00
def get_enable_url(self) -> str:
2024-11-28 10:06:37 +00:00
return '/ui/#/profile/index'
2024-11-12 09:28:43 +00:00
def get_disable_url(self) -> str:
2024-11-28 10:06:37 +00:00
return '/ui/#/profile/index'
2024-11-12 09:28:43 +00:00
def disable(self):
assert self.is_authenticated()
self.user.face_vector = ''
self.user.save(update_fields=['face_vector'])
def can_disable(self) -> bool:
return True
@staticmethod
def help_text_of_enable():
2024-11-28 10:06:37 +00:00
return _("Bind face to enable")
@staticmethod
def help_text_of_disable():
return _("Unbind face to disable")