2019-11-01 12:34:56 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-11-05 10:46:29 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.conf import settings
|
2021-10-07 07:30:39 +00:00
|
|
|
from rest_framework import status
|
2019-11-01 12:34:56 +00:00
|
|
|
|
2020-07-31 10:18:52 +00:00
|
|
|
from common.exceptions import JMSException
|
2019-11-05 10:46:29 +00:00
|
|
|
from .signals import post_auth_failed
|
2021-04-08 04:47:49 +00:00
|
|
|
from users.utils import LoginBlockUtil, MFABlockUtils
|
2019-11-01 12:34:56 +00:00
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
reason_password_failed = 'password_failed'
|
2020-06-30 09:12:38 +00:00
|
|
|
reason_password_decrypt_failed = 'password_decrypt_failed'
|
2019-11-05 10:46:29 +00:00
|
|
|
reason_mfa_failed = 'mfa_failed'
|
2020-03-12 08:24:38 +00:00
|
|
|
reason_mfa_unset = 'mfa_unset'
|
2019-11-05 10:46:29 +00:00
|
|
|
reason_user_not_exist = 'user_not_exist'
|
|
|
|
reason_password_expired = 'password_expired'
|
|
|
|
reason_user_invalid = 'user_invalid'
|
|
|
|
reason_user_inactive = 'user_inactive'
|
2021-04-09 07:01:22 +00:00
|
|
|
reason_user_expired = 'user_expired'
|
2021-02-26 09:33:11 +00:00
|
|
|
reason_backend_not_match = 'backend_not_match'
|
2021-03-11 12:17:44 +00:00
|
|
|
reason_acl_not_allow = 'acl_not_allow'
|
2021-03-24 11:01:35 +00:00
|
|
|
only_local_users_are_allowed = 'only_local_users_are_allowed'
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
reason_choices = {
|
|
|
|
reason_password_failed: _('Username/password check failed'),
|
2020-06-30 09:12:38 +00:00
|
|
|
reason_password_decrypt_failed: _('Password decrypt failed'),
|
2020-03-12 08:24:38 +00:00
|
|
|
reason_mfa_failed: _('MFA failed'),
|
|
|
|
reason_mfa_unset: _('MFA unset'),
|
2019-11-05 10:46:29 +00:00
|
|
|
reason_user_not_exist: _("Username does not exist"),
|
|
|
|
reason_password_expired: _("Password expired"),
|
|
|
|
reason_user_invalid: _('Disabled or expired'),
|
2021-02-26 09:33:11 +00:00
|
|
|
reason_user_inactive: _("This account is inactive."),
|
2021-04-09 07:01:22 +00:00
|
|
|
reason_user_expired: _("This account is expired"),
|
2021-03-11 12:17:44 +00:00
|
|
|
reason_backend_not_match: _("Auth backend not match"),
|
2021-03-22 05:56:40 +00:00
|
|
|
reason_acl_not_allow: _("ACL is not allowed"),
|
2021-03-24 11:01:35 +00:00
|
|
|
only_local_users_are_allowed: _("Only local users are allowed")
|
2019-11-05 10:46:29 +00:00
|
|
|
}
|
|
|
|
old_reason_choices = {
|
|
|
|
'0': '-',
|
|
|
|
'1': reason_choices[reason_password_failed],
|
|
|
|
'2': reason_choices[reason_mfa_failed],
|
|
|
|
'3': reason_choices[reason_user_not_exist],
|
|
|
|
'4': reason_choices[reason_password_expired],
|
|
|
|
}
|
|
|
|
|
|
|
|
session_empty_msg = _("No session found, check your cookie")
|
|
|
|
invalid_login_msg = _(
|
|
|
|
"The username or password you entered is incorrect, "
|
|
|
|
"please enter it again. "
|
|
|
|
"You can also try {times_try} times "
|
|
|
|
"(The account will be temporarily locked for {block_time} minutes)"
|
|
|
|
)
|
|
|
|
block_login_msg = _(
|
|
|
|
"The account has been locked "
|
|
|
|
"(please contact admin to unlock it or try again after {} minutes)"
|
|
|
|
)
|
2021-04-08 04:47:49 +00:00
|
|
|
block_mfa_msg = _(
|
|
|
|
"The account has been locked "
|
|
|
|
"(please contact admin to unlock it or try again after {} minutes)"
|
|
|
|
)
|
2021-11-10 03:30:48 +00:00
|
|
|
mfa_error_msg = _(
|
|
|
|
"{error},"
|
2021-08-24 06:20:54 +00:00
|
|
|
"You can also try {times_try} times "
|
|
|
|
"(The account will be temporarily locked for {block_time} minutes)"
|
|
|
|
)
|
2019-11-05 10:46:29 +00:00
|
|
|
mfa_required_msg = _("MFA required")
|
2020-03-12 08:24:38 +00:00
|
|
|
mfa_unset_msg = _("MFA not set, please set it first")
|
2021-10-18 10:41:41 +00:00
|
|
|
otp_unset_msg = _("OTP not set, please set it first")
|
2019-11-05 10:46:29 +00:00
|
|
|
login_confirm_required_msg = _("Login confirm required")
|
2019-11-07 10:06:58 +00:00
|
|
|
login_confirm_wait_msg = _("Wait login confirm ticket for accept")
|
2019-11-08 07:48:01 +00:00
|
|
|
login_confirm_error_msg = _("Login confirm ticket was {}")
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthFailedNeedLogMixin:
|
|
|
|
username = ''
|
|
|
|
request = None
|
|
|
|
error = ''
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
post_auth_failed.send(
|
|
|
|
sender=self.__class__, username=self.username,
|
|
|
|
request=self.request, reason=self.error
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AuthFailedNeedBlockMixin:
|
|
|
|
username = ''
|
|
|
|
ip = ''
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2021-04-08 04:47:49 +00:00
|
|
|
LoginBlockUtil(self.username, self.ip).incr_failed_count()
|
2019-11-01 12:34:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthFailedError(Exception):
|
2019-11-05 10:46:29 +00:00
|
|
|
username = ''
|
|
|
|
msg = ''
|
|
|
|
error = ''
|
|
|
|
request = None
|
|
|
|
ip = ''
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
|
|
|
def as_data(self):
|
|
|
|
return {
|
|
|
|
'error': self.error,
|
|
|
|
'msg': self.msg,
|
|
|
|
}
|
|
|
|
|
2020-04-20 02:44:45 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.msg)
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
class CredentialError(AuthFailedNeedLogMixin, AuthFailedNeedBlockMixin, AuthFailedError):
|
|
|
|
def __init__(self, error, username, ip, request):
|
|
|
|
super().__init__(error=error, username=username, ip=ip, request=request)
|
2021-04-08 04:47:49 +00:00
|
|
|
util = LoginBlockUtil(username, ip)
|
|
|
|
times_remainder = util.get_remainder_times()
|
2019-11-05 10:46:29 +00:00
|
|
|
block_time = settings.SECURITY_LOGIN_LIMIT_TIME
|
|
|
|
|
2021-10-25 03:32:13 +00:00
|
|
|
if times_remainder < 1:
|
|
|
|
self.msg = block_login_msg.format(settings.SECURITY_LOGIN_LIMIT_TIME)
|
|
|
|
return
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
default_msg = invalid_login_msg.format(
|
2021-04-08 04:47:49 +00:00
|
|
|
times_try=times_remainder, block_time=block_time
|
2019-11-05 10:46:29 +00:00
|
|
|
)
|
|
|
|
if error == reason_password_failed:
|
|
|
|
self.msg = default_msg
|
|
|
|
else:
|
|
|
|
self.msg = reason_choices.get(error, default_msg)
|
|
|
|
|
|
|
|
|
|
|
|
class MFAFailedError(AuthFailedNeedLogMixin, AuthFailedError):
|
2019-11-18 10:12:03 +00:00
|
|
|
error = reason_mfa_failed
|
2021-04-08 04:47:49 +00:00
|
|
|
msg: str
|
2019-11-05 10:46:29 +00:00
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
def __init__(self, username, request, ip, mfa_type, error):
|
|
|
|
super().__init__(username=username, request=request)
|
2021-04-08 04:47:49 +00:00
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
util = MFABlockUtils(username, ip)
|
|
|
|
times_remainder = util.incr_failed_count()
|
2021-04-08 04:47:49 +00:00
|
|
|
block_time = settings.SECURITY_LOGIN_LIMIT_TIME
|
|
|
|
|
|
|
|
if times_remainder:
|
2021-11-10 03:30:48 +00:00
|
|
|
self.msg = mfa_error_msg.format(
|
|
|
|
error=error, times_try=times_remainder, block_time=block_time
|
|
|
|
)
|
2021-04-08 04:47:49 +00:00
|
|
|
else:
|
|
|
|
self.msg = block_mfa_msg.format(settings.SECURITY_LOGIN_LIMIT_TIME)
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
class BlockMFAError(AuthFailedNeedLogMixin, AuthFailedError):
|
|
|
|
error = 'block_mfa'
|
|
|
|
|
|
|
|
def __init__(self, username, request, ip):
|
|
|
|
self.msg = block_mfa_msg.format(settings.SECURITY_LOGIN_LIMIT_TIME)
|
|
|
|
super().__init__(username=username, request=request, ip=ip)
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class MFAUnsetError(AuthFailedNeedLogMixin, AuthFailedError):
|
|
|
|
error = reason_mfa_unset
|
|
|
|
msg = mfa_unset_msg
|
|
|
|
|
|
|
|
def __init__(self, user, request, url):
|
|
|
|
super().__init__(username=user.username, request=request)
|
|
|
|
self.user = user
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
class BlockLoginError(AuthFailedNeedBlockMixin, AuthFailedError):
|
|
|
|
error = 'block_login'
|
|
|
|
|
|
|
|
def __init__(self, username, ip):
|
2020-01-02 08:24:13 +00:00
|
|
|
self.msg = block_login_msg.format(settings.SECURITY_LOGIN_LIMIT_TIME)
|
2019-11-05 10:46:29 +00:00
|
|
|
super().__init__(username=username, ip=ip)
|
|
|
|
|
|
|
|
|
|
|
|
class SessionEmptyError(AuthFailedError):
|
|
|
|
msg = session_empty_msg
|
2019-11-06 04:06:30 +00:00
|
|
|
error = 'session_empty'
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
|
2019-11-08 08:32:00 +00:00
|
|
|
class NeedMoreInfoError(Exception):
|
|
|
|
error = ''
|
|
|
|
msg = ''
|
|
|
|
|
|
|
|
def __init__(self, error='', msg=''):
|
|
|
|
if error:
|
|
|
|
self.error = error
|
|
|
|
if msg:
|
2019-11-11 08:43:09 +00:00
|
|
|
self.msg = msg
|
2019-11-08 08:32:00 +00:00
|
|
|
|
|
|
|
def as_data(self):
|
|
|
|
return {
|
|
|
|
'error': self.error,
|
|
|
|
'msg': self.msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MFARequiredError(NeedMoreInfoError):
|
2019-11-05 10:46:29 +00:00
|
|
|
msg = mfa_required_msg
|
2019-11-06 04:06:30 +00:00
|
|
|
error = 'mfa_required'
|
2019-11-05 10:46:29 +00:00
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
def __init__(self, error='', msg='', mfa_types=()):
|
2021-08-24 06:20:54 +00:00
|
|
|
super().__init__(error=error, msg=msg)
|
|
|
|
self.choices = mfa_types
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
def as_data(self):
|
|
|
|
return {
|
|
|
|
'error': self.error,
|
|
|
|
'msg': self.msg,
|
2019-11-06 08:59:54 +00:00
|
|
|
'data': {
|
2021-08-24 06:20:54 +00:00
|
|
|
'choices': self.choices,
|
2019-11-06 08:59:54 +00:00
|
|
|
'url': reverse('api-auth:mfa-challenge')
|
|
|
|
}
|
2019-11-05 10:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-22 05:56:40 +00:00
|
|
|
class ACLError(AuthFailedNeedLogMixin, AuthFailedError):
|
|
|
|
msg = reason_acl_not_allow
|
|
|
|
error = 'acl_error'
|
|
|
|
|
|
|
|
def __init__(self, msg, **kwargs):
|
|
|
|
self.msg = msg
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def as_data(self):
|
|
|
|
return {
|
|
|
|
"error": reason_acl_not_allow,
|
|
|
|
"msg": self.msg
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LoginIPNotAllowed(ACLError):
|
|
|
|
def __init__(self, username, request, **kwargs):
|
|
|
|
self.username = username
|
|
|
|
self.request = request
|
|
|
|
super().__init__(_("IP is not allowed"), **kwargs)
|
|
|
|
|
|
|
|
|
2021-10-20 09:56:59 +00:00
|
|
|
class TimePeriodNotAllowed(ACLError):
|
|
|
|
def __init__(self, username, request, **kwargs):
|
|
|
|
self.username = username
|
|
|
|
self.request = request
|
|
|
|
super().__init__(_("Time Period is not allowed"), **kwargs)
|
|
|
|
|
|
|
|
|
2019-11-08 08:32:00 +00:00
|
|
|
class LoginConfirmBaseError(NeedMoreInfoError):
|
2019-11-07 10:06:58 +00:00
|
|
|
def __init__(self, ticket_id, **kwargs):
|
|
|
|
self.ticket_id = ticket_id
|
2019-11-05 10:46:29 +00:00
|
|
|
super().__init__(**kwargs)
|
2019-11-01 12:34:56 +00:00
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
def as_data(self):
|
|
|
|
return {
|
|
|
|
"error": self.error,
|
|
|
|
"msg": self.msg,
|
2019-11-06 08:59:54 +00:00
|
|
|
"data": {
|
2019-11-07 10:06:58 +00:00
|
|
|
"ticket_id": self.ticket_id
|
2019-11-06 08:59:54 +00:00
|
|
|
}
|
2019-11-05 10:46:29 +00:00
|
|
|
}
|
2019-11-01 12:34:56 +00:00
|
|
|
|
|
|
|
|
2019-11-08 08:32:00 +00:00
|
|
|
class LoginConfirmWaitError(LoginConfirmBaseError):
|
2019-11-05 10:46:29 +00:00
|
|
|
msg = login_confirm_wait_msg
|
2019-11-06 04:06:30 +00:00
|
|
|
error = 'login_confirm_wait'
|
2019-11-01 12:34:56 +00:00
|
|
|
|
|
|
|
|
2019-11-08 08:32:00 +00:00
|
|
|
class LoginConfirmOtherError(LoginConfirmBaseError):
|
2019-11-08 07:48:01 +00:00
|
|
|
error = 'login_confirm_error'
|
2019-11-01 12:34:56 +00:00
|
|
|
|
2019-11-08 07:48:01 +00:00
|
|
|
def __init__(self, ticket_id, status):
|
|
|
|
msg = login_confirm_error_msg.format(status)
|
|
|
|
super().__init__(ticket_id=ticket_id, msg=msg)
|
2020-07-31 10:18:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SSOAuthClosed(JMSException):
|
|
|
|
default_code = 'sso_auth_closed'
|
|
|
|
default_detail = _('SSO auth closed')
|
2020-08-05 08:04:05 +00:00
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
class PasswordTooSimple(JMSException):
|
2020-08-05 08:04:05 +00:00
|
|
|
default_code = 'passwd_too_simple'
|
|
|
|
default_detail = _('Your password is too simple, please change it for security')
|
|
|
|
|
|
|
|
def __init__(self, url, *args, **kwargs):
|
2020-12-09 10:17:10 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
class PasswordNeedUpdate(JMSException):
|
2021-04-28 11:25:30 +00:00
|
|
|
default_code = 'passwd_need_update'
|
2021-04-29 04:21:39 +00:00
|
|
|
default_detail = _('You should to change your password before login')
|
2021-04-28 11:25:30 +00:00
|
|
|
|
|
|
|
def __init__(self, url, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
|
2020-12-09 10:17:10 +00:00
|
|
|
class PasswordRequireResetError(JMSException):
|
|
|
|
default_code = 'passwd_has_expired'
|
|
|
|
default_detail = _('Your password has expired, please reset before logging in')
|
|
|
|
|
|
|
|
def __init__(self, url, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-05 08:04:05 +00:00
|
|
|
self.url = url
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WeComCodeInvalid(JMSException):
|
|
|
|
default_code = 'wecom_code_invalid'
|
|
|
|
default_detail = 'Code invalid, can not get user info'
|
|
|
|
|
|
|
|
|
|
|
|
class WeComBindAlready(JMSException):
|
|
|
|
default_code = 'wecom_bind_already'
|
|
|
|
default_detail = 'WeCom already binded'
|
|
|
|
|
|
|
|
|
|
|
|
class WeComNotBound(JMSException):
|
|
|
|
default_code = 'wecom_not_bound'
|
|
|
|
default_detail = 'WeCom is not bound'
|
|
|
|
|
|
|
|
|
|
|
|
class DingTalkNotBound(JMSException):
|
|
|
|
default_code = 'dingtalk_not_bound'
|
|
|
|
default_detail = 'DingTalk is not bound'
|
|
|
|
|
|
|
|
|
2021-08-12 08:44:06 +00:00
|
|
|
class FeiShuNotBound(JMSException):
|
|
|
|
default_code = 'feishu_not_bound'
|
|
|
|
default_detail = 'FeiShu is not bound'
|
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
class PasswordInvalid(JMSException):
|
2021-03-24 11:01:35 +00:00
|
|
|
default_code = 'passwd_invalid'
|
|
|
|
default_detail = _('Your password is invalid')
|
2021-10-07 07:30:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotHaveUpDownLoadPerm(JMSException):
|
|
|
|
status_code = status.HTTP_403_FORBIDDEN
|
|
|
|
code = 'not_have_up_down_load_perm'
|
|
|
|
default_detail = _('No upload or download permission')
|
2021-10-18 10:41:41 +00:00
|
|
|
|
|
|
|
|
2021-10-25 08:39:03 +00:00
|
|
|
class OTPBindRequiredError(JMSException):
|
2021-10-18 10:41:41 +00:00
|
|
|
default_detail = otp_unset_msg
|
|
|
|
|
|
|
|
def __init__(self, url, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.url = url
|
2021-10-25 08:39:03 +00:00
|
|
|
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
class MFACodeRequiredError(AuthFailedError):
|
2021-10-25 08:39:03 +00:00
|
|
|
msg = _("Please enter MFA code")
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
2021-10-25 08:39:03 +00:00
|
|
|
class SMSCodeRequiredError(AuthFailedError):
|
|
|
|
msg = _("Please enter SMS code")
|
|
|
|
|
2021-11-10 03:30:48 +00:00
|
|
|
|
2021-10-25 08:39:03 +00:00
|
|
|
class UserPhoneNotSet(AuthFailedError):
|
|
|
|
msg = _('Phone not set')
|