authentik.lib.tests.test_validators

Test password hash validators

 1"""Test password hash validators"""
 2
 3from argon2.low_level import Type, hash_secret
 4from django.conf import settings
 5from django.contrib.auth.hashers import (
 6    Argon2PasswordHasher,
 7    PBKDF2PasswordHasher,
 8    PBKDF2SHA1PasswordHasher,
 9    check_password,
10)
11from django.test import TestCase
12from django.utils.module_loading import import_string
13
14from authentik.lib.generators import generate_key
15from authentik.lib.validators import (
16    PasswordHashRequiresOverride,
17    PasswordHashValidator,
18)
19
20
21class TestPasswordHashValidator(TestCase):
22    """Test password hash import settings."""
23
24    def test_rejected_algorithm_lists_allowed_algorithms(self):
25        """The algorithm error lists every accepted algorithm."""
26        password = generate_key()
27        validator = PasswordHashValidator()
28
29        sha1 = PBKDF2SHA1PasswordHasher()
30        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
31            validator(sha1.encode(password, sha1.salt()))
32        algorithm_error = str(ctx.exception.detail[0])
33        self.assertIn("pbkdf2_sha1", algorithm_error)
34        allowed_algorithms = ", ".join(
35            import_string(path)().algorithm for path in settings.PASSWORD_HASHERS_IMPORT_ALLOWED
36        )
37        self.assertIn(allowed_algorithms, algorithm_error)
38
39    def test_argon2i_requires_override(self):
40        """Django's must_update flags an otherwise current argon2i hash."""
41        password = generate_key()
42        validator = PasswordHashValidator()
43        argon2 = Argon2PasswordHasher()
44        params = argon2.params()
45        argon2i = (
46            argon2.algorithm
47            + hash_secret(
48                password.encode(),
49                argon2.salt().encode(),
50                time_cost=params.time_cost,
51                memory_cost=params.memory_cost,
52                parallelism=params.parallelism,
53                hash_len=params.hash_len,
54                type=Type.I,
55            ).decode()
56        )
57        self.assertTrue(check_password(password, argon2i))
58        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
59            validator(argon2i)
60        self.assertIn("'variety': 'argon2i'", str(ctx.exception.detail[0]))
61        self.assertIn("'variety': 'argon2id'", str(ctx.exception.detail[0]))
62
63    def test_parameters_and_salt_have_separate_errors(self):
64        """Parameter and salt mismatches are reported separately."""
65        password = generate_key()
66        validator = PasswordHashValidator()
67        hasher = PBKDF2PasswordHasher()
68        hasher.iterations -= 1
69        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
70            validator(hasher.encode(password, "salt"))
71        self.assertEqual(len(ctx.exception.detail), 2)
72        self.assertIn("entropy", str(ctx.exception.detail[0]))
73        self.assertIn("Provided:", str(ctx.exception.detail[1]))
74        self.assertIn(f"'iterations': {hasher.iterations}", str(ctx.exception.detail[1]))
75        self.assertIn(
76            f"'iterations': {PBKDF2PasswordHasher.iterations}", str(ctx.exception.detail[1])
77        )
78        self.assertNotIn("argon2", str(ctx.exception.detail[1]))
class TestPasswordHashValidator(django.test.testcases.TestCase):
22class TestPasswordHashValidator(TestCase):
23    """Test password hash import settings."""
24
25    def test_rejected_algorithm_lists_allowed_algorithms(self):
26        """The algorithm error lists every accepted algorithm."""
27        password = generate_key()
28        validator = PasswordHashValidator()
29
30        sha1 = PBKDF2SHA1PasswordHasher()
31        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
32            validator(sha1.encode(password, sha1.salt()))
33        algorithm_error = str(ctx.exception.detail[0])
34        self.assertIn("pbkdf2_sha1", algorithm_error)
35        allowed_algorithms = ", ".join(
36            import_string(path)().algorithm for path in settings.PASSWORD_HASHERS_IMPORT_ALLOWED
37        )
38        self.assertIn(allowed_algorithms, algorithm_error)
39
40    def test_argon2i_requires_override(self):
41        """Django's must_update flags an otherwise current argon2i hash."""
42        password = generate_key()
43        validator = PasswordHashValidator()
44        argon2 = Argon2PasswordHasher()
45        params = argon2.params()
46        argon2i = (
47            argon2.algorithm
48            + hash_secret(
49                password.encode(),
50                argon2.salt().encode(),
51                time_cost=params.time_cost,
52                memory_cost=params.memory_cost,
53                parallelism=params.parallelism,
54                hash_len=params.hash_len,
55                type=Type.I,
56            ).decode()
57        )
58        self.assertTrue(check_password(password, argon2i))
59        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
60            validator(argon2i)
61        self.assertIn("'variety': 'argon2i'", str(ctx.exception.detail[0]))
62        self.assertIn("'variety': 'argon2id'", str(ctx.exception.detail[0]))
63
64    def test_parameters_and_salt_have_separate_errors(self):
65        """Parameter and salt mismatches are reported separately."""
66        password = generate_key()
67        validator = PasswordHashValidator()
68        hasher = PBKDF2PasswordHasher()
69        hasher.iterations -= 1
70        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
71            validator(hasher.encode(password, "salt"))
72        self.assertEqual(len(ctx.exception.detail), 2)
73        self.assertIn("entropy", str(ctx.exception.detail[0]))
74        self.assertIn("Provided:", str(ctx.exception.detail[1]))
75        self.assertIn(f"'iterations': {hasher.iterations}", str(ctx.exception.detail[1]))
76        self.assertIn(
77            f"'iterations': {PBKDF2PasswordHasher.iterations}", str(ctx.exception.detail[1])
78        )
79        self.assertNotIn("argon2", str(ctx.exception.detail[1]))

