authentik.policies.password.models

password policy

  1"""password policy"""
  2
  3import re
  4from hashlib import sha1
  5
  6from django.db import models
  7from django.utils.translation import gettext_lazy as _
  8from rest_framework.serializers import BaseSerializer
  9from structlog.stdlib import get_logger
 10
 11from authentik.lib.utils.http import get_http_session
 12from authentik.policies.models import Policy
 13from authentik.policies.types import PolicyRequest, PolicyResult
 14from authentik.stages.prompt.stage import PLAN_CONTEXT_PROMPT
 15
 16LOGGER = get_logger()
 17RE_LOWER = re.compile("[a-z]")
 18RE_UPPER = re.compile("[A-Z]")
 19RE_DIGITS = re.compile("[0-9]")
 20
 21
 22class PasswordPolicy(Policy):
 23    """Policy to make sure passwords have certain properties"""
 24
 25    password_field = models.TextField(
 26        default="password",
 27        help_text=_("Field key to check, field keys defined in Prompt stages are available."),
 28    )
 29
 30    check_static_rules = models.BooleanField(default=True)
 31    check_have_i_been_pwned = models.BooleanField(default=False)
 32    check_zxcvbn = models.BooleanField(default=False)
 33
 34    amount_digits = models.PositiveIntegerField(default=0)
 35    amount_uppercase = models.PositiveIntegerField(default=0)
 36    amount_lowercase = models.PositiveIntegerField(default=0)
 37    amount_symbols = models.PositiveIntegerField(default=0)
 38    length_min = models.PositiveIntegerField(default=0)
 39    symbol_charset = models.TextField(default=r"!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ ")
 40    error_message = models.TextField(blank=True)
 41
 42    hibp_allowed_count = models.PositiveIntegerField(
 43        default=0,
 44        help_text=_("How many times the password hash is allowed to be on haveibeenpwned"),
 45    )
 46
 47    zxcvbn_score_threshold = models.PositiveIntegerField(
 48        default=2,
 49        help_text=_("If the zxcvbn score is equal or less than this value, the policy will fail."),
 50    )
 51
 52    @property
 53    def serializer(self) -> type[BaseSerializer]:
 54        from authentik.policies.password.api import PasswordPolicySerializer
 55
 56        return PasswordPolicySerializer
 57
 58    @property
 59    def component(self) -> str:
 60        return "ak-policy-password-form"
 61
 62    def passes(self, request: PolicyRequest) -> PolicyResult:
 63        password = request.context.get(PLAN_CONTEXT_PROMPT, {}).get(
 64            self.password_field, request.context.get(self.password_field)
 65        )
 66        if not password:
 67            LOGGER.warning(
 68                "Password field not set in Policy Request",
 69                field=self.password_field,
 70                fields=request.context.keys(),
 71            )
 72            return PolicyResult(False, str(_("Password not set in context")))
 73        password = str(password)
 74
 75        if self.check_static_rules:
 76            static_result = self.passes_static(password, request)
 77            if not static_result.passing:
 78                return static_result
 79        if self.check_have_i_been_pwned:
 80            hibp_result = self.passes_hibp(password, request)
 81            if not hibp_result.passing:
 82                return hibp_result
 83        if self.check_zxcvbn:
 84            zxcvbn_result = self.passes_zxcvbn(password, request)
 85            if not zxcvbn_result.passing:
 86                return zxcvbn_result
 87        return PolicyResult(True)
 88
 89    def passes_static(self, password: str, request: PolicyRequest) -> PolicyResult:
 90        """Check static rules"""
 91        error_message = self.error_message
 92        if error_message == "":
 93            error_message = str(_("Invalid password."))
 94
 95        if len(password) < self.length_min:
 96            LOGGER.debug("password failed", check="static", reason="length")
 97            return PolicyResult(False, error_message)
 98
 99        if self.amount_digits > 0 and len(RE_DIGITS.findall(password)) < self.amount_digits:
