Exemplo n.º 1
0
def _handle_failed_authentication(user, authenticated_user):
    """
    Handles updating the failed login count, inactive user notifications, and logging failed authentications.
    """
    failure_count = 0
    if user:
        if LoginFailures.is_feature_enabled():
            LoginFailures.increment_lockout_counter(user)

        if authenticated_user and not user.is_active:
            _log_and_raise_inactive_user_auth_error(user)

        # if we didn't find this username earlier, the account for this email
        # doesn't exist, and doesn't have a corresponding password
        loggable_id = user.id if user else "<unknown>"
        AUDIT_LOG.warning(
            f"Login failed - password for user.id: {loggable_id} is invalid")

    if user and LoginFailures.is_feature_enabled():
        blocked_threshold, failure_count = LoginFailures.check_user_reset_password_threshold(
            user)
        if blocked_threshold:
            if not LoginFailures.is_user_locked_out(user):
                max_failures_allowed = settings.MAX_FAILED_LOGIN_ATTEMPTS_ALLOWED
                remaining_attempts = max_failures_allowed - failure_count
                error_message = Text(
                    _('Email or password is incorrect.'
                      '{li_start}You have {remaining_attempts} more sign-in '
                      'attempts before your account is temporarily locked.{li_end}'
                      '{li_start}If you\'ve forgotten your password, click '
                      '{link_start}here{link_end} to reset.{li_end}')
                ).format(link_start=HTML(
                    '<a http="#login" class="form-toggle" data-type="password-reset">'
                ),
                         link_end=HTML('</a>'),
                         li_start=HTML('<li>'),
                         li_end=HTML('</li>'),
                         remaining_attempts=remaining_attempts)
                raise AuthFailedError(error_message,
                                      error_code='failed-login-attempt',
                                      context={
                                          'remaining_attempts':
                                          remaining_attempts,
                                          'allowed_failure_attempts':
                                          max_failures_allowed,
                                          'failure_count': failure_count,
                                      })

            _generate_locked_out_error_message()

    raise AuthFailedError(
        _('Email or password is incorrect.'),
        error_code='incorrect-email-or-password',
        context={'failure_count': failure_count},
    )
Exemplo n.º 2
0
def _handle_failed_authentication(user, authenticated_user):
    """
    Handles updating the failed login count, inactive user notifications, and logging failed authentications.
    """
    if user:
        if LoginFailures.is_feature_enabled():
            LoginFailures.increment_lockout_counter(user)

        if authenticated_user and not user.is_active:
            _log_and_raise_inactive_user_auth_error(user)

        # if we didn't find this username earlier, the account for this email
        # doesn't exist, and doesn't have a corresponding password
        if settings.FEATURES['SQUELCH_PII_IN_LOGS']:
            loggable_id = user.id if user else "<unknown>"
            AUDIT_LOG.warning(u"Login failed - password for user.id: {0} is invalid".format(loggable_id))
        else:
            AUDIT_LOG.warning(u"Login failed - password for {0} is invalid".format(user.email))

    if user and LoginFailures.is_feature_enabled():
        blocked_threshold, failure_count = LoginFailures.check_user_reset_password_threshold(user)
        if blocked_threshold:
            if not LoginFailures.is_user_locked_out(user):
                max_failures_allowed = settings.MAX_FAILED_LOGIN_ATTEMPTS_ALLOWED
                remaining_attempts = max_failures_allowed - failure_count
                if not should_redirect_to_logistration_mircrofrontend:  # pylint: disable=no-else-raise
                    raise AuthFailedError(Text(_('Email or password is incorrect.'
                                                 '{li_start}You have {remaining_attempts} more sign-in '
                                                 'attempts before your account is temporarily locked.{li_end}'
                                                 '{li_start}If you\'ve forgotten your password, click '
                                                 '{link_start}here{link_end} to reset.{li_end}'
                                                 ))
                                          .format(
                        link_start=HTML('<a http="#login" class="form-toggle" data-type="password-reset">'),
                        link_end=HTML('</a>'),
                        li_start=HTML('<li>'),
                        li_end=HTML('</li>'),
                        remaining_attempts=remaining_attempts))
                else:
                    raise AuthFailedError(Text(_('Email or password is incorrect.\n'
                                                 'You have {remaining_attempts} more sign-in '
                                                 'attempts before your account is temporarily locked.\n'
                                                 'If you{quote}ve forgotten your password, click '
                                                 '{link_start}here{link_end} to reset.\n'
                                                 ))
                                          .format(
                        quote=HTML("'"),
                        link_start=HTML('<a href="/reset" >'),
                        link_end=HTML('</a>'),
                        remaining_attempts=remaining_attempts))
            else:
                _generate_locked_out_error_message()

    raise AuthFailedError(_('Email or password is incorrect.'))