Test password hash import settings.

def test_rejected_algorithm_lists_allowed_algorithms(self):
25    def test_rejected_algorithm_lists_allowed_algorithms(self):
26        """The algorithm error lists every accepted algorithm."""
27        password = generate_key()
28        validator = PasswordHashValidator()
29
30        sha1 = PBKDF2SHA1PasswordHasher()
31        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
32            validator(sha1.encode(password, sha1.salt()))
33        algorithm_error = str(ctx.exception.detail[0])
34        self.assertIn("pbkdf2_sha1", algorithm_error)
35        allowed_algorithms = ", ".join(
36            import_string(path)().algorithm for path in settings.PASSWORD_HASHERS_IMPORT_ALLOWED
37        )
38        self.assertIn(allowed_algorithms, algorithm_error)

The algorithm error lists every accepted algorithm.

def test_argon2i_requires_override(self):
40    def test_argon2i_requires_override(self):
41        """Django's must_update flags an otherwise current argon2i hash."""
42        password = generate_key()
43        validator = PasswordHashValidator()
44        argon2 = Argon2PasswordHasher()
45        params = argon2.params()
46        argon2i = (
47            argon2.algorithm
48            + hash_secret(
49                password.encode(),
50                argon2.salt().encode(),
51                time_cost=params.time_cost,
52                memory_cost=params.memory_cost,
53                parallelism=params.parallelism,
54                hash_len=params.hash_len,
55                type=Type.I,
56            ).decode()
57        )
58        self.assertTrue(check_password(password, argon2i))
59        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
60            validator(argon2i)
61        self.assertIn("'variety': 'argon2i'", str(ctx.exception.detail[0]))
62        self.assertIn("'variety': 'argon2id'", str(ctx.exception.detail[0]))

Django's must_update flags an otherwise current argon2i hash.

def test_parameters_and_salt_have_separate_errors(self):
64    def test_parameters_and_salt_have_separate_errors(self):
65        """Parameter and salt mismatches are reported separately."""
66        password = generate_key()
67        validator = PasswordHashValidator()
68        hasher = PBKDF2PasswordHasher()
69        hasher.iterations -= 1
70        with self.assertRaises(PasswordHashRequiresOverride) as ctx:
71            validator(hasher.encode(password, "salt"))
72        self.assertEqual(len(ctx.exception.detail), 2)
73        self.assertIn("entropy", str(ctx.exception.detail[0]))
74        self.assertIn("Provided:", str(ctx.exception.detail[1]))
75        self.assertIn(f"'iterations': {hasher.iterations}", str(ctx.exception.detail[1]))
76        self.assertIn(
77            f"'iterations': {PBKDF2PasswordHasher.iterations}", str(ctx.exception.detail[1])
78        )
79        self.assertNotIn("argon2", str(ctx.exception.detail[1]))

Parameter and salt mismatches are reported separately.