2019-10-07 14:33:48 +00:00
|
|
|
"""password factor models"""
|
|
|
|
from django.contrib.postgres.fields import ArrayField
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2020-02-20 12:51:41 +00:00
|
|
|
from passbook.core.types import UIUserSettings
|
|
|
|
from passbook.core.models import Factor, Policy, User
|
2019-10-07 14:33:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PasswordFactor(Factor):
|
|
|
|
"""Password-based Django-backend Authentication Factor"""
|
|
|
|
|
|
|
|
backends = ArrayField(models.TextField())
|
|
|
|
password_policies = models.ManyToManyField(Policy, blank=True)
|
2019-12-31 11:51:16 +00:00
|
|
|
reset_factors = models.ManyToManyField(
|
|
|
|
Factor, blank=True, related_name="reset_factors"
|
|
|
|
)
|
2019-10-07 14:33:48 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
type = "passbook.factors.password.factor.PasswordFactor"
|
|
|
|
form = "passbook.factors.password.forms.PasswordFactorForm"
|
2019-10-07 14:33:48 +00:00
|
|
|
|
2020-02-20 12:51:41 +00:00
|
|
|
@property
|
|
|
|
def ui_user_settings(self) -> UIUserSettings:
|
|
|
|
return UIUserSettings(
|
|
|
|
name="Change Password",
|
|
|
|
icon="pficon-key",
|
|
|
|
view_name="passbook_core:user-change-password",
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2019-10-07 14:33:48 +00:00
|
|
|
|
|
|
|
def password_passes(self, user: User) -> bool:
|
|
|
|
"""Return true if user's password passes, otherwise False or raise Exception"""
|
|
|
|
for policy in self.policies.all():
|
|
|
|
if not policy.passes(user):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Password Factor %s" % self.slug
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Password Factor")
|
|
|
|
verbose_name_plural = _("Password Factors")
|