100            LOGGER.debug("password failed", check="static", reason="amount_digits")
101            return PolicyResult(False, error_message)
102        if self.amount_lowercase > 0 and len(RE_LOWER.findall(password)) < self.amount_lowercase:
103            LOGGER.debug("password failed", check="static", reason="amount_lowercase")
104            return PolicyResult(False, error_message)
105        if self.amount_uppercase > 0 and len(RE_UPPER.findall(password)) < self.amount_uppercase:
106            LOGGER.debug("password failed", check="static", reason="amount_uppercase")
107            return PolicyResult(False, error_message)
108        if self.amount_symbols > 0:
109            count = 0
110            for symbol in self.symbol_charset:
111                count += password.count(symbol)
112            if count < self.amount_symbols:
113                LOGGER.debug("password failed", check="static", reason="amount_symbols")
114                return PolicyResult(False, error_message)
115
116        return PolicyResult(True)
117
118    def check_hibp(self, short_hash: str) -> str:
119        """Check the haveibeenpwned API"""
120        url = f"https://api.pwnedpasswords.com/range/{short_hash}"
121        return get_http_session().get(url).text
122
123    def passes_hibp(self, password: str, request: PolicyRequest) -> PolicyResult:
124        """Check if password is in HIBP DB. Hashes given Password with SHA1, uses the first 5
125        characters of Password in request and checks if full hash is in response. Returns 0
126        if Password is not in result otherwise the count of how many times it was used."""
127        pw_hash = sha1(password.encode("utf-8")).hexdigest()  # nosec
128        result = self.check_hibp(pw_hash[:5])
129        final_count = 0
130        for line in result.split("\r\n"):
131            full_hash, count = line.split(":")
132            if pw_hash[5:] == full_hash.lower():
133                final_count = int(count)
134        LOGGER.debug("got hibp result", count=final_count, hash=pw_hash[:5])
135        if final_count > self.hibp_allowed_count:
136            LOGGER.debug("password failed", check="hibp", count=final_count)
137            message = _("Password exists on {count} online lists.").format(count=final_count)
138            return PolicyResult(False, message)
139        return PolicyResult(True)
140
141    def passes_zxcvbn(self, password: str, request: PolicyRequest) -> PolicyResult:
142        """Check Dropbox's zxcvbn password estimator"""
143        from zxcvbn import zxcvbn
144
145        user_inputs = []
146        if request.user.is_authenticated:
147            user_inputs.append(request.user.username)
148            user_inputs.append(request.user.name)
149            user_inputs.append(request.user.email)
150        if request.http_request:
151            user_inputs.append(request.http_request.brand.branding_title)
152        # Only calculate result for the first 72 characters, as with over 100 char
153        # long passwords we can be reasonably sure that they'll surpass the score anyways
154        # See https://github.com/dropbox/zxcvbn#runtime-latency
155        results = zxcvbn(password[:72], user_inputs)
156        LOGGER.debug("password failed", check="zxcvbn", score=results["score"])
157        result = PolicyResult(results["score"] > self.zxcvbn_score_threshold)
158        if not result.passing:
159            result.messages += tuple((str(_("Password is too weak.")),))
160        if isinstance(results["feedback"]["warning"], list):
161            result.messages += tuple(results["feedback"]["warning"])
162        if isinstance(results["feedback"]["suggestions"], list):
163            result.messages += tuple(results["feedback"]["suggestions"])
164        return result
165
166    class Meta(Policy.PolicyMeta):
167        verbose_name = _("Password Policy")
168        verbose_name_plural = _("Password Policies")
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
RE_LOWER = re.compile('[a-z]')
RE_UPPER = re.compile('[A-Z]')
RE_DIGITS = re.compile('[0-9]')
class PasswordPolicy(authentik.policies.models.Policy):
 23class PasswordPolicy(Policy):
 24    """Policy to make sure passwords have certain properties"""
 25
 26    password_field = models.TextField(
 27        default="password",
 28        help_text=_("Field key to check, field keys defined in Prompt stages are available."),
 29    )
 30
 31    check_static_rules = models.BooleanField(default=True)
 32    check_have_i_been_pwned = models.BooleanField(default=False)
 33    check_zxcvbn = models.BooleanField(default=False)
 34
 35    amount_digits = models.PositiveIntegerField(default=0)
 36    amount_uppercase = models.PositiveIntegerField(default=0)
 37    amount_lowercase = models.PositiveIntegerField(default=0)
 38    amount_symbols = models.PositiveIntegerField(default=0)
 39    length_min = models.PositiveIntegerField(default=0)
 40    symbol_charset = models.TextField(default=r"!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ ")
 41    error_message = models.TextField(blank=True)
 42
 43    hibp_allowed_count = models.PositiveIntegerField(
 44        default=0,
 45        help_text=_("How many times the password hash is allowed to be on haveibeenpwned"),
 46    )
 47
 48    zxcvbn_score_threshold = models.PositiveIntegerField(
 49        default=2,
 50        help_text=_("If the zxcvbn score is equal or less than this value, the policy will fail."),
 51    )
 52
 53    @property
 54    def serializer(self) -> type[BaseSerializer]:
 55        from authentik.policies.password.api import PasswordPolicySerializer
 56
 57        return PasswordPolicySerializer
 58
 59    @property
 60    def component(self) -> str:
 61        return "ak-policy-password-form"
 62
 63    def passes(self, request: PolicyRequest) -> PolicyResult:
 64        password = request.context.get(PLAN_CONTEXT_PROMPT, {}).get(
 65            self.password_field, request.context.get(self.password_field)
 66        )
 67        if not password:
 68            LOGGER.warning(
 69                "Password field not set in Policy Request",
 70                field=self.password_field,
 71                fields=request.context.keys(),
 72            )
 73            return PolicyResult(False, str(_("Password not set in context")))
 74        password = str(password)
 75
 76        if self.check_static_rules:
 77            static_result = self.passes_static(password, request)
 78            if not static_result.passing:
 79                return static_result
 80        if self.check_have_i_been_pwned:
 81            hibp_result = self.passes_hibp(password, request)
 82            if not hibp_result.passing:
 83                return hibp_result
 84        if self.check_zxcvbn:
 85            zxcvbn_result = self.passes_zxcvbn(password, request)
 86            if not zxcvbn_result.passing:
 87                return zxcvbn_result
 88        return PolicyResult(True)
 89
 90    def passes_static(self, password: str, request: PolicyRequest) -> PolicyResult:
 91        """Check static rules"""
 92        error_message = self.error_message
 93        if error_message == "":
 94            error_message = str(_("Invalid password."))
 95
 96        if len(password) < self.length_min:
 97            LOGGER.debug("password failed", check="static", reason="length")
 98            return PolicyResult(False, error_message)
 99
