2019-10-07 14:33:48 +00:00
|
|
|
"""passbook reputation request policy"""
|
2020-07-07 15:03:57 +00:00
|
|
|
from django.core.cache import cache
|
2019-03-03 19:26:25 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.core.models import User
|
2019-12-05 13:33:55 +00:00
|
|
|
from passbook.lib.utils.http import get_client_ip
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.policies.models import Policy
|
2020-02-20 12:52:05 +00:00
|
|
|
from passbook.policies.types import PolicyRequest, PolicyResult
|
2019-03-03 19:26:25 +00:00
|
|
|
|
2020-07-07 15:03:57 +00:00
|
|
|
CACHE_KEY_IP_PREFIX = "passbook_reputation_ip_"
|
|
|
|
CACHE_KEY_USER_PREFIX = "passbook_reputation_user_"
|
|
|
|
|
2019-03-03 19:26:25 +00:00
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
class ReputationPolicy(Policy):
|
2019-03-03 19:26:25 +00:00
|
|
|
"""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-12-31 11:51:16 +00:00
|
|
|
form = "passbook.policies.reputation.forms.ReputationPolicyForm"
|
2019-03-03 19:26:25 +00:00
|
|
|
|
2019-10-03 08:45:31 +00:00
|
|
|
def passes(self, request: PolicyRequest) -> PolicyResult:
|
2020-07-07 15:03:57 +00:00
|
|
|
remote_ip = get_client_ip(request.http_request) or "255.255.255.255"
|
2019-03-03 19:26:25 +00:00
|
|
|
passing = True
|
|
|
|
if self.check_ip:
|
2020-07-07 15:03:57 +00:00
|
|
|
score = cache.get_or_set(CACHE_KEY_IP_PREFIX + remote_ip, 0)
|
|
|
|
passing = passing and score <= self.threshold
|
2019-03-03 19:26:25 +00:00
|
|
|
if self.check_username:
|
2020-07-07 15:03:57 +00:00
|
|
|
score = cache.get_or_set(CACHE_KEY_USER_PREFIX + request.user.username, 0)
|
|
|
|
passing = passing and score <= self.threshold
|
2019-10-01 08:17:39 +00:00
|
|
|
return PolicyResult(passing)
|
2019-03-03 19:26:25 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Reputation Policy")
|
|
|
|
verbose_name_plural = _("Reputation Policies")
|
2019-03-03 19:26:25 +00:00
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
|
|
|
|
class IPReputation(models.Model):
|
2019-03-03 19:26:25 +00:00
|
|
|
"""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-03-03 19:26:25 +00:00
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
class UserReputation(models.Model):
|
2019-03-03 19:26:25 +00:00
|
|
|
"""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}"
|