mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-18 16:39:28 +00:00
perf: 优化confirm接口 (#8451)
* perf: 优化confirm接口 * perf: 修改 校验 * perf: 优化 confirm API 逻辑 * Delete django.po Co-authored-by: feng626 <1304903146@qq.com> Co-authored-by: ibuler <ibuler@qq.com> Co-authored-by: Jiangjie.Bai <bugatti_it@163.com> Co-authored-by: feng626 <57284900+feng626@users.noreply.github.com>
This commit is contained in:
5
apps/authentication/confirm/__init__.py
Normal file
5
apps/authentication/confirm/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from .mfa import ConfirmMFA
|
||||
from .password import ConfirmPassword
|
||||
from .relogin import ConfirmReLogin
|
||||
|
||||
CONFIRM_BACKENDS = [ConfirmReLogin, ConfirmPassword, ConfirmMFA]
|
30
apps/authentication/confirm/base.py
Normal file
30
apps/authentication/confirm/base.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import abc
|
||||
|
||||
|
||||
class BaseConfirm(abc.ABC):
|
||||
|
||||
def __init__(self, user, request):
|
||||
self.user = user
|
||||
self.request = request
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def name(self) -> str:
|
||||
return ''
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def display_name(self) -> str:
|
||||
return ''
|
||||
|
||||
@abc.abstractmethod
|
||||
def check(self) -> bool:
|
||||
return False
|
||||
|
||||
@property
|
||||
def content(self):
|
||||
return ''
|
||||
|
||||
@abc.abstractmethod
|
||||
def authenticate(self, secret_key, mfa_type) -> tuple:
|
||||
return False, 'Error msg'
|
26
apps/authentication/confirm/mfa.py
Normal file
26
apps/authentication/confirm/mfa.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from users.models import User
|
||||
|
||||
from .base import BaseConfirm
|
||||
|
||||
|
||||
class ConfirmMFA(BaseConfirm):
|
||||
name = 'mfa'
|
||||
display_name = 'MFA'
|
||||
|
||||
def check(self):
|
||||
return self.user.active_mfa_backends
|
||||
|
||||
@property
|
||||
def content(self):
|
||||
backends = User.get_user_mfa_backends(self.user)
|
||||
return [{
|
||||
'name': backend.name,
|
||||
'disabled': not bool(backend.is_active()),
|
||||
'display_name': backend.display_name,
|
||||
'placeholder': backend.placeholder,
|
||||
} for backend in backends]
|
||||
|
||||
def authenticate(self, secret_key, mfa_type):
|
||||
mfa_backend = self.user.get_mfa_backend_by_type(mfa_type)
|
||||
ok, msg = mfa_backend.check_code(secret_key)
|
||||
return ok, msg
|
17
apps/authentication/confirm/password.py
Normal file
17
apps/authentication/confirm/password.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from authentication.mixins import authenticate
|
||||
from .base import BaseConfirm
|
||||
|
||||
|
||||
class ConfirmPassword(BaseConfirm):
|
||||
name = 'password'
|
||||
display_name = _('Password')
|
||||
|
||||
def check(self):
|
||||
return self.user.is_password_authenticate()
|
||||
|
||||
def authenticate(self, secret_key, mfa_type):
|
||||
ok = authenticate(self.request, username=self.user.username, password=secret_key)
|
||||
msg = '' if ok else _('Authentication failed password incorrect')
|
||||
return ok, msg
|
30
apps/authentication/confirm/relogin.py
Normal file
30
apps/authentication/confirm/relogin.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .base import BaseConfirm
|
||||
|
||||
SPECIFIED_TIME = 5
|
||||
|
||||
RELOGIN_ERROR = _('Login time has exceeded {} minutes, please login again').format(SPECIFIED_TIME)
|
||||
|
||||
|
||||
class ConfirmReLogin(BaseConfirm):
|
||||
name = 'relogin'
|
||||
display_name = 'Re-Login'
|
||||
|
||||
def check(self):
|
||||
return not self.user.is_password_authenticate()
|
||||
|
||||
def authenticate(self, secret_key, mfa_type):
|
||||
now = timezone.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
now = datetime.strptime(now, '%Y-%m-%d %H:%M:%S')
|
||||
login_time = self.request.session.get('login_time')
|
||||
msg = RELOGIN_ERROR
|
||||
if not login_time:
|
||||
return False, msg
|
||||
login_time = datetime.strptime(login_time, '%Y-%m-%d %H:%M:%S')
|
||||
if (now - login_time).seconds >= SPECIFIED_TIME * 60:
|
||||
return False, msg
|
||||
return True, ''
|
Reference in New Issue
Block a user