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
|
2019-11-01 12:34:56 +00:00
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
from .signals import post_auth_failed
|
|
|
|
from users.utils import (
|
|
|
|
increase_login_failed_count, get_login_failed_count
|
|
|
|
)
|
2019-11-01 12:34:56 +00:00
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
reason_password_failed = 'password_failed'
|
|
|
|
reason_mfa_failed = 'mfa_failed'
|
|
|
|
reason_user_not_exist = 'user_not_exist'
|
|
|
|
reason_password_expired = 'password_expired'
|
|
|
|
reason_user_invalid = 'user_invalid'
|
|
|
|
reason_user_inactive = 'user_inactive'
|
|
|
|
|
|
|
|
reason_choices = {
|
|
|
|
reason_password_failed: _('Username/password check failed'),
|
|
|
|
reason_mfa_failed: _('MFA authentication failed'),
|
|
|
|
reason_user_not_exist: _("Username does not exist"),
|
|
|
|
reason_password_expired: _("Password expired"),
|
|
|
|
reason_user_invalid: _('Disabled or expired'),
|
|
|
|
reason_user_inactive: _("This account is inactive.")
|
|
|
|
}
|
|
|
|
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)"
|
|
|
|
)
|
|
|
|
mfa_failed_msg = _("MFA code invalid, or ntp sync server time")
|
|
|
|
|
|
|
|
mfa_required_msg = _("MFA required")
|
|
|
|
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)
|
|
|
|
increase_login_failed_count(self.username, self.ip)
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CredentialError(AuthFailedNeedLogMixin, AuthFailedNeedBlockMixin, AuthFailedError):
|
|
|
|
def __init__(self, error, username, ip, request):
|
|
|
|
super().__init__(error=error, username=username, ip=ip, request=request)
|
|
|
|
times_up = settings.SECURITY_LOGIN_LIMIT_COUNT
|
|
|
|
times_failed = get_login_failed_count(username, ip)
|
|
|
|
times_try = int(times_up) - int(times_failed)
|
|
|
|
block_time = settings.SECURITY_LOGIN_LIMIT_TIME
|
|
|
|
|
|
|
|
default_msg = invalid_login_msg.format(
|
|
|
|
times_try=times_try, block_time=block_time
|
|
|
|
)
|
|
|
|
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
|
2019-11-05 10:46:29 +00:00
|
|
|
msg = mfa_failed_msg
|
|
|
|
|
|
|
|
def __init__(self, username, request):
|
|
|
|
super().__init__(username=username, request=request)
|
|
|
|
|
|
|
|
|
|
|
|
class BlockLoginError(AuthFailedNeedBlockMixin, AuthFailedError):
|
|
|
|
error = 'block_login'
|
|
|
|
msg = block_login_msg.format(settings.SECURITY_LOGIN_LIMIT_TIME)
|
|
|
|
|
|
|
|
def __init__(self, username, ip):
|
|
|
|
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
|
|
|
|
|
|
|
def as_data(self):
|
|
|
|
return {
|
|
|
|
'error': self.error,
|
|
|
|
'msg': self.msg,
|
2019-11-06 08:59:54 +00:00
|
|
|
'data': {
|
|
|
|
'choices': ['otp'],
|
|
|
|
'url': reverse('api-auth:mfa-challenge')
|
|
|
|
}
|
2019-11-05 10:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|