2018-12-10 13:05:27 +00:00
|
|
|
"""passbook core signals"""
|
2019-03-21 10:29:11 +00:00
|
|
|
from logging import getLogger
|
2018-12-10 13:05:27 +00:00
|
|
|
|
2019-03-21 10:29:11 +00:00
|
|
|
from django.core.cache import cache
|
2018-12-10 13:05:27 +00:00
|
|
|
from django.core.signals import Signal
|
2019-03-21 10:29:11 +00:00
|
|
|
from django.db.models.signals import post_save
|
2019-02-26 14:40:58 +00:00
|
|
|
from django.dispatch import receiver
|
2018-12-10 13:05:27 +00:00
|
|
|
|
2019-02-26 14:40:58 +00:00
|
|
|
from passbook.core.exceptions import PasswordPolicyInvalid
|
2018-12-10 13:05:27 +00:00
|
|
|
|
2019-03-21 10:29:11 +00:00
|
|
|
LOGGER = getLogger(__name__)
|
|
|
|
|
2018-12-10 13:05:27 +00:00
|
|
|
user_signed_up = Signal(providing_args=['request', 'user'])
|
2018-12-10 13:21:42 +00:00
|
|
|
invitation_created = Signal(providing_args=['request', 'invitation'])
|
|
|
|
invitation_used = Signal(providing_args=['request', 'invitation', 'user'])
|
2019-02-25 14:41:36 +00:00
|
|
|
password_changed = Signal(providing_args=['user', 'password'])
|
2019-02-26 14:40:58 +00:00
|
|
|
|
|
|
|
@receiver(password_changed)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def password_policy_checker(sender, password, **kwargs):
|
|
|
|
"""Run password through all password policies which are applied to the user"""
|
|
|
|
from passbook.core.models import PasswordFactor
|
2019-10-01 08:17:39 +00:00
|
|
|
from passbook.policy.engine import PolicyEngine
|
2019-02-26 14:40:58 +00:00
|
|
|
setattr(sender, '__password__', password)
|
|
|
|
_all_factors = PasswordFactor.objects.filter(enabled=True).order_by('order')
|
|
|
|
for factor in _all_factors:
|
2019-02-27 14:49:20 +00:00
|
|
|
policy_engine = PolicyEngine(factor.password_policies.all().select_subclasses())
|
2019-03-03 19:26:25 +00:00
|
|
|
policy_engine.for_user(sender).build()
|
2019-02-27 14:49:20 +00:00
|
|
|
passing, messages = policy_engine.result
|
|
|
|
if not passing:
|
|
|
|
raise PasswordPolicyInvalid(*messages)
|
2019-03-21 10:29:11 +00:00
|
|
|
|
|
|
|
@receiver(post_save)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def invalidate_policy_cache(sender, instance, **kwargs):
|
|
|
|
"""Invalidate Policy cache when policy is updated"""
|
|
|
|
from passbook.core.models import Policy
|
|
|
|
if isinstance(instance, Policy):
|
|
|
|
LOGGER.debug("Invalidating cache for %s", instance.pk)
|
|
|
|
keys = cache.keys("%s#*" % instance.pk)
|
|
|
|
cache.delete_many(keys)
|
|
|
|
LOGGER.debug("Deleted %d keys", len(keys))
|