remove audit's LoginAttempt
This commit is contained in:
		@ -75,43 +75,3 @@ class AuditEntry(UUIDModel):
 | 
			
		||||
 | 
			
		||||
        verbose_name = _('Audit Entry')
 | 
			
		||||
        verbose_name_plural = _('Audit Entries')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LoginAttempt(CreatedUpdatedModel):
 | 
			
		||||
    """Track failed login-attempts"""
 | 
			
		||||
 | 
			
		||||
    target_uid = models.CharField(max_length=254)
 | 
			
		||||
    request_ip = models.GenericIPAddressField()
 | 
			
		||||
    attempts = models.IntegerField(default=1)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def attempt(target_uid, request):
 | 
			
		||||
        """Helper function to create attempt or count up existing one"""
 | 
			
		||||
        if not target_uid:
 | 
			
		||||
            return
 | 
			
		||||
        client_ip, _ = get_client_ip(request)
 | 
			
		||||
        # Since we can only use 254 chars for target_uid, truncate target_uid.
 | 
			
		||||
        target_uid = target_uid[:254]
 | 
			
		||||
        time_threshold = timezone.now() - timedelta(minutes=10)
 | 
			
		||||
        existing_attempts = LoginAttempt.objects.filter(
 | 
			
		||||
            target_uid=target_uid,
 | 
			
		||||
            request_ip=client_ip,
 | 
			
		||||
            last_updated__gt=time_threshold).order_by('created')
 | 
			
		||||
        if existing_attempts.exists():
 | 
			
		||||
            attempt = existing_attempts.first()
 | 
			
		||||
            attempt.attempts += 1
 | 
			
		||||
            attempt.save()
 | 
			
		||||
            LOGGER.debug("Increased attempts on %s", attempt)
 | 
			
		||||
        else:
 | 
			
		||||
            attempt = LoginAttempt.objects.create(
 | 
			
		||||
                target_uid=target_uid,
 | 
			
		||||
                request_ip=client_ip)
 | 
			
		||||
            LOGGER.debug("Created new attempt %s", attempt)
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return "LoginAttempt to %s from %s (x%d)" % (self.target_uid,
 | 
			
		||||
                                                     self.request_ip, self.attempts)
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
 | 
			
		||||
        unique_together = (('target_uid', 'request_ip', 'created'),)
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,8 @@
 | 
			
		||||
"""passbook audit signal listener"""
 | 
			
		||||
from django.contrib.auth.signals import (user_logged_in, user_logged_out,
 | 
			
		||||
                                         user_login_failed)
 | 
			
		||||
from django.contrib.auth.signals import user_logged_in, user_logged_out
 | 
			
		||||
from django.dispatch import receiver
 | 
			
		||||
 | 
			
		||||
from passbook.audit.models import AuditEntry, LoginAttempt
 | 
			
		||||
from passbook.audit.models import AuditEntry
 | 
			
		||||
from passbook.core.signals import (invitation_created, invitation_used,
 | 
			
		||||
                                   user_signed_up)
 | 
			
		||||
 | 
			
		||||
@ -34,8 +33,3 @@ def on_invitation_used(sender, request, invitation, **kwargs):
 | 
			
		||||
    """Log Invitation usage"""
 | 
			
		||||
    AuditEntry.create(AuditEntry.ACTION_INVITE_USED, request,
 | 
			
		||||
                      invitation_uuid=invitation.uuid.hex)
 | 
			
		||||
 | 
			
		||||
@receiver(user_login_failed)
 | 
			
		||||
def on_user_login_failed(sender, request, credentials, **kwargs):
 | 
			
		||||
    """Log failed login attempt"""
 | 
			
		||||
    LoginAttempt.attempt(target_uid=credentials.get('username'), request=request)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user