100        if self.amount_digits > 0 and len(RE_DIGITS.findall(password)) < self.amount_digits:
101            LOGGER.debug("password failed", check="static", reason="amount_digits")
102            return PolicyResult(False, error_message)
103        if self.amount_lowercase > 0 and len(RE_LOWER.findall(password)) < self.amount_lowercase:
104            LOGGER.debug("password failed", check="static", reason="amount_lowercase")
105            return PolicyResult(False, error_message)
106        if self.amount_uppercase > 0 and len(RE_UPPER.findall(password)) < self.amount_uppercase:
107            LOGGER.debug("password failed", check="static", reason="amount_uppercase")
108            return PolicyResult(False, error_message)
109        if self.amount_symbols > 0:
110            count = 0
111            for symbol in self.symbol_charset:
112                count += password.count(symbol)
113            if count < self.amount_symbols:
114                LOGGER.debug("password failed", check="static", reason="amount_symbols")
115                return PolicyResult(False, error_message)
116
117        return PolicyResult(True)
118
119    def check_hibp(self, short_hash: str) -> str:
120        """Check the haveibeenpwned API"""
121        url = f"https://api.pwnedpasswords.com/range/{short_hash}"
122        return get_http_session().get(url).text
123
124    def passes_hibp(self, password: str, request: PolicyRequest) -> PolicyResult:
125        """Check if password is in HIBP DB. Hashes given Password with SHA1, uses the first 5
126        characters of Password in request and checks if full hash is in response. Returns 0
127        if Password is not in result otherwise the count of how many times it was used."""
128        pw_hash = sha1(password.encode("utf-8")).hexdigest()  # nosec
129        result = self.check_hibp(pw_hash[:5])
130        final_count = 0
131        for line in result.split("\r\n"):
132            full_hash, count = line.split(":")
133            if pw_hash[5:] == full_hash.lower():
134                final_count = int(count)
135        LOGGER.debug("got hibp result", count=final_count, hash=pw_hash[:5])
136        if final_count > self.hibp_allowed_count:
137            LOGGER.debug("password failed", check="hibp", count=final_count)
138            message = _("Password exists on {count} online lists.").format(count=final_count)
139            return PolicyResult(False, message)
140        return PolicyResult(True)
141
142    def passes_zxcvbn(self, password: str, request: PolicyRequest) -> PolicyResult:
143        """Check Dropbox's zxcvbn password estimator"""
144        from zxcvbn import zxcvbn
145
146        user_inputs = []
147        if request.user.is_authenticated:
148            user_inputs.append(request.user.username)
149            user_inputs.append(request.user.name)
150            user_inputs.append(request.user.email)
151        if request.http_request:
152            user_inputs.append(request.http_request.brand.branding_title)
153        # Only calculate result for the first 72 characters, as with over 100 char
154        # long passwords we can be reasonably sure that they'll surpass the score anyways
155        # See https://github.com/dropbox/zxcvbn#runtime-latency
156        results = zxcvbn(password[:72], user_inputs)
157        LOGGER.debug("password failed", check="zxcvbn", score=results["score"])
158        result = PolicyResult(results["score"] > self.zxcvbn_score_threshold)
159        if not result.passing:
160            result.messages += tuple((str(_("Password is too weak.")),))
161        if isinstance(results["feedback"]["warning"], list):
162            result.messages += tuple(results["feedback"]["warning"])
163        if isinstance(results["feedback"]["suggestions"], list):
164            result.messages += tuple(results["feedback"]["suggestions"])
165        return result
166
167    class Meta(Policy.PolicyMeta):
168        verbose_name = _("Password Policy")
169        verbose_name_plural = _("Password Policies")

