This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/policies/reputation/models.py

57 lines
2 KiB
Python
Raw Normal View History

2019-10-07 14:33:48 +00:00
"""passbook reputation request policy"""
from django.db import models
from django.utils.translation import gettext as _
from passbook.core.models import Policy, User
from passbook.lib.utils.http import get_client_ip
2019-10-07 14:33:48 +00:00
from passbook.policies.struct import PolicyRequest, PolicyResult
2019-10-07 14:33:48 +00:00
class ReputationPolicy(Policy):
"""Return true if request IP/target username's score is below a certain threshold"""
check_ip = models.BooleanField(default=True)
check_username = models.BooleanField(default=True)
threshold = models.IntegerField(default=-5)
2019-10-07 14:33:48 +00:00
form = 'passbook.policies.reputation.forms.ReputationPolicyForm'
def passes(self, request: PolicyRequest) -> PolicyResult:
remote_ip = get_client_ip(request.http_request)
passing = True
if self.check_ip:
2019-10-07 14:33:48 +00:00
ip_scores = IPReputation.objects.filter(ip=remote_ip, score__lte=self.threshold)
passing = passing and ip_scores.exists()
if self.check_username:
2019-10-07 14:33:48 +00:00
user_scores = UserReputation.objects.filter(user=request.user,
score__lte=self.threshold)
passing = passing and user_scores.exists()
return PolicyResult(passing)
class Meta:
2019-10-07 14:33:48 +00:00
verbose_name = _('Reputation Policy')
verbose_name_plural = _('Reputation Policies')
2019-10-07 14:33:48 +00:00
class IPReputation(models.Model):
"""Store score coming from the same IP"""
ip = models.GenericIPAddressField(unique=True)
score = models.IntegerField(default=0)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
2019-10-07 14:33:48 +00:00
return f"IPReputation for {self.ip} @ {self.score}"
2019-10-07 14:33:48 +00:00
class UserReputation(models.Model):
"""Store score attempting to log in as the same username"""
user = models.OneToOneField(User, on_delete=models.CASCADE)
score = models.IntegerField(default=0)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
2019-10-07 14:33:48 +00:00
return f"UserReputation for {self.user} @ {self.score}"