Policy to make sure passwords have certain properties

def password_field(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def check_static_rules(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def check_have_i_been_pwned(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def check_zxcvbn(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def amount_digits(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def amount_uppercase(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def amount_lowercase(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def amount_symbols(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def length_min(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def symbol_charset(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def error_message(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def hibp_allowed_count(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

def zxcvbn_score_threshold(unknown):

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

serializer: type[rest_framework.serializers.BaseSerializer]
53    @property
54    def serializer(self) -> type[BaseSerializer]:
55        from authentik.policies.password.api import PasswordPolicySerializer
56
57        return PasswordPolicySerializer

Get serializer for this model

component: str
59    @property
60    def component(self) -> str:
61        return "ak-policy-password-form"

Return component used to edit this object

63    def passes(self, request: PolicyRequest) -> PolicyResult:
64        password = request.context.get(PLAN_CONTEXT_PROMPT, {}).get(
65            self.password_field, request.context.get(self.password_field)
66        )
67        if not password:
68            LOGGER.warning(
69                "Password field not set in Policy Request",
70                field=self.password_field,
71                fields=request.context.keys(),
72            )
73            return PolicyResult(False, str(_("Password not set in context")))
74        password = str(password)
75
76        if self.check_static_rules:
77            static_result = self.passes_static(password, request)
78            if not static_result.passing:
79                return static_result
80        if self.check_have_i_been_pwned:
81            hibp_result = self.passes_hibp(password, request)
82            if not hibp_result.passing:
83                return hibp_result
84        if self.check_zxcvbn:
85            zxcvbn_result = self.passes_zxcvbn(password, request)
86            if not zxcvbn_result.passing:
87                return zxcvbn_result
88        return PolicyResult(True)

Check if request passes this policy

def passes_static( self, password: str, request: authentik.policies.types.PolicyRequest) -> authentik.policies.types.PolicyResult:
 90    def passes_static(self, password: str, request: PolicyRequest) -> PolicyResult:
 91        """Check static rules"""
 92        error_message = self.error_message
 93        if error_message == "":
 94            error_message = str(_("Invalid password."))
 95
 96        if len(password) < self.length_min:
 97            LOGGER.debug("password failed", check="static", reason="length")
 98            return PolicyResult(False, error_message)
 99
100        if self.amount_digits > 0 and len(RE_DIGITS.findall(password)) < self.amount_digits:
101            LOGGER.debug("password failed", check="static", reason="amount_digits")
102            return PolicyResult(False, error_message)
103        if self.amount_lowercase > 0 and len(RE_LOWER.findall(password)) < self.amount_lowercase:
104            LOGGER.debug("password failed", check="static", reason="amount_lowercase")
105            return PolicyResult(False, error_message)
106        if self.amount_uppercase > 0 and len(RE_UPPER.findall(password)) < self.amount_uppercase:
107            LOGGER.debug("password failed", check="static", reason="amount_uppercase")
108            return PolicyResult(False, error_message)
109        if self.amount_symbols > 0:
110            count = 0
111            for symbol in self.symbol_charset:
112                count += password.count(symbol)
113            if count < self.amount_symbols:
114                LOGGER.debug("password failed", check="static", reason="amount_symbols")
115                return PolicyResult(False, error_message)
116
117        return PolicyResult(True)

Check static rules

def check_hibp(self, short_hash: str) -> str:
119    def check_hibp(self, short_hash: str) -> str:
120        """Check the haveibeenpwned API"""
121        url = f"https://api.pwnedpasswords.com/range/{short_hash}"
122        return get_http_session().get(url).text

Check the haveibeenpwned API

def passes_hibp( self, password: str, request: authentik.policies.types.PolicyRequest) -> authentik.policies.types.PolicyResult:
124    def passes_hibp(self, password: str, request: PolicyRequest) -> PolicyResult:
125        """Check if password is in HIBP DB. Hashes given Password with SHA1, uses the first 5
126        characters of Password in request and checks if full hash is in response. Returns 0
127        if Password is not in result otherwise the count of how many times it was used."""
128        pw_hash = sha1(password.encode("utf-8")).hexdigest()  # nosec
129        result = self.check_hibp(pw_hash[:5])
130        final_count = 0
131        for line in result.split("\r\n"):
132            full_hash, count = line.split(":")
133            if pw_hash[5:] == full_hash.lower():
134                final_count = int(count)
135        LOGGER.debug("got hibp result", count=final_count, hash=pw_hash[:5])
136        if final_count > self.hibp_allowed_count:
137            LOGGER.debug("password failed", check="hibp", count=final_count)
138            message = _("Password exists on {count} online lists.").format(count=final_count)
139            return PolicyResult(False, message)
140        return PolicyResult(True)

Check if password is in HIBP DB. Hashes given Password with SHA1, uses the first 5 characters of Password in request and checks if full hash is in response. Returns 0 if Password is not in result otherwise the count of how many times it was used.

def passes_zxcvbn( self, password: str, request: authentik.policies.types.PolicyRequest) -> authentik.policies.types.PolicyResult:
142    def passes_zxcvbn(self, password: str, request: PolicyRequest) -> PolicyResult:
143        """Check Dropbox's zxcvbn password estimator"""
144        from zxcvbn import zxcvbn
145
146        user_inputs = []
147        if request.user.is_authenticated:
148            user_inputs.append(request.user.username)
149            user_inputs.append(request.user.name)
150            user_inputs.append(request.user.email)
151        if request.http_request:
152            user_inputs.append(request.http_request.brand.branding_title)
153        # Only calculate result for the first 72 characters, as with over 100 char
154        # long passwords we can be reasonably sure that they'll surpass the score anyways
155        # See https://github.com/dropbox/zxcvbn#runtime-latency
156        results = zxcvbn(password[:72], user_inputs)
157        LOGGER.debug("password failed", check="zxcvbn", score=results["score"])
158        result = PolicyResult(results["score"] > self.zxcvbn_score_threshold)
159        if not result.passing:
160            result.messages += tuple((str(_("Password is too weak.")),))
161        if isinstance(results["feedback"]["warning"], list):
162            result.messages += tuple(results["feedback"]["warning"])
163        if isinstance(results["feedback"]["suggestions"], list):
164            result.messages += tuple(results["feedback"]["suggestions"])
165        return result

Check Dropbox's zxcvbn password estimator

policy_ptr_id
policy_ptr

Accessor to the related object on the forward side of a one-to-one relation.

In the example::

class Restaurant(Model):
    place = OneToOneField(Place, related_name='restaurant')

Restaurant.place is a ForwardOneToOneDescriptor instance.

class PasswordPolicy.DoesNotExist(authentik.policies.models.Policy.DoesNotExist):

The requested object does not exist

class PasswordPolicy.MultipleObjectsReturned(authentik.policies.models.Policy.MultipleObjectsReturned):

The query returned multiple objects when only one